Sharing a pizza bill
Four friends share a 50 pound pizza bill. How much does each person pay? 50 divided by 4 is 12.5.
But they only have pound coins. Each friend puts in 12, and 2 pounds are left over to pay. A program must know how to say both things.
Key words
| Word | What it means | Example |
|---|---|---|
| Operator | A symbol that does something to values. | + |
| Arithmetic operator | An operator for maths. | + - * / |
| Exponentiation | Multiplying a number by itself again and again. It is also called power. | 2 ** 3 is 2 x 2 x 2, which is 8 |
| Integer division | Division that gives only the whole number part. | 7 // 2 is 3 |
| Modulus | The remainder after a division. | 7 % 2 is 1 |
| Remainder | What is left over after sharing equally. | 7 shared by 2 leaves 1 |
| Float | A number with a decimal point. | 12.5 |
| Integer | A whole number. | 12 |
The cells students fill in are left blank in this preview.
Try it: the first four
print(8 + 2)
print(8 - 2)
print(8 * 2)
print(8 / 2)
# Change the numbers and run again.
# Notice that 8 / 2 gives 4.0, a float.
The seven arithmetic operators
| Operator | Name | Example | Result |
|---|---|---|---|
| `+` | Add | `7 + 2` | 9 |
| `-` | Subtract | `7 - 2` | 5 |
| `*` | Multiply | `7 * 2` | 14 |
| `/` | Divide | `7 / 2` | 3.5 |
| `**` | Exponentiation (power) | `2 ** 3` | 8 |
| `//` | Integer division | `7 // 2` | 3 |
| `%` | Modulus (remainder) | `7 % 2` | 1 |
The cells students fill in are left blank in this preview.
Work it out
Have a go
Work it out
4 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
Worked example: 50 shared by 4
| Question | Operator | Calculation | Answer |
|---|---|---|---|
| How much each, with a decimal? | `/` | 50 / 4 | 12.5 |
| How many whole pounds each? | `//` | 50 // 4 | 12 |
| How many pounds are left over? | `%` | 50 % 4 | 2 |
| Check: 4 people times 12, plus the leftover | `*` and `+` | 4 * 12 + 2 | 50 |
The cells students fill in are left blank in this preview.
Integer division and modulus
Think about sharing sweets (small candies). 7 sweets, 2 friends. Each friend gets 3. One sweet is left.
7 // 2 gives 3: how many times 2 fits into 7. 7 % 2 gives 1: what is left. You can use them together to change minutes into hours and minutes.
Try it: minutes to hours
total = 135
hours = total // 60
minutes = total % 60
print(hours, "hours", minutes, "minutes")
# 60 goes into 135 two times, so hours is 2.
# 135 - 120 leaves 15, so minutes is 15.
# Change total to 200.
More than one operator
You can use several operators in one line. Python works out what is inside brackets first.
So (2 + 3) * 4 is 20. The full order of operations has its own lesson.