python · level 2 · unit 2 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a sealed packet of values that belong together.
A tuple is like a list in round brackets, but sealed: once made, it cannot change. Use one for values that belong together and should stay put, like a city and its map position.
To a tuple, put one variable per value on the left of =. city, lat, lon = place puts each value in its own box, in order. Your two loop variables with .items() were unpacking tuples all along.
A function can return a tuple, so it can hand back two answers at once. Try to change a tuple, though, and you get a . A tuple has no .append() either, so trying it gives an .
def coldest_and_warmest(temps):
return min(temps), max(temps)
low, high = coldest_and_warmest([12, 17, 9, 14])
print(f"Low {low}, high {high}")
place = ("London", 51.5, -0.12)
city, lat, lon = place
print(city, lat) The commas make a tuple, so return min(temps), max(temps) hands back a tuple even without brackets.
guess first
Can you change a sealed packet? What happens?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
screen = (1920, 1080)
width, height = screen
print(width * height)
screen[0] = 1280
print(screen)
your turn
Your turn. Hand back two answers at once.
Return the total first, then the average. On line 4, type some scores and unpack both answers into total and average.
fix it
The café order will not grow. Can you fix it?
- Read the error. Which kind of value does Python say has no
append? - Round brackets make a sealed tuple, which can never grow. This order needs to grow, so it should be a list.
- Change the round brackets on line 1 to square brackets, then run.
nailed it
you packed and unpacked tuples. :D
- A tuple is a fixed group of values in round brackets.
- Unpack it with one variable per value:
a, b = pair. - Need to change or grow it? Use a list instead.
new words in your collection
- tuple
- unpack
- TypeError
- AttributeError
This lesson isn’t saved. A free account remembers every lesson you finish.