python operator precedence of in and comparison

The Python manual says in and == are of equal precedence. Thus, they’re evaluated from left to right by default, but there’s also chaining to consider. The expression you put above ('1' in '11' == True) is actually being evaluated as…

('1' in '11') and ('11' == True)

which of course is False. If you don’t know what chaining is, it’s what allows you to do something like…

if 0 < a < 1:

in Python, and have that mean what you expect (“a is greater than 0 but less than 1”).

Leave a Comment