Operator Precedence and Associativity in Python
Please see the following precedence and associativity table for
reference. This table lists all operators from the highest precedence to the
lowest precedence.
Precedence Operators Description Associativity
1 () Parentheses Left to right
Subscription,
2 x[index], x[index:index] Left to right
slicing
Await
3 await x N/A
expression
4 ** Exponentiation Right to left
Positive,
5 +x, -x, ~x negative, bitwise Right to left
NOT
Multiplication,
matrix, division,
6 *, @, /, //, % Left to right
floor division,
remainder
Addition and
7 +, – Left to right
subtraction
8 <<, >> Shifts Left to right
9 & Bitwise AND Left to right
10 ^ Bitwise XOR Left to right
11 | Bitwise OR Left to right
Precedence Operators Description Associativity
Comparisons,
in, not in, is, is membership
12 Left to Right
not, <, <=, >, >=, !=, == tests, identity
tests
13 not x Boolean NOT Right to left
14 and Boolean AND Left to right
15 or Boolean OR Left to right
Conditional
16 if-else Right to left
expression
Lambda
17 lambda N/A
expression
Assignment
expression
18 := Right to left
(walrus
operator)