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.
Watch: the operators share the bill
Press Play on the short film below. It shares the pizza bill with /, // and %, then shows ** and two operators in one line.
Film: arithmetic operators, what each one is for
Arithmetic operators: what each one is for
Every scene, as text
- Four friends share a 50 pound pizza bill: print(50 / 4) shows 12.5, a float, a number with a decimal point. But they only have pound coins.
- Integer division gives only the whole number part: print(50 // 4) shows 12, the pounds each. Modulus gives the remainder after a division: print(50 % 4) shows 2, the pounds left over.
- Myth (false): % works out a share of 100. % is the remainder sign: 7 sweets shared by 2 friends leave 1, so print(7 % 2) shows 1, what is left over, not a part of anything.
- Check: 4 people times 12, plus the leftover. print(4 * 12 + 2) works out 4 * 12 first, which is 48, then adds 2, and shows 50.
- Myth (false): / gives an integer when it divides exactly. print(8 / 2) shows 4.0, because / always gives a float. For the whole number part, print(8 // 2) shows 4.
- print(2 * 3) shows 6, but exponentiation, also called power, is different: print(2 ** 3) shows 8, because 2 ** 3 is 2 x 2 x 2.
- With more than one operator, * comes before +: print(2 + 3 * 4) shows 14, as 3 * 4 is 12 first. Brackets come first of all: print((2 + 3) * 4) shows 20, as 2 + 3 is 5 first (a preview: lesson 2.9 has the full order).
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.
Make a free account to save your work and track your progress.
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 questionsWorked 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.
Make a free account to save your work and track your progress.
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.
Extra: Worked example: the order of work
| Expression | Step 1 | Step 2 | Answer |
|---|---|---|---|
| `2 + 3 * 4` | 3 * 4 is 12 | 2 + 12 | 14 |
| `(2 + 3) * 4` | 2 + 3 is 5 | 5 * 4 | 20 |
| `10 - 4 / 2` | 4 / 2 is 2.0 | 10 - 2.0 | 8.0 |
| `2 * 3 ** 2` | 3 ** 2 is 9 | 2 * 9 | 18 |
| `7 + 7 // 2` | 7 // 2 is 3: // and % go with * and / | 7 + 3 | 10 |
The cells students fill in are left blank in this preview.
Match the operator to its job
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Your turn: use the operators
Use the right operator in four functions. hours_and_minutes turns 135 into 2 hours 15 minutes. It always writes the words hours and minutes, even for 1 hour. square multiplies a number by itself using the power operator. each_person_pays gives the whole pounds each person pays when a bill is shared. left_over gives the pounds that remain. Press Run Tests.
def hours_and_minutes(total_minutes):
# Text like: 2 hours 15 minutes. For 135 minutes.
return None
def square(number):
# A number times itself. Use the power operator.
return None
def each_person_pays(total, people):
# Share the total between people. Give back the whole pounds each person pays.
return None
def left_over(total, people):
# The pounds that are left after everyone pays the whole amount.
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
When to use each one
Tip
Use // and % for whole things: people, minutes, sweets. Use / for measurements. Use % to check if a number is even or odd: a number is even if it leaves no remainder when divided by 2.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go
Exit quiz
6 questionsRemember
Important
+ - * / ** // %. // is the whole part. % is the remainder. / always gives a float.