+ 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?
10 Antworten
+ 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
+ 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
+ 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
+ 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 .
+ 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.
+ 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?
+ 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)
+ 2
Sami Khan thanks for the example. It helped .