That the following expression evaluates to False may be surprising:
12 in [12,13,14] == True
Here's what's happening: the in
and ==
operators have the same precedence,
and they support left-to-right chaining (see the docs), so
the expression is equivalent to
12 in [12,13,14] and [12,13,14] == True
Now and
is less binding, and the left-hand side obviously evaluates to
True
. Now for the tricky part: a non-empty sequence, such as the [12, 13, 14]
list evaluates to True
, but it is not equal to True
. It's a so-called
"truthy" value. Truthy and Falsy values are not booleans (not instances of type bool), but they
evaluate to either True
or False
.
So they right-hand side of the and
comparison evaluates to False
.