0
0
Pythonprogramming~10 mins

Tuple indexing and slicing in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the first element of the tuple.

Python
my_tuple = (10, 20, 30)
first_element = my_tuple[1]
Drag options to blanks, or click blank then click option'
A[0]
B<0>
C{0}
D(0)
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using parentheses instead of square brackets for indexing.
Using curly braces or angle brackets which are invalid for indexing.
2fill in blank
medium

Complete the code to get the last element of the tuple using negative indexing.

Python
my_tuple = ('a', 'b', 'c', 'd')
last_element = my_tuple[1]
Drag options to blanks, or click blank then click option'
A[1]
B[-1]
C[-4]
D[0]
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using positive index 1 which gives the second element.
Using -4 which gives the first element but is less clear.
3fill in blank
hard

Fix the error in the code to slice the tuple from index 1 to 3 (excluding 3).

Python
my_tuple = (5, 10, 15, 20, 25)
slice_part = my_tuple[1]
Drag options to blanks, or click blank then click option'
A{1:3}
B(1:3)
C[1,3]
D[1:3]
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using parentheses instead of square brackets for slicing.
Using comma instead of colon inside the brackets.
4fill in blank
hard

Fill both blanks to get a slice of the tuple from the second element to the end.

Python
my_tuple = ('x', 'y', 'z', 'w')
slice_part = my_tuple[1][2]
Drag options to blanks, or click blank then click option'
A[1:
B1:
C]
D:]
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Forgetting the closing bracket.
Using parentheses instead of square brackets.
5fill in blank
hard

Complete the code to get every second element from the tuple starting at index 0.

Python
my_tuple = (2, 4, 6, 8, 10, 12)
slice_part = my_tuple[[1]]
Drag options to blanks, or click blank then click option'
A[
B::2
C]
D:
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Missing the closing bracket.
Using single colon instead of double colon for step.