Lists
A list stores values in order.
Output:
[8, 6, 9]
Lists are useful when order matters or when you need to process several values one by one.
Iterating Over a List
Use a for loop to visit each item:
Output:
8
6
9
The loop variable score refers to one value at a time.
Appending Values
append() adds one item to the end:
Output:
[8, 6, 9]
This pattern is common when a program builds a result gradually.
Build a list
The list starts empty and grows one item at a time.
Ready to run.
Lists Can Hold Any Values
Lists can hold strings:
tokens = ["large", "language", "model"]
They can also hold records:
Later, we will usually prefer dictionaries or dataclasses for named records. For now, notice that a list can hold other lists.
Lists Keep Order and Duplicates
Do not name a list as if it were one item:
score = [8, 6, 9]
This works, but scores is clearer because the value holds several scores.
What does this program print?
Answer it first, then check.
HintKeep the existing items
append() changes the list by adding one item at its end.
SolutionSix is appended
The list begins as [2, 4]. After values.append(6), it is [2, 4, 6],
which is what Python prints.
Lists Store Sequences You Can Change
Use a list when you have several values and their order is meaningful.