Python for AIPython for AI
Data Structures

Tuples

Work with immutable sequences

Let's explore tuples. They are a fundamental data structure in Python, perfect for grouping related items that should not change.

What are tuples?

Tuples are ordered collections of items. Think of them like lists, but with a key difference: once a tuple is created, you cannot change its contents. This makes them 'immutable'.

Because they are immutable, tuples are often used for data that shouldn't be modified, like coordinates or database records. They are also faster to process than lists in some scenarios.

Creating tuples

You create a tuple by placing all the items inside a pair of parentheses (), separated by commas.

python
# A tuple of numbers
my_numbers = (1, 2, 3, 4, 5)

# A tuple of mixed data types
my_info = ("Alice", 30, True)

# An empty tuple
empty_tuple = ()

# A tuple with a single item (note the comma!)
single_item_tuple = ("hello",)
Remember the comma after a single item in a tuple. Without it, Python treats it as just the item in parentheses, not a tuple!

Accessing items

You can access individual items in a tuple using an index, just like with lists. Python uses zero-based indexing, so the first item is at index 0.

python
coordinates = (10, 20)

x_coord = coordinates[0] # Gets 10
y_coord = coordinates[1] # Gets 20

print(f"X: {x_coord}, Y: {y_coord}")

# You can also use negative indices
last_item = coordinates[-1] # Gets 20
print(f"Last item: {last_item}")

Tuple unpacking

Tuple unpacking is a neat feature that lets you assign the items of a tuple to individual variables all at once. The number of variables on the left must match the number of items in the tuple.

python
name_age = ("Bob", 25)

# Unpack the tuple
name, age = name_age

print(f"{name} is {age} years old.")

# This is also useful for swapping variables!
a = 5
b = 10
a, b = b, a # a becomes 10, b becomes 5
print(f"a: {a}, b: {b}")

Common mistakes

The main mistake beginners make with tuples is trying to change them after creation. Remember, tuples are immutable!

python
my_tuple = (1, 2, 3)

# This will cause an error!
# my_tuple[0] = 5  # TypeError: 'tuple' object does not support item assignment

# You also cannot add or remove items
# my_tuple.append(4) # AttributeError: 'tuple' object has no attribute 'append'
If you need to change a sequence, use a list instead of a tuple. If you truly need a 'modified' tuple, you'll have to create a new one based on the old one.

Practice

Try these exercises to lock in what you just learned.

Quiz
Not started
Which creates a tuple with three items?
Quiz
Not started
What happens if you try `t[0] = 5` on a tuple?

What's next?

Tuples provide a simple, effective way to group related data that should remain constant. They are often used as keys in dictionaries, for return values from functions, or when you need a lightweight, immutable collection.

Next, you might explore other data structures like dictionaries to store key-value pairs, or sets for unique, unordered collections.