Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Order, speed and memory

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

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

WordWhat it meansExample
Order of operationsThe rules for which part of a calculation is worked out first.
BracketA ( ) pair. What is inside is worked out first.(2 + 3) * 4
EfficientDoes the job with little time and little memory.
Execution timeHow long a program takes to run.
MemoryThe space a program needs to hold its data.
Data structureA way to store a group of values.a list, a dictionary
ListA group of values kept in order.[5, 8, 2]
DictionaryA 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your 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

OrderWhatPython sign
1stBrackets( )
2ndPowers** (2 ** 3 means 2 x 2 x 2, which is 8)
3rdMultiply and divide* (times) and / (divide)
4thAdd and subtract+ and -

The cells students fill in are left blank in this preview.

Worked example: the average of three marks

CalculationStepsAnswer
60 + 70 + 80 / 380 / 3 = 26.67, then 60 + 70 + 26.67156.67: wrong
(60 + 70 + 80) / 360 + 70 + 80 = 210, then 210 / 370: right
20 * 9 / 5 + 3220 * 9 = 180, then 180 / 5 = 36, then 36 + 3268

The cells students fill in are left blank in this preview.

Try it: the order matters

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

10 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.