Dictionaries and tuples complete Python's core data structures. Dictionaries store key-value pairs (like a real dictionary: word → definition). Tuples are immutable sequences — once created, they can't be changed.
Create: student = {"name": "Alice", "age": 14, "grade": "A"}. Access: student["name"] → "Alice". Safe access: student.get("phone", "N/A") — returns default if key missing. Add/Update: student["city"] = "Delhi". Delete: del student["age"] or student.pop("age"). Methods: .keys() (all keys), .values() (all values), .items() (key-value tuple pairs). Loop: for key, value in student.items():. Check: "name" in student → True. Nested dicts: dict inside dict — for complex data.
Create: coords = (10, 20) or single: t = (5,) — comma required for single element. Immutable: coords[0] = 15 → TypeError! Why use? Faster than lists, safe from accidental changes, can be dict keys. Operations: indexing, slicing, concatenation (+), repetition (*), len(), count(), index(). Packing: t = 1, 2, 3. Unpacking: a, b, c = t. Swap: a, b = b, a (uses tuple packing/unpacking). When to use: lists for mutable collections, tuples for fixed collections (coordinates, RGB colours, database records).
Tuples are immutable by design for several reasons: (1) Safety — data that shouldn't change (coordinates, config) is protected from accidental modification. (2) Performance — immutability allows Python to optimise memory. (3) Hashable — tuples can be used as dictionary keys and set elements (lists cannot). (4) Thread safety — immutable data is safe in concurrent programs. Think of tuples as "read-only lists" — when you want to guarantee data won't be altered, use a tuple.
Book a Trial + Diagnostic session. Get a personalized Learning Path with clear milestones, tutor match, and a plan recommendation — all within 24 hours.
Book Trial + Diagnostic →