Lesson 5 of 35Python
Python · Lesson 5 · 25 minutes
Lists – many values at once
What you'll learn
- Create lists and access items by position (index)
- Append, change and count items
- Analyse lists with
sum(),max()and a loop
A box with compartments
So far every variable held exactly one value. A list stores many values in a fixed order. You write it in square brackets, separated by commas:
friends = ["Mia", "Ben", "Lea", "Tom"]
print(friends)
print(len(friends))len() counts how many items are in the list.
Accessing single items
Every item has a position, the index. Careful: Python starts counting at 0!
| Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Value | “Mia” | “Ben” | “Lea” | “Tom” |
friends = ["Mia", "Ben", "Lea", "Tom"]
print(friends[0])
print(friends[2])
print(friends[-1])With [-1] you get the last item – handy when you don’t know how long the list is.
Changing lists
Lists can be changed: you can append, replace and remove items. Press Steps and watch the list in memory:
backpack = ["bottle", "bread"]
backpack.append("apple")
backpack[0] = "water bottle"
backpack.remove("bread")
print(backpack)
print("apple" in backpack)With in you check whether something is in the list. The result is True or False.
Analysing lists
For lists of numbers Python has ready-made tools: sum() adds everything up, max() and min() find the biggest and smallest value. And with for you go through every item one by one:
temperatures = [18, 21, 25, 19, 23]
print(f"Warmest day: {max(temperatures)} degrees")
print(f"Total: {sum(temperatures)}")
for t in temperatures:
if t >= 20:
print(f"{t} degrees – T-shirt weather")Remember: The first index is 0, the last is
len(list) - 1. If you writelist[len(list)], you get anIndexError.
Quick check
Before you start the task: what does this program print? Type your guess – then Python works it out.
animals = ["dog", "cat", "mouse"]
animals.append("hedgehog")
print(animals[1], len(animals))Your task
Your turn
The list points contains the results of a game. Print three lines: Highest score: 19, Average: 11.2 (rounded to one decimal place with round(value, 1)) and Over 10 points: 3 (how many values are greater than 10). Let Python work everything out – don't type the numbers yourself, because the test also tries a different list.
points = [12, 7, 19, 3, 15]
# Analyse the list here:- Something to think about
Three questions about the same list: which value is the largest? What do you get if you add them all up and divide by how many there are? And how often is a condition true?
- Which command helps?
max(points)gives you the biggest value.Average
=total divided by count, sosum(points)/len(points). Round withround(..., 1).Go through the list with
forand count along with a variable whenever a value is greater than 10.With the list [5, 30, 10, 11] something else came out than expected. Did you type numbers in yourself? (10 doesn't count as “over 10”.)
- Gap code – fill in the ___
points = [12, 7, 19, 3, 15] print(___) print(___) count = ___ for p in ___: if ___: count = ___ print(___)
Solve it without hints to earn a ⭐ on your progress page.
Show solution🔒 Unlocks after all hints or 2 checks
Try it yourself first – mistakes teach you the most. Here is one possible solution:
points = [12, 7, 19, 3, 15]
print(f"Highest score: {max(points)}")
print(f"Average: {round(sum(points) / len(points), 1)}")
count = 0
for p in points:
if p > 10:
count = count + 1
print(f"Over 10 points: {count}")For fast learners
Pro task
Find the second largest number in numbers – without sorted(), .sort() and without max(). Equal numbers only count once: in [4, 9, 2, 9, 7] the second largest is 7. Print Second largest: 7.
numbers = [4, 9, 2, 9, 7]
# Remember the largest and the second largest number while going throughShow pro solution
numbers = [4, 9, 2, 9, 7]
largest = None
second = None
for n in numbers:
if largest is None or n > largest:
second = largest
largest = n
elif n != largest and (second is None or n > second):
second = n
print(f"Second largest: {second}")