+ 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)
3 Answers
+ 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
+ 2
I think in is evaluated from right to left.
+ 1
python 2 used to work without parentheses, but in python 3 y need to separate your code using parentheses