Lists, explained simply
A variable holds one thing. A list holds many things in order, like a shopping list or a line of books on a shelf. You write a list with square brackets and put commas between the items.
Lists can grow. The append() method adds a new item to the end. The len() function tells you how many items are in the list right now.
Lists and loops are best friends. A for loop can visit every item in a list, one at a time, so you can print, check or change each one without knowing in advance how long the list is.
Example: Pack a bag
bag = ["map", "torch"]
bag.append("snack")
print(len(bag))
for item in bag:
print("Packed:", item)Output
3
Packed: map
Packed: torch
Packed: snackTry this
- Make a list of your three favourite foods and print how many there are.
- Use append() inside a loop to add five numbers to an empty list.
- Print the whole list at once with print(bag) and compare it with the loop.
