+ 2

Operator Precedence

What is the result of this code? x = 4 y = 2 if not 1 + 1 == y or x == 4 and 7 == 8: print("Yes") elif x > y: print("No")

14th Apr 2020, 8:28 AM
M∆π∆$
M∆π∆$ - avatar
5 Antworten
+ 6
Precedence for if statement is in the following order + == not and or 1+1 is done first, then all == in left to right order. So now you have not true or true and false then the boolean not is applied false or true and false then the and comparison is done false or false then the or comparison is done resulting in false Then the elif is done. Resulting in true and "No" is output.
14th Apr 2020, 9:13 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
No is answer as In if case and is firstly processed which results in 0 then or is processed not 2==y or x==1 this also results in 0 , so it goes for elif which results No.
14th Apr 2020, 8:47 AM
Raj Kalash Tiwari
Raj Kalash Tiwari - avatar
+ 3
M∆π∆$ This sounds like a homework question which could have been resolved by running the code yourself. Or are you looking for the logic behind the result?
14th Apr 2020, 9:04 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Well,I guess I was just a little confused about operator precedence.Thanks for helping me out
14th Apr 2020, 12:36 PM
M∆π∆$
M∆π∆$ - avatar
0
No
11th Jul 2020, 3:39 PM
Anmol Agrawal