Operator Precedence and Associativity in Python
Precedence Operators Description Associativity
1 () Parentheses Left to right
2 x[index], x[index:index] Subscription, slicing Left to right
3 await x Await expression N/A
4 ** Exponentiation Right to left
5 +x, -x, ~x Positive, negative, bitwise NOT Right to left
Multiplication, matrix, division,
6 *, /, //, % Left to right
floor division, remainder
7 +,- Addition and subtraction Left to right
8 << ,>> Shifts Left to right
9 & Bitwise AND Left to right
10 ^ Bitwise XOR Left to right
11 | Bitwise OR Left to right
in, not in, is, is Comparisons, membership tests,
12 Left to Right
not, <, <=, >, >=, !=, == identity tests
13 not x Boolean NOT Right to left
14 and Boolean AND Left to right
Operator Precedence and Associativity in Python
Precedence Operators Description Associativity
15 or Boolean OR Left to right
16 If else Conditional expression Right to left
17 lambda Lambda expression N/A
Assignment expression (walrus
18 := Right to left
operator)
Example -
result = 5 + 2 ** 3 * 2 // 3 - 4 + (6 - 2) * 3
print(result)
res = 5+4*9%(3+1)/6-1
print(res)