Tuples
Tuples are Python's immutable sequence type - once created, they cannot be changed. This makes them perfect for data that should stay constant, like coordinates, RGB colors, or database records. They're also faster and use less memory than lists!
Creating Tuples
Tuples are created with parentheses () or just comma-separated values.
Watch out for the single-item tuple gotcha - it needs a trailing comma!
Click Run to execute your code
("apple")
is just a string, not a tuple! You must add a trailing comma: ("apple",). This is one
of Python's most common gotchas for beginners.
Tuple Unpacking
One of Python's most elegant features is tuple unpacking - extracting tuple values into separate variables in a single statement. It's used everywhere from variable swapping to function returns.
Click Run to execute your code
_ for values you don't need:
name, _, age = ("Alice", "ignored", 25). Use *_ to ignore
multiple values: first, *_, last = (1, 2, 3, 4, 5).
Tuple Operations and Methods
Tuples support most list operations except modification. They have only two methods
(count() and index()) but work with all built-in functions.
Click Run to execute your code
Tuples vs Lists
When should you use a tuple instead of a list? Understanding the differences helps you write more efficient and safer code.
Click Run to execute your code
t = ([1, 2], 3) - you can do
t[0].append(4) but not t[0] = [5, 6].
Common Mistakes
1. Forgetting comma in single-item tuple
# Wrong - this is a string, not a tuple!
single = ("apple")
print(type(single)) #
# Correct - need trailing comma
single = ("apple",)
print(type(single)) #
2. Trying to modify a tuple
# Wrong - tuples are immutable!
point = (10, 20)
point[0] = 15 # TypeError!
# Correct - create a new tuple
point = (15, point[1])
# Or convert to list, modify, convert back
temp = list(point)
temp[0] = 15
point = tuple(temp)
3. Wrong number of variables in unpacking
# Wrong - count mismatch
point = (10, 20, 30)
x, y = point # ValueError: too many values to unpack
# Correct - match the count
x, y, z = point
# Or use * to capture extras
x, *rest = point # x=10, rest=[20, 30]
4. Confusing tuple methods with list methods
# Wrong - tuples don't have append, sort, etc.
t = (3, 1, 2)
t.append(4) # AttributeError!
t.sort() # AttributeError!
# Correct - tuples only have count() and index()
print(t.count(1)) # 1
print(t.index(2)) # 2
# For sorting, use sorted() which returns a list
sorted_list = sorted(t) # [1, 2, 3]
Exercise: Working with Coordinates
Task: Work with tuples representing 2D coordinates.
Requirements:
- Create a tuple for point (10, 20)
- Unpack it into variables x and y
- Print the coordinates
Click Run to execute your code
Show Solution
# Create a tuple for point (10, 20)
point = (10, 20)
# Unpack into x and y
x, y = point
# Print the coordinates
print(f"Point coordinates: x={x}, y={y}")
# Output: Point coordinates: x=10, y=20
Summary
- Creating: Use
()or comma-separated values - Single item: Requires trailing comma:
("item",) - Immutable: Cannot modify after creation
- Unpacking:
x, y, z = tuple - Extended unpacking:
first, *rest, last = tuple - Methods: Only
count()andindex() - Use tuples: For fixed data, dict keys, function returns
- Use lists: When you need to modify the collection
What's Next?
Now let's explore Sets - Python's unordered collection of unique elements. Sets are perfect for removing duplicates and performing mathematical set operations like union, intersection, and difference!
Enjoying these tutorials?