Year 9 · Autumn 1
Python II: lists and strings · Lesson 3 of 5
Iterating with for
PracticalTopic 11 AlgorithmsTopic 13 Programming basics
Year 9 · Python II: lists and strings · Lesson 3 of 5
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.
Year 9 · Python II: lists and strings · Lesson 3 of 5
Success criteria
You have got it when you can say:
- 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.
Year 9 · Python II: lists and strings · Lesson 3 of 5
Retrieval starter
Questions from before. Write your answers, then show them.
- Last lesson
In def area(w, h): … and a = area(4, 3), which are the arguments?
A 4 and 3B area and aC w and h
A. 4 and 3
- Earlier this unit
Typing your new password twice so they can be compared is…
A AuthenticationB VerificationC Validation
B. Verification
- Last year
Testing a 0–50 range with 50 is…
A Boundary dataB Erroneous dataC Normal data
A. Boundary data
Year 9 · Python II: lists and strings · Lesson 3 of 5
Key words (1 of 4)
- 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.
Year 9 · Python II: lists and strings · Lesson 3 of 5
Key words (2 of 4)
- 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.
Year 9 · Python II: lists and strings · Lesson 3 of 5
Key words (3 of 4)
- 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.
Year 9 · Python II: lists and strings · Lesson 3 of 5
Key words (4 of 4)
- Iteration
- Repeating steps: a loop.
- Boolean
- A value that is only True or False.
The explanation · step 1 of 3
Trace tables
A trace table records every variable at each step, to check the algorithm works.
| Pass | n | n > biggest? | biggest after |
|---|---|---|---|
| 1 | 7 | No | 7 |
| 2 | 3 | No | 7 |
| 3 | 12 | Yes | 12 |
| 4 | 5 | No | 12 |
Output: 12. Correct.
The explanation · step 2 of 3
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.
Scratch: 'repeat 5'.
The explanation · step 3 of 3
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.
If the condition can never become false, it loops for ever: an infinite loop.
Year 9 · Python II: lists and strings · Lesson 3 of 5
Try it: Trace 'find the biggest'
Year 9 · Python II: lists and strings · Lesson 3 of 5
Quick quiz · 1 of 3
An 'if… else…' is…
- A Sequence
- B Selection
- C Iteration
B. Selection
It selects one path.
Year 9 · Python II: lists and strings · Lesson 3 of 5
Quick quiz · 2 of 3
In a flowchart, a decision is a…
- A Rectangle
- B Oval
- C Diamond
C. Diamond
Diamonds have yes/no arrows coming out.
Year 9 · Python II: lists and strings · Lesson 3 of 5
Quick quiz · 3 of 3
The Tube map leaves out real distances. That's…
- A Pattern recognition
- B Decomposition
- C Abstraction
C. Abstraction
Abstraction removes detail that doesn't matter for the problem.
Year 9 · Python II: lists and strings · Lesson 3 of 5
Exit ticket
Answer on paper before you go. Then check.
How many times does FOR i ← 2 TO 8 repeat?
A 6B 7C 8
B. 7 Both ends count: 8 − 2 + 1 = 7 (2, 3, 4, 5, 6, 7, 8).
Which loop always runs at least once?
A REPEAT … UNTILB WHILE … ENDWHILEC FOR i ← 1 TO 0
A. REPEAT … UNTIL REPEAT … UNTIL checks its condition at the end, after the first run.