Lesson 06
Tuple Deep Dive — Immutable Sequences
Create Python tuples safely, avoid the single-element trap, convert from lists and strings, unpack coordinates, and know when tuples beat lists.
What you will learn
Immutable · Ordered · Hashable · Fixed Data
A tuple is an ordered collection just like a list, but it cannot be modified after creation. Use tuples for data that should stay constant — GPS coordinates, RGB colors, database records, and function return values.
| Property | Tuple behavior |
|---|---|
| Ordered | Yes — index and slice work |
| Mutable | No — no item assignment |
| Methods | Only index() and count() |
| Hashable | Yes (when all items are hashable) |
Memory formula: Create → Access → Convert → Protect
After File 05 — Nested Lists, you know how to structure 2D data with lists. Tuples protect fixed records that should not change accidentally.
Basics: tuple — Immutable Ordered Collection.
Learning roadmap
1. Tuple vs list
Although they look similar, their behavior is very different.
Mutable
Immutable
| Feature | List | Tuple |
|---|---|---|
| Syntax | [] |
() or comma |
| Mutable | Yes | No |
| Add items | append() |
Not allowed |
| Delete items | remove() |
Not allowed |
| Best for | Dynamic data | Fixed records |
2. Creating tuples
Different ways
t1 = ()
t2 = tuple()
t3 = (10, 20, 30)
t4 = 10, 20, 30t1 and t2 are empty tuples. t3 and t4 both create (10, 20, 30).
Important: Parentheses are optional. The comma actually creates the tuple.
3. The single-element trap
This is one of the most common interview questions.
not_tuple = (10)
real_tuple = (10,)
also_tuple = 10,
print(type(not_tuple)) # int
print(type(real_tuple)) # tupleRemember:
()groups expressions.,creates tuples.
4. Converting between lists and tuples
Python provides two built-in constructors.
print(tuple([10, 20, 30]))
print(tuple("PYTHON"))
print(tuple(range(5)))
print(list((1, 2, 3)))Output:
(10, 20, 30)
('P', 'Y', 'T', 'H', 'O', 'N')
(0, 1, 2, 3, 4)
[1, 2, 3]| Function | Result |
|---|---|
tuple(list) |
List → tuple |
list(tuple) |
Tuple → list |
tuple(str) |
Characters as tuple |
tuple(range()) |
Number sequence |
5. Indexing and slicing
Tuples follow exactly the same indexing rules as lists — but no assignment.
t = (10, "RS", 34.56, True)
print(t[0]) # 10
print(t[-1]) # True
print(t[1:3]) # ('RS', 34.56)6. Immutability in action
Trying to change a tuple causes an error.
t = (10, 20, 30)
t[0] = 99 # TypeError: 'tuple' object does not support item assignmentKey idea: You may read values, but not replace them.
7. Can tuples contain mutable objects?
Yes! The tuple is immutable, but objects inside it may not be.
record = (
100,
"RS",
{"Python": 95}
)
record[2]["OS"] = 90
print(record) # (100, 'RS', {'Python': 95, 'OS': 90})You cannot do record[2] = {} — that would replace a tuple slot. But the dict object at that slot can still be modified. See Immutable Types for the full rule.
8. Packing and unpacking
Packing puts multiple values into one tuple. Unpacking assigns them to separate variables.
# Packing
point = 12.9716, 77.5946
# Unpacking
lat, lon = point
print(lat, lon)Star unpacking
first, *middle, last = (1, 2, 3, 4, 5)
print(first) # 1
print(middle) # [2, 3, 4]
print(last) # 5*middle collects remaining items into a list.
When to choose a tuple
- Return multiple values from a function
- Dictionary keys (must be hashable)
- Protect data from accidental in-place edits
- Slightly smaller and faster than lists for fixed data
Mini project — GPS coordinates
Store latitude and longitude as a fixed tuple; unpack when passing to a function.
def distance_from_origin(point):
lat, lon = point
return (lat ** 2 + lon ** 2) ** 0.5
location = (12.9716, 77.5946)
print(distance_from_origin(location))The tuple keeps the coordinate pair fixed — you cannot accidentally append a third value like you could with a list.
One-page revision sheet
| Task | Syntax |
|---|---|
| Empty tuple | () or tuple() |
| Single-item tuple | (10,) or 10, |
| List → tuple | tuple(my_list) |
| Tuple → list | list(my_tuple) |
| Read element | t[i] |
| Slice | t[start:end] |
| Unpack | a, b = t |
| Star unpack | first, *rest = t |
Common interview mistakes
Mistake 1 — (10) is not a tuple:
type((10)) # int — wrong
type((10,)) # tuple — correctMistake 2 — assuming tuple means everything inside is frozen:
t = ([1, 2],)
t[0].append(3) # works — inner list is mutableCorrect: Immutability applies to tuple slots, not necessarily to nested mutable objects.
Practice questions
Test yourself before File 07 — Tuple Methods:
- What is
type((10))vstype((10,))? - How do you convert a list to a tuple?
- Why does
t[0] = 5fail on a tuple? - Can a tuple contain a dictionary? Can you modify that dictionary?
- How do you unpack
(lat, lon)into two variables?
Knowledge check
Q1: type((10)) vs type((10,))?
Answer: int vs tuple — trailing comma required for single-item tuples.
Q2: Convert list to tuple?
Answer: tuple(my_list).
Q3: Why does t[0] = 5 fail?
Answer: Tuples are immutable — item assignment is not supported.
Q4: Tuple with a dict inside — can you modify the dict?
Answer: Yes — the dict is mutable; you just cannot replace the tuple slot with a different object.
Q5: Unpack (lat, lon)?
Answer: lat, lon = point — one variable per element, left to right.
Loading playground…