Search code examples
pythonin-operatorand-operator

Conjunction (AND operator) of IN operator in Python


I'd like to understand why the conjunction of results of IN operator in Python it not working as regular conjunction.

Example:

  1. False and False and True = False (obvious)
  2. 'a' in 'ccc' and 'b' in 'ccc' and 'c' in 'ccc' = False (ok)
  3. 'a' and 'b' and 'c' in 'ccc' = True (blows my mind)

I'd expect that the third line would return the same values as the first and second ones, but it's not.

Why is it so?


Solution

  • Your Operation results in true as:

    result = 'a' and 'b' and 'c' in 'ccc'
    #is the same as
    result = bool('a') and bool('b') and bool('c' in 'ccc')
    #i.e
    result = True and True and True #which is true