Lists and tuples
A list keeps many values in order under one name, and you can change it: add items, replace them, read them back by index. A tuple keeps values in order too, but once it is made it cannot change. Step through a six-line program, lists.py, that builds a list, grows it with append(), changes one slot, prints from it, then makes a tuple and watches Python refuse to change it.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01After colors = ["red", "green", "blue"], what is colors[1]?
Indexes count from 0, so colors[0] is 'red' and colors[1] is the second item, 'green'.
02nums = [4, 7] then nums.append(9). What are nums and len(nums) now?
append() adds one item to the end of the list, so nums becomes [4, 7, 9] and has 3 items.
03What does ["a", "b", "c"][-1] give?
A negative index counts from the end: -1 is the last item, 'c'.
04point = (3, 4). What happens on point[0] = 9 ?
A tuple is immutable: its items cannot be replaced. To get (9, 4), make a new tuple: point = (9, 4).
05Which is the better fit for a tuple rather than a list?
A tuple suits a small, fixed group of values that belong together, such as the parts of a date. Things that grow or change suit a list.
Glossary
- List
- An ordered collection of values in square brackets, such as ["apple", "fig"]. It can change after it is made.
- Tuple
- An ordered collection of values, usually in round brackets, such as (3, 4). It cannot change after it is made.
- Item
- One value inside a list or tuple.
- Index
- An item's position, counted from 0. fruits[0] is the first item; fruits[-1] is the last.
- len()
- A built-in function that gives the number of items, such as len(["a", "b"]) == 2.
- append()
- A list method that adds one item to the end of the list.
- Mutable / immutable
- Mutable values (lists) can be changed in place; immutable values (tuples, strings, numbers) cannot.
- TypeError
- The error Python raises when a value does not support an operation, such as assigning to an item of a tuple.
Notes and sources
- 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.
- An error like this stops the program at that line: lines 1 to 5 have already run, which is why fig 3 is printed first.
- An index must point at an existing slot: with three items, fruits[3] raises IndexError: list index out of range.
- It is the comma that makes a tuple; the round brackets are usually optional. A one-item tuple needs a trailing comma, (5,), because (5) is just the number 5.
- Python tutorial: An informal introduction (lists)
- Python tutorial: Data structures (more on lists)
- Python tutorial: Tuples and sequences
- Python library: Sequence types (list, tuple, range)
- Python library: Mutable sequence types
- Python library: Built-in functions (len)
- Python library: Built-in exceptions (TypeError, IndexError)
- Python glossary (mutable, immutable, sequence)
Quiz Unlocks after the last step.