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 =)

+3


source to share


3 answers


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

.



+8


source


the keyword not

basically "overrides" the boolean returned here.

In the first example, it a

is in an array, so true, but not true

false. So false.



In the second example, a

it is not in the array, so false, but not false

true. So true.

+2


source


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

0


source







All Articles