Tuples: a row that stays fixed · Python Programming Class 9–10 | StemPy Lab
Course 1.2·Lesson 1 of 5
Tuples: a row that stays fixed
A tuple is a row of values written with round brackets that cannot be changed after it is made. It is read by position, like a list, but an assignment to an item gives a TypeError. The commas make a tuple, and a tuple with one item needs a comma. Unpacking puts each item into its own variable, which also swaps two variables. A function that returns two values sends back a tuple. A list of tuples is a good list of small records, and a tuple can be a dictionary key.
about 40 min1 simulation2 practice questions
Lesson 1 of 5Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
After this lesson you can
make a tuple and read it by position,
explain why a tuple cannot change, and what to do instead,
unpack a tuple into variables,
write a function that returns two values, and
keep records as a list of tuples.
A row that cannot change
A tuple is like a list that is locked. You write it with round brackets, and you read it by position, as for a list. But you cannot change, add or remove items after it is made.
Why would you want that? Some values belong together and must not change by accident: the two numbers of a point, the three numbers of a colour, the day, the month and the year of a date.
Four cards for the four containers. A list is written with square brackets, like 1, 2, 3: it keeps an order, it can change, and repeats are allowed. A tuple is written with round brackets: it keeps an order, it is fixed and cannot change, and repeats are allowed. A set is written with curly brackets: it has no order, no repeats, and gives a fast yes or no test. A dictionary is written with curly brackets and key and value pairs: you look up by key, and keys are unique.
The picture compares the four containers that you will know by the end of this course. Look at the tuple card for now: round brackets, an order, fixed.
Predict first, then run it. Then check these points.
Everything you know from lists works for reading: positions, negative positions, slices, len, in, index and count. A slice of a tuple is a tuple.
The commas make the tuple, not the brackets. 3, 4 is a tuple. The brackets only make it easier to read.
A tuple with one item needs a comma: (5,). Without the comma, (5) is only the number 5 in brackets.
point + (5, 6) joins two tuples into a new tuple. The old ones are not changed.
It cannot change
It looks like a loss. It is a gain. If a function receives a tuple, it cannot change your data by accident. A tuple is safe. And because it never changes, it can be a key of a dictionary, as you will see.
Unpacking
Try (3, 4): the distance is exactly 5. A tuple is also how a function hands back two answers at once.
A function that gives two answers
A function can only return one thing. But that one thing can be a tuple, which holds two or more values.
Lists of tuples, and tuples as keys
Common mistakes
Trying to change a tuple. A TypeError. Make a new tuple.
A one-item tuple without a comma.(5) is the number 5.
Unpacking into the wrong number of names. A ValueError.
Using a list where a key is needed. Use a tuple.
Confusing () for an empty tuple with {} for an empty dictionary and [] for an empty list.
Practice
Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Warm-up. Open the lab Unpack and swap. You unpack a tuple, and the checker confirms each variable.
Build. Open the problem Read and unpack a tuple and the problem A function with two answers.
Stretch. Open the problem A tuple cannot change and explain how you would "change" one item.
Remember these
A tuple is written with round brackets and cannot be changed. The commas make it.
x, y = point unpacks a tuple. a, b = b, a swaps two variables.
A function can return two values as a tuple, and the caller can unpack them.
A tuple can be a dictionary key. A list cannot.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
What is (5, 8, 2)[1]?
What happens with t = (1, 2) and then t[0] = 5?
What is the type of (5), and of (5,)?
How do you swap a and b?
Enroll free in Tuples and Sets
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.