0
0
Pythonprogramming~10 mins

Tuple immutability 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 create a tuple named my_tuple with values 1, 2, and 3.

Python
my_tuple = ([1])
Drag options to blanks, or click blank then click option'
A[1, 2, 3]
B{1, 2, 3}
C1, 2, 3
D"1, 2, 3"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using square brackets instead of parentheses creates a list, not a tuple.
Using curly braces creates a set, not a tuple.
Putting values inside quotes creates a string, not a tuple.
2fill in blank
medium

Complete the code to access the second element of the tuple my_tuple.

Python
second_element = my_tuple[[1]]
Drag options to blanks, or click blank then click option'
A1
B2
C-1
D0
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using index 2 to get the second element (actually the third).
Using index 0 to get the second element (actually the first).
3fill in blank
hard

Fix the error in the code that tries to change the first element of the tuple my_tuple to 10.

Python
my_tuple[[1]] = 10
Drag options to blanks, or click blank then click option'
A-1
B1
C2
D0
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to assign a value to a tuple index causes a TypeError.
Thinking negative indices allow assignment in tuples.
4fill in blank
hard

Fill both blanks to create a new tuple new_tuple by replacing the first element of my_tuple with 10.

Python
new_tuple = ([1],) + my_tuple[[2]:]
Drag options to blanks, or click blank then click option'
A10
B1
C0
D2
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Forgetting the comma after 10, which makes it an int, not a tuple.
Using the wrong slice index, which includes the first element.
5fill in blank
hard

Fill all three blanks to create a tuple filtered_tuple with only elements greater than 2 from my_tuple.

Python
filtered_tuple = tuple(x for x in my_tuple if x [1] [2])
result = filtered_tuple[[3]]
Drag options to blanks, or click blank then click option'
A>
B2
C0
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the less than operator instead of greater than.
Using the wrong index to access the first element.