Module 22: Unpacking Tuples

Discover Python's ordered, unchangeable sequences.

📦 What are Tuples?

Imagine a list, but once you create it, you cannot change its contents. That's the essence of a tuple!

Tuples are ordered sequences, like lists, but they are immutable. This makes them useful for representing fixed collections of items, like coordinates or database records.

⚙️ Creating and Accessing Tuples

Use parentheses () and square brackets [].

point = (10, 20, "Origin") # Create a tuple print(point[0]) # Access the first element (index 0)

Tuples are created using parentheses () with items separated by commas.

Like lists, you access elements using square brackets [index]Access item by its position (starts at 0).

The tuple point now holds three fixed pieces of data.

🔒 The Immutable Rule

Once created, a tuple's elements cannot be changed.

colors = ("red", "green", "blue") # A tuple of colors colors[1] = "yellow" # Attempt to change 'green'

This is the key difference from lists! Trying to assign a new value to an element's index results in an error.

This "unchangeable" nature (immutability) guarantees that the tuple's data remains constant after creation.

This property makes tuples reliable for storing data that shouldn't be accidentally modified (data integrityEnsuring data accuracy and consistency).

🤔 Why Use Tuples?

If they're like restricted lists, what are they good for?

Data Integrity

Immutability prevents accidental changes to important, fixed data sets (like coordinates, RGB colors).

Dictionary Keys

Because they are immutable, tuples can be used as keys in dictionaries, while lists cannot.

Slight Performance Edge

Tuples can sometimes be slightly faster to process than lists due to their fixed nature.

Click on the cards above to reveal more!

🧠 Quick Check!

Module 22 Theory Complete!

You've grasped the concept of immutable tuples! Ready to put it into practice?
Head to the Practice Zone or challenge yourself with the Advanced Practice.