Quiz
Warm-up from lesson 5-3: in the build pattern you started a string accumulator as `result = ""`. What was the job of that line?
A list holds many values in order
One variable holds one value, but real data comes in bundles: a class's scores, the rows a database query returned, every message in a chat. The list is Python's workhorse container for ordered bundles, and after this unit it will appear in nearly every program you write.
A list is a sequence of values in square brackets, separated by commas:
scores = [85, 92, 78] names = ["Ada", "Grace"] empty = []
Everything you learned about string positions in lesson 2-1 transfers directly: len(scores) is 3, scores[0] is the first item, scores[-1] the last, and scores[0:2] slices out a new list.
The big difference from strings: lists are mutable, you can change them in place.
scores[1] = 95 # replace the item at index 1
A string method handed you a new string and left the original alone. A list assignment like this genuinely edits the list.
Code exercise · python
Run this. The indexing lines should feel familiar from strings. The assignment on line 5 is the new power: it edits the list.
Quiz
What does `[10, 20, 30, 40][1:3]` evaluate to?
Code exercise · python
Your turn. The list has a typo at index 1. Replace it with the correct spelling `green` using index assignment, then print the list.
Code exercise · python
One more. `readings` holds a week of temperatures, oldest first. Slice out the LAST three with one negative-index slice and print that list, then print the newest single reading using a negative index.