How does Python parse `7 in x == True`?

108 Views Asked by At

I saw a question on es.stackoverflow.com in which the author tried to explicitly compare the result of a boolean expression to True.

if nota in lista1 == True:
    ...

I wasn't sure of the operator precedence myself, so I tried this in the python3 repl:

solomon@Solomons-Macintosh ~ % python3
Python 3.9.6 (default, May  7 2023, 23:32:44) 
[Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> x = [3, 7]
>>> (7 in x) == True
True
>>> 7 in x == True
False
>>> 7 in (x == True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable

How does Python parse 7 in x == True? It's not the same as (7 in x) == True because it returns a different result. It's not the same as 7 in (x == True) because it doesn't raise an error. So what is it? What does it mean?

0

There are 0 best solutions below