Open In App

Precedence and Associativity of Operators in Python

Last Updated : 17 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Operator-Precedence

Example: This code shows how Python evaluates + and * operators according to precedence rules.

Python
# Precedence of '+' & '*'
expr = 10 + 20 * 30
print(expr)

Output
610

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.

  1. () : Parentheses (highest precedence) -> Associativity: Left to right
  2. x[index], x[index:index] : Subscription, slicing -> Associativity: Left to right
  3. await x : Await expression
  4. ** : Exponentiation -> Associativity: Right to left
  5. +x, -x, ~x : Unary plus, unary minus, bitwise NOT -> Associativity: Right to left
  6. *, @, /, //, % : Multiplication, matrix multiplication, division, floor division, remainder -> Associativity: Left to right
  7. +, - : Addition and subtraction -> Associativity: Left to right
  8. <<, >> : Bitwise shifts -> Associativity: Left to right
  9. & : Bitwise AND -> Associativity: Left to right
  10. ^ : Bitwise XOR -> Associativity: Left to right
  11. | : Bitwise OR -> Associativity: Left to right
  12. in, not in, is, is not, <, <=, >, >=, !=, == : Comparisons, membership, identity tests -> Associativity: Left to right
  13. not x : Boolean NOT -> Associativity: Right to left
  14. and : Boolean AND -> Associativity: Left to right
  15. or : Boolean OR -> Associativity: Left to right
  16. if-else : Conditional expression -> Associativity: Right to left
  17. lambda : Lambda expression
  18. := : 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

Operator-Associativity

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))   

Output
100.0
6
0

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)  

Output
512

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

Operator-Precedence-and-Associativity

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)

Output
90.0

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