6

In Python,

1 in {1} == True

is False - why?

I first thought it's due to operator precedence, but all I can find (see below) tells me that the expression is evaluated from left to right:

  • 1 in {1} is True
  • True == True is True

Also, any other precedence would lead to a TypeError:

  • {1} == True is False
  • 1 in False returns a TypeError

Python evaluates expressions from left to right.

https://docs.python.org/3/reference/expressions.html#evaluation-order

Also,

Operators in the same box have the same precedence. [...] Operators in the same box group left to right ([...]).
[...] enter image description here

https://docs.python.org/3/reference/expressions.html#operator-precedence

bers
  • 4,817
  • 2
  • 40
  • 59

0 Answers0