Sub topic 7
Putting the levels together
This chapter assumes you can add, subtract, multiply, and divide whole numbers, and that you know what a negative integer is and what an exponent means. Earlier chapters covered each rule alone. Here you combine them into one procedure and apply it to expressions that use several levels at once.
The hierarchy
Every expression is evaluated by working through four levels, from highest priority to lowest.
| Level | What it contains | Direction |
|---|---|---|
| 1 | Grouping symbols: parentheses, brackets, braces (and a fraction bar) | Innermost group first |
| 2 | Exponents | Top-down for stacked exponents |
| 3 | Multiplication and division | Left to right |
| 4 | Addition and subtraction | Left to right |
The mnemonics PEMDAS (US) and BODMAS (UK) both encode this table. Their letters suggest that multiplication comes before division and that addition comes before subtraction. That is wrong. Within levels 3 and 4, the two operations have equal rank, and you move left to right.
The procedure
Use this loop until a single number remains.
- Find the highest-priority operation that is not yet evaluated. Inside any grouping symbol, treat the contents as a separate expression and apply the same rules to it.
- If several operations share that level, take the leftmost one.
- Replace that operation and its operands with the result, and rewrite the whole expression.
- Repeat.
Rewriting the full expression after every step is the habit that prevents most errors. It keeps unfinished work visible.
Worked example 1: all four levels
Evaluate 2 + 3 * 4 ** 2, written in math notation as 2 + 3 · 4².
- There are no grouping symbols. The exponent comes first: 4² = 16. The expression becomes 2 + 3 · 16.
- Multiplication is next: 3 · 16 = 48. The expression becomes 2 + 48.
- Addition last: 50.
Now add parentheses: (2 + 3) · 4².
- The group comes first: 2 + 3 = 5. The expression becomes 5 · 4².
- The exponent: 4² = 16. The expression becomes 5 · 16.
- Multiplication: 80.
The numbers are the same and only the grouping differs, so the results are 50 and 80.
print(2 + 3 * 4 ** 2) # 50: exponent first (16), then 3*16 = 48, then 2 + 48
print((2 + 3) * 4 ** 2) # 80: parentheses first (5), then 4**2 = 16, then 5*16Worked example 2: equal rank, left to right
Evaluate 24 ÷ 4 · 3.
Division and multiplication share a level, so go left to right: 24 ÷ 4 = 6, then 6 · 3 = 18. A learner who reads "M before D" would compute 4 · 3 = 12 first and get 24 ÷ 12 = 2, which is wrong.
To get 2 on purpose, you must write 24 ÷ (4 · 3).
The same applies to subtraction and addition. In 10 − 4 + 3, going left to right gives 6 + 3 = 9. Doing the addition first gives 10 − 7 = 3, which is wrong.
print(24 / 4 * 3) # 18.0: division and multiplication share a rank, so go left to right
print(24 / (4 * 3)) # 2.0: parentheses change the grouping
print(10 - 4 + 3) # 9: left to right gives 6 + 3, not 10 - 7Worked example 3: negative numbers and fractions together
Evaluate 1/2 + 3/4 · (2/3)².
- No grouping symbols other than the parentheses around 2/3, which already hold a single value. The exponent applies to the whole fraction: (2/3)² = 4/9. The expression becomes 1/2 + 3/4 · 4/9.
- Multiplication: 3/4 · 4/9 = 12/36 = 1/3. The expression becomes 1/2 + 1/3.
- Addition needs a common denominator of 6: 3/6 + 2/6 = 5/6.
from fractions import Fraction
result = Fraction(1, 2) + Fraction(3, 4) * Fraction(2, 3) ** 2
print(result) # 5/6: (2/3)**2 = 4/9, then 3/4 * 4/9 = 1/3, then 1/2 + 1/3 = 5/6The Fraction class keeps every step exact, so each intermediate value in your hand work can be compared with the code.
Worked example 4: a longer expression
Evaluate 18 − 2 · (3 + 1)² ÷ 8 + 5.
- Group: 3 + 1 = 4. The expression becomes 18 − 2 · 4² ÷ 8 + 5.
- Exponent: 4² = 16. The expression becomes 18 − 2 · 16 ÷ 8 + 5.
- Multiplication and division, left to right: 2 · 16 = 32, so the expression becomes 18 − 32 ÷ 8 + 5. Then 32 ÷ 8 = 4, so it becomes 18 − 4 + 5.
- Addition and subtraction, left to right: 18 − 4 = 14, then 14 + 5 = 19.
The answer is 19. Notice that step 4 follows left to right: 18 − (4 + 5) would give 9, which is a different value.
Where the levels interact
Some expressions have a hidden grouping because of how they are written.
- Exponents bind to the nearest base. In 3 · 2², only the 2 is squared. The result is 3 · 4 = 12, not 6² = 36.
- A leading minus sign is not part of the base. −3² means −(3²) = −9. Only (−3)² equals 9. The next chapters treat this case fully, but you must apply the hierarchy correctly now: the exponent (level 2) comes before the negation, which behaves like multiplication by −1.
- A fraction bar groups its top and bottom. (3 + 5)/(2 + 2) = 8/4 = 2. If you type it without parentheses as 3 + 5/2 + 2, only the 5 is divided, and the result is 7.5.
print(-3 ** 2) # -9: the exponent applies to 3 first, then the minus sign
print((-3) ** 2) # 9: the parentheses make -3 the base
print((3 + 5) / (2 + 2)) # 2.0: the numerator and denominator are each grouped
print(3 + 5 / 2 + 2) # 7.5: without grouping, only the 5 is divided by 2Common errors when combining levels
| Error | Example | Correct handling |
|---|---|---|
| Adding before multiplying | 2 + 3 · 4 computed as 5 · 4 = 20 | Multiply first: 2 + 12 = 14 |
| Multiplying before dividing "because M comes first" | 24 ÷ 4 · 3 computed as 24 ÷ 12 | Left to right: 6 · 3 = 18 |
| Subtracting a group's parts incorrectly | 10 − (4 + 3) computed as 10 − 4 + 3 | Evaluate the group first: 10 − 7 = 3 |
| Multiplying the base by the exponent | 2³ computed as 6 | 2 · 2 · 2 = 8 |
| Skipping the rewrite | Losing a term halfway through | Rewrite the whole expression each step |
A self-check habit
After you finish by hand, type the expression into Python with explicit parentheses that match your reading. If the two results disagree, compare your rewritten lines with the operator levels until you find the first step where they diverge. Python follows the same hierarchy for **, *, /, +, and -, so a mismatch usually means a slip in your hand work or a missing parenthesis in the code.
Exercise
Evaluate 6 + 4 · (5 − 3)² ÷ 2 − 1, showing the expression after each step. Then say what value you would get if you wrongly did the addition 6 + 4 first.
Show answer
Group: 5 − 3 = 2, giving 6 + 4 · 2² ÷ 2 − 1. Exponent: 2² = 4, giving 6 + 4 · 4 ÷ 2 − 1. Multiplication and division left to right: 4 · 4 = 16, giving 6 + 16 ÷ 2 − 1, then 16 ÷ 2 = 8, giving 6 + 8 − 1. Addition and subtraction left to right: 6 + 8 = 14, then 14 − 1 = 13. The answer is 13. If you added 6 + 4 first, you would get 10 · 4 ÷ 2 − 1 after the group and exponent steps, which is 40 ÷ 2 − 1 = 19.
Exercise
Evaluate 20 − 12 ÷ 3 · 2 + 1, and explain why 20 − 12 ÷ (3 · 2) + 1 gives a different value.
Show answer
Division and multiplication share a level, so go left to right: 12 ÷ 3 = 4, then 4 · 2 = 8. The expression becomes 20 − 8 + 1, which is 12 + 1 = 13. In the second expression the parentheses force 3 · 2 = 6 first, so 12 ÷ 6 = 2, and 20 − 2 + 1 = 19. The grouping changes the value from 13 to 19.
Exercise
Evaluate 1/3 + 1/2 · (2/5)² using exact fractions, and write one Python line that checks it.
Show answer
Exponent: (2/5)² = 4/25. Multiplication: 1/2 · 4/25 = 4/50 = 2/25. Addition with common denominator 75: 1/3 = 25/75 and 2/25 = 6/75, so the sum is 31/75. Python check: print(Fraction(1, 3) + Fraction(1, 2) Fraction(2, 5) * 2), after from fractions import Fraction, which prints 31/75.