0
0
Pythonprogramming~5 mins

Tuple methods in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the count() method do in a tuple?
The count() method returns how many times a specific value appears in the tuple.
Click to reveal answer
beginner
What is the purpose of the index() method in a tuple?
The index() method returns the first position (index) of a specified value in the tuple. If the value is not found, it raises a ValueError.
Click to reveal answer
beginner
Can you add or remove items from a tuple using tuple methods?
No, tuples are unchangeable (immutable). You cannot add, remove, or change items using tuple methods.
Click to reveal answer
beginner
Example: What is the output of (1, 2, 3, 2).count(2)?
The output is 2 because the number 2 appears twice in the tuple.
Click to reveal answer
beginner
Example: What does ("a", "b", "c").index("b") return?
It returns 1 because "b" is at index 1 in the tuple (counting starts at 0).
Click to reveal answer
Which tuple method tells you how many times a value appears?
Acount()
Bindex()
Cfind()
Dlength()
What does index() return when called on a tuple?
AThe number of times a value appears
BThe length of the tuple
CThe last position of a value
DThe first position of a value
What happens if you try to change a tuple item using a method?
AThe item changes
BAn error occurs
CThe tuple doubles in size
DThe tuple becomes a list
What is the output of (4, 5, 4, 4).count(4)?
A1
B2
C3
D4
If t = ("x", "y", "z"), what does t.index("y") return?
A1
B2
C0
DError
Explain how the count() and index() methods work on tuples.
Think about what each method returns when you look for a value.
You got /3 concepts.
    Why can't you add or remove items from a tuple using methods?
    Consider what 'immutable' means for tuples.
    You got /3 concepts.