Python 3.6.2 console:
>>> 11 > 0 is True
False
but
>>> 0 is True
False
>>> 11 > False
True
So, why 11 > 0 is True
is False
?
Python 3.6.2 console:
>>> 11 > 0 is True
False
but
>>> 0 is True
False
>>> 11 > False
True
So, why 11 > 0 is True
is False
?
This is an example of comparison chaining since both >
and is
are comparison operators.
Comparisons can be chained arbitrarily, e.g.,
x < y <= z
is equivalent tox < y and y <= z
, except thaty
is evaluated only once (but in both casesz
is not evaluated at all whenx < y
is found to be false).Formally, if
a, b, c, …, y, z
are expressions andop1, op2, …, opN
are comparison operators, thena op1 b op2 c ... y opN z
is equivalent toa op1 b and b op2 c and ... y opN z
, except that each expression is evaluated at most once.
Thus, it is equivalent to:
>>> (11 > 0) and (0 is True)
False