Operator chain at work.
'a' in 'ab' == True
equivalently
'a' in 'ab' and 'ab' == True
Take a look:
>>> 'a' in 'ab' == True False >>> ('a' in 'ab') == True True >>> 'a' in ('ab' == True) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: argument of type 'bool' is not iterable >>> 'a' in 'ab' and 'ab' == True False
From the documents mentioned above:
The comparison can be braced arbitrarily, for example, x <y <= z is equivalent to x <y and y <= z, except that y is evaluated only once (but in both cases z are not evaluated at all when x <y is considered false).
Formally, if a, b, c, ..., y, z are expressions and op1, op2, ..., opN are comparison operators, then op1 b op2 c ... y opN z is equivalent to the operations op1 b and b op2 c and ... y opN z, except that each expression is evaluated no more than once.
The real advantage of the chain of operators is that each expression is evaluated once, at most. Thus, with a < b < c , b is evaluated only once, and then first compared with a and secondly (if necessary) to c .
As a more specific example, consider the expression 0 < x < 5 . Semantically, we want to say that x is in a closed range [0.5]. Python fixes this by evaluating the logically equivalent expression 0 < x and x < 5 . Hope, which somewhat clarifies the purpose of the operator chain.