Year 9 · Autumn 1
Python II: lists and strings · Lesson 2 of 5
append, remove, len and in
PracticalTopic 14 Programming further
Year 9 · Python II: lists and strings · Lesson 2 of 5
You will learn to
- Use append to add each item the user types, until they type 'stop'.
- Use len to count the items, and in to check if an item is there.
- Use remove to delete one item, and explain the error if it is not in the list.
- Predict what each line will print before running it.
Year 9 · Python II: lists and strings · Lesson 2 of 5
Success criteria
You have got it when you can say:
- I can build a list from input that stops when I type 'stop'.
- I can check if 'milk' is in the list and print True or False.
- I can write my prediction down before each run, then check it.
Year 9 · Python II: lists and strings · Lesson 2 of 5
Retrieval starter
Questions from before. Write your answers, then show them.
- Last lesson
Testing a 0–50 range with 50 is…
A Erroneous dataB Boundary dataC Normal data
B. Boundary data
- Last year
What does 17 % 5 give?
A 2B 3.4C 3
A. 2
- Last lesson
In def area(w, h): … and a = area(4, 3), which are the arguments?
A area and aB 4 and 3C w and h
B. 4 and 3
Year 9 · Python II: lists and strings · Lesson 2 of 5
Key words (1 of 2)
- list
- One name holding many values in order. Also called an array.
- index
- An item's position in a list, counting from 0.
- Subroutine
- A named block of code you can call when needed.
- Function
- A subroutine that returns a value.
Year 9 · Python II: lists and strings · Lesson 2 of 5
Key words (2 of 2)
- Parameter
- A placeholder in a subroutine for a value passed in.
- Validation
- Checking input is reasonable before using it.
- Boundary data
- Test data at the very edge of what's allowed.
The explanation · step 1 of 1 · part 1 of 2
Lists
A list (or array) stores many values under one name, in order.
scores = [12, 45, 33, 50]
| Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Value | 12 | 45 | 33 | 50 |
Why start at 0? The index means 'how many steps from the start'. The first item is 0 steps away.
The explanation · step 1 of 1 · part 2 of 2
Lists (continued)
scores[0]→ 12scores[-1]→ 50 (the last one)scores.append(28)adds to the endlen(scores)→ how many itemsscores[10]→ error: no item there
Year 9 · Python II: lists and strings · Lesson 2 of 5
Quick quiz · 1 of 3
Typing your new password twice so they can be compared is…
- A Authentication
- B Validation
- C Verification
C. Verification
Verification checks data was entered correctly. Double entry is one method.
Year 9 · Python II: lists and strings · Lesson 2 of 5
Quick quiz · 2 of 3
open("log.txt", "w") on a file that already holds 100 lines…
- A Gives an error
- B Wipes it first
- C Adds to the end
B. Wipes it first
"w" empties the file before writing. Use "a" to append.
Year 9 · Python II: lists and strings · Lesson 2 of 5
Quick quiz · 3 of 3
The program runs but gives the wrong answer. That's a…
- A Runtime error
- B Syntax error
- C Logic error
C. Logic error
The code is valid and doesn't crash; the thinking is wrong. Only testing finds it.
Year 9 · Python II: lists and strings · Lesson 2 of 5
Exit ticket
Answer on paper before you go. Then check.
Program runs but gives the wrong answer. That's a…
A Syntax errorB Logic error
B. Logic error The code is valid; the thinking is wrong.
In ["a", "b", "c"], the index of "c" is…
A 3B 2C c
B. 2 Counting starts at 0: a = 0, b = 1, c = 2.