AQA 7517, section 4.1.1.3
Programs do a lot of calculation, so you need to know the standard arithmetic operations and what each one gives you. The symbols below are the ones Python uses. Other languages use similar ones, but not always the same.
The operations
| Operation | Python | Example | Result |
|---|---|---|---|
| Addition | + |
7 + 2 |
9 |
| Subtraction | - |
7 - 2 |
5 |
| Multiplication | * |
7 * 2 |
14 |
| Real (float) division | / |
7 / 2 |
3.5 |
| Integer division | // |
7 // 2 |
3 |
| Remainder (modulus) | % |
7 % 2 |
1 |
| Exponentiation (power) | ** |
7 ** 2 |
49 |
print(7 / 2) # 3.5 real division keeps the fraction
print(7 // 2) # 3 integer division: whole part only
print(7 % 2) # 1 what is left over
print(2 ** 10) # 1024
Real division and integer division
Real division gives the exact answer as a real number, fraction included. Integer division gives only the whole number of times one number goes into another, and throws the rest away. The remainder (also called modulus or MOD) is what was thrown away, so the two always work together:
Here 3 is the integer division result and 1 is the remainder.
Where integer division and remainder are useful
total_minutes = 135
hours = total_minutes // 60 # 2
minutes = total_minutes % 60 # 15
print(hours, "h", minutes, "min")
# Is a number even?
n = 14
print(n % 2 == 0) # True
# Wrap around, such as the next day of the week (0 to 6)
day = 6
print((day + 1) % 7) # 0
Exponentiation
Exponentiation raises a number to a power. In Python, 3 ** 4 means . Square roots can be written as a power of 0.5, so 25 ** 0.5 is 5.0.
Rounding and truncation
Both turn a real number into a simpler one, but they do it differently.
- Rounding gives the nearest value to a stated number of decimal places.
- Truncation simply cuts off the digits after a point, without rounding. For positive numbers it always goes down.
| Value | Rounded to a whole number | Truncated |
|---|---|---|
3.7 |
4 |
3 |
3.2 |
3 |
3 |
-3.7 |
-4 |
-3 |
print(round(3.756, 2)) # 3.76
print(round(3.7)) # 4
print(int(3.7)) # 3 int() truncates
print(int(-3.7)) # -3 truncates towards zero
Watch out: the rules for exact halves, negative numbers and integer division differ between languages. In Python, round(2.5) gives 2, because exact halves go to the nearest even number, and -7 // 2 gives -4, because // rounds down rather than towards zero. In an exam, work through the values step by step instead of assuming.
Worked example
Task: a cinema sells tickets at £8.50. A group of 23 people pay with £5 notes only. How many notes does the cashier need, and how much change is given back?
price = 8.50
people = 23
cost = price * people # 195.5
notes = int(cost // 5) # 39 whole notes is not enough
if notes * 5 < cost:
notes = notes + 1 # so 40 notes
change = notes * 5 - cost
print(notes) # 40
print(round(change, 2)) # 4.5
Steps: the cost is . Integer division by 5 gives 39, which covers only £195, so one more note is needed. Forty notes is £200, giving £4.50 change. Rounding the change to 2 decimal places guards against tiny floating point errors.
Exam tip: "DIV" and "MOD" are the usual exam names for integer division and remainder. If you're asked what 17 DIV 5 and 17 MOD 5 are, the answers are 3 and 2. Check: .
Check yourself
What are 25 // 4, 25 % 4 and 25 / 4?
25 // 4 is 6, 25 % 4 is 1, and 25 / 4 is 6.25.
What is the difference between rounding 4.8 and truncating 4.8?
Rounding gives the nearest whole number, 5. Truncating cuts off the fraction and gives 4.
Which operator would you use to test whether a number is a multiple of 3?
The remainder operator. If n % 3 == 0, then n is a multiple of 3.
A video lasts 3725 seconds. Give the hours, minutes and seconds
Hours: 3725 // 3600 = 1. Remaining: 3725 % 3600 = 125. Minutes: 125 // 60 = 2. Seconds: 125 % 60 = 5. So 1 h 2 min 5 s.