Lists
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.
Each slot has an index: its position, counted from 0. So fruits[0] is 'apple' and fruits[1] is 'fig'. A negative index counts from the end: fruits[-1] is the last item.
A list can grow. fruits.append("kiwi") adds one item to the end, so the list now has three slots and 'kiwi' gets the next index, 2. Nothing else moves.
fruits[0] = "pear" puts a new value in slot 0, so 'apple' is replaced. It is still the same list, with one item changed. A list is mutable: it can change after it is made.
Line 4 reads fruits[1], which is 'fig', and len(fruits), the number of items: 3. print() writes both with a space between them: fig 3.
Tuples
A tuple is a list's fixed cousin, written with round brackets: point = (3, 4). It keeps its items in order and indexes them from 0 too, so point[0] is 3 and point[1] is 4.
A tuple is immutable: once made, its items cannot be replaced, added or removed. Line 6 tries point[0] = 9, so Python stops the program: TypeError: 'tuple' object does not support item assignment.
Choosing
Use a list for a collection that grows or changes, such as a shopping list. Use a tuple for a few values that belong together and stay fixed, such as the x and y of a point.
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.