Sub topic 5
Addition and subtraction: left to right
This chapter covers one rule: addition and subtraction share a rank, so you evaluate them in the order they appear, from left to right. It assumes you can add and subtract integers, including negative ones, and that you know a fraction is a numerator over a denominator. Grouping symbols, exponents, and multiplication and division have their own chapters. They appear here only where they affect the examples.
The rule
Addition and subtraction have equal precedence. When an expression contains only these two operations, no operation "goes first" because of its type. You start at the left and work to the right, one operation at a time.
print(10 - 4 + 3) # 9
print(10 + 3 - 4) # 9
print(10 - 4 + 3 - 8) # 1For 10 - 4 + 3, the steps are:
10 - 4gives6.6 + 3gives9.
A common mistake is to do the addition first because "addition comes before subtraction" in the letters PEMDAS or in the phrase "add and subtract". That gives 10 - 7 = 3, which is wrong. The mnemonic lists the operations in a line, but A and S are the same rank, just as M and D are.
Why left to right gives the right answer
You can read an expression like 10 - 4 + 3 - 8 as a list of signed terms: +10, -4, +3, -8. Each sign belongs to the number on its right. The value is the sum of the terms.
10 + (-4) + 3 + (-8) = 1
Left to right does exactly this one term at a time, so the sign of each number is never lost. The wrong answer 10 - 7 comes from grouping 4 + 3 into one lump. That changes the sign of 3 from positive to negative, so you have subtracted the 3 instead of adding it.
Because every term keeps its own sign, addition and subtraction are flexible once you see the terms. You can add the positive terms and the negative terms separately:
- Positive terms:
10 + 3 = 13 - Negative terms:
4 + 8 = 12 - Result:
13 - 12 = 1
This agrees with the left-to-right result. It is a valid shortcut only because each number carries its sign with it. Moving a bare number without its sign gives a different answer.
Subtraction is not associative
Addition is associative: (a + b) + c equals a + (b + c). Subtraction is not. The grouping decides the result.
print((10 - 4) - 3) # 3
print(10 - (4 - 3)) # 9
print(10 - 4 - 3) # 3: with no parentheses, left to right means (10 - 4) - 3An expression with no grouping symbols has a single value. It is the one you get by treating it as (10 - 4) - 3. If you want 10 - (4 - 3), you must write the parentheses. The next chapters on grouping symbols build on this.
Working with negative integers
Negative numbers add one wrinkle. A minus sign can mean "subtract" or "this number is negative". Read each one by its position:
- A minus between two numbers is subtraction.
- A minus at the start of an expression, or right after another operator or an opening parenthesis, marks a negative number.
print(5 - 8 + 2) # -1
print(-4 - 6 + 3) # -7
print(7 - -2) # 9
print(7 - (-2) + 1) # 10Here are the steps for each:
5 - 8 + 2:5 - 8 = -3, then-3 + 2 = -1.-4 - 6 + 3:-4 - 6 = -10, then-10 + 3 = -7.7 - -2: subtracting-2is the same as adding2, so the result is9.7 - (-2) + 1:7 - (-2) = 9, then9 + 1 = 10.
Writing 7 - -2 is legal in Python but hard to read. In hand work, put the negative number in parentheses: 7 - (-2).
Mixed expressions: addition and subtraction at the lowest level
In an expression that also has multiplication, division, or exponents, those operations are done first. Addition and subtraction are the last level. After the higher operations have each been replaced by a value, what is left is a plain left-to-right list of additions and subtractions.
print(20 - 3 * 4 + 6 / 2) # 11.0The steps are:
- Multiplication and division first:
3 * 4 = 12and6 / 2 = 3. The expression becomes20 - 12 + 3. - Then left to right:
20 - 12 = 8, and8 + 3 = 11.
Python prints 11.0 because / always returns a float. The value is the same as the integer 11.
The same pattern works with fractions, where each operand is a fraction. Subtraction and addition are done in order.
from fractions import Fraction
result = Fraction(3, 4) - Fraction(1, 2) + Fraction(1, 4)
print(result) # 1/2Step by step: 3/4 - 1/2 = 3/4 - 2/4 = 1/4, then 1/4 + 1/4 = 2/4 = 1/2. Each step needs a common denominator, which is why exact fraction arithmetic helps when you check your work.
The evaluation as a picture
This diagram shows how 10 - 4 + 3 - 8 reduces one operation at a time.
At every step the leftmost remaining operation is the one that is performed. The expression gets shorter by one operation each time.
Common errors
| Error | Example | Wrong result | Correct result |
|---|---|---|---|
| Adding before subtracting | `10 - 4 + 3` | `3` | `9` |
| Dropping a sign when regrouping | `8 - 5 + 2` computed as `8 - 7` | `1` | `5` |
| Reading `- -` as a single minus | `7 - (-2)` | `5` | `9` |
| Working right to left | `12 - 5 + 4 - 9` computed from the right | varies | `2` |
The first error is the most frequent. The second comes from putting 5 + 2 into one group, which flips the sign of the 2. The third forgets that subtracting a negative number adds. The fourth treats subtraction as if the order of numbers did not matter, which it does.
Checking with code
Python follows the same rule, so it is a quick check on a hand calculation. Run each step of your work and compare with the final line.
# Hand steps for 12 - 5 + 4 - 9 + 2
step1 = 12 - 5 # 7
step2 = step1 + 4 # 11
step3 = step2 - 9 # 2
step4 = step3 + 2 # 4
print(step4) # 4
print(12 - 5 + 4 - 9 + 2) # 4
print(step4 == 12 - 5 + 4 - 9 + 2) # TrueIf the two results disagree, compare your steps one by one with the step variables to find where a sign slipped.
Exercises
Exercise
Evaluate 12 - 5 + 4 - 9 + 2 by working left to right. Write each intermediate value.
Show answer
12 - 5 = 7, then 7 + 4 = 11, then 11 - 9 = 2, then 2 + 2 = 4. The value is 4.
Exercise
A student evaluates 15 - 6 + 2 as 15 - 8 = 7. Explain the mistake and give the correct value.
Show answer
The student added 6 and 2 first and then subtracted the sum, which turns +2 into -2. Left to right, 15 - 6 = 9 and 9 + 2 = 11. The correct value is 11.
Exercise
Evaluate 7 - 10 + 6 - (-2), and then say what 7 - 10 + 6 - 2 would give. Why do the two answers differ?
Show answer
7 - 10 = -3, then -3 + 6 = 3, then 3 - (-2) = 5. The second expression gives 3 - 2 = 1. They differ because subtracting -2 adds 2, while subtracting 2 takes away 2, a difference of 4.