Python Operator Precedence and Evaluation Order
Learn how Python groups operators and evaluates expressions, so you can read complex expressions confidently and add parentheses when they make the meaning clearer.
Basic
Python can combine several operators in one expression. To read such an expression correctly, you need to know two different rules: operator precedence decides how the expression is grouped, while evaluation order decides when its parts are evaluated.
Precedence decides the grouping
|
|
Multiplication has higher precedence than addition, so the first expression is grouped like `2 + (3 * 4)`. Parentheses explicitly change the grouping.
Precedence is about which operators bind more tightly. A higher-precedence operator is grouped before a lower-precedence operator unless parentheses change the grouping. Python's reference lists the complete precedence table from the most binding forms to the least binding ones.
Parentheses make intent explicit
|
|
Parentheses are useful not only for changing the result, but also for showing the reader exactly how you intend the expression to be grouped.
When a calculation matters to the reader, explicit parentheses can be better than relying on memory of the precedence table. They turn an implicit grouping rule into visible intent.
Some operators group right-to-left
|
|
Exponentiation groups from right to left, so `2 ** 3 ** 2` means `2 ** (3 ** 2)`, not `(2 ** 3) ** 2`.
Most binary operators at the same precedence group from left to right. Exponentiation is an important exception, and conditional expressions also have their own right-to-left grouping rule. The conditional-expression lesson owns that syntax; here the important idea is that grouping direction is part of the language definition.
Unary minus and exponentiation
|
|
The power operator binds more tightly than unary minus on its left. Therefore `-2 ** 2` means `-(2 ** 2)`.
This is a classic place where parentheses help. If the negative number itself is meant to be the base, write `(-2) ** 2` so the intended grouping is unmistakable.
Same precedence is not the same as same operation
|
|
Division and multiplication have the same precedence and group from left to right, so the first expression is `(20 / 5) * 2`.
A useful habit is to identify the precedence level first, then apply the grouping direction for operators at that level. Parentheses can always make a different grouping explicit.
Precedence is different from evaluation order
|
|
The multiplication is grouped before the addition, but the operand expressions are still evaluated from left to right. The recorded order is `left`, `middle`, then `right`.
This distinction matters whenever evaluating an expression has an observable effect, such as calling a function. Precedence answers “how is the expression grouped?” Evaluation order answers “which subexpression runs first?” Python specifies left-to-right evaluation of expressions.
Function-call arguments are evaluated before the call
|
|
Both argument expressions are evaluated before `add()` is called, and the arguments themselves are evaluated from left to right.
Do not use precedence to predict side-effect order. A subexpression can belong to a lower-precedence part of the final grouping and still be evaluated before another subexpression that is grouped more tightly.
A practical reading method
|
|
Read each expression in layers: grouping first, then evaluation order, then the resulting values.
When an expression looks dense, break it mentally into smaller parts. Decide which operators bind first, note any left-to-right or right-to-left grouping rule, then follow Python's evaluation order. If you need to explain the result to someone else, add parentheses or split the expression into named intermediate values.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.