Precedence and Associativity of Operators in Python
Last Updated :
17 Sep, 2025
In Python, operators have different precedence levels, which determine order in which expressions are evaluated. If operators have same precedence, associativity decides whether they are evaluated left-to-right or right-to-left.
Operators Precedence
Operator precedence defines order in which Python evaluates different operators in an expression. When an expression has multiple operators, Python follows precedence rules to decide order of evaluation.
Expression:
10 + 20 * 30

Example: This code shows how Python evaluates + and * operators according to precedence rules.
Python
# Precedence of '+' & '*'
expr = 10 + 20 * 30
print(expr)
Precedence of Logical Operators
Logical operators (and, or, not) also follow precedence.
Example: This code demonstrates how and has higher precedence than or affecting conditional evaluation. Using parentheses can override default order.
Python
# Precedence of 'or' & 'and'
name = "Alex"
age = 0
if name == "Alex" or name == "John" and age >= 2:
print("Hello! Welcome.")
else:
print("Good Bye!!")
Output
Hello! Welcome.
Explanation:
- and has higher precedence than or.
- So name == "John" and age >= 2 is checked first.
- Since name == "Alex" is already true, whole condition passes.
To fix this, use parentheses:
if (name == "Alex" or name == "John") and age >= 2:
Here, parentheses ensure the or part is checked first.
Operator Precedence and Associativity List in Python
Consider following list of operator precedence and associativity in Python. It shows all operators from highest precedence to lowest precedence.
- () : Parentheses (highest precedence) -> Associativity: Left to right
- x[index], x[index:index] : Subscription, slicing -> Associativity: Left to right
- await x : Await expression
- ** : Exponentiation -> Associativity: Right to left
- +x, -x, ~x : Unary plus, unary minus, bitwise NOT -> Associativity: Right to left
- *, @, /, //, % : Multiplication, matrix multiplication, division, floor division, remainder -> Associativity: Left to right
- +, - : Addition and subtraction -> Associativity: Left to right
- <<, >> : Bitwise shifts -> Associativity: Left to right
- & : Bitwise AND -> Associativity: Left to right
- ^ : Bitwise XOR -> Associativity: Left to right
- | : Bitwise OR -> Associativity: Left to right
- in, not in, is, is not, <, <=, >, >=, !=, == : Comparisons, membership, identity tests -> Associativity: Left to right
- not x : Boolean NOT -> Associativity: Right to left
- and : Boolean AND -> Associativity: Left to right
- or : Boolean OR -> Associativity: Left to right
- if-else : Conditional expression -> Associativity: Right to left
- lambda : Lambda expression
- := : Assignment expression (Walrus operator) -> Associativity: Right to left
Note: Parentheses () have highest precedence and can override default order. Some operators, like await and lambda, do not have associativity.
Operators Associativity
If an expression contains two or more operators with same precedence then Operator Associativity is used. It can either be Left to Right or from Right to Left.
Expression:
100 / 10 * 10

Example 1: Demonstrates left-to-right associativity for division, multiplication, addition and subtraction.
Python
# Division and multiplication
print(100 / 10 * 10)
# Addition and subtraction
print(5 - 2 + 3)
print(5 - (2 + 3))
Explanation:
- 100 / 10 * 10: division and multiplication have same precedence, so they are evaluated left to right: (100 / 10) * 10 = 100.0.
- 5 - 2 + 3: subtraction and addition also have same precedence, so it goes left to right: (5 - 2) + 3 = 6.
- 5 - (2 + 3): parentheses change the order, so 5 - 5 = 0.
Example 2: Demonstrates right-to-left associativity for exponentiation (**).
Python
# Exponentiation is right-to-left
print(2 ** 3 ** 2)
Explanation: Exponentiation (**) is evaluated right to left, so 2 ** (3 ** 2) = 2 ** 9 = 512.
Operators Precedence and Associativity
Operators Precedence and Associativity are two main characteristics of operators that determine evaluation order of sub-expressions in the absence of brackets.
Expression:
100 + 200 / 10 - 3 * 10

Example: In this example, we calculate an expression containing addition, subtraction, multiplication and division to demonstrate how Python evaluates operators based on precedence and associativity.
Python
expression = 100 + 200 / 10 - 3 * 10
print(expression)
Non-associative Operators
In Python, most operators have associativity, which means they are evaluated from left to right or right to left when they have same precedence. However, there are a few operators that are non-associative, meaning they cannot be chained together.
Example: This code demonstrates that = and += cannot be chained because they are non-associative operators.
Python
a = 5
b = 10
c = 15
a = b = (a < b) += (b < c)
Output
a = b= (a < b) += (b < c)
^^
SyntaxError: invalid syntax
Explanation: = and += are non-associative; combining them in one expression causes a syntax error.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice