Q1Which of the following is declared correctly as a tuple?
How to Use Python Tuples (tuple)
Learn the Python tuple — the immutable cousin of list — and pick up unpacking and dict-key tricks right in your browser.
What a tuple is — the "can't be changed" version of a list
A tuple (tuple) lets you hold several values together in a single variable. It's very similar to the list you saw in the previous article, but the key difference is that once you create one, you can't change its contents.
If a list is a container you plan to rewrite (like a shopping list), a tuple is a container you decide once and leave alone — think coordinates (x, y) or an RGB color like (255, 0, 0).
Only the type of brackets and whether it can change differ; the job of grouping several values together is the same.
If it changes, use list; if it stays put, use tuple is a simple rule of thumb.
To declare one, just wrap values in ( and ), separated by commas. Where a list uses [ ], a tuple uses ( ).
Accessing elements (indexing and slicing) works exactly the same as with a list: [0] for the first, [-1] for the last, [1:3] for a slice.
# Declaring a tuple
fruits = ("apple", "banana", "lemon")
print(fruits) # ('apple', 'banana', 'lemon')
print(type(fruits)) # <class 'tuple'>
print(fruits[0]) # apple
print(fruits[-1]) # lemon
print(fruits[1:]) # ('banana', 'lemon') ← a slice is also a tuple
# Mixed types are fine
profile = ("Alice", 28, 165.5)
A single-element tuple needs a trailing comma
If you write fruits = ("apple"), that's a string, not a tuple. ( ) is also used as the math grouping parentheses, so Python can't tell them apart when there's only one value inside.
To make a one-element tuple, add a trailing comma like ("apple",).
Python also lets you drop the parentheses entirely and write something like 1, 2, 3 as a tuple, but for readability you should still keep the ( ).
Immutable — "rewriting an element" and "swapping the box" are different
A tuple is immutable (unchangeable). Once you've created it, you can't overwrite, add, or remove its elements.
The thing people most often trip over here is the difference between "changing an element" and "putting a brand-new tuple into the variable." The first is a no-go; the second is fine.
t[0] = 9 tries to change the contents directly, so it's an error.
t = (4, 5, 6) just points the variable at a new tuple, so it's fine.
Concatenation and methods (count / index)
You can build a new tuple by joining two tuples together. Using + to concatenate returns a brand-new tuple.
There aren't many methods — the two you'll actually use are count and index. They feel the same as on a list.
| What you want | How to write it | Key point |
|---|---|---|
| Concatenate | t1 + t2 | Returns a new tuple; the originals don't change |
| Add one element | t + (x,) | Don't forget the trailing comma |
| Count values | t.count(x) | How many times x appears |
| Find a position | t.index(x) | The first position where x appears |
fruits = ("apple", "banana", "lemon")
# Concatenate (returns a new tuple)
more = fruits + ("grape",)
print(more) # ('apple', 'banana', 'lemon', 'grape')
print(fruits) # ('apple', 'banana', 'lemon') ← original is unchanged
# count and index
nums = (1, 2, 2, 3, 2, 4)
print(nums.count(2)) # 3 (there are three 2s)
print(nums.index(3)) # 3 (3 is the 4th item = index 3)
# Converting between list and tuple
list_fruits = list(fruits) # ['apple', 'banana', 'lemon']
tuple_fruits = tuple(list_fruits) # ('apple', 'banana', 'lemon')
If you want to keep editing, convert to a list
A tuple is for "values you barely move," so it's a poor fit when you need to add and remove items repeatedly. The standard pattern is convert to a list with list(t) → edit → convert back with tuple(...) if needed.
Unpacking and dict keys — what only tuples can do
There are two things a tuple can do that a list can't: unpacking and being used as a dict key.
Put comma-separated variable names on the left-hand side and each element of the tuple goes straight into its own variable.
The match is position-by-position, and the number of variables has to equal the number of elements.
# Unpacking
coord = (135.0, 35.0)
longitude, latitude = coord
print(longitude) # 135.0
print(latitude) # 35.0
# You can also unpack from a literal
r, g, b = (255, 128, 0)
print(r, g, b) # 255 128 0
# A common trick: swap two values in one line
a, b = 1, 2
a, b = b, a
print(a, b) # 2 1
A function's return value is also a tuple (preview)
A Python function can return multiple values with return a, b, and those are automatically packed into a tuple. On the caller side, writing x, y = func() unpacks them into two variables. The functions chapter comes later, so we won't go deep here — but remembering "multiple return values and unpacking go together" will make that chapter click faster.
The other strength is that a tuple can be used as a dict key. A list can't be a dict key, but a tuple can. This is useful when you want to use a combination of several values as a single key — like mapping coordinates to a country name.
# A dict with tuple keys
country = {
(135.0, 35.0): "Japan",
(-77.0, 38.9): "USA",
}
print(country[(135.0, 35.0)]) # Japan
print(country.get((0.0, 0.0), "Unknown")) # Unknown
# A list can't be a key
# bad = {[135.0, 35.0]: "Japan"} # TypeError: unhashable type: 'list'
In this article, framed around differences from a list, you learned how to declare a tuple, what immutability means, concatenation and count / index, and unpacking and dict keys.
Knowledge Check
Answer each question one by one.
Q2Given t = (1, 2, 3), which one does not raise an error?
Q3For coord = (135.0, 35.0), what's the simplest way to pull its two values into variables lng and lat?