I would have expected it to either have parenthesis to the left or to the right.
But it seems to do something else!
>>> 12 in [12,13,14] == True
False
>>> (12 in [12,13,13]) == True
True
>>> 12 in ([12,13,14] == True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
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.
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