+ 6

Python - Why without parentheses is False?

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

1st May 2019, 3:38 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
3 Antworten
+ 3
'a' in arr in arr == ('a' in arr) and (arr in arr) == (True) and (False) == False. "Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are comparison operators, then (a op1 b op2 c ... y opN z) is equivalent to (a op1 b and b op2 c and ... y opN z), except that each expression is evaluated at most once". https://docs.python.org/3/reference/expressions.html#comparisons
1st May 2019, 4:00 PM
Diego
Diego - avatar
+ 2
I think in is evaluated from right to left.
1st May 2019, 3:40 PM
Dragonxiv
Dragonxiv - avatar
+ 1
python 2 used to work without parentheses, but in python 3 y need to separate your code using parentheses
1st May 2019, 11:38 PM
Malek Trad