-1
not 1==2 or 3==3 and 5==6

How is this evaluated? I know the order is not, and, or (strongest to weakest)

so if i do the not first, are these the steps:

  1. not 1==2 or 3==3 which equates to False
  2. then False and False equates to False

But I'm confused because the order is not, and, or

So why do I do the or first before the and?

flakes
  • 21,558
  • 8
  • 41
  • 88
123
  • 1
  • That expression evaluates to True when I run it. How did you get False as a result? – John Gordon Jan 11 '22 at 01:48
  • The expression contains another operator, the "==", and it has the highest priority. Does it make the evaluation clearer? – Nir H. Jan 11 '22 at 01:51
  • @JohnGordon yup i just ran it, sorry i made a mistake it does evaluate to True. But i posted this question cuz i was confused about why **or** is evaluated bfr **and**. Thank you!! – 123 Jan 11 '22 at 03:09

5 Answers5

5

First of all that expression gives out True as an output. The operation goes on like this step by step:

1. (not 1==2) or (3==3 and 5==6) 
2. (True) or (True and False)
3. True or False 
4. True 

for further reference check this out: Operator precedence in python

raiyan22
  • 1,043
  • 10
  • 20
  • Adding to the answer, since `(not 1==2)` is `True`. `(3==3 and 5==6)` wouldn't even be evaluated due to short-circuit evaluation. For example, `True or print("This will never execute")` gives `True` but doesn't print `This will never execute`. [Try it Online!](https://godbolt.org/z/zYrE5Maf5) – Ch3steR Jan 11 '22 at 05:38
3

We know not before and before or. or has logical short-circuitry - it stops as soon as one of the or expressions evaluates to True by returning True. and has logical short-circuitry concerning the first False occurring for any of the elements connected by and.

In (not 1==2) or (3==3 and 5==6):

(not 1==2) is already True. Due to logical short-circuitry we don't need to consider the rest, but know that this entire or expression returns then True.

Gwang-Jin Kim
  • 9,303
  • 17
  • 30
1

I'll use brackets to explain how this is being treated:

(not 1==2) or (3==3 and 5==6)

It performs the first check of not 1==2 which is True.

Let's say it equalled false, because there is an or next to it, it would then perform the next check of 3==3 and 5==6 which would then return False

Josh Ackland
  • 603
  • 4
  • 19
0

A or B returns A if A is True else it returns B i.e lazy evaluation.

(not 1==2)---> True

This might sound dumb but the code just stops there. the and part is not even reached

You don't believe?

print(not 1==2 or (print('The Code Reaches Here!'),4==2)[0] and (print('The Code Reaches Here!'),4==5)[0])

Try running this code. it will not print because the code is not reached there.

0

The reasoning isn't correct. You need to look at operator-precedence

so of the four operators you have, not x, ==, or, and, the ordering is:

  1. ==
  2. not x
  3. and
  4. or

This means not 1==2 or 3==3 and 5==6 equates to:

  1. not (1==2) or (3==3) and (5==6) => not False or True and False
  2. (not False) or True and False => True or True and False
  3. True or (True and False) => True or False
  4. True or False => True
flakes
  • 21,558
  • 8
  • 41
  • 88