Help with Operator Precedence | SoloLearn: Learn to code for FREE!

+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")

8/16/2018 11:49:58 AM

Gui Domingues

3 Answers

New Answer

+1

because first if condition is false if (not 1+1 == y) or ((x == 4) and (7 == 8))

+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.

0

Thank you for the help!