Tuples 🔒📦🛡️
Tuples store data that should never change. Like a sealed locker — you can read it but never edit it!
Day 16: Tuples — Data That Stays Locked!
What's a Tuple?
Imagine a sealed locker. You put items inside, lock it, and can look at what's inside anytime. But you can never change what's in there. That's a tuple! It's like a list but permanently locked. Perfect for data that should never change.
Creating a Tuple
colors = ("Red", "Blue", "Green")
print(colors[0]) # Red
print(colors[1]) # Blue
print(colors[2]) # Green
Round brackets ( ) instead of square [ ]. Reading works exactly like a list — positions start from 0. But try to change it and Python stops you!
Tuples Cannot Be Changed
colors = ("Red", "Blue", "Green")
colors[0] = "Black" # ERROR! Tuples are locked!
Python throws an error immediately. This is not a bug — it's the feature! The lock protects your data from accidental changes.
Real World Connection
The days of the week never change — perfect for a tuple! Country codes like +91 for India never change — tuple! The coordinates of a fixed location like your school — tuple! Whenever data is permanent and must stay safe, apps use tuples.
List vs Tuple — When to Use Which
# Use a LIST when data changes
cart = ["Burger", "Fries"]
cart.append("Coke") # works fine!
# Use a TUPLE when data must stay fixed
days = ("Mon", "Tue", "Wed", "Thu", "Fri")
days[0] = "Sunday" # ERROR — protected!
Simple rule — will this data ever need to change? Yes = list. No = tuple!
Common Mistakes
Mistake 1 — Trying to change a tuple.
colors = ("Red", "Blue")
colors[0] = "Black" # WRONG — tuples are locked!
Mistake 2 — Using square brackets instead of round.
colors = ["Red", "Blue"] # WRONG — that's a list!
colors = ("Red", "Blue") # CORRECT — that's a tuple!
Mini Challenge
Mini Challenge
Create a tuple of the 4 seasons. Print each one using its position. Then try to change one season and see the error Python gives you. Now you understand why the lock exists!
Quick Quiz
Q: What brackets do tuples use? A: Round brackets ( ) — not square [ ] like lists!
Q: Can you change a value inside a tuple? A: Never! Tuples are permanently locked.
Q: Your app stores the 7 days of the week. List or tuple? A: Tuple — days never change!
Key Takeaways
Key Takeaways
- Tuples store data that must never change — locked forever.
- Use round brackets ( ) to create a tuple.
- Reading works like lists — positions start from 0.
- Trying to change a tuple causes an error — that's the protection!
- Use lists when data changes. Use tuples when data must stay safe.
Continue Learning with Rohi
You've used your 3 free Rohi questions. Create a free account to continue learning.