+ 1

Help with Operator Precedence

Can someone explain to me why the OUTPUT of this code is "No"? x = 4 y = 2 if not 1+1 == y or x == 4 and 7 == 8 print("Yes") elif x>y print("No")

16th Aug 2018, 11:49 AM
Gui Domingues
Gui Domingues - avatar
3 odpowiedzi
+ 1
because first if condition is false if (not 1+1 == y) or ((x == 4) and (7 == 8))
16th Aug 2018, 12:48 PM
Roman Khristoforov
Roman Khristoforov - avatar
+ 1
In canonical form the if-statemet looks like this: (not ((1+1) ==y)) or ((x==4) and (7==8)) the priority of operators is as following: + == not and or. so left part gives us: (1+1) = 2 2 == y -> True (cuz y=2) not (true) -> False and right part gives us: (x==4) -> True (cuz x=4) (7==8) ->False (obvious) (True and False) - > False (logical AND) now we have both left and right parts False. and finally: (False) or (False) -> False (logiacal OR) if-satement failed to succeed, so we go on with elif statement, which is True per conditions.
16th Aug 2018, 3:17 PM
strawdog
strawdog - avatar
0
Thank you for the help!
17th Aug 2018, 11:29 AM
Gui Domingues
Gui Domingues - avatar