Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In Python, sequences are categorized into two fundamental philosophical camps: collections that are meant to change over time (mutable, like lists) and collections that represent fixed, tamper-proof snapshots of data (immutable, like tuples).
A tuple is an ordered, immutable, and heterogeneous collection of objects enclosed in parentheses (...). Once a tuple is initialized, its size and contents are locked forever. This immutability provides write-protection for sensitive records, optimizes runtime memory consumption, and allows tuples to serve as dictionary keys.
+-------------------------------------------------------------------------+
| PYTHON TUPLE REAL-WORLD ANALOGY |
+-------------------------------------------------------------------------+
1. THE AADHAAR CARD (Immutable Tuple):
- An Aadhaar card issued by UIDAI contains your 12-digit number,
full legal name, and date of birth:
citizen = ("1234-5678-9012", "Rohit Verma", "15-08-1995")
- You cannot erase or alter these fields with a pen. It is a permanent,
tamper-proof record that guarantees data integrity across government systems.
2. THE KIRANA CHALKBOARD (Mutable List):
- In contrast, your neighborhood grocery shopping list is a list:
items = ["Atta", "Chawal", "Dal"]
- You can cross items out, add extra ginger at the bottom, or rewrite
quantities at will.
3. GPS LATITUDE-LONGITUDE COORDINATES:
- The geographical location of the Taj Mahal is fixed:
taj_mahal = (27.1751, 78.0421)
- A coordinate pair should never change midway through route computation.
+-------------------------------------------------------------------------+Because a tuple's size is fixed at inception, Python allocates only the exact memory required. In contrast, lists allocate over-provisioned buffer headroom to support future append() operations.
Tuples are primarily created using parentheses (...). However, in Python, the comma is the actual tuple constructor operator:
Attempting to reassign an index, append, or delete elements from a tuple raises a TypeError:
Because tuples are statically sized, Python uses specialized free-lists to recycle memory blocks, yielding memory savings and faster execution:
| Scenario | ❌ Anti-Pattern | ✅ Pythonic Idiom |
|---|---|---|
| Single-Item Tuple | item = (50) (Evaluates to integer 50!) | item = (50,) (Trailing comma creates tuple) |
| Fixed Constant Records | Storing GPS coordinates in mutable [lat, lon] | Storing in immutable (lat, lon) |
| Dictionary Keys | Using a list as key: { [1, 2]: "Val" } (TypeError) | Using a tuple as key: { (1, 2): "Val" } |
| Unnecessary Conversion | Converting a tuple to a list for read-only iteration | Iterate directly across the tuple |
| Empty Tuple Creation | tuple() | () (Faster literal) |
+---------------------------------------------------------------------------+ | PYTHON TUPLE PROPERTIES CHEAT SHEET | +---------------------------------------------------------------------------+ | Property | Behavior / Syntax | |----------------------+----------------------------------------------------| | Enclosure Syntax | Parentheses: (item1, item2, item3) or comma-pack | | Single-Item Syntax | (item,) -> Mandatory trailing comma | | Mutability | 100% Immutable (Cannot add, remove, or alter items)| | Ordering | Ordered sequence (Zero-indexed, supports negative) | | Hashability | Hashable (Can be dictionary key if items immutable)| | Memory Performance | Smaller memory footprint than lists, faster access | | Type Conversion | tuple(iterable) converts list/string to tuple | +---------------------------------------------------------------------------+
A. Lists are ordered, while tuples are unordered B. Lists are mutable, while tuples are immutable C. Tuples can contain strings, while lists can only contain numbers D. Lists use parentheses, while tuples use square brackets
x = (42) in Python?A. tuple B. int C. set D. list
t = (1, 2, 3); t[0] = 99?A. The first element changes to 99 B. A new tuple is silently created C. Python raises a TypeError: 'tuple' object does not support item assignment D. Python raises a ValueError
A. ["delhi", "mumbai"] B. ("delhi", "mumbai") C. {"delhi", "mumbai"} D. [108]
type(1, 2, 3) evaluate to if packed as x = 1, 2, 3?A. list B. tuple C. int D. generator
Write a Python script that defines an immutable server configuration profile containing network host address, port, protocol, and database connection settings. Demonstrate that configuration records cannot be corrupted in memory, verify tuple hashability by using it as a cache key, and calculate memory savings over a standard list.
In the next lesson, we will continue your Python learning journey with Accessing Items (9: Tuples).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Nested Lists | Accessing Items |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.