Why does "not(True) in [False, True]" return False?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Light Drops
--
Chapters
00:00 Why Does &Quot;Not(True) In [False, True]&Quot; Return False?
00:30 Accepted Answer Score 750
00:54 Answer 2 Score 78
01:47 Answer 3 Score 37
02:01 Answer 4 Score 34
02:25 Answer 5 Score 14
02:41 Thank you
--
Full question
https://stackoverflow.com/questions/3142...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #operatorprecedence #comparisonoperators
#avk47
ACCEPTED ANSWER
Score 750
Operator precedence 2.x, 3.x. The precedence of not is lower than that of in. So it is equivalent to:
>>> not ((True) in [False, True])
False
This is what you want:
>>> (not True) in [False, True]
True
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.
ANSWER 2
Score 37
Operator precedence. in binds more tightly than not, so your expression is equivalent to not((True) in [False, True]).
ANSWER 3
Score 34
It's all about operator precedence (in is stronger than not). But it can be easily corrected by adding parentheses at the right place:
(not(True)) in [False, True] # prints true
writing:
not(True) in [False, True]
is the same like:
not((True) in [False, True])
which looks if True is in the list and returns the "not" of the result.
ANSWER 4
Score 14
It is evaluating as not True in [False, True], which returns False because True is in [False, True]
If you try
>>>(not(True)) in [False, True]
True
You get the expected result.