[0][1][2][]fruits =1fruits = ["apple", "fig"]2fruits.append("kiwi")3fruits[0] = "pear"4print(fruits[1], len(fruits))5point = (3, 4)6point[0] = 9[0][1]()point =TypeError["apple", "fig"]"kiwi""pear"fig 3(3, 4)9
Code card: lists.py; the highlighted line is the one runningfruits: a list, one slot per item, index on the frontpoint: a tuple, locked once it is madeTerminal: where print() and errors appearA list holds values in order between square brackets and can change; a tuple, between round brackets, cannot. Each step lights the parts it is about. A token hops from a line of the code card to a slot: the list appears, grows a third slot as its closing bracket slides out, and has slot 0 replaced. Then fig 3 is printed and the tuple appears. In line 6 the 9 bounces off the locked tuple: Python stops with a TypeError, and the terminal shows the end of the error.

Lists

  1. A list holds many values, in order, as one value. Square brackets make one: line 1 builds a list of two strings and = gives it the name fruits. Each item sits in its own slot.

Tuples

Choosing

Try it yourself

;; step 1/8 · line 1: fruits = ["apple", "fig"]
>>> fruits = ["apple", "fig"]
>>> fruits
['apple', 'fig']
>>> type(fruits)
<class 'list'>
>>> empty = []        # a list with no items

In the boxes and at the >>> prompt, Python writes strings with single quotes, such as 'fig'; print() writes them without quotes. In the terminal, the figure shortens the traceback: the real one also names the file and line (line 6) and repeats the failing line with ~~~~~^^^ under it. The last line is exactly as Python prints it.