Why does (1 in [1,0] == True) evaluate to False?
Solution 1:
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.