2

Why does python obtain such a result?

>>> 1 in [] == False
False

Obviously, '1 in []' is evaluated to False, and 'False == False' is evaluated to True. And 1 in ([] == False) is not valid expression.

On the other hand '(1 in []) == False' is evaluated to True, but I still do not understand why '1 in [] == False' is evaluated to False,

N.Moudgil
  • 709
  • 5
  • 11
  • The expression should be *(1 in []) == False* – Mario Camilleri Feb 16 '20 at 10:26
  • I think `1 in [] == False` is the same as `1 in [] and [] == False`, Similar to python letting you do `10 <= x <= 50`. It converts `1 in x == False` into `1 in x and x == False`. Comparison with False won't even happen if first expression is false. Edit: Typo. – ThatAnnoyingDude Feb 16 '20 at 10:38

1 Answers1

1

Python interprets 1 in [] == False as

1 in [] AND [] == False. Both of which are False.

Also 1 in [1] == False is False since

1 in [1] AND [1] == False generates True and False. True and False gives us False.

David Collins
  • 848
  • 3
  • 10
  • 28