05/09/2025
Python Tuples — Fixed, fast, and safe 🪄🔒
A tuple is an ordered, immutable collection. Perfect when your data shouldn’t change.
📚 Quick basics (with examples):
✅ Create → t = (1, 2, 3) • t = 1, 2, 3
✅ Single item → t = (5,) (comma mandatory!)
✅ Index & slice → t[0] • t[-1] • t[1:3]
✅ Immutable → t[0] = 9 ❌
✅ But nested can change → t = (1, [2,3]); t[1].append(4) ✅
✅ Ops → len(t) • sum(t) • min(t) • max(t)
✅ Join & repeat → (1,2) + (3,) → (1,2,3) • (0,) * 3 → (0,0,0)
✅ Methods → t.count(x) • t.index(x)
🧰 Packing / Unpacking magic:
a, b = (10, 20) # unpack
x, *mid, y = (1,2,3,4,5) # star-unpack → x=1, mid=[2,3,4], y=5
t = 10, 20, 30 # pack → (10, 20, 30)
💡 When to use a tuple?
• Fixed records: user = ("Archana", "IN", 7010537325)
• Return multiple values from functions
• Dictionary keys / set items (hashable): prices[("apple","kg")] = 120
• Slightly lighter & faster than lists for read-only data
🔁 Conversions & tips:
• tuple([1,2,3]) ↔ list((1,2,3))
• sorted(t) returns a list (tuples themselves aren’t sortable in place)
• Prefer tuples for constants to avoid accidental edits
📞 DM “PY-TUPLE” for a 1-page cheat sheet + practice tasks.
Save 📌 for revision. Share 🔁 with your Python buddy!
🔥 BlastOut Solutions — Your growth, our mission.
“Real Skills + Real Results”
#தமிழில் #கடலூர் #ஐடி_கற்றல் (IT learning) #தொழில்நுட்பம்_கற்றல் (Technology learning) #இணையவழி_கற்றல் (Online learning) #தகவல்_தொழில்நுட்பம்_கற்றல் (Information Technology learning) #ஐடி_திறன்கள் (IT skills)