+ 4

Python: ‘a’ in arr in arr - Why False?

arr = [1, True, 'a', 2] print('a' in arr in arr) # False This was a SoloLearn match and I would like to understand how it is exactly interpreted by Python. Certainly not this way: print(('a' in arr) in arr) because it would be equivalent to 1 in arr which is clearly True. I can also not translate it to print('a' in (arr in arr)) because I get a TypeError: argument of type 'bool' is not iterable. Is the first code refer to an embedded list? If yes, where is the embedded list exactly?

12th Feb 2020, 4:49 AM
Prof. Dr. Zoltán Vass
7 ответов
+ 4
It's because of operator chaining. Similar to how you can write in python: 0 < x < 10 https://www.tutorialspoint.com/chaining-comparison-operators-in-python
12th Feb 2020, 5:01 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Tibor Santa Thanks Tibor! “if the expression is x < y < z, then it is similar to the x < y and y < z”
12th Feb 2020, 5:19 AM
Prof. Dr. Zoltán Vass
+ 1
Mirielle👽 Seems to solve the problem. Could you formulate a general rule for that?
12th Feb 2020, 4:59 AM
Prof. Dr. Zoltán Vass
0
Jay Matthews See Mirielle’s answer
12th Feb 2020, 10:40 AM
Prof. Dr. Zoltán Vass
- 2
Suraj parida
13th Feb 2020, 7:22 PM
Narayan Parida
Narayan Parida - avatar