Challenge - 5 Problems
Tuple Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of count() method on a tuple
What is the output of this Python code?
t = (1, 2, 3, 2, 2, 4) print(t.count(2))
Python
t = (1, 2, 3, 2, 2, 4) print(t.count(2))
Attempts:
2 left
๐ก Hint
The count() method returns how many times the given value appears in the tuple.
โ Incorrect
The number 2 appears three times in the tuple, so t.count(2) returns 3.
โ Predict Output
intermediate2:00remaining
Output of index() method with repeated elements
What will this code print?
t = ('a', 'b', 'c', 'b', 'd')
print(t.index('b'))Python
t = ('a', 'b', 'c', 'b', 'd') print(t.index('b'))
Attempts:
2 left
๐ก Hint
The index() method returns the first position of the value.
โ Incorrect
The first 'b' is at position 1 (counting from zero), so t.index('b') returns 1.
โ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this code produce?
t = (1, 2, 3) print(t.index(4))
Python
t = (1, 2, 3) print(t.index(4))
Attempts:
2 left
๐ก Hint
The value 4 is not in the tuple, so index() cannot find it.
โ Incorrect
Calling index() with a value not in the tuple raises a ValueError.
โ Predict Output
advanced2:00remaining
Output of count() with no matching elements
What does this code print?
t = ('x', 'y', 'z')
print(t.count('a'))Python
t = ('x', 'y', 'z') print(t.count('a'))
Attempts:
2 left
๐ก Hint
count() returns zero if the value is not found.
โ Incorrect
Since 'a' is not in the tuple, count() returns 0.
๐ง Conceptual
expert2:00remaining
Why can't you use append() on a tuple?
Which statement best explains why the tuple method append() does not exist?
Attempts:
2 left
๐ก Hint
Think about whether tuples can be changed after you make them.
โ Incorrect
Tuples cannot be changed after creation, so methods that modify them like append() do not exist.