Phase 1: Prepare
Step 1 Warm-up recall
From M4.L1: what position does the first item have?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- loop over a list with for (Foundation);
- total, average and search (Core);
- use sum, max, min and sorted (Core).
Phase 2: Teach
Step 3 A familiar example
A teacher goes through a pile of marked scripts one by one, keeping a running total and noting the highest mark. A loop over a list does the same.
Step 4 Clear explanation
A for loop can walk straight through a list, with no need for positions:
scores = [70, 85, 60, 90]
total = 0
for s in scores:
total = total + s
print(total)
print(total / len(scores))305
76.25Python has short tools for common jobs. sum(scores) gives 305, max(scores) gives 90, min(scores) gives 60 and sorted(scores) gives [60, 70, 85, 90] without changing the original.
To search, keep a flag that starts False and set it to True when an item passes the test:
found = False
for s in scores:
if s > 80:
found = True
print(found)TrueTake care with the range of items. If a calculation leaves out some items, the answer is wrong without any error message, so check how many items a loop really visits.
Step 5 Worked example (4 marks)
For nums = [4, 9, 2] find sum, max and sorted.
- sum: 4 + 9 + 2 = 15. (1 mark)
- max: the largest is 9. (1 mark)
- sorted: [2, 4, 9]. (1 mark)
- Answer: 15, 9 and [2, 4, 9]. (1 mark)
Go deeper: the professor's notes
Add each.
len(list).
First and last.
Count
| Items in | Items used |
|---|---|
| 10 | 10 good |
| 10 | 5 bad |
At work: An average
The average is based on a list.
- Print len(list).
- Compare with the source.
What a careful practitioner decides. Always compare the count in with the count used.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. sum([2, 3, 5]) is:
2 + 3 + 5.
2. max([4, 9, 2]) is:
The largest.
3. sorted([3, 1, 2]) gives:
Smallest first.
4. The average of [70, 85, 60, 90] is:
305 / 4.
Phase 4: Close
Step 7 Recap
- for s in scores visits every item.
- sum, max, min and sorted do common jobs.
- Check how many items the loop really covers.
Step 8 Exit ticket (ungraded)
Write a loop that finds how many items in [5, 12, 8, 20] are over 10.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write a program that prints the largest mark without using max.
In industry: A range that stopped short
A coding error in a working spreadsheet left five countries out of an average used in a widely cited 2010 economics paper. A student found it in 2013 when he obtained the spreadsheet.
Link to this lesson. When you total or average a list, check how many items you really included.
Source: Herndon, Ash and Pollin (2013), PERI; New Statesman, 17 April 2013
Common mistakes
| Mistake | Why it fails |
|---|---|
| Dividing by the wrong count | Use len(list). |
| Leaving items out of the loop | Check the first and last item. |
| Expecting sorted to change the list | It returns a new list. |
Limits. Large lists of decimals can show tiny rounding differences when summed.