100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Tuples in Python

An immutable, ordered sequence type in Python, covering tuple syntax, the nested-mutable-element trap, tuple packing/unpacking, and use cases.

Data StructuresBeginner10 min readJul 7, 2026
Analogies

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

python
# 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 TypeError

3. 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

python
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

text
before: ([1, 2], 3)
error: 'tuple' object does not support item assignment
after mutating nested list: ([1, 2, 3], 3)
px: 3 py: 4

6. 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

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#TuplesInPython#Tuples#Syntax#Explanation#Example#DataStructures#StudyNotes#SkillVeris