Operator Precedence | SoloLearn: Learn to code for FREE!

+3

Operator Precedence

What is the answer for this: 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") i understand up to part 7==8.And then i don't understand what happen and what to do after print("Yes").I don't get it and what is the answer?

5/5/2020 8:50:44 AM

Syakirin Ooi

2 Answers

New Answer

+2

First of all we have three conditions : 1+1==y [y=2] 1+1==2 Hence this statement is True x==4 [x=4] 4==4 This is also True 7==8 This is False So our condition not 1 + 1 == y or x == 4 and 7 == 8 is now not True or True and False Now according to operators precedence: 1.Arithmetic operators[already solved above] 2.not 3.and 4.or Now evaluating it not True or True and False False or True and False False or False False Hence if condition is false andelif is true because x>y therefore answer will be No. I Hope this helps🙂🙂

0

x = 4 y = 2 Let's break down the conditions : 1+1 == y # True. Because 1+1 = 2 and y is also 2. So 2 == 2 is True However adding 'not' in front of True makes it false. So not 1+1 == y # False As it is false it moves forward and check the next condition. The next condition is : x == 4 and 7 == 8 x == 4 is True but 7 == 8 is false. And know we know operator 'and' returns true only if all the condition is true. We can see that one condition is True and the second one is False. Therefore x == 4 and 7 == 8 # False And hence not 1+1 == y or y == 4 and 7 == 8 # False That is why 'Yes' doesn’t get printed. However we let's move forward. x > y # True. Because 4 > 2 As a result we get 'No' as output.