I can't find an explanation for a seemingly simple problem:why is the answer always False in this task? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I can't find an explanation for a seemingly simple problem:why is the answer always False in this task?

arr=[1,True,"a",2] print("a" in arr in arr)

26th Mar 2022, 6:51 PM
ice
ice - avatar
2 Answers
+ 4
Chained operators tend to work a funny way in python. In your case, there are two and-chained containment operations. ("a" in arr) and (arr in arr) The first is true, the second however false. Hence the output. This is different from ("a" in arr) in arr, this would evaluate first to True for "a" in arr. Then evaluate True in arr, which is also true The output then would be True.
26th Mar 2022, 7:18 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
0
arr=[1,True,"a",2] Because arr is a list. Did you find a list in arr? No. Let me show it: Is 1 == [1,True,"a",2] ? No Is True == [1,True,"a",2] ? No Is "a" == [1,True,"a",2] ? No Is 2 == [1,True,"a",2] ? No So it will be never true.
26th Mar 2022, 7:24 PM
Stefanoo
Stefanoo - avatar