☰ Year plan · Communication Technology

Lists and functions

Week 12 · Chapter 12 Programming (2): lists and functions in Python, with small programs that combine them · 3 lessons
1

Lists: many values under one name

ICT Secondary 1 Term 1, Chapter 12, Lessons 12-4 to 12-5: pp. 168–174
GoalStore many values in a list, read them by index, change them, and loop over them.
New words
list · an ordered collection of values under one nameindex · the position of an item; the first item has index 0append · add an item at the end of a listaccumulator · a variable that collects a total or a best value while looping
Explanation

A list holds several values in order under one name, written between square brackets: scores = [12, 7, 9]. Each item has an index, its position, and counting starts at 0: scores[0] is 12, scores[1] is 7, scores[2] is 9. A negative index counts from the end, so scores[-1] is 9. len(scores) gives the number of items, 3. scores.append(15) adds an item at the end, and scores[1] = 8 replaces the item at index 1. After these two changes the list is [12, 8, 9, 15]. remove(9) deletes the first item with that value, and pop() removes and gives back the last item. To visit every item, use for x in scores. To collect a result we use a variable that is built up as the loop runs: total = 0 and then total = total + x for each x gives the sum 44; best = scores[0] and then replace best whenever a bigger x is found gives the largest, 15. Python also has ready tools, sum(scores) and max(scores), that give the same results; writing the loop by hand once helps you understand them. Do not name your own variables list, sum or max, because the name would hide the tool.

index0123
value128915
Worked examples
Python
1scores = [12, 7, 9]
2print(scores[0])
3print(scores[-1])
4print(len(scores))
5scores.append(15)
6scores[1] = 8
7print(scores)
Output
12
9
3
[12, 8, 9, 15]
The first program prints the first item, the last item, the length, and the list after two changes.
Python
1scores = [12, 8, 9, 15]
2best = scores[0]
3for x in scores:
4    if x > best:
5        best = x
6print(best)
7print(sum(scores), max(scores))
Output
15
44 15
The accumulator loops. total adds each item (12 + 8 + 9 + 15 = 44). best starts as the first item and is replaced whenever a bigger item is found; the last line shows that sum and max give the same results.
Python
1scores = [12, 8, 9, 15]
2scores.remove(9)
3last = scores.pop()
4print(last)
5print(scores)
Output
15
[12, 8]
Changing a list: remove(9) deletes the value 9, and pop() takes the last item (15) out and gives it back, leaving [12, 8].
Class activity

On paper, write a list of five numbers of your choice (for example the ages of five invented characters). Draw it as boxes with indexes 0 to 4, then write the result of len, of the first item, of the last item with index -1, and of a loop that adds them all.

Practice
1
names = ["Mina", "Omar", "Hana"]. What is names[2]?
2
What does len([4, 8, 6, 2]) give?
3
What does append do?
4
Why should we not call a variable sum?