Debugging Expressions
Order of operations (PEMDAS) determines which calculations happen first: Parentheses, Exponentiation, Multiplication/Division, Addition/Subtraction.
Why Evaluation Order Matters
When an expression contains several operations, Python must determine which calculation happens first. It follows the mathematical convention called PEMDAS: Parentheses, Exponentiation, Multiplication and Division, then Addition and Subtraction. The higher-priority level is handled before a lower-priority level, even when the lower-priority operator appears earlier in the expression.
Tracing PEMDAS Step by Step
PEMDAS gives the evaluation hierarchy from highest to lowest priority. Parentheses are handled first. Exponentiation comes next. Multiplication and division share the next level, so they are handled from left to right. Addition and subtraction share the final level and are also handled from left to right.
A complete precedence trace
Evaluate 2 + 3 × (4 raised to the second power − 10) ÷ 7.
1. Parentheses: Work inside the parentheses. The exponent is evaluated before the subtraction: 4 raised to the second power becomes 16, and 16 − 10 becomes 6.
2. Multiplication and division: The expression is now 2 + 3 × 6 ÷ 7. Multiplication and division share a precedence level, so evaluate them from left to right: 3 × 6 becomes 18, then 18 ÷ 7 remains.
3. Addition: Addition is evaluated after the higher-precedence operations, so the remaining expression is 2 + 18 ÷ 7, with the division completed before the addition.
The expression evaluates by completing the parenthesized calculation first, then the multiplication and division from left to right, and finally the addition.
Resolving Equal-Priority Operations
Multiplication and division are not separate priority levels. They have the same precedence. When both appear in an expression, the operation that appears farther left is performed first. Addition and subtraction follow the same rule: they share a precedence level and are evaluated from left to right.
The left-to-right tie-breaker
Evaluate 24 ÷ 3 × 2.
Find the shared level: Division and multiplication have the same precedence.
Choose the first operation: Division appears to the left of multiplication, so calculate 24 ÷ 3 first.
Continue with the remainder: The expression becomes 8 × 2.
16
Controlling the Grouping
Parentheses override the default precedence rules. Any expression inside parentheses is evaluated before operations outside those parentheses. This lets you deliberately change the result and makes the intended grouping visible to other readers.
| Expression | First calculation | Evaluation path | Result |
|---|---|---|---|
| 2 + 3 × 4 | 3 × 4 | 2 + 12 | 14 |
| (2 + 3) × 4 | 2 + 3 | 5 × 4 | 20 |
Use parentheses when you need a different evaluation order and when they make your intent easier to read. They can improve clarity even when they do not change the result. For example, (minute × 100) ÷ 60 is clearer than minute × 100 ÷ 60, even though both expressions evaluate in the same order.
Misconceptions to Correct
Evaluating every expression strictly from left to right
Precedence determines which level is handled first. Left-to-right order is only used to break ties within one precedence level.
Fix:
Identify parentheses, exponentiation, multiplication or division, and addition or subtraction in that order.Treating multiplication as higher priority than division
Multiplication and division share the same precedence.
Fix:
Evaluate whichever of the two appears first from left to right.Treating addition as higher priority than subtraction
Addition and subtraction share the same lower precedence level.
Fix:
Evaluate addition and subtraction from left to right.Avoiding parentheses because the default order is enough
Parentheses can override precedence and improve readability.
Fix:
Add parentheses when they communicate the intended grouping or make the expression easier to understand.
Practice the Trace
Determine the evaluation order for 6 + 2 × (3 + 1). Do not calculate everything at once. Write the first operation, simplify the expression, and then identify the next operation.
Hints
- Start inside the parentheses.
- After the parentheses are simplified, multiplication has priority over addition.
- Use left-to-right order only when two remaining operations share a precedence level.
What do you think happens?
Which operation should be performed first in 6 + 2 × (3 + 1)?
Reveal answer
Answer: 3 + 1
The operation is inside parentheses, and parentheses have the highest precedence. After that group is simplified, the remaining multiplication is handled before the addition.
Key Takeaways
- PEMDAS orders evaluation as Parentheses, Exponentiation, Multiplication and Division, then Addition and Subtraction.
- Multiplication and division share a precedence level and are evaluated from left to right.
- Addition and subtraction also share a precedence level and are evaluated from left to right.
- Precedence comes before left-to-right order; left-to-right is the tie-breaker for equal-precedence operators.
- Parentheses can override the default order and make the intended grouping clearer.
Key Takeaways
- Use PEMDAS to identify the highest-priority operation before calculating.
- Treat multiplication and division as equal in precedence, resolving them from left to right.
- Treat addition and subtraction as equal in precedence, also resolving them from left to right.
- Use parentheses to change evaluation order or make the intended calculation easier to read.
- Do not confuse the precedence hierarchy with a rule that always evaluates every operation from left to right.