Python 3 Logical not is returning True as True

67 Views Asked by At

In my attempt to learn Python I have been writing code from tutorials and my own. I am using Python 3.

The piece of code that is doing me in:

>>>print(not 1 == 1 or 6 == 6 and 9 == 9)  
True

I've run the separate parts of this code. It seems the OR operator is negating itself with a double True. (not True or True) outputs True instead of False? Isn't the or operator suppose to conclude upon the first True and the not operator returns True as False?

1

There are 1 best solutions below

2
On

You should look in to operator precedence.

Let's examine this expression and handle each operator according to their precedence:

not 1 == 1 or 6 == 6 and 9 == 9

First, the == operators are executed, so we get:

not True or True and True

Then, the not operator:

False or True and True

Then, the and operator:

False or True

Then, the or operator, producing the result you're seeing:

True