+ 5

Why is the output False?

a=[1,8,8,False] print ((True is a[2]) in a) #True print(True is (a[2] in a)) #True print(True is a[2] 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[2] in a) #True - why??? The same as above b=[0,0] print(8 is a[2] 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[2] in b) #False

23rd Mar 2019, 3:52 PM
Эдуард
Эдуард - avatar
15 Antworten
+ 8
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[2] in a should be equal to: (True is a[2]) and (a[2] 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
26th Mar 2019, 8:13 AM
HonFu
HonFu - avatar
+ 3
print(True is a[2] in a) # False - why??? Because "python" Do not care about "in a " Example : print(True is a[2] in C) # False Although "C" is not defined
26th Mar 2019, 3:54 AM
Hicham Lamghari
Hicham Lamghari - avatar
+ 2
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[2] in a If the left/‘is’ part is evaluated first True is a[2] = False False in a = False (because they are two different boolean objects?) If the right/‘in’ part is evaluated first a[2] 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
24th Mar 2019, 3:07 PM
Jenine
+ 2
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?
25th Mar 2019, 9:20 PM
Jenine
26th Mar 2019, 4:16 AM
Hicham Lamghari
Hicham Lamghari - avatar
+ 2
HonFu master of Python 👏👏👏👏👏👏👏👏👏
26th Mar 2019, 9:03 AM
Gordon
Gordon - avatar
+ 2
Gordon explains it. False is True in invalid_name -> False is True and True in invalid_name Works because 'and' short circuits the check.
26th Mar 2019, 9:33 AM
HonFu
HonFu - avatar
+ 1
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'
24th Mar 2019, 3:28 PM
Hicham Lamghari
Hicham Lamghari - avatar
+ 1
Hicham thanks so much for your reply 🙂 I understand what you said and have learned that booleans can be used that way as integers
24th Mar 2019, 3:47 PM
Jenine
+ 1
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..
26th Mar 2019, 1:35 AM
Aravazhi
0
a=[1,8,8,False] print ((True is a[2]) in a) #True print(True is (a[2] in a)) #True print(True is a[2] in a) #False - why??? print(True is 8) # diff values, so False print(8 is a[2] in a) #True - why print(8 is 8) #same values, so true b=[0,0] print(8 is a[2] in b) #False b=[True] print(8 is a[2] in b) #False
24th Mar 2019, 1:41 AM
Aravazhi
0
So without brackets - like the first print statements - the ‘in’ part of the expression is evaluated first? a[2] in a and return a[2]?
24th Mar 2019, 8:13 AM
Jenine
0
Jenine Thanks, your query clarified that my response is not correct.. a[2] in a will return True, not a[2].. In that case, I am not sure how the statement print(True is a[2] in a) returns False, as I feel it should return True whichever operator ( is or in) is evaluated first..
24th Mar 2019, 11:35 AM
Aravazhi
0
And i'm still don't understand what's going on in this code😔
25th Mar 2019, 8:06 PM
Эдуард
Эдуард - avatar