1. Introduction
A tuple is an ordered, immutable sequence type in Python, similar to a list but whose contents cannot be changed after creation. Tuples are commonly used to group related, fixed pieces of data together, such as coordinates (x, y) or a database record.
Cricket analogy: A player's fixed birth details like ("Sachin", "Tendulkar", 1973) form a tuple — grouped, ordered facts that together represent one unchangeable record, unlike a lineup that gets reshuffled every match.
Tuples are defined using parentheses () with items separated by commas. Because they are immutable, they are hashable (when their elements are also hashable) and can be used as dictionary keys or stored in sets, unlike lists.
Cricket analogy: You'd write a match result as ("India", "Australia", 2011) using parentheses, and because it never changes, you could use it as a key in a dictionary tracking historic World Cup finals — something a mutable list of scores could never do.
2. Syntax
# Creating tuples
empty_tuple = ()
single_item = (5,) # comma required for a single-element tuple
coordinates = (10, 20)
mixed = (1, "two", 3.0)
# Accessing elements
x = coordinates[0]
y = coordinates[-1]
# Tuple packing and unpacking
point = 3, 4 # packing (parentheses optional)
px, py = point # unpacking
# Tuples are immutable
# coordinates[0] = 99 # this line would raise TypeError3. Explanation
Once created, a tuple's elements cannot be reassigned, and no items can be added or removed. This immutability makes tuples slightly faster than lists and safe to share across code without fear of accidental modification.
Cricket analogy: Once the final scorecard (250, 4) for an innings is recorded, no one can go back and add extra runs or remove a wicket from that tuple — it's locked in as the permanent, shareable record of that innings.
Tuple unpacking is a widely used idiom in Python, allowing multiple variables to be assigned from a tuple in a single statement, and it is what makes patterns like for key, value in dict.items(): work.
Cricket analogy: Unpacking runs, wickets = (250, 4) in one line is exactly how commentators quickly grab both numbers from a match summary, the same pattern used when looping for team, score in results.items(): through a tournament table.
Immutability only applies to the tuple's top-level references, not to mutable objects it contains. A tuple like t = ([1, 2], 3) cannot be reassigned (t[0] = [] fails), but the list inside it CAN still be mutated in place: t[0].append(3) works and changes the tuple's contents.
A single-element tuple requires a trailing comma: (5,) is a tuple, but (5) is just the integer 5 in parentheses.
4. Example
t = ([1, 2], 3)
print("before:", t)
try:
t[1] = 99
except TypeError as e:
print("error:", e)
# Mutating the nested list is allowed
t[0].append(3)
print("after mutating nested list:", t)
# Packing and unpacking
point = 3, 4
px, py = point
print("px:", px, "py:", py)5. Output
before: ([1, 2], 3)
error: 'tuple' object does not support item assignment
after mutating nested list: ([1, 2, 3], 3)
px: 3 py: 46. Key Takeaways
- Tuples are ordered and immutable; elements cannot be reassigned, added, or removed.
- A trailing comma is required to create a single-element tuple:
(5,). - Immutability applies only to the tuple's references, not to mutable objects nested inside it.
- Tuple packing/unpacking allows concise multi-variable assignment, e.g.
x, y = 1, 2. - Tuples with only hashable elements can be used as dictionary keys or set members, unlike lists.
- Tuples are generally faster and use less memory than lists for fixed collections of data.
Practice what you learned
1. Which of the following creates a valid single-element tuple?
2. Given `t = ([1, 2], 3)`, what happens when you run `t[0].append(3)`?
3. What does `px, py = (3, 4)` demonstrate?
4. Can a tuple be used as a dictionary key?
5. What error does `t = (1, 2); t[0] = 99` raise?
6. Which statement about tuples vs lists is TRUE?
Was this page helpful?
You May Also Like
Lists in Python
A mutable, ordered sequence type in Python used to store collections of items, with a deep dive into mutation, aliasing, and common list operations.
Dictionaries in Python
Python's key-value mapping type, covering hashable keys, insertion order guarantees (3.7+), common dict methods, and typical gotchas.
Sets in Python
An unordered collection of unique, hashable elements in Python, covering set creation, operations, and the no-order/no-duplicates gotcha.
Strings in Python
Python's immutable text sequence type, covering string creation, indexing, slicing, immutability, and formatting.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics