Rewrite the following code snippet in 1 line:
x = 3
y = 3.0
if x == y :
print('x and y are equal')
else :
print('x and y are not equal')
Answer:
print('x and y are equal' if x == y else 'x and y are not equal')
The condition x == y is evaluated.
If the condition is True, the expression before the if (i.e., 'x and y are equal') is executed.
If the condition is False, the expression after the else (i.e., 'x and y are not equal') is executed.
So, in a single line, this code snippet prints either 'x and y are equal' or 'x and y are not equal' based on the equality of x and y.
0 Comments:
Post a Comment