Understanding Operator Precedence
Operator precedence is a set of rules that determines which operations Python performs first when multiple operators appear in the same expression.
Why Order Matters
When an expression contains several operators, Python must decide which operation to perform first. It does not simply evaluate every expression from left to right. Instead, it follows operator precedence: a set of rules that determines the order in which operations are performed.
Consider the expression 2 + 3 * 4. A strict left-to-right reading would add 2 and 3 first, producing 5, and then multiply by 4, producing 20. Python instead follows the familiar mathematical convention that multiplication comes before addition. It evaluates 3 * 4 first, producing 12, and then adds 2, producing 14.
Reading the Precedence Hierarchy
Python organizes operators into a precedence hierarchy. Operators with higher precedence bind more strongly and are evaluated before operators with lower precedence. Multiplication and division have higher precedence than addition and subtraction, so they are handled first when these operators appear together.
Operator precedence is the set of rules that determines which operations Python performs first when multiple operators appear in the same expression.
A useful first question is: which operators have the highest precedence in this expression? Evaluate those operations before moving to the lower-precedence operations.
Tracing a Mixed Expression
Evaluating 10 - 2 * 3 + 8 / 4
Determine the result by applying precedence rather than evaluating strictly from left to right.
Identify the operators: The expression contains subtraction, multiplication, addition, and division.
Find the higher-precedence operations: Multiplication and division have higher precedence than addition and subtraction, so evaluate 2 * 3 and 8 / 4 first.
Evaluate the higher-precedence operations: The multiplication produces 6, and the division produces 2.
Continue with the remaining expression: The expression is now 10 - 6 + 2. Subtraction and addition have equal precedence, so Python evaluates them from left to right: 10 - 6 produces 4, and then 4 + 2 produces 6.
The result is 6.
What do you think happens?
Predict the result of 8 + 2 * 5 before checking the explanation.
Reveal answer
Answer: 18
Multiplication has higher precedence than addition, so 2 * 5 is evaluated first, producing 10. Then 8 is added, producing 18.
Equal-Precedence Operators
Precedence alone does not decide everything. If the operators that remain have equal precedence, Python evaluates them from left to right. In the source expression 10 - 6 + 2, subtraction and addition have equal precedence, so Python performs 10 - 6 first and then adds 2 to the result.
Left-to-right evaluation at one precedence level
Determine the result of 18 - 5 + 3.
Compare the operators: Subtraction and addition have equal precedence.
Apply the direction rule: Because the operators have equal precedence, evaluate from left to right.
Calculate: First calculate 18 - 5, producing 13. Then calculate 13 + 3.
The result is 16.
The complete reasoning pattern is: first group operations by precedence, then evaluate equal-precedence operations from left to right.
Parentheses as an Override
Parentheses override the default precedence rules. They explicitly show which operation should happen first, even when that operation would normally have lower precedence. Parentheses therefore affect both the calculation and the readability of an expression.
Use parentheses when the intended grouping might not be immediately obvious. Even when you understand the default precedence, explicit grouping can make the expression easier for another reader to interpret and maintain.
Mistakes in Evaluation
Evaluating every expression strictly from left to right
Multiplication has higher precedence than addition.
Fix:
Evaluate 3 * 4 first, then add 2.Stopping after applying precedence and forgetting the left-to-right rule
Subtraction and addition have equal precedence, so they are evaluated from left to right.
Fix:
Calculate 10 - 6 first, then add 2.Assuming parentheses are only for changing a result
Parentheses also make the intended operation order explicit and improve readability.
Fix:
Add parentheses when they clarify which operations belong together.
Practice the Trace
Without running code, determine the result of 12 + 4 * 2 - 3. Write the intermediate expression after evaluating multiplication, then apply the remaining addition and subtraction from left to right.
Hints
- Multiplication has higher precedence than addition and subtraction.
- After multiplication, addition and subtraction have equal precedence.
- Evaluate the remaining addition and subtraction from left to right.
Practice solution
Evaluate 12 + 4 * 2 - 3.
Evaluate multiplication: 4 * 2 produces 8, so the expression becomes 12 + 8 - 3.
Evaluate equal-precedence operators: Addition and subtraction have equal precedence, so move from left to right: 12 + 8 produces 20, and 20 - 3 produces 17.
The result is 17.
Working with Intent
To predict an expression's result, do not begin by scanning blindly from left to right. Identify the operators with higher precedence, evaluate them first, and then handle equal-precedence operators from left to right. When the intended grouping should be especially clear, use parentheses to override the default order or to communicate that order directly to readers.
- Operator precedence determines which operations Python performs first.
- Multiplication and division are evaluated before addition and subtraction.
- Operators with equal precedence are evaluated from left to right.
- Parentheses override default precedence and make grouping explicit.
- Clear grouping improves the readability and maintainability of expressions.
Key Takeaways
- Python uses precedence rules instead of evaluating every mixed expression strictly from left to right.
- Higher-precedence operations such as multiplication and division are performed before lower-precedence addition and subtraction.
- When operators have equal precedence, Python evaluates them from left to right.
- Parentheses override the default order and make intended grouping visible.
- Tracing an expression step by step helps you predict results and avoid evaluation mistakes.