Why is the output False?
a=[1,8,8,False] print ((True is a) in a) #True print(True is (a in a)) #True print(True is a in a) #False - why??? True is not 8. So it's False. But False in a - so It must be True. It seems like 'in a' ignores at all print(8 is a in a) #True - why??? The same as above b=[0,0] print(8 is a in b) #False . In this case we see that 'in b' wasn't be ignored. And result is totally different. b=[True] print(8 is a in b) #False
3/23/2019 3:52:46 PMЭдуард
15 AnswersNew Answer
The doc puts is, is not, in, not in and every comparison on one precedence level and states they are evaluated from left to right, chained up. So True is a in a should be equal to: (True is a) and (a in a) Just like 1<3<5 equals 1<3 and 3<5 Since only one of the terms is True: False. https://docs.python.org/3/reference/expressions.html
print(True is a in a) # False - why??? Because "python" Do not care about "in a " Example : print(True is a in C) # False Although "C" is not defined
That’s ok Aravazhi I was thinking the same 🤔 Thanks Hicham I already knew the difference in those operators but I’m missing how they’re applied in the flow of execution here? For example True is a in a If the left/‘is’ part is evaluated first True is a = False False in a = False (because they are two different boolean objects?) If the right/‘in’ part is evaluated first a in a = True True is True = False (again different boolean objects?) If so, I then wouldn’t know how the first two statements evaluate to their boolean values
Sorry Эдуард for hijacking the comments My take from Hicham‘s answer is that booleans have been evaluated as integers which makes the ‘is’ operator see them as different boolean objects/values Someone correct me if otherwise?
HonFu master of Python 👏👏👏👏👏👏👏👏👏
Gordon explains it. False is True in invalid_name -> False is True and True in invalid_name Works because 'and' short circuits the check.
sometimes the bool(True/False ) takes intigers values Like : (True * 3 )== 3 I think that our bollien taking integer values, so using 'is' gives us 'False'
Hicham thanks so much for your reply 🙂 I understand what you said and have learned that booleans can be used that way as integers
https://code.sololearn.com/cUbwA585OqYl/?ref=app I referred to the site: https://stackoverflow.com/questions/13650293/understanding-pythons-is-operator and tried to verify the objects being referred using id() function.. still could not understand the statement in question why it returns False.. throughout the execution, true is pointing to single object and false is pointing to single object..
a=[1,8,8,False] print ((True is a) in a) #True print(True is (a in a)) #True print(True is a in a) #False - why??? print(True is 8) # diff values, so False print(8 is a in a) #True - why print(8 is 8) #same values, so true b=[0,0] print(8 is a in b) #False b=[True] print(8 is a in b) #False
So without brackets - like the first print statements - the ‘in’ part of the expression is evaluated first? a in a and return a?
Jenine Thanks, your query clarified that my response is not correct.. a in a will return True, not a.. In that case, I am not sure how the statement print(True is a in a) returns False, as I feel it should return True whichever operator ( is or in) is evaluated first..
And i'm still don't understand what's going on in this code😔