+ 1
boolean and operator precedence
i understand the answer of this program is "no" please can someone help me break it down What is the result of this code? x = 4 y = 2 if not 1 + 1 == y or x == 4 and 7 == 8: #i am more confused here, as there are or and and print("Yes") elif x > y: print("No")
2 odpowiedzi
+ 2
not 1 + 1 == y or x == 4 and 7 == 8
Let's change the variable names to their values to increase readability:
not 1 + 1 == 2 or 4 == 4 and 7 == 8
Arithmetic operators are performed first:
1 + 1 = 2
not 2 == 2 or 4 == 4 and 7 == 8
Comparison operators are performed second:
2 == 2 = True
4 == 4 = True
7 == 8 = False
not True or True and False
Now there are only logical operators left.
Comparison operators also have an operator precedence: not: first, and: second, or: third.
First let's get not True:
not True = False
False or True and False
Then let's get True and False:
True and False = False
False or False
Then let's get False or False and we'll have the final result:
False or False = False
final result:
False