Difference between (a not in b) & (not a in b). python
Hi, can someone shed some light on the mechanics of the "in" statement in Python.
Now I am dealing with the examples below:
print ('a' not in ['a', 'b']) # outputs False
print (not 'a' in ['a', 'b']) # outputs False -- how ???
print ('c' not in ['a', 'b']) # outputs True
print (not 'c' in ['a', 'b']) # outputs True
print (not 'a') # outputs False
# ok is so then...
print (not 'a' in ['b', False]) # outputs True --- why ???
Now I wonder how this can be so. If anyone knows, please share your knowledge. Thank you =)
source to share
in
has a higher priority thannot
. Thus, a containment check is performed, and then the result is canceled if necessary. 'a'
is not in ['b', False]
, but the result is False
denied to lead to True
.
source to share
print (not 'a' in ['a', 'b'])
do the following:
not 'a'
computes by itself False
(since everything counts True
except 0, None, False, empty lists and empty dictionaries)
and is False
not in ['a','b']
, therefore it
False in ['a','b']
is evaluated asFalse
and on the latter it not 'a'
is evaluated False
, therefore it False in ['b', False]
is evaluated asTrue
source to share