5

How does Python parse the first of the following three expressions? (I expected it to be the same as the second, since == and in have the same precedence.)

>>> 1 == 0 in (0,1), (1==0) in (0,1), 1 == (0 in (0,1))
(False, True, True)
Alan
  • 9,410
  • 15
  • 20
  • The confusion is a common one and finding the canonical is always going to be hard as the search terms involve punctuation that most search engines ignore. Your post can help provide additional keywords to guide people to the canonical, however. I'd keep it. – Martijn Pieters May 01 '15 at 22:59

1 Answers1

5

See the documentation of comparison operators: they are chained rather than grouped. So 1 == 0 in (0,1) is equivalent to (1==0) and (0 in (0,1)), which is obviously false.

Alan
  • 9,410
  • 15
  • 20