+ 2

logic behind this:

x = 4 y = 2 if not 1 + 1 == y or x == 4 and 7 == 8: print("Yes") elif x > y: print("No") Can anyone please clear me above code line to line?

5th Jun 2020, 3:14 PM
Anil Gurjar
Anil Gurjar - avatar
4 Antworten
+ 4
Order of precedence: == > Not > And > Or Step 1- Evaluate all "==" first. (not 1+1==y or x==4 and 7==8 ) => (not True or True and False ) Step 2- Evaluate "not" => (False or True and False ) Step 3- Evaluate "and" => (False or False ) Step 4- Evaluate "or" => (False ) So now the code is simplified to if False==True: print("Yes") elif x>y: print("No") Hope it helped. 🤗
5th Jun 2020, 6:12 PM
Manish Kumar Mohanty
Manish Kumar Mohanty - avatar
+ 2
DeWill Firstly, 7==8 gives False i.e. 0 *instead of* 1. You can check it out in Code Playground. Secondly, comparison operator like '==' have higher precedence than 'not' operator. So,I think that 1+1==y gets evaluated first, then 'not' operator is used on it. Mods, please correct me if I am wrong.
5th Jun 2020, 7:14 PM
Manish Kumar Mohanty
Manish Kumar Mohanty - avatar
+ 2
My bad.... 😅 Thanks for correcting me.....
6th Jun 2020, 3:12 PM
DeWill
DeWill - avatar
+ 1
DeWill Its ok. I am also learning..we all are here to learn 😁
6th Jun 2020, 3:21 PM
Manish Kumar Mohanty
Manish Kumar Mohanty - avatar