AQA GCSE Computer Science (8525), section 3.2.3 (Arithmetic operations in a programming language)
The operators
| Operation | Pseudo-code | Python | Example | Result |
|---|---|---|---|---|
| Addition | + |
+ |
7 + 2 |
9 |
| Subtraction | - |
- |
7 - 2 |
5 |
| Multiplication | * |
* |
7 * 2 |
14 |
| Real division | / |
/ |
7 / 2 |
3.5 |
| Integer division (quotient) | DIV |
// |
7 DIV 2 |
3 |
| Remainder (modulus) | MOD |
% |
7 MOD 2 |
1 |
Integer division and remainders
Integer division gives the whole-number part of a division and ignores the remainder. MOD gives the remainder.
For :
11 DIV 2= 5 (2 goes into 11 five times)11 MOD 2= 1 (what is left over)
Check: .
print(11 // 2, 11 % 2) # 5 1
print(11 / 2) # 5.5
Using MOD and DIV
- Even or odd:
n MOD 2 = 0means is even. - Is it a multiple of 5?
n MOD 5 = 0. - Convert minutes to hours and minutes:
hours ← total DIV 60andminutes ← total MOD 60.
total = 135
print(total // 60, "hours", total % 60, "minutes") # 2 hours 15 minutes
Order of operations
As in maths: brackets first, then multiplication and division (including DIV and MOD), then addition and subtraction.
result ← 2 + 3 * 4 (14)
result ← (2 + 3) * 4 (20)
Common mistake: mixing up DIV and MOD. DIV is the whole-number answer; MOD is the remainder. Also, 7 / 2 is 3.5, not 3, because / is real division.
Exam tip: for a division question, work out both parts: write the quotient first, then the remainder. Check that gives the original number.
Check yourself
What is 17 DIV 5?
What is 17 MOD 5?
How can you test whether a number n is even?
Check whether n MOD 2 equals 0.