Complete the code to create a tuple with three elements.
my_tuple = (1, 2, [1])
The tuple elements are separated by commas inside parentheses. The element 3 is added as a number, not as a list, set, or string.
Complete the code to access the second element of the tuple.
second_element = my_tuple[1]Tuples are accessed by index using square brackets []. The second element is at index 1 because counting starts at 0.
Complete the code to attempt to modify the first element of the tuple, demonstrating its immutability by causing an error.
my_tuple = (1, 2, 3) my_tuple[1] = 4
Tuples are immutable, so you cannot change elements by index. my_tuple[0] = 4 raises TypeError: 'tuple' object does not support item assignment, demonstrating immutability.
Fill both blanks to create a tuple and check its type.
data = [1](1, 2, 3) type_of_data = type(data) == [2]
The tuple() function creates a tuple from the given elements. The type() function returns the type, which is compared to tuple to check if the data is a tuple.
Fill all three blanks to create a tuple, access an element, and check immutability.
my_tuple = ([1], [2], [3]) value = my_tuple[1] try: my_tuple[1] = 10 except TypeError: error = True
The tuple is created with three numbers 10, 20, and 30. Accessing the second element (index 1) gives 20. Trying to assign a new value to a tuple element raises a TypeError because tuples are immutable.