Write an if statement that assigns 1 to x if y is greater than 0.
Program Code:
# Initialize the variables
y = 10
x = 0
# Check if y is greater than 0
if y > 0:
x = 1 # Assign 1 to x
# Display the values of x and y
print("x =", x)
print("y =", y)
Executed Output:
x = 1
y = 10
Explanation:
y
with a value of 5 and x
with a value of 0.y
is greater than 0 by using the comparison operator >
. If the condition is true, the code block indented under the if statement is executed.x
using the assignment operator =
.x
and y
using the print()
function to verify the assignment.y
is indeed greater than 0, the if statement is true and the assignment x = 1
is executed.