Student dictionary with Insert, Update, and Delete operations in Python

Student dictionary with Insert, Update, and Delete operations in Python

Python Program to create a dictionary for student data which contain Roll No, Name, Div., Marks and City. Perform Insert, Update, Delete operation on it.

Student Dictionary

students = {}

while True:
print(“\n1. Insert”)
print(“2. Update”)
print(“3. Delete”)
print(“4. Display”)
print(“5. Exit”)

choice = input("Enter choice: ")

# Insert
if choice == '1':
    roll = input("Enter Roll No: ")
    name = input("Enter Name: ")
    div = input("Enter Division: ")
    marks = input("Enter Marks: ")
    city = input("Enter City: ")

    students[roll] = {
        "Name": name,
        "Div": div,
        "Marks": marks,
        "City": city
    }
    print("Student inserted.")

# Update
elif choice == '2':
    roll = input("Enter Roll No to update: ")
    if roll in students:
        students[roll]["Name"] = input("Enter new Name: ")
        students[roll]["Div"] = input("Enter new Division: ")
        students[roll]["Marks"] = input("Enter new Marks: ")
        students[roll]["City"] = input("Enter new City: ")
        print("Student updated.")
    else:
        print("Student not found.")

# Delete
elif choice == '3':
    roll = input("Enter Roll No to delete: ")
    if roll in students:
        del students[roll]
        print("Student deleted.")
    else:
        print("Student not found.")

# Display
elif choice == '4':
    if students:
        for roll in students:
            print("\nRoll No:", roll)
            print("Name:", students[roll]["Name"])
            print("Div:", students[roll]["Div"])
            print("Marks:", students[roll]["Marks"])
            print("City:", students[roll]["City"])
    else:
        print("No records found.")

# Exit
elif choice == '5':
    break

else:
    print("Invalid choice!")