Python Lists
Keep ordered values together.
On this page
The concept
A list stores an ordered collection. Indexes start at zero. Use append to add an item, and a for loop to visit each value.
Code example
Python
scores = [82, 91, 76]
scores.append(95)
for score in scores:
print(score)Output
82 91 76 95
Follow the code
Open the walkthrough to inspect the example one step at a time.
Show guided walkthrough
Guided walkthroughReady
1
scores = [82, 91, 76]2
scores.append(95)3
for score in scores:4
print(score)Follow the example
Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.
View the complete dry-run table
| Step / line | What happens | State after step | Output so far |
|---|---|---|---|
| Create an ordered list of three scores. | scores = [82, 91, 76] | — | |
| Append 95 to the end of the same list. | scores = [82, 91, 76, 95] | — | |
| The loop visits each item in order. The print statement runs four times. | scores = [82, 91, 76, 95] | 82 91 76 95 |
Behind the scenes
How Python processes this code
Python runs this source through its interpreter. It creates objects for values and resolves names as each statement runs.