Robotics & Artificial Intelligence

Write a Python program that performs the following operations on a tuple:

  1. Create a tuple with the elements: (5, 10, 15, 20, 25).
  2. Access and print the element at index 2.
  3. Convert the tuple into a list.
  4. Append the integer 30 to the list.
  5. Convert the list back into a tuple.
  6. Print the final tuple.

Python Tuples

1 Like

Answer

numbers_tuple = (5, 10, 15, 20, 25)
element = numbers_tuple[2]
print("Element at index 2:", element)
numbers_list = list(numbers_tuple)
numbers_list.append(30)
final_tuple = tuple(numbers_list)
print("Final tuple:", final_tuple)

Output

Element at index 2: 15
Final tuple: (5, 10, 15, 20, 25, 30)

Answered By

2 Likes


Related Questions