Python's operator precedence and associativity rules determine how expressions are evaluated. They control which operations run first, how equal-precedence operators group, and when parentheses are needed to make code clearer.
Parentheses, exponentiation, multiplication/division, and addition/subtraction form the basic hierarchy. Comparison and logical operators have their own precedence levels. Associativity further refines how operators of equal precedence are evaluated, with most being left-associative except for exponentiation.
Operator Precedence and Associativity in Python
Order of operations in Python
- Operator precedence determines the order in which operations are performed in an expression
- Operations with higher precedence execute before those with lower precedence (multiplication before addition)
- Parentheses have the highest precedence and override the default order of operations (grouping expressions)
- Python follows PEMDAS (Parentheses, Exponentiation, Multiplication/Division, Addition/Subtraction) for operator precedence
- Parentheses: Expressions inside parentheses evaluate first
(2 + 3) * 4 = 20 - Exponentiation: Exponentiation (
**) has the highest precedence among arithmetic operators2 ** 3 = 8 - Multiplication and Division: Multiplication (
*), division (/), floor division (//), and modulo (%) have equal precedence and perform from left to right6 * 5 / 2 = 15 - Addition and Subtraction: Addition (
+) and subtraction (-) have equal precedence and perform from left to right5 + 3 - 2 = 6
- Parentheses: Expressions inside parentheses evaluate first
- Comparison operators (
==,!=,<,>,<=,>=) have lower precedence than arithmetic operators but higher than logical operators3 + 4 < 5 + 6evaluates toTrue - Logical operators (
and,or,not) have lower precedence than comparison operators, withnothaving the highest precedence among themTrue and False or Trueevaluates toTrue - Precedence rules govern the order of operations in complex expressions

Impact of operator associativity
- Associativity determines the order of evaluation for operators with equal precedence
- Left-associative operators evaluate from left to right
a - b - cequals(a - b) - c - Right-associative operators evaluate from right to left
a ** b ** cequalsa ** (b ** c)
- Left-associative operators evaluate from left to right
- Most Python operators are left-associative, including arithmetic, comparison, and logical operators
- Example:
10 - 5 + 3evaluates as(10 - 5) + 3 = 8due to left associativity
- Example:
- The exponentiation operator (
**) is right-associative- Example:
2 ** 3 ** 2evaluates as2 ** (3 ** 2) = 512due to right associativity
- Example:
- Associativity matters when dealing with operators of equal precedence to avoid unexpected results
8 / 4 * 2equals4, not1

Parentheses for expression control
- Parentheses explicitly specify the order of operations in an expression
- Expressions inside parentheses always evaluate first, regardless of operator precedence
(3 + 4) * 2 = 14
- Expressions inside parentheses always evaluate first, regardless of operator precedence
- Nested parentheses evaluate from the innermost to the outermost
- Example:
((2 + 3) * 4) - 5evaluates as(2 + 3)first, then multiplies by4, and finally subtracts5
- Example:
- Parentheses improve readability and clarity of complex expressions
- Example:
x * (y + z)is more readable thanx * y + x * z, even though they are equivalent
- Example:
- When in doubt, use parentheses to ensure the desired order of operations
- Example:
10 + 20 * 30 ** 2can be written as10 + (20 * (30 ** 2))to make the order explicit
- Example:
Expression Evaluation and Operator Chaining
- Expression evaluation follows the rules of precedence and associativity to determine the final result
- Operator chaining allows multiple comparison operators to be used in a single expression
- Example:
0 < x < 10is equivalent to0 < x and x < 10
- Example:
- The order of evaluation in chained comparisons is left-to-right, with short-circuiting applied