AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Cases and lists

Week 8 · 1.5 Programming with data · 3 lessons
2

Lists of many items

Computing 9: Topic 1.5, pp. 89–118
GoalStore many items in one list, read and change them by index, and add new items with append().
New words
array · a row of numbered boxes that keeps many values under a single namelist · the Python structure used instead of an array; it can grow and shrinkappend · to add an item to the end of a list
Explanation

A variable holds a single value. If you had fifty related values, you would need fifty names, and that would be hard to manage. An array solves this: one identifier, many items, and each item has an index starting at 0. All the items should be of the same type. In Python the structure is a list, written with square brackets, such as fares = [3, 5, 8, 12]. fares[2] reads the third item, and len(fares) gives how many items there are. fares[1] = 6 replaces an item, but only if that index already exists. An empty list is written [], and fares.append(15) adds a new item at the end. Using an index that does not exist stops the program with an IndexError.

Index0123
fares35812
Worked examples
Python
1fares = [3, 5, 8, 12]
2print(fares[2])
3print(len(fares))
4fares[1] = 6
5print(fares)
Output
8
4
[3, 6, 8, 12]
Reading and changing a list. fares[2] is the third item, 8. The list has 4 items. After fares[1] = 6, the second item has been replaced. Printing a whole list shows square brackets and commas.
Python
1queue = []
2queue.append("Ali")
3queue.append("Mia")
4queue.append("Ben")
5print(queue[1])
6print(len(queue))
7print(queue)
Output
Mia
3
['Ali', 'Mia', 'Ben']
A list that grows. It starts empty, and each append adds one item at the end. The list ends with three items. queue[1] is Mia. Python shows text items with single quotation marks when it prints a whole list.
QuestionArray (some other languages)Python list
Can it start with no items?no: the size is stated when it is createdyes: [] is an empty list
Can it get longer while the program runs?noyes
Where does counting start in this course?index 0index 0
How do you add one more item at the end?you cannotlist.append(item)
Arrays and lists compared. A Python list can change its length while the program runs, but an array in other languages has a fixed size. Both start counting at index 0.
Class activity

Seat row (no computer). Draw six seats in a row on a sheet and number them 0 to 5. Put a small name card on each seat. Your partner gives orders such as ‘who is in seat 3?’, ‘put Lina in seat 1’, ‘how many seats are there?’ and ‘seat 6?’. Say what Python would do for each order. For seat 6 the program stops with an IndexError. Then draw a new seat at the end to act out append. Swap roles after five orders.

Practice
1
The list is nums = [4, 9, 2, 7, 5]. Complete the table.
Index01234
nums49275
2
Write down what this program prints.
Python
1tasks = ["wash", "cook"]
2tasks.append("clean")
3tasks[0] = "shop"
4print(tasks)
5print(len(tasks))
3
This program stops with an error. Give the error message and correct the program so that 50 is stored.
Python
1scores = []
2scores[0] = 50
4
Write a program that creates the list temps = [21, 24, 19, 23] and prints the third item, the last item, and the number of items.