Python Tuple Methods — Python Technical Interview Questions & Answers
Python Tuple Methods
Python Tuple Methods
- Tuples barely have methods because they cannot change. count(x) tells how many times x appears. index(x) tells the first position. That is the whole everyday list. To “change” a tuple you build a new one from pieces: t[:1] + (9,).
- Contrast with list: list has append, pop, sort. Tuple does not. If they ask why, say immutability. Don’t invent tuple.append in the viva.
- Python Tuple Methods — output: 2 then 3. count how many 2s; index of 3.
point = (3, 4) immutable
x=3 y=4Technical Interview Tip: Why tuples have fewer methods than lists.
Code Examples for Python Tuple Methods
Example
# Tuple methods
t = (1, 2, 2, 3)
print(t.count(2))
print(t.index(3))
Python Tuple Methods — output: 2 then 3. count how many 2s; index of 3.
How to Explain Python Tuple Methods to an Interviewer
Explain Tuple Methods as if you are teaching a junior — definition, then one tiny script.
Count / index only No append
What does the example print, and why?
Python Tuple Methods — output: 2 then 3
What mistake do freshers make with Tuple Methods?
Build a new tuple to change