Tea in the wrong order
Making tea has an order. Boil the water. Put the tea bag in the cup. Pour the water. If you pour before the water boils, you get a cup of cold, weak tea.
Programs are the same. Order decides if the answer is right. It also decides if the program is quick.
Key words
| Word | What it means | Example |
|---|---|---|
| Order of operations | The rules for which part of a calculation is worked out first. | |
| Bracket | A ( ) pair. What is inside is worked out first. | (2 + 3) * 4 |
| Efficient | Does the job with little time and little memory. | |
| Execution time | How long a program takes to run. | |
| Memory | The space a program needs to hold its data. | |
| Data structure | A way to store a group of values. | a list, a dictionary |
| List | A group of values kept in order. | [5, 8, 2] |
| Dictionary | A group of pairs. You find a value by its key. | {"tea": 1.2} |
The cells students fill in are left blank in this preview.
Put the actions in a logical order
Some steps need others to finish first. You cannot add up a bill before you have the prices. You cannot read a file before you open it.
Work backwards. Ask: what does this step need? Put that step earlier.
Quick check: the logical order
Have a go
Quick check: the logical order
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
The order inside a calculation
A calculation has an order too. Python follows the same rules as maths class. Brackets come first. Then powers. Then multiply and divide. Then add and subtract.
When two operations are at the same level, Python goes from left to right. A bracket is the easy way to be sure.
The order of operations
| Order | What | Python sign |
|---|---|---|
| 1st | Brackets | ( ) |
| 2nd | Powers | ** (2 ** 3 means 2 x 2 x 2, which is 8) |
| 3rd | Multiply and divide | * (times) and / (divide) |
| 4th | Add and subtract | + and - |
The cells students fill in are left blank in this preview.
Worked example: the average of three marks
| Calculation | Steps | Answer |
|---|---|---|
| 60 + 70 + 80 / 3 | 80 / 3 = 26.67, then 60 + 70 + 26.67 | 156.67: wrong |
| (60 + 70 + 80) / 3 | 60 + 70 + 80 = 210, then 210 / 3 | 70: right |
| 20 * 9 / 5 + 32 | 20 * 9 = 180, then 180 / 5 = 36, then 36 + 32 | 68 |
The cells students fill in are left blank in this preview.
Try it: the order matters
print(60 + 70 + 80 / 3)
print((60 + 70 + 80) / 3)
print(2 + 3 * 4)
print((2 + 3) * 4)
print(2 ** 3 + 1)
# Predict each answer before you run it.
Quick check: what is the answer?
Have a go
Quick check: what is the answer?
4 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.