+ 2

Boolean logic

x = 4 y = 2 if not 1 + 1 == y or x == 4 and 7 == 8: print("Yes") elif x > y: print("No") why is the ouput "No" for this code?

24th Sep 2020, 3:02 PM
Nishtha
Nishtha - avatar
7 Respuestas
+ 8
Abhay Me* thinks you meant: (not 2==2) or (4==4 and 7==8) 0 or (1 and 0) 0 or 0 0 Same result, different order of precedence. 😉👌 You can test this using values that would yield different results based on order of precedence. print(1 or 1 and 0) #1 print(1 or (1 and 0)) #1 print((1 or 1) and 0) #0 Here, we can see the first two match, therefore, the 2nd line reflects the actual order of precedence. I hope this was helpful. 🤓🤘
24th Sep 2020, 5:18 PM
David Carroll
David Carroll - avatar
+ 3
Abhay Yeesh... that TutorialsPoint article is about as bad as something I'd see on a W3schools or GeeksForGeeks website. Here's a tip, stay away from articles that don't have a date, the author's information, and / or comments section. These are SEO websites that prey on learners doing searches and specialize more on garbage content than being accurate to focus more on ad revenue. 😁 I do typically prefer the language websites for the most accurate information.
24th Sep 2020, 11:11 PM
David Carroll
David Carroll - avatar
+ 2
Inline Responses: ---- "I really meant if not 1 or 1 and 0" -> I was only focusing on the boolean operators, not the comparison operator. ---- "As == has highest precedence" -> For clarification, while comparison operators do have a higher precendence over boolean (and|or|not) operators, the == is NOT the highest when compared to bitwise, arithmetic, and a few other operators. ---- "I read that operator with same precedence can be evaluted left to right or right to left" -> Operators with the same precendence can be evaluated left-to-right. -> For example (3 - 4) can only be evaluated from left-to-right as (4 - 3) would yield a different result. ---- Ultimately, you're understanding is very close. The one thing that might have tripped you up is not realizing that the logical "or" has a lower precendence than logical "and". This link might help clarify things: https://docs.python.org/3/reference/expressions.html#operator-precedence NOTE: The order is listed from lowest to highest in this list.
24th Sep 2020, 6:51 PM
David Carroll
David Carroll - avatar
+ 1
#Boolean logic x = 4 y = 2 if not 1 + 1 == y or ( x == 4 and 7 == 8) : #works like this print("Yes") elif x > y: print("No") #first 'and' logic gets executed according to precedence rule, so x==4 true but 7==8 false so totally true and false => false #then it is now : not 1+1 == y or false #not 2 == 2 or false #not true or false #false or false => false # so in else if part x>y true so prints No.
24th Sep 2020, 3:10 PM
Jayakrishna 🇮🇳
+ 1
if not 2==2 or x==4 and 7==8 if not 1 or 1 and 0 if 0 or 1 and 0 if 1 and 0 if 0 So the first statement is false Edit: answer is wrong ,refer to the one by David
24th Sep 2020, 3:12 PM
Abhay
Abhay - avatar
+ 1
David Carroll sorry I mistakenly read it as you were talking of the comparison operator as well ,wasn't clear from the answer (for me), By "highest" I didn't literally meant it has the highest precedence ,higher precedence than others in the expression for sure ,sorry for that typo as well, I was using some other article as a reference , https://www.tutorialspoint.com/JUMP_LINK__&&__python__&&__JUMP_LINK/operators_precedence_example.htm maybe I misunderstood that or I should start reading from python docs , Lastly ty for the article
24th Sep 2020, 10:43 PM
Abhay
Abhay - avatar
0
David Carroll I really meant if not 1 or 1 and 0 As == has highest precedence I read that operator with same precedence can be evaluted left to right or right to left if not 1 or 1 and 0 left-right=>0 or 1 and 0=>1 and 0=>0 And, right-left=>not 1 or 0=>not 1=>0 but maybe I am wrong about operators with same precedence due to my lack of understanding how it works ,as your example showed "and" has higher precedence than "or" otherwise it will result in different value
24th Sep 2020, 5:26 PM
Abhay
Abhay - avatar