Tuple Methods0%
Tuple Methods
Beginner7 min read•Updated: 2026-09-03
Tuple Methods
Because tuples are immutable, they have only two built-in inspection methods: count() and index().
Key Concepts & Detailed Explanation
undefined
Code Examples & Output
Python
numbers = (10, 20, 30, 20, 40, 20, 50)
# count(value): occurrences
twenties = numbers.count(20)
print("Occurrences of 20:", twenties) # 3
# index(value): first occurrence index
first_thirty = numbers.index(30)
print("Index of 30:", first_thirty) # 2
Expected Output:
Output
Occurrences of 20: 3
Index of 30: 2
Best Practices & Common Pitfalls
Calling .index() on an item not present in the tuple raises ValueError. Check 'if item in tup:' first.
Practice Quiz
1. How many built-in methods do tuples have in Python?
- A) 2 (count, index)
- B) 11
- C) 0
- D) 5
Answer: A
Explanation: Tuples have only two methods: count() and index().
2. What happens if you search for a non-existent item using tuple.index(x)?
- A) Returns -1
- B) Raises ValueError
- C) Returns None
- D) Returns False
Answer: B
Explanation: index() raises ValueError if the element is not found.
Practice Challenge
Count how many times the letter 'A' appears in a tuple of grades and find the index of grade 'C'.
Next Lesson
Nested Tuples
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Join Tuples | Nested Tuples |
Practice Quiz
Test your understanding of this lesson with 1 questions. Each question has one correct answer.