Skip to the page
Year 9 · Autumn 1 · Python II: lists and strings

Iterating with for

PracticalPresent ▶

What this lesson covers

Topic 11 Algorithms

KS3 Computing · An algorithm is a precise, step-by-step plan to solve a problem. Computational thinking turns a messy problem into one. Every algorithm is built from just three things: sequence, selection and iteration.

Topic 13 Programming basics

KS3 Computing · A program stores information in variables, works on it with operators, and controls the order with sequence, selection and iteration. Examples are in Python, with Scratch alongside for Year 7.

Programming Computational ThinkingCambridge Lower Secondary Computing 0860

You will learn to

  • Use a for loop to visit every item in a list, one at a time.
  • Total a list of prices with a running total that starts at 0.
  • Find the biggest value by keeping the 'biggest so far', and trace it.
  • Count how many prices are over 10, using a for loop with an if inside.

Success criteria

  • I can total a list of prices with a for loop, without using sum().
  • I can fill in a trace table for the 'biggest so far' program.
  • I can count the prices over 10 and check the answer by hand.

Key words

Say each word, then read what it means. Every one is in the glossary too.

trace table
Records variable values step by step to check logic.
infinite loop
A loop whose condition never becomes false.
Algorithm
Precise, ordered, finite steps to solve a problem.
Decomposition
Breaking a problem into smaller ones.
Abstraction
Ignoring detail that doesn't matter for the problem.
Sequence · selection · iteration
Steps one after another. First this, then this.
Pseudocode
Code-like writing for algorithms, without strict rules.
Variable
A named place in memory that holds a value.
Assignment
Storing a value in a variable. = in Python, ← in pseudocode.
Casting
Changing a value from one data type to another, like int("13").
Concatenation
Joining strings together: "7" + "7" = "77".
Selection
Choosing a path with if / else.
Iteration
Repeating steps: a loop.
Boolean
A value that is only True or False.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

Trace tables

A trace table records every variable at each step, to check the algorithm works.

Passnn > biggest?biggest after
17No7
23No7
312Yes12
45No12
e.g.Example

Output: 12. Correct.

Loops: for

for i in range(5):
    print("Lap", i)

Prints Lap 0 to Lap 4. range(5) gives 0, 1, 2, 3, 4: it starts at 0 and stops before 5.

e.g.Example

Scratch: 'repeat 5'.

Loops: while

password = ""
while password != "dragon":
    password = input("Password: ")
print("Access granted")

Repeats while the condition is true. As soon as the user types dragon, the loop ends.

!Watch out

If the condition can never become false, it loops for ever: an infinite loop.

Study it

Open the study page and go one step at a time.

The steps for this lesson

Try it

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.