0

The question was this in my python beginners course Add a pair of parentheses to each expression so that it evaluates to True. 0 == 1 == 2

And the answer given was ( 0 == (1==2)) Isnt it still false in the answer too as 1 is not equals to 2 and 0 is not equal to any of them

5

1 Answer 1

5
0 == 1 == 2

This is a chained comparison, equivalent to

0 == 1 and 1 == 2

Neither statement is true, so the whole thing is false. This is the behavior you're imagining.

0 == (1 == 2)

This is not a chained comparison. This is a comparison of a number (zero) against a Boolean (1 == 2). We know 1 == 2 is false, so this is equivalent to

0 == False

And, in Python, 0 and False are (mostly) synonymous, and crucially they compare equal.

True

Not the answer you're looking for? Browse other questions tagged or ask your own question.