Ask Your Question
3

What is the order and way of grouping for the `in` and `==` operators in Python?

asked 2021-04-24 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-07-04 16:00:00 +0000

bukephalos gravatar image

The == (equality) and in (membership) operators have the same precedence level, which is lower than most of the arithmetic and bitwise operators.

They associate to the left, meaning that if there are multiple comparisons of equal or lower precedence in a single expression, they are evaluated from left to right.

For example, the expression 1 == 2 in [3, 4] is equivalent to (1 == 2) and (2 in [3, 4]), which evaluates to False.

The expressions 1 in [1, 2] == True and 1 in [1, 2] == False are both invalid syntax since they are ambiguous and do not have a well-defined order of evaluation.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-04-24 11:00:00 +0000

Seen: 18 times

Last updated: Jul 04 '21