Lists
A list holds many values in order, inside square brackets:
fruits = ["apple", "banana", "cherry"]
0"apple"-3
1"banana"-2
2"cherry"-1
fruits[0]reads an item (indexes start at 0, like strings)fruits[1] = "kiwi"replaces an item, because lists are mutablefruits.append("date")adds to the endlen(fruits)counts items
See it run
fruits = ["fig", "kiwi", "lime"] fruits.append("plum") fruits[0] = "pear" print(fruits[-1], len(fruits))
One list, changed in place. Watch the same box grow and change.
Practice lists
Exercise 1
Start with fruits = ["apple", "banana", "cherry"]. Append "date", then replace "banana" with "blueberry" by index. Print len(fruits).
Exercise 2
Use the playlist in the editor below. The radio host wants to:
- append
"Outro"to the end - replace the first song with
"Sunrise"by index - set
firstto the first song andlastto the last song (use a negative index), after the changes - set
countto the number of songs
Exercise 3
In the line list in the editor below, the first and last people want to swap places. Swap line[0] and line[-1] using indexes (don't retype the names), then append "Eve", who just arrived.
The result should be ["Dee", "Ben", "Cy", "Ana", "Eve"].
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Loading exercise content…
Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.
Memory · names → values
Variables appear here as Python creates them.
Output & plots