w3resource

Python Exercise: Colon of a tuple


8. Create the Colon of a Tuple

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

For more Practice: Solve these Related Problems:

  • Write a Python program to create a slice (colon notation) of a tuple to extract a sub-tuple from a given range.
  • Write a Python program to use colon notation to reverse a tuple and then extract every second element.
  • Write a Python program to generate a new tuple that contains only the elements at even indices using colon slicing.
  • Write a Python program to demonstrate slicing a tuple from a start index to an end index using colon syntax.

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.



Follow us on Facebook and Twitter for latest update.