Skip to main content

Python Precedence and Associativity of Operators

Precedence and Associativity are characteristics that determine how the Python interpreter should evaluate expressions.

The combination of values, variables, operators, and function calls is termed as an expression. The Python interpreter can evaluate a valid expression.

Precedence of Python Operators

The precedence of operators in Python is shown in the following table in descending order (i.e., higher rows have higher priority than lower rows)

OperatorsDescription
()Parentheses
**Exponent
+x, -x, ~xUnary plus, Unary minus, Bitwise NOT
*, /, //, %Multiplication, Division, Floor division, Modulus
+, -Addition, Subtraction
<<, >>Bitwise shift operators
&Bitwise AND
^Bitwise XOR
&124;Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not inComparisons, Identity, Membership operators
notLogical NOT
andLogical AND
orLogical OR
=
+= -=
*= /=
%= &=
^= &124;=
<<= >>=
Assignment
Addition/subtraction assignment
Multiplication/division assignment
Modulus/bitwise AND assignment
Bitwise exclusive/inclusive OR assignment
Bitwise shift left/right assignment

Examples

Precedence of + and *

expr = 10 + 20 * 30
print(expr) # 610

Precedence of AND, OR and parenthesis ()

a = "ok"
b = 10

print(a == "ok" or a == "oook" and b>=15) # True
print((a == "ok" or a == "oook") and b>=15) # False

Associativity of Python Operators

Groups of operators in the same rows in the previous table have the same precedence.

Associativity makes it possible to determine the order of operations that have the same precedence.

Associativity is the order in which an expression is evaluated that has multiple operators of the same precedence.

note

Almost all the operators have left-to-right associativity.

This table lists all operators from the highest precedence to the lowest precedence with associativity

OperatorDescriptionAssociativity
( )Parenthesesleft-to-right
**Exponentright-to-left
* / %Multiplication/division/modulusleft-to-right
+ -Addition/subtractionleft-to-right
<< >>Bitwise shift left, Bitwise shift rightleft-to-right
< <= > >=Relational less than/less than or equal to Relational greater than/greater than or equal toleft-to-right
== !=Relational is equal to/is not equal toleft-to-right
is, is not, in, not inIdentity
Membership operators
left-to-right
&Bitwise ANDleft-to-right
^Bitwise exclusive ORleft-to-right
&124;Bitwise inclusive ORleft-to-right
notLogical NOTright-to-left
andLogical ANDleft-to-right
orLogical ORleft-to-right
=
+= -=
*= /=
%= &=
^= &124;=
<<= >>=
Assignment
Addition/subtraction assignment
Multiplication/division assignment
Modulus/bitwise AND assignment
Bitwise exclusive/inclusive OR assignment
Bitwise shift left/right assignment
right-to-left

For examples:

# Left-right associativity
print(5 * 2 // 3) # 3
print(5 * (2 // 3)) # 0

# Right-left associativity of **
print(2 ** 3 ** 2) # 512 (Since 2**(3**2) = 2**9

# If 2 needs to be exponated fisrt, need to use ()
print((2 ** 3) ** 2) # 64

Non associative operators

Some operators, like assignment operators and comparison operators, do not have associativity in Python.

For example, x < y < z means neither (x < y) < z nor x < (y < z). x < y < z is equivalent to x < y and y < z, and is evaluated from left to right.

# Initialize x, y, z
x = y = z = 1

# Expression is invalid (Non-associative operators)
# x = y = z+= 2
# SyntaxError: invalid syntax