Write a Python program to create the colon of a tuple


The program imports the deepcopy() function from the copy module. Then, it defines a tuple t with 6 elements of different data types (strings, integers, float, list, and boolean). The original tuple is then printed to the console using the print() function.

Next, a deep copy of the original tuple t is created using the deepcopy() function. The result is assigned to the variable tc. The deepcopy() function creates a new object that is an independent copy of the original object, including any nested objects.

The program then modifies the 4th element of the copy tuple tc (which is a list) by appending a new value 50. Finally, both the original tuple t and the deep copy tc are printed to the console.

As a result of the deep copy, the original tuple t remains unchanged, while the copy tuple tc is modified. This can be seen in the output, where the values of t are the same before and after the modification, while the values of tc have changed.

Source Code

from copy import deepcopy
#create a tuple
t = ("Tutor", 'J', 23 , 56.67 , [23,12] , True) 
print(t)
#make a copy of a tuple using deepcopy() function
tc = deepcopy(t)
tc[4].append(50)
print(tc)
print(t)
 

Output

('Tutor', 'J', 23, 56.67, [23, 12], True)
('Tutor', 'J', 23, 56.67, [23, 12, 50], True)
('Tutor', 'J', 23, 56.67, [23, 12], True)

Example Programs