w3resource

Python Exercise: Colon of a tuple

Python tuple: Exercise-8 with Solution

Write a Python program to create the colon of a tuple.

Visual Presentation:

Python Tuple: Colon of a tuple.

Sample Solution:

Python Code:

# Import the 'deepcopy' function from the 'copy' module
from copy import deepcopy

# Create a tuple containing various data types
tuplex = ("HELLO", 5, [], True)
# Print the contents of the 'tuplex' tuple
print(tuplex)

# Create a deep copy of the 'tuplex' tuple using the 'deepcopy()' function
tuplex_colon = deepcopy(tuplex)

# Modify the third element of the 'tuplex_colon' tuple, which is a list, by appending the value 50
tuplex_colon[2].append(50)

# Print the 'tuplex_colon' tuple after the modification
print(tuplex_colon)

# Print the original 'tuplex' tuple to demonstrate that it remains unchanged
print(tuplex) 

Sample Output:

('HELLO', 5, [], True)                                                                                        
('HELLO', 5, [50], True)                                                                                      
('HELLO', 5, [], True) 

Flowchart:

Flowchart: Colon of a tuple

Python Code Editor:

Previous: Write a Python program to get the 4th element and 4th element from last of a tuple.
Next: Write a Python program to find the repeated items of a tuple.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/tuple/python-tuple-exercise-8.php