If I do this:
JavaScript
x
3
1
>>> False in [False, True]
2
True
3
That returns True
. Simply because False
is in the list.
But if I do:
JavaScript
1
3
1
>>> not(True) in [False, True]
2
False
3
That returns False
. Whereas not(True)
is equal to False
:
JavaScript
1
3
1
>>> not(True)
2
False
3
Why?
Advertisement
Answer
Operator precedence 2.x, 3.x. The precedence of not
is lower than that of in
. So it is equivalent to:
JavaScript
1
3
1
>>> not ((True) in [False, True])
2
False
3
This is what you want:
JavaScript
1
3
1
>>> (not True) in [False, True]
2
True
3
As @Ben points out: It’s recommended to never write not(True)
, prefer not True
. The former makes it look like a function call, while not
is an operator, not a function.