Explain that a list (known as an array in some languages) is another data structure. It lets you store a list of things. Show how lists use square brackets .
Demonstrate how to create a list and how lists are indexed. Demonstrate how to display individual elements and a range of elements in a list.
Demonstrate how to add items to a list, delete items from a list and how to check that an item is in a list. (Activities 11.1 & 11.2)
Explain that all data types (strings, integers, floats, Boolean and other lists) can be stored in a list.
Give pupils the opportunity to write programs that use lists. (Activity 11.3)
Explain that lists are mutable, i.e. the items in them can be changed. Demonstrate the commands to change values in a list. (Tuples are like lists but are immutable; they cannot be changed).
Demonstrate to pupils how to use the Python docs tutorial to find out more about handling lists.
Complete this table to summarise the list commands.
|Things to do with lists||Commands|
|Create a list|
|Reference an item in a list|
|Delete an item in a list|
|Append an item to the end of a list|
A list is a data structure that stores a set of elements. Lists are assigned to a name using square brackets.
Each element in a list has an index location. The first element of the list is in position zero (0).
Elements of a list are referenced using their index location (an integer number).
A range of elements can be displayed using
[start index: end index]
Start index is the position to start at (remember that indexing starts at zero). End position is the index AFTER the index required.
- Make this list and experiment with the list commands.
- What does mylist display?
- What does mylist[1:3] display?
- What does mylist[-1] display?
- What command will display just apple?
- What command will display lemon and pear?
- Make a new list called myfood containing your five favourite foods.
- Display the whole list.
- Display the food item at index position 3.
- Display the food item at index position 0.
- Display the food items at index position 1 to 4.
- Make the list that contains the class marks for Amy Jones.
Marks = [‘Amy’, ‘Jones’, ‘English’, 67, ‘Maths’, 76, ‘Computer Science’, 96]
- The English teacher has entered Amy’s mark incorrectly; it should be 72 not 67. Alter this item in the list.
- Add the mark for Physics to the end of the list. “Physics”, 65
- Remove “Maths” and the score 76 from the list.
- Write a program to find the average score for the three subjects (English, Computer Science and Physics).
Using Python docs help
- Select help/python docs then select the Python tutorial and go to 1.4 Lists.
- Read through the discussion of lists and try out the examples. Make a note of three more facts about lists to share in the next lesson.
Python is a very powerful programming language which is used in universities and commercial organisations. You do not need to know all the details provided in the Python docs but, with practice, you should be able to find information about Python that can be very useful.