Declaring lists
- Python course
- Beginner
- Lesson 53/116
- 4 min
Learn to declare lists with [].
Declaring lists
A list is an ordered, mutable collection. It is written with square brackets []:
fruits = ["Apple", "Pear", "Banana"]
numbers = [10, 20, 30]
mixed = [1, "two", 3.0]Lists can hold items of different types. Since they are ordered, the order of items is preserved. An empty list is written as [].
The biggest advantage of lists is mutability: you can change them later with append, insert, remove.
Empty and single-item lists
An empty list is written as []. A single-item list uses the same structure:
empty = []
single = [42]An empty list can be filled later with append. This is a common way to collect data from the user.
Practice Declare a list and print it
Assign fruits = ["Apple", "Pear", "Banana"]. Print print(fruits).
Starter code
# Declare the listSample solution
fruits = ["Apple", "Pear", "Banana"]
print(fruits)Expected output
['Apple', 'Pear', 'Banana']
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is a valid list declaration?
- fruits = {"Apple"}
- fruits = "Apple", "Pear"
- fruits = ("Apple",)
- fruits = ["Apple", "Pear"] Correct answer
Explanation: Lists are written with square brackets [].
2 Define an empty list
cart = ____
cart.append("Apple")
cart = []
cart.append("Apple")
Explanation: cart = [] defines an empty list.
3 Match the list features
| Term | Meaning |
|---|---|
[] |
List brackets |
ordered |
Item order preserved |
mutable |
Can add later |
append |
Add item method |