3

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?

Alex
  • 189
  • 1
  • 9

1 Answers1

4

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 to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a 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
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • 2
    this one never gets old :) it's a duplicate, but hey, not all cases & combinations of `is`, `=`, `<`, `>` isn't covered and OPs don't have a clue of this chaining pattern and fail to see it most of the time. Every one of us should have a duplicate answer of that (I have one :)) – Jean-François Fabre Apr 18 '18 at 07:49
  • This particular case may take the prize for most naturally confusing combination though. `11 > 0 is True` is a nice sentence. – miradulo Apr 18 '18 at 07:53