Can anybody tell me the reason for the output of this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can anybody tell me the reason for the output of this code?

diCt = { 4:[3,4,5],5:{34:"er"},6:57 } print(False==False)#True print(7 in diCt)#False print(bool(None))#False print(7 in diCt == bool(None))#False Why last one in "False"? If False==False shows True then why it is not the same for the last line?

4th Jun 2020, 8:39 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
8 Answers
+ 4
Consider this example print(5<6 == 6) #TRUE since < and == are of same precedence therefore chaining is done from left to right, so after < operator 6 is present and then it finds == operator so it chains 6 with the upcoming expression. So the expression turns to be : print(5<6 and 6 == 6) But if we use () it has higher precedence hence it bounds and seperates other opertors print((5<6) == 6) #FALSE
4th Jun 2020, 9:31 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 3
Well in last condition , it works as follows: print( 7 in diCt and diCt == bool(None)) So it returns false Change it to: print(( 7 in diCt) == bool(None)) It will return true The middle term participates in both comparisions
4th Jun 2020, 8:45 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 3
Python Documentation: "Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chaining features" These Operators have same precendence: in, not in, is, is not, <, <=, >, >=, !=, == So any term between them will be chained to the next expression
4th Jun 2020, 9:22 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 3
Sami Khan thanks Rithea Sreng thanks I think I have understand it. I will try different examples to make it clear. Thanks a lot .
4th Jun 2020, 9:23 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 2
If you go to the 'code' tab and press the plus, it'll open up a playground for you to code. You can copy & paste into there and test it.
4th Jun 2020, 8:43 AM
Olivia
Olivia - avatar
+ 2
Sami Khan Rithea Sreng Does this mean "==" has precedence over "in"? Sami Khan why (7 in diCt and diCt==bool(None)) ,why (7 in (diCt==bool(None))) doesn't work? How the "and" worked can you give me some hint?
4th Jun 2020, 9:07 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 2
Can you please break down the code line by line if you have time? Or may be some link to read about? (I am a new learner)
4th Jun 2020, 9:09 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 2
Sami Khan thanks for the example. It helped .
4th Jun 2020, 9:55 AM
Mir Abir Hossain
Mir Abir Hossain - avatar