The Wayback Machine - https://web.archive.org/web/20261024044425/https://www.geeksforgeeks.org/python/precedence-and-associativity-of-operators-in-python/

Precedence and Associativity of Operators in Python

Last Updated : 7 Oct, 2026

Operator precedence and associativity determine the order in which Python evaluates expressions containing multiple operators. Precedence decides which operator is evaluated first, while associativity determines the evaluation order when operators have the same precedence. Understanding these rules helps in writing correct and readable expressions.

Precedence vs. Associativity

Operator PrecedenceOperator Associativity
Determines which operator is evaluated first.Determines the evaluation order when operators have the same precedence.
Applies to operators with different precedence levels.Applies only to operators with equal precedence.
Example: 10 + 20 * 30Example: 100 / 10 * 10

Operators Precedence

Operator precedence determines the order in which Python evaluates operators in an expression. Operators with higher precedence are evaluated before operators with lower precedence. Parentheses can be used to override the default evaluation order.

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

Operator Precedence with 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.

Precedence Table

Precedence (Highest to Lowest)OperatorsAssociativity
Parentheses()Left to Right
Subscription[], slicingLeft to Right
Awaitawait—
Exponentiation**Right to Left
Unary+x, -x, ~xRight to Left
Multiplicative*, @, /, //, %Left to Right
Additive+, -Left to Right
Shifts<<, >>Left to Right
Bitwise AND&Left to Right
Bitwise XOR^Left to Right
Bitwise OR|Left to Right
Comparisons<, ==, is, inLeft to Right
Boolean NOTnotRight to Left
Boolean ANDandLeft to Right
Boolean ORorLeft to Right
Conditionalif...elseRight to Left
Lambdalambda—
Assignment Expression:=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

Operator associativity determines the order in which operators with the same precedence are evaluated. Most Python operators are evaluated from left to right, while operators such as exponentiation (**) are evaluated 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.

Combining 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

Step-by-Step Expression Evaluation

Python first evaluates operators according to their precedence. If multiple operators have the same precedence, they are evaluated according to their associativity.

Example:

Python
expression = 100 + 200 / 10 - 3 * 10
print(expression)

Output
90.0

Evaluation Steps:

100 + 200 / 10 - 3 * 10

Step 1: Evaluate division (/) and multiplication (*), as they have higher precedence than addition and subtraction.

100 + 20.0 - 30

Step 2: Evaluate addition (+) and subtraction (-) from left to right because they have the same precedence.

120.0 - 30

Step 3: Compute the final result.

90.0

Explanation:

  • Division (/) and multiplication (*) are evaluated before addition (+) and subtraction (-) because they have higher precedence.
  • Addition and subtraction have the same precedence, so they are evaluated from left to right.
  • The final value of the expression is 90.0.

Non-associative Operators

Unlike most operators, assignment operators such as =, +=, -=, *=, and /= are statements rather than expressions. They cannot be used inside another expression or combined in the same way as arithmetic or logical operators.

Example: This code demonstrates that = and += cannot be chained because they are non-associative operators.

Python
a = 5
b = 10

a += b
print(a)

Output
15

Invalid Usage: The following code raises a syntax error because the augmented assignment operator (+=) cannot be used as part of another expression.

Python
a = 5
b = 10

a = (b += 1)

Output

SyntaxError: invalid syntax

Explanation:

  • = assigns a value to a variable.
  • += updates the value of a variable by adding the right-hand operand.
  • Augmented assignment operators (+=, -=, *=, etc.) are statements and cannot be nested inside another assignment or expression.
  • If multiple operations are required, perform them as separate statements.
Comment