PY-101 ยท Collections
Tuples
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
A tuple is an ordered group of values in round brackets, such as (12.97, 77.59), that cannot be changed after it is made. Use it for things that belong together and should stay fixed, like coordinates or an RGB colour. You read it like a list, but you cannot append, remove or reassign items.
- Create a tuple
- Read items like a list
- Know when fixed is better
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
office = (12.97, 77.59)print("Latitude:", office[0])lat, lng = officeprint(lat, lng)try:office[0] = 0except TypeError as e:print("Cannot change:", e)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Latitude: 12.97
12.97 77.59
Cannot change: 'tuple' object does not support item assignment ๐ฏ Write min_max(nums) that returns a tuple (smallest, largest). Unpack it into low, high for [8, 3, 12, 5] and print them.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
return min(nums), max(nums), then low, high = min_max([8, 3, 12, 5])
- The problemA delivery app stores each shop's location as latitude and longitude. If some code accidentally swapped one number, the rider would go to the wrong place. A tuple makes that impossible.
- The Python idea
t = (a, b, c)A tuple is an ordered group of values in round brackets, such as (12.97, 77.59), that cannot be changed after it is made. - In real lifeDatabase libraries return each row as a tuple, so the row you read stays exactly as it came from the database.
Start with a question
A delivery app stores each shop's location as latitude and longitude. If some code accidentally swapped one number, the rider would go to the wrong place. A tuple makes that impossible.
Fixed data is safer data. Functions also use tuples to return several values at once.
Explain it like I'm 10
A tuple is a sealed lunchbox: you can see what is inside and take a look at each item, but you cannot swap anything once it is packed.
The technical explanation
point = (3, 4) makes a tuple. point[0] reads 3. point[0] = 9 raises TypeError.
Tuples can be dictionary keys and set members because they never change; lists cannot.
return name, age in a function returns a tuple, which you can unpack: n, a = get_user().
Syntax
t = (a, b, c)
x, y = (3, 4) Real-world scenario
Database libraries return each row as a tuple, so the row you read stays exactly as it came from the database.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Sets โQuestions people ask
When should I use a tuple instead of a list?
Use a tuple for a fixed group whose size and meaning do not change, like (x, y) or (red, green, blue). Use a list for a growing collection of similar items.
Are tuples faster than lists?
Slightly, and they use a little less memory, but choose them for meaning and safety, not speed.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner When should I use a tuple instead of a list?
Use a tuple for a fixed group whose size and meaning do not change, like (x, y) or (red, green, blue). Use a list for a growing collection of similar items.
beginner Are tuples faster than lists?
Slightly, and they use a little less memory, but choose them for meaning and safety, not speed.
intermediate Can you change an item in a tuple?
No. Tuples are immutable.
intermediate Which makes a one-item tuple?
(5,). The comma makes it a tuple.
Quick revision
Remember
- Create a tuple
- Read items like a list
- Know when fixed is better
- A tuple with one item needs a comma:
(5,). Without it,(5)is just the number 5 in brackets.
Syntax
t = (a, b, c)
x, y = (3, 4) Official documentation
- Tuples and sequences (docs.python.org)