Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
count & index)Compared to Python lists which offer 11 distinct built-in methods for appending, removing, and sorting, Python tuples possess only two built-in methods: count() and index().
This extreme minimalism is by design: because tuples are strictly immutable, any method that would add, alter, reorder, or delete elements is inherently incompatible with the tuple contract. In exchange for this simplicity, tuples deliver higher speed, minimal memory overhead, and thread safety.
+-------------------------------------------------------------------------+
| TUPLE METHODS REAL-WORLD ANALOGY |
+-------------------------------------------------------------------------+
1. THE HERITAGE MUSEUM DISPLAY (The Tuple):
- Inside the National Museum in New Delhi, the ancient Harappan seals
rest inside a sealed, bulletproof glass showcase.
- As a visitor, you have only two external sensor buttons:
a) [COUNT BUTTON]: "How many seals have the unicorn motif?" (count)
b) [LOCATE BUTTON]: "At what position is the Dancing Girl seal?" (index)
- You cannot hammer, chisel, polish, or move the artifacts inside!
2. THE CARPENTER'S WORKBENCH (The List):
- On a carpenter's bench, you have saws, drills, chisels, glue, and sanders.
- You can cut, glue on extra planks (append), or plane the surface (sort).
+-------------------------------------------------------------------------+count() Methodtuple.count(value) tallies how many times a given element appears in the sequence:
index() Methodtuple.index(value, [start, [stop]]) returns the zero-based index of the first occurrence of the search value:
While tuples have only two methods of their own, Python's universal built-in functions work seamlessly on tuples:
| Scenario | ❌ Anti-Pattern | ✅ Pythonic Idiom |
|---|---|---|
| Attempting In-Place Sort | t.sort() (AttributeError) | sorted_list = sorted(t) |
| Unchecked Index Search | t.index("missing") (Crashes) | if "missing" in t: t.index("missing") |
| Tallying Frequency | Writing a custom count loop | t.count(val) |
| Expect Tuple from sorted() | Assuming sorted(t) returns tuple | Remember sorted() returns list; cast with tuple() if needed |
| Attempt In-Place Reverse | t.reverse() (AttributeError) | reversed_tup = t[::-1] |
+---------------------------------------------------------------------------+ | PYTHON TUPLE METHODS CHEAT SHEET | +---------------------------------------------------------------------------+ | Method / Function | Action | |----------------------+----------------------------------------------------| | tuple.count(x) | Returns frequency count of x (0 if not found) | | tuple.index(x) | Returns index of first occurrence (or ValueError) | | tuple.index(x, a, b)| Searches for x within slice window [a, b) | | len(tuple) | Total number of elements | | min(t) / max(t) | Returns smallest / largest value | | sum(t) | Sum of all numeric elements | | sorted(tuple) | Returns a NEW sorted LIST (leaves tuple untouched) | | t[::-1] | Idiomatic reversed tuple | +---------------------------------------------------------------------------+
Without a trailing comma, parentheses are treated as grouping symbols, not a tuple:
A. 11 B. 5 C. 2 D. 0
(10, 20, 30).count(99) return?A. None B. -1 C. ValueError D. 0
sorted((5, 2, 8, 1))?A. tuple B. list C. set D. generator
t.sort() on a tuple t = (3, 1, 2)?A. The tuple is sorted in-place B. A new sorted tuple is returned C. Python raises an AttributeError: 'tuple' object has no attribute 'sort' D. Python raises a TypeError
A. 1 B. 3 C. [1, 3] D. ValueError
Create a legal docket inspector that stores daily court hearing session room numbers as an immutable tuple. Implement lookups to count how many cases are scheduled in Courtroom 3 (count), locate the first and subsequent hearing times in Courtroom 2 (index), and compute the overall hearing docket statistics.
In the next lesson, we will continue your Python learning journey with Nested Tuples (9: Tuples).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Join Tuples | Nested Tuples |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.