Module 8 · Python Data Structures — topic 10 of 22
Tuples
Tuples are ordered, immutable sequences used for fixed records and multiple values. Learn tuple syntax including the one-item tuple, why immutability is useful, and where Python creates tuples for you.
Learning objectives
- Create tuples including empty and single-item tuples
- Explain when a tuple is better than a list
- Recognise tuples created by packing and functions
Definition
Tuples are ordered, immutable sequences used for fixed records and multiple values. Learn tuple syntax including the one-item tuple, why immutability is useful, and where Python creates tuples for you.
Introduction
A tuple is like a list that can’t be changed. It is written with parentheses and commas: ("Asha", 82, "Pune"). Once created, its items can’t be replaced, added or removed.
Why it matters
Immutability is a feature. A tuple communicates “this is a fixed record”, such as a coordinate, a date or an RGB colour, and protects it from accidental changes. Tuples can also be dictionary keys and set members, which lists can’t.
Detailed explanation
The commas make the tuple, not the parentheses: t = 1, 2, 3 is also a tuple. Parentheses are needed for clarity and inside larger expressions.
An empty tuple is (). A single-item tuple needs a trailing comma: (5,). Without it, (5) is just the number 5 in parentheses.
Tuples support indexing, slicing, len(), in, iteration, + and * like other sequences.
They have only two methods: count() and index().
Python creates tuples automatically when you write several values separated by commas: return a, b returns a tuple, and x, y = y, x swaps through a temporary tuple.
Tuples are usually slightly smaller and faster than lists, but the main reason to choose one is meaning: a fixed group of related values.
A tuple can contain mutable items such as a list; the tuple can’t be changed, but the list inside it can.
Syntax
- Commas create the tuple.
- A one-item tuple needs a trailing comma.
- Values separated by commas are packed into a tuple.
Code examples
Creating tuples
tuple tuple int
('Asha', 82) 2Immutability
255 0 TypeError: 'tuple' object does not support item assignment
More examples
Tuples as records and dictionary keys
45 91
230
Lists can't be keys; tuples can
('Asha', [82, 91, 78])Step-by-step explanation
- min_max returns two values, which Python packs into a tuple.
- low, high = ... unpacks that tuple into two names.
- A pair of cities is a natural tuple key for a distance lookup.
- The tuple record can’t be reassigned, but the list inside it can still change.
Expected output
Running “Tuples as records and dictionary keys” prints exactly:
Practical applications
- Map applications store coordinates as (latitude, longitude) tuples.
- Databases return rows as tuples.
- Functions return several results as a tuple, such as divmod().
Best practices
- Use tuples for fixed-size records whose fields have meanings.
- Use lists for collections that grow or change.
- Unpack tuples into well-named variables.
Common mistakes
Forgetting the comma in a single-item tuple
Avoid
Prefer
Without the comma it is just a string in parentheses.
Trying to append to a tuple
Avoid
Prefer
Tuples have no append; build a new tuple if you must.
Key takeaways
- Tuples are ordered and immutable.
- Commas create tuples; (x,) is a single-item tuple.
- Tuples can be dictionary keys and are returned for multiple values.
Try it yourself
- 1Store a date as a (day, month, year) tuple and print it in day/month/year form.
- 2Write a function that returns the sum and average of a list as a tuple, and unpack the result.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?