Skip to content

Write a Python Program to create a Dynamic Tuple and Display Its Elements

Class 11th IP (065)

Write a Python Program to create a Dynamic Tuple and Display Its Elements

SOLUTION..

t = () while True: print("\n--- Tuple Menu ---") print("1. Create Tuple") print("2. Display Tuple") print("3. Search Element in Tuple") print("4. Add Element (Re-create Tuple)") print("5. Exit") choice = int(input("Enter your choice: ")) if choice == 1: lst = [] n = int(input("Enter number of elements: ")) for i in range(n): element = input(f"Enter element {i+1}: ") lst.append(element) t = tuple(lst) print("Tuple created successfully") elif choice == 2: if len(t) == 0: print("Tuple is empty") else: print("Tuple:", t) elif choice == 3: if len(t) == 0: print("Tuple is empty") else: search = input("Enter element to search: ") if search in t: print("Element found") print("Occurrences:", t.count(search)) else: print("Element not found") elif choice == 4: if len(t) == 0: print("Tuple is empty, create it first") else: element = input("Enter element to add: ") lst = list(t) lst.append(element) t = tuple(lst) print("Element added (tuple re-created)") elif choice == 5: print("Program exited") break else: print("Invalid choice")