+ 2
In operator confusing example
Hi, In the following code snippet: arr = ["a", True] print("a" in arr in arr) #False I feel like this should either err (trying to check if “a” is in True) or give True (True is in arr). However it gives false, how do you explain it?
3 Réponses
+ 11
Edward Finkelstein ,
python evaluates this expression like: (read more in python reference/ expressions)
see also this thread in stackoverflow.com:
https://stackoverflow.com/questions/60400708/why-a-in-arr-in-arr-a-in-arr-in-arr#60400956
first evaluation: ("a" in arr) => True
second evaluation: (arr in arr) => False, because arr is not in arr
True and False => False
if we use parenthesis for grouping, the result is different:
print(("a" in arr) in arr) => True
+ 4
Here is a similar question:
https://www.sololearn.com/Discuss/2166930/?ref=app
If I understand it correct it first checks if "a" in arr (True), then it checks if arr in arr (False).
True and False leads to False.
+ 2
EDIT: I'm wrong. Always learning! Check answers below 👍