0

The order precedence of python comparison operators is left to right. With this, print(3 > 0 == True) shows False, but the equivalent statement: print((3 > 0) == True) shows True. Additionally, print(3 > (0 == True)) shows True.

So why is it that print(3 > 0 == True) shows False?

My python version is 3.8.2.

  • 1
    They are *not* equivalent statements. `x > y == z` is not equivalent to *either* `(x>y) == z` or `x > (y == z)`. Comparison operators are not associative. – chepner Aug 17 '21 at 17:24

1 Answers1

3

What happens is this:

The value 3 > 0 == True is interpreted as (3>0) AND (0==True) which gives True AND False which is of course False

This is why for example the statement: 3 > 1 == True evaluates to True

aydee45
  • 516
  • 3
  • 14
  • 2
    See https://docs.python.org/3/reference/expressions.html#comparisons where comparisons can be chained. – Andrew Aug 17 '21 at 17:30