0

can someone explain me this example with precedence

x = 4 y = 2 if not 1 + 1 == y or x == 4 and 7 == 8: print("Yes") elif x > y: print("No")

3rd Sep 2020, 7:34 AM
Raghav A
Raghav A - avatar
4 Answers
+ 3
Let's break it down to smaller pieces. if 1 +1 == y => True if not 1+1 ==y => False So the first part of the expression before 'or' is False, therefore the code ignores and looks at the next piece. x==4 and 7==8 => False Both expressions must be True before the 'and' will elevate to True. Therefore, the first complete expression is False, which means print('Yes') will NOT happen. elif x>y: 4 > 2? => True print('No') will happen Output => No
3rd Sep 2020, 7:46 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
1+1==y is 2==2 this is True x==4 True 7==8 False If not True or True and False not True = False If False or True and False If True and False If False: print("yes") elif x>y: print("No") x>y is True So prints No
3rd Sep 2020, 7:43 AM
Muhammadamin
Muhammadamin - avatar
+ 1
Raghav A The most difficult part: if 1) (not 1+1==y) --> not 2==2 -->False --> 0 2) (x==4) --> True --> 1 3) (7==8) --> False --> 0 4) or is equal to + 5) and is equal to * 6) 0+1*0 --> 0 7) As the if statement is False, it never runs. elif 1) x>y --> 4>2 --> True. 2) As the elif statement is True, it runs and prints "No".
3rd Sep 2020, 7:48 AM
Web-Learner
+ 1
x = 4 y = 2 In line 3, if (not 1+1==y or x==4 and 7==8) here, Let's assign the value first, not 1+1==2 or 4==4 and 7==8 Here precendence is ==, and, or, not Let's move back to the point. Then as per precendence we are gonna solve this, Firstly ==. Then, not 2==2 or 4==4 and 7==8 not True or True and False I think you would have learnt about Boolean operator. However let me explain. Secondly and. Then, True and True = True otherwise False. not True or Ture and False not True or False I think you would have understood how it's false Thirdly or. Then, False or False = False otherwise True. not True or False not True = False Then, if False so it goes to the elif x>y where x>y = 4>2 = True so elif statement is performed. https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2280/?ref=app
3rd Sep 2020, 8:50 AM
Jenson Y