Work Left To Do
Work Left To Do
def display_menu():
print("\nDictionary Operations Menu:")
print("1. Add a key-value pair")
print("2. Remove a key")
print("3. Get value by key")
print("4. Check if key exists")
print("5. Get all keys")
print("6. Get all values")
print("7. Get all items")
print("8. Clear dictionary")
print("9. Exit")
def add_key_value(dictionary):
while True:
key = input("Enter key (or 'done' to finish):
")
if key == "done":
break
def remove_key(dictionary):
key = input("Enter key to remove: ")
if key in dictionary:
del dictionary[key]
print(f"Removed key '{key}' from the
dictionary.")
else:
print(f"Key '{key}' not found in the
dictionary.")
def get_value(dictionary):
key = input("Enter key to get value: ")
if key in dictionary:
print(f"Value for key '{key}':
{dictionary[key]}")
else:
print(f"Key '{key}' not found in the
dictionary.")
def check_key_exists(dictionary):
key = input("Enter key to check: ")
if key in dictionary:
print(f"Key '{key}' exists in the
dictionary.")
else:
print(f"Key '{key}' does not exist in the
dictionary.")
def get_all_keys(dictionary):
print("All keys in the dictionary:",
list(dictionary.keys()))
def get_all_values(dictionary):
print("All values in the dictionary:",
list(dictionary.values()))
def get_all_items(dictionary):
print("All items in the dictionary:",
list(dictionary.items()))
def clear_dictionary(dictionary):
dictionary.clear()
print("Dictionary cleared.")
def main():
dictionary = {}
while True:
display_menu()
choice = input("Enter your choice (1-9): ")
if choice == '1':
add_key_value(dictionary)
elif choice == '2':
remove_key(dictionary)
elif choice == '3':
get_value(dictionary)
elif choice == '4':
check_key_exists(dictionary)
elif choice == '5':
get_all_keys(dictionary)
elif choice == '6':
get_all_values(dictionary)
elif choice == '7':
get_all_items(dictionary)
elif choice == '8':
clear_dictionary(dictionary)
if __name__ == "__main__":
main()
#Program to demonstrate all type of list inbuilt
functions
def create_list():
my_list = []
num_elements = int(input("Enter the number
of elements in your list: "))
print("Enter elements one by one:")
for _ in range(num_elements):
element = input()
my_list.append(element)
return my_list
def print_list(my_list):
print("Your list:", my_list)
def access_element(my_list):
index = int(input("Enter the index of the
element you want to access: "))
if 0 <= index < len(my_list):
print("Element at index", index, ":",
my_list[index])
else:
print("Invalid index.")
def add_element(my_list):
new_element = input("Enter the element to
add: ")
my_list.append(new_element)
print("Element added successfully.")
def insert_element(my_list):
index = int(input("Enter the index where you
want to insert: "))
new_element = input("Enter the element to
insert: ")
if 0 <= index <= len(my_list):
my_list.insert(index, new_element)
print("Element inserted successfully.")
else:
print("Invalid index.")
def remove_element(my_list):
element_to_remove = input("Enter the
element to remove: ")
if element_to_remove in my_list:
my_list.remove(element_to_remove)
print("Element removed successfully.")
else:
print("Element not found in the list.")
def update_element(my_list):
index = int(input("Enter the index of the
element to update: "))
if 0 <= index < len(my_list):
new_value = input("Enter the new value: ")
my_list[index] = new_value
print("Element updated successfully.")
else:
print("Invalid index.")
def main():
my_list = create_list()
while True:
print("\nChoose an operation:")
print("1. Print list")
print("2. Access element")
print("3. Add element")
print("4. Insert element")
print("5. Remove element")
print("6. Update element")
print("7. Exit")
choice = input("Enter your choice: ")
if choice == '1':
print_list(my_list)
elif choice == '2':
access_element(my_list)
elif choice == '3':
add_element(my_list)
elif choice == '4':
insert_element(my_list)
elif choice == '5':
remove_element(my_list)
elif choice == '6':
update_element(my_list)
elif choice == '7':
break
else:
print("Invalid choice.")
if __name__ == "__main__":
main()
OUTPUT:
9. Exit
OUTPUT:
Choose an operation:
1. Print list
2. Access element
3. Add element
4. Insert element
5. Remove element
6. Update element
7. Exit
Enter your choice: 1
Your list: ['apple', 'banana', 'cherry']
Choose an operation:
1. Print list
2. Access element
3. Add element
4. Insert element
5. Remove element
6. Update element
7. Exit
Enter your choice: 3
Enter the element to add: date
Element added successfully.
Choose an operation:
1. Print list
2. Access element
3. Add element
4. Insert element
5. Remove element
6. Update element
7. Exit
Choose an operation:
1. Print list
2. Access element
3. Add element
4. Insert element
5. Remove element
6. Update element
7. Exit
Enter your choice:7