1 is considered to be true because it is non-zero. The fourth expression assigns a value of 0 to i. 0 is considered to be false.
FALSE evaluates to TRUE, so xor(FALSE, TRUE) evaluates to TRUE. On the other hand if the first argument was changed to 5 == 5 and the second argument was unchanged then both arguments would have been TRUE, so xor(TRUE, TRUE) would have evaluated to FALSE.
Python actually applies comparison operator chaining here. The expression is translated to
(1 in [1, 0]) and ([1, 0] == True)
which is obviously False
.
This also happens for expressions like
a < b < c
which translate to
(a < b) and (b < c)
(without evaluating b
twice).
See the Python language documentation for further details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With