Issue
I don't see why the following expression evaluates to False
:
>>> 1 in [1,2,3] == True
False
in
and ==
has the same precedence and groups left to right, but:
>>> (1 in [1,2,3]) == True
True
and
>>> 1 in ([1,2,3] == True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
Where is the problem with the parenthesis?
Solution
Comparison operators at the same level "chain". You may have seen something such as 10 < x < 25
, which expands to (10 < x) and (x < 25)
.
Similarly, your original expression expands to
(1 in [1,2,3]) and ([1,2,3] == True)
The first expression evaluates as True
, as you already know. The second expression returns False
, giving the result you see.
Answered By - Prune
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.