Search code examples
pythonoperator-precedencecomparison-operators

Why does print(3 > 0 == True) show False?


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.


Solution

  • 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