2

why python 3 evaluate 3 in [] == False to be False? But I know (3 in []) == False is True.

zjx
  • 21
  • 1

1 Answers1

5

This is caused by chained comparisons

Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics:

comparison    ::=  or_expr (comp_operator or_expr)*
comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!="
                  | "is" ["not"] | ["not"] "in" 

Comparisons yield boolean values: True or False.

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

Consider the case 1 < 2 < 3. This really breaks out to (1 < 2) and (2 < 3)

So the statement 3 in [] == False breaks out to (3 in []) and ([] == False)

flakes
  • 21,558
  • 8
  • 41
  • 88