7.1 Execute various tuple operations.

Develop Python program to perform following operations on tuples:
a) Create
b) Access
c) Update
d) Delete tuple elements

Tuples in Python

A tuple is a collection data type in Python that is similar to a list, but with one key difference: tuples are immutable, meaning that once a tuple is created, its elements cannot be modified, added, or removed. Tuples are often used when you want to store a collection of items that should not be changed.

Key Characteristics of Tuples:

  1. Ordered:
    • Tuples maintain the order of elements, similar to lists. The elements can be accessed using indices.
  2. Immutable:
    • The elements of a tuple cannot be changed after the tuple is created. This immutability makes tuples suitable for data that should remain constant.
  3. Heterogeneous:
    • A tuple can contain elements of different data types, just like a list.
  4. Indexing and Slicing:
    • Tuples support indexing and slicing, allowing you to access individual elements or a sub-range of elements.
  5. Fixed Size:
    • Since tuples are immutable, their size is fixed once they are created.
# a) Create a Tuple
my_tuple = ("apple", "banana", "cherry", "date")
print("Original Tuple:", my_tuple)

# b) Access elements in a Tuple
print("\nAccessing elements in the tuple:")
print("First element:", my_tuple[0])
print("Second element:", my_tuple[1])
print("Last element:", my_tuple[-1])

# c) Update elements in a Tuple
# Since tuples are immutable, you cannot update them directly.
# However, you can convert a tuple to a list, update the list, and then convert it back to a tuple.

# Attempting to update the tuple directly would result in an error
# my_tuple[1] = "blueberry"  # This will raise a TypeError

print("\nUpdating elements in the tuple (via conversion to list):")
temp_list = list(my_tuple)  # Convert tuple to list
temp_list[1] = "blueberry"  # Update the list
my_tuple = tuple(temp_list)  # Convert list back to tuple
print("Updated Tuple:", my_tuple)

# d) Delete elements in a Tuple
# Tuples are immutable, so elements cannot be deleted directly.
# However, you can delete the entire tuple, or convert it to a list, delete elements, and then convert it back to a tuple.

# Deleting the entire tuple
print("\nDeleting the entire tuple:")
del my_tuple  # This will delete the entire tuple
# print(my_tuple)  # This will raise a NameError because the tuple no longer exists

# Deleting elements by converting to a list
my_tuple = ("apple", "banana", "cherry", "date")
temp_list = list(my_tuple)  # Convert tuple to list
del temp_list[2]  # Delete the third element from the list
my_tuple = tuple(temp_list)  # Convert list back to tuple
print("Tuple after deletion:", my_tuple)

OUTPUT:
Original Tuple: ('apple', 'banana', 'cherry', 'date')

Accessing elements in the tuple:
First element: apple
Second element: banana
Last element: date

Updating elements in the tuple (via conversion to list):
Updated Tuple: ('apple', 'blueberry', 'cherry', 'date')

Deleting the entire tuple:
# The tuple has been deleted

Tuple after deletion: ('apple', 'banana', 'date')

Explanation:

  • Create: Tuples are created using parentheses () with elements separated by commas.
  • Access: Elements are accessed using their index, similar to lists.
  • Update: Tuples are immutable, so elements cannot be directly updated. However, you can convert the tuple to a list, modify the list, and convert it back to a tuple.
  • Delete: While you cannot delete individual elements from a tuple directly, you can delete the entire tuple or convert it to a list, delete elements, and convert it back to a tuple.
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top