Year 9 · Autumn 1
Python II: lists and strings · Lesson 1 of 5
Lists: creating and indexing
PracticalTopic 14 Programming further
Year 9 · Python II: lists and strings · Lesson 1 of 5
You will learn to
- Create a Python list with square brackets and print one item using its index.
- Explain why the first item is at index 0 and the last is at len(list) - 1.
- Use a negative index, like -1, to get the last item.
- Explain the error that an index past the end of the list gives.
Year 9 · Python II: lists and strings · Lesson 1 of 5
Success criteria
You have got it when you can say:
- I can print the first and the last item of my shopping list.
- I can say which index holds the 4th item in a list.
- I can explain 'list index out of range' in my own words.
Year 9 · Python II: lists and strings · Lesson 1 of 5
Retrieval starter
Questions from before. Write your answers, then show them.
- Last year
A microphone hearing your voice is…
A OutputB InputC Process
B. Input
- Last year
A AND B, with A = 1 and B = 0, gives…
A 1B 0
B. 0
- Last year
In https://www.bbc.co.uk/news, the path is…
A /newsB https://C www.bbc.co.uk
A. /news
Year 9 · Python II: lists and strings · Lesson 1 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 1 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 1 of 5
Quick quiz · 1 of 3
In ["a", "b", "c"], the index of "c" is…
- A 3
- B c
- C 2
C. 2
Counting starts at 0: a = 0, b = 1, c = 2.
Year 9 · Python II: lists and strings · Lesson 1 of 5
Quick quiz · 2 of 3
word = "Python". What is word[1:4]?
- A "yth"
- B "Pyth"
- C "ytho"
A. "yth"
Start at index 1 (y), stop before index 4 (o): y, t, h. That's 4 − 1 = 3 characters.
Year 9 · Python II: lists and strings · Lesson 1 of 5
Quick quiz · 3 of 3
In def area(w, h): … and a = area(4, 3), which are the arguments?
- A area and a
- B 4 and 3
- C w and h
B. 4 and 3
Arguments are the actual values passed in the call. w and h are parameters.
Year 9 · Python II: lists and strings · Lesson 1 of 5
Exit ticket
Answer on paper before you go. Then check.
A variable made inside a function is…
A Global: usable anywhereB Local: only exists inside it while it runsC Saved to a file
B. Local: only exists inside it while it runs Local variables are wiped when the function returns.
Testing a 0–50 range with 50 is…
A Erroneous dataB Normal dataC Boundary data
C. Boundary data It's right at the edge of what's allowed.