Can someone explain to me why the Python interpreter evaluates this expression to be False?
1 in [1] == True
I would expect that 1 in [1] would evaluate to True, and obviously True == True would be True. However this isn't what happens - the expression is False. Why does this happen?
==
and in
are both comparison operators. And when you have multiple comparison operators like this, Python considers it a chained comparison. For example, 1 < x < 10
is equivalent to 1 < x and x < 10
.
In your case, 1 in [1] == True
is equivalent to (1 in [1]) and ([1] == True)
, which evaluates to False
.
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