Sub topic 2
Quick review: working with negative integers
This chapter reviews the sign rules you need before the order-of-operations rules. It assumes you can add, subtract, multiply and divide whole numbers. It also assumes you know what positive and negative integers are on the number line. Later chapters apply the ordering rules to expressions such as -3 ** 2. Those cases are hard to follow if the sign rules aren't automatic.
What you will be able to do
- Add and subtract integers of any sign by moving on the number line.
- Rewrite subtraction as adding the opposite.
- Predict the sign of a product or quotient.
- Read a minus sign correctly as either an operation or part of a number.
The number line and opposites
An integer is a whole number that can be positive, negative or zero. On the number line, numbers to the right of zero are positive and numbers to the left are negative. Larger numbers sit further right, so -2 is greater than -9.
The opposite of a number is the number the same distance from zero on the other side. The opposite of 7 is -7, and the opposite of -7 is 7. The absolute value of a number is its distance from zero, written |n|. So |-7| = 7 and |7| = 7.
print(abs(-7)) # 7
print(abs(7)) # 7
print(abs(-3 - 4)) # 7: the distance between 4 and -3 on the number lineAdding integers
Think of addition as movement. Start at the first number. Adding a positive number moves you right, and adding a negative number moves you left.
- 5 + (-3): start at 5, move 3 left, land on 2.
- -5 + 3: start at -5, move 3 right, land on -2.
- -5 + (-3): start at -5, move 3 left, land on -8.
Two shortcuts follow from this:
- If both numbers have the same sign, add their absolute values and keep that sign.
- If the signs differ, subtract the smaller absolute value from the larger. Keep the sign of the number with the larger absolute value.
print(5 + -3) # 2
print(-5 + 3) # -2
print(-5 + -3) # -8Subtracting integers: add the opposite
The rule that removes most sign errors is that subtracting a number is the same as adding its opposite.
- 5 - 3 is the same as 5 + (-3), which is 2.
- 5 - (-3) is the same as 5 + 3, which is 8.
- -5 - 3 is the same as -5 + (-3), which is -8.
- -5 - (-3) is the same as -5 + 3, which is -2.
Subtracting a negative moves you right, because it undoes a leftward step. Python accepts 5 - -3, but that is hard to read, so parentheses help:
print(5 - 3) # 2
print(5 - (-3)) # 8
print(-5 - 3) # -8
print(-5 - (-3)) # -2Strings of additions and subtractions
When an expression has several terms, rewrite each subtraction as adding an opposite. You can then add the terms in any order. For example, 4 - 9 + 2 becomes 4 + (-9) + 2. Working from left to right gives -5 + 2 = -3.
print(4 - 9 + 2) # -3
print(4 - (9 + 2)) # -7: different grouping, different answerThe second line shows why grouping matters. This is the idea behind the whole tutorial.
Multiplying and dividing integers
The sign rules for multiplication and division are the same. Multiply or divide the absolute values, then choose the sign:
| First number | Second number | Result sign | Example |
|---|---|---|---|
| positive | positive | positive | 6 × 4 = 24 |
| positive | negative | negative | 6 × (-4) = -24 |
| negative | positive | negative | (-6) × 4 = -24 |
| negative | negative | positive | (-6) × (-4) = 24 |
The same table works for division. For example, -24 ÷ 6 = -4 and -24 ÷ (-6) = 4.
One way to see why a negative times a negative is positive is to look at a pattern. Take 3 × (-4) = -12, then 2 × (-4) = -8, then 1 × (-4) = -4, then 0 × (-4) = 0. Each step up in the first factor adds 4, so the next product has to be -1 × (-4) = 4.
print(6 * -4) # -24
print(-6 * 4) # -24
print(-6 * -4) # 24
print(-24 / 6) # -4.0
print(-24 / -6) # 4.0Python's / always returns a decimal number, which is why you see -4.0 and not -4. The value is the same.
Many factors at once
For a product of several numbers, count the negative factors. An even count gives a positive product, and an odd count gives a negative one.
print(-2 * -3 * -4) # -24: three negatives
print(-2 * -3 * -4 * -1) # 24: four negativesZero
Zero times any number is zero. Zero divided by a nonzero number is zero. Division by zero has no value, and Python raises an error.
print(0 * -5) # 0
print(0 / -5) # -0.0The -0.0 is a quirk of decimal numbers in Python. It equals 0.0, so it is the same value.
Which sign sorts the result?
The diagram summarizes how to decide the sign of a product or quotient of two numbers.
One symbol, two jobs
The minus sign does two different jobs:
- Subtraction is a binary operation. It sits between two numbers, as in 8 - 3.
- A negative sign is attached to a single number, as in -3. It says which side of zero the number is on. You can also read it as "the opposite of".
In -5 - 3, the first minus is a negative sign and the second is subtraction. Reading them correctly lets you rewrite the expression as -5 + (-3).
A negative number written after an operation usually gets parentheses, as in 5 × (-3). This is not decoration. It keeps the two operation symbols from running together and makes the grouping clear.
print(-2 * 3 - 4 * -5) # 14: -6 minus -20 is -6 + 20Common errors
- Dropping the sign on the second term. In 10 - 4 + 3, the 3 is added. Some learners treat everything after the minus as subtracted and get 3. The correct result is 9.
- Treating "minus a negative" as a double penalty. 5 - (-3) is 8, not 2.
- Adding signs instead of using the rule. (-6) × (-4) is 24, not -24 and not -10.
- Mixing up the rules for addition and multiplication. In addition, -6 + (-4) = -10, which is negative. In multiplication, two negatives give a positive.
Why this matters for what follows
The next chapters show that the order of operations decides where a negative sign applies. For example, -3² and (-3)² give different answers because of which operation uses the sign first. If the arithmetic with signs is already secure, you can focus on order, not on slips.
Exercise
Evaluate -8 - (-3) + 2 (-4) by hand. Show the rewrite of the subtraction. Then say what sign the product (-1) (-2) (-3) (-4) * (-5) has, and give its value.
Show answer
Do the multiplication first: 2 * (-4) = -8. Rewrite the subtraction as adding the opposite: -8 + 3 + (-8). Left to right, -8 + 3 = -5, then -5 + (-8) = -13. The product has five negative factors, an odd count, so it is negative: -120.
Exercise
A learner computes 5 - (-3) and gets 2. What mistake did they make, and what is the correct value?
Show answer
They treated subtracting -3 as subtracting 3. Subtracting a number means adding its opposite, so 5 - (-3) = 5 + 3 = 8.