Welcome to Day 23 of the 100 Days of Python series!
Today we explore tuples, Python’s immutable sibling of the list.
If you’ve mastered lists, learning tuples is easy — but knowing when to use them is what makes your code cleaner, safer, and faster.
Let’s dive in. 🐍
📦 What You’ll Learn
- What tuples are and how they differ from lists
 - How to create and access tuples
 - When to use tuples instead of lists
 - Real-world use cases
 - Tuple tricks (unpacking, swapping, returning multiple values)
 
🧱 What is a Tuple?
A tuple is a collection of ordered, immutable items.
That means you can access its elements like a list, but you can’t change them once created.
🔹 Syntax:
my_tuple = (1, 2, 3)
Yes, they look like lists, but use parentheses () instead of brackets [].
🔍 Tuple vs List
| Feature | List | Tuple | 
|---|---|---|
| Syntax | [] | 
() | 
| Mutable | ✅ Yes | ❌ No | 
| Faster | ❌ Slightly slower | ✅ Slightly faster | 
| Hashable | ❌ No | ✅ Yes (if immutable) | 
| Use case | Dynamic data | Fixed data | 
🛠 How to Create Tuples
1. Basic Tuple
person = ("Alice", 30, "Engineer")
2. Tuple Without Parentheses (Python allows it)
coordinates = 10, 20
3. Single-Item Tuple (must include a comma!)
single = (42,)     # ✅ Tuple
not_a_tuple = (42) # ❌ Just an integer
🎯 Accessing Tuple Elements
Use indexing and slicing just like lists:
colors = ("red", "green", "blue")
print(colors[0])      # 'red'
print(colors[-1])     # 'blue'
print(colors[1:3])    # ('green', 'blue')
❌ Tuples are Immutable
Once created, you can’t modify, add, or remove elements:
colors[0] = "yellow"  # ❌ Error: 'tuple' object does not support item assignment
✅ When to Use Tuples
- Fixed collections of data: like (latitude, longitude)
 - Function returns: return multiple values cleanly
 - As dictionary keys: only hashable types allowed
 - Faster performance: slight speed benefits
 - Safety: prevent accidental modification
 
🧪 Real-World Examples
1. Returning Multiple Values
def get_user():
    return ("Alice", 25)
name, age = get_user()
print(name, age)
2. Storing Coordinates
point = (10.5, 20.3)
Tuples work well for fixed data where names like x and y are implied.
3. Dictionary Key Example
location_data = {
    (28.6139, 77.2090): "Delhi",
    (40.7128, -74.0060): "New York"
}
You can’t use lists as keys, but tuples are allowed.
🪄 Tuple Tricks
1. Tuple Unpacking
name, age, city = ("John", 30, "Paris")
2. Swapping Variables (Pythonic way)
a, b = 5, 10
a, b = b, a
3. Nested Tuples
matrix = ((1, 2), (3, 4))
print(matrix[1][0])  # 3
✅ Recap
Today you learned:
- What tuples are and how they differ from lists
 - How to create, access, and unpack tuples
 - Why tuples are useful when dealing with fixed data
 - Common tuple use cases in real-world scenarios
 - Tuple tricks like unpacking and swapping
 
              
    
Top comments (0)