0
0
Pythonprogramming~20 mins

Tuple indexing and slicing in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Tuple Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of tuple slicing with negative indices
What is the output of this Python code?
Python
t = (10, 20, 30, 40, 50, 60)
print(t[-4:-1])
A(40, 50, 60)
B(20, 30, 40)
C(30, 40, 50)
D(30, 40, 50, 60)
Attempts:
2 left
๐Ÿ’ก Hint
Remember that slicing excludes the end index and negative indices count from the end.
โ“ Predict Output
intermediate
2:00remaining
Result of tuple indexing with nested tuples
What will be printed by this code?
Python
t = (1, (2, 3), 4, (5, 6, 7))
print(t[3][1])
A6
B5
C7
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
First index selects the nested tuple, second index selects element inside it.
โ“ Predict Output
advanced
2:00remaining
Output of tuple slicing with step and negative step
What is the output of this code?
Python
t = (0, 1, 2, 3, 4, 5, 6)
print(t[5:1:-2])
A(5, 4, 3, 2)
B(5, 3)
C(5, 3, 1)
D()
Attempts:
2 left
๐Ÿ’ก Hint
Slicing with a negative step goes backwards and excludes the end index.
โ“ Predict Output
advanced
2:00remaining
Value of variable after tuple slicing and concatenation
What is the value of variable result after running this code?
Python
t = (1, 2, 3, 4, 5)
result = t[:2] + t[-2:]
print(result)
A(2, 3, 4)
B(1, 2, 3, 4, 5)
C(3, 4, 5)
D(1, 2, 4, 5)
Attempts:
2 left
๐Ÿ’ก Hint
Slicing t[:2] gets first two elements, t[-2:] gets last two elements, then they are joined.
โ“ Predict Output
expert
2:00remaining
Output of complex tuple slicing with omitted indices
What does this code print?
Python
t = (10, 20, 30, 40, 50, 60, 70)
print(t[::3][1:])
A(40, 70)
B(10, 40, 70)
C(30, 60)
D(40, 50, 60, 70)
Attempts:
2 left
๐Ÿ’ก Hint
First slice picks every 3rd element, second slice takes from index 1 to end.