🔗 Python Tuple — Immutable & Efficient Data Collection

Introduction 🌟

A tuple in Python is an ordered, immutable (unchangeable) collection of items. Tuples are similar to lists, but once created, their values cannot be modified. Because of immutability, tuples are **faster**, **memory-efficient**, and safer for storing constant data.

Note

💡 Tuples are defined using ( ) parentheses.

1. Creating Tuples 🧱

create_tuple.py

# Empty tuple
empty_tuple = ()

# Tuple with values
numbers = (10, 20, 30)

# Mixed data types
mixed = (10, "hello", 3.5, True)

# Nested tuple
nested = (1, 2, (3, 4, 5))

# Tuple without parentheses (packing)
packed = 10, 20, 30

# Single element tuple (important!)
single = (5,)  # NOT (5)

Note

✔️ A single-element tuple must end with a comma.

2. Accessing Tuple Elements 🔢

access_tuple.py

fruits = ("apple", "banana", "mango")

print(fruits[0])   # apple
print(fruits[1])   # banana
print(fruits[-1])  # mango

✔️ Supports indexing & negative indexing like lists.

3. Tuple Slicing ✂️

tuple_slice.py

nums = (10, 20, 30, 40, 50)

print(nums[1:4])   # (20, 30, 40)
print(nums[:3])    # (10, 20, 30)
print(nums[::-1])  # reverse tuple

4. Tuple Immutability ❌✏️

Tuples cannot be changed after creation.

immutability.py

numbers = (1, 2, 3)
# numbers[1] = 10  # ❌ Error: Tuples are immutable

Note

✔️ Immutability makes tuples safe for data that should not change.

5. Tuple Methods 🧰

MethodDescription
count()Counts occurrences of a value
index()Returns index of first occurrence

tuple_methods.py

nums = (10, 20, 30, 20)

print(nums.count(20))   # 2
print(nums.index(30))   # 2

6. Tuple Packing & Unpacking 📦

packing_unpacking.py

# Packing
person = ("Sathish", 25, "India")

# Unpacking
name, age, country = person
print(name)
print(age)
print(country)

Note

✔️ Unpacking assigns tuple items to individual variables.

7. Using * for Extended Unpacking 🔧

extended_unpacking.py

numbers = (1, 2, 3, 4, 5)

a, b, *rest = numbers
print(a, b, rest)    # 1 2 [3, 4, 5]

8. Nested Tuples 🧩

nested_tuple.py

matrix = (
    (1, 2, 3),
    (4, 5, 6),
    (7, 8, 9)
)

print(matrix[1][2])  # 6

9. Tuple vs List ⚖️

FeatureTupleList
MutabilityImmutableMutable
SpeedFasterSlower
Memory UsageLowerHigher
Use-caseFixed dataDynamic data

Note

✔️ Use tuples for data that should never change.
✔️ Use lists when you need modifications.

10. Tuple as Dictionary Keys 🔑

Because tuples are immutable, they can be used as dictionary keys (lists cannot).

tuple_as_key.py

location = {(10, 20): "Point A", (30, 40): "Point B"}

print(location[(10, 20)])

11. Looping Through Tuples 🔁

loop_tuple.py

colors = ("red", "green", "blue")

for color in colors:
    print(color)

12. Real-World Example 🌍

real_world_example.py

# Storing constant configuration data
config = ("localhost", 3306, "root", "password")

host, port, user, pwd = config
print("Connecting to:", host, port)

Conclusion 🎉

>>“Tuples provide safety, speed, and structure — the perfect choice for fixed collections.” ✨

You now fully understand Python Tuples! Want the next topic? Try Set, Dictionary, Strings, or List Comprehension. Just tell me! 😊