Lists
Prerequisites: for loops
A list is an ordered, changeable collection of values.
Syntax
fruits = ["apple", "banana", "cherry"]
Example
fruits.append("date")
print(fruits[0])
print(fruits[-1])
print(len(fruits))
Expected output
apple
date
4
Indexing starts at 0; negative indices count from the end
(-1 is the last item). .append() adds to the end.
Common mistakes
- Accessing an index that doesn't exist raises
IndexError— checklen(list)first if you're not sure the index is valid.
Practice
Create a list of three favorite movies and print the second one.
Show solution
movies = ["Inception", "Arrival", "Interstellar"]
print(movies[1])