Why is the output False? | SoloLearn: Learn to code for FREE!

+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

3/23/2019 3:52:46 PM

Эдуард

+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

+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

+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

+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?

+2

https://code.sololearn.com/cWdzQwVT0bpC/?ref=app

+2

HonFu master of Python 👏👏👏👏👏👏👏👏👏

+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.

+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'

+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

+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..

+1

Hicham https://code.sololearn.com/cZJtgU67wiuv/?ref=app

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

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]?

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..

0

And i'm still don't understand what's going on in this code😔