HomepageTeaching PageResearch PageResources Page

Homework: Lists

The book does a great job of introducing lists. Read through sections 10.1 and 10.2 and do the examples.

One additional thing to notice: you can't extend the length of a list by just using assignment. For example:

>>> t = []
t[0] = "monkey"
Traceback .....
.....
IndexError: list assignment index out of range
>>> t
[]
>>> t = ["unicorn", "ocean"]
>>> t[2] = "narwhal"
...
IndexError: ...
>>>
        

You'll learn later how to use string methods to append elements and increase the size of a list.

Read through sections 10.3 and 10.4 of the book and do the examples.

To see more clearly why len returns 4 on the last list in 10.3, let's write a loop that prints out each element. (t is a common variable for a general list.

>>> t = ['spam', 1, ['Brie', 'Roquefort', 'Pol de Veq'], [1, 2, 3]]
>>> len(t)
4
>>> for element in t:
...     print(element)
...
spam
1
['Brie', 'Roquefort', 'Pol de Veq']
[1, 2, 3]
>>>