Phase 1: Prepare
Step 1 Warm-up recall
From Module 3: where should a running total start?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- create a list and read an item by position (Foundation);
- use len, in and append (Core);
- predict an IndexError (Core).
Phase 2: Teach
Step 3 A familiar example
A register lists students in order: first, second, third. A program does the same with a list, and asks for the item at a given position.
Step 4 Clear explanation
A list holds several values under one name, in square brackets. Each place is called an index, and counting starts at 0.
scores = [70, 85, 60]
print(scores[0])
print(scores[-1])
print(len(scores))
scores.append(90)
print(scores)
print(85 in scores)70
60
3
[70, 85, 60, 90]
True| Code | Meaning | Result for scores = [70, 85, 60, 90] |
|---|---|---|
| len(scores) | how many items | 4 |
| scores[1] | item at position 1 | 85 |
| scores[-1] | last item | 90 |
| 85 in scores | is it in the list? | True |
A negative index counts from the end, so [-1] is the last item. Asking for a place that does not exist, such as scores[4] in a list of four, stops the program with an IndexError. Python stops here instead of reading memory that is not yours.
Step 5 Worked example (4 marks)
For names = ["Ann", "Bo", "Cy"] find names[1], names[-1] and len(names).
- Position 1 is the second item: Bo. (1 mark)
- -1 is the last item: Cy. (1 mark)
- There are three items: len is 3. (1 mark)
- Answer: Bo, Cy and 3. (1 mark)
Go deeper: the professor's notes
From 0.
len(list).
IndexError.
Positions
| Code | Gives |
|---|---|
| items[0] | first |
| items[-1] | last |
At work: A length check
A request asks for 1,000 items from a list of 10.
- Compare the request with len.
- Refuse if too large.
What a careful practitioner decides. Check a requested position against len before using it.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. In [10, 20, 30], the item at position 0 is:
Counting starts at 0.
2. In [10, 20, 30], item [-1] is:
-1 is the last.
3. len([4, 5, 6, 7]) is:
Four items.
4. For a list of four items, item [4] gives:
The last position is 3.
Phase 4: Close
Step 7 Recap
- A list holds values in order; positions start at 0.
- len counts, in tests, append adds to the end.
- A position that does not exist gives an IndexError.
Step 8 Exit ticket (ungraded)
For colours = ["red", "green", "blue"], write colours[1], colours[-1] and len(colours).
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Make a list of five items you buy often and print the third one.
In industry: Heartbleed: reading past the end
The OpenSSL heartbeat feature copied back as many bytes as the sender asked for without checking the real length. Attackers could read up to 64 KB of server memory per request. The bug dated from 2012 and was made public in April 2014.
Link to this lesson. In Python a list stops you: asking for a position that does not exist gives an IndexError instead of reading beyond the end.
Source: OWASP, Heartbleed Bug; Invicti, The Heartbleed Bug
Common mistakes
| Mistake | Why it fails |
|---|---|
| Counting from 1 | The first item is at position 0. |
| Using len as the last position | The last position is len minus 1. |
| Forgetting that append changes the list | The list is changed in place. |
Limits. This lesson covers lists of numbers and text; other list tools appear as needed.