Understanding Operator Precedence
A useful first question is: which operators have the highest precedence in this expression? Evaluate those operations before moving to the lower-precedence operations.
Try it: Order of Operations
How Python decides which operator in an expression runs first — precedence, left-to-right order and right-to-left **, plus what /, // and % really return.
How it works
- Parentheses first; then **, which groups right to left and binds tighter than a minus sign on its left.
- Then *, /, // and %, left to right; then + and −, left to right.
- / always gives a float; // rounds down; % takes the sign of the divisor.
- Each step replaces one operation with its value until one number is left.
Default run (7 steps): Expression: 2 + 3 * (7 - 4) ** 2 // 5. Python evaluates left to right, but higher-precedence operators bind first. … Result: 7 (an int).
Simplified: Numbers (at most 6 digits) and arithmetic operators only; parsed and evaluated by the lab's own evaluator (no eval), checked against real Python results.
Loading the simulation…
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.
- 1Identify the operators
The expression contains subtraction, multiplication, addition, and division.
- 2Find the higher-precedence operations
Multiplication and division have higher precedence than addition and subtraction, so evaluate 2 * 3 and 8 / 4 first.
- 3Evaluate the higher-precedence operations
The multiplication produces 6, and the division produces 2.
- 4Continue 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
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.
- 1Compare the operators
Subtraction and addition have equal precedence.
- 2Apply the direction rule
Because the operators have equal precedence, evaluate from left to right.
- 3Calculate
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.
2 + 3 * 4Multiplication happens before addition, producing 14.
(2 + 3) * 4The parentheses make addition happen first, producing 20.
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
Common mistake Evaluating every expression strictly from left to right
What it looks likeReading 2 + 3 * 4 as (2 + 3) * 4Why it failsMultiplication has higher precedence than addition.
Do this insteadEvaluate 3 * 4 first, then add 2.
Common mistake Stopping after applying precedence and forgetting the left-to-right rule
What it looks likeTreating 10 - 6 + 2 as 10 - (6 + 2)Why it failsSubtraction and addition have equal precedence, so they are evaluated from left to right.
Do this insteadCalculate 10 - 6 first, then add 2.
Common mistake Assuming parentheses are only for changing a result
What it looks likeLeaving a complicated expression ungrouped even when its intent is important
Why it failsParentheses also make the intended operation order explicit and improve readability.
Do this insteadAdd 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.
- 1Evaluate multiplication
4 * 2 produces 8, so the expression becomes 12 + 8 - 3.
- 2Evaluate 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.