Student List operations in Python

Student List operations in Python

Python Program to create a list for student name and perform Append Name in list, Display list, Count Total number of students in list, Display last 3 names of a list.

Student Name List

students = []

while True:
print(“\n1. Append Name”)
print(“2. Display List”)
print(“3. Count Students”)
print(“4. Display Last 3 Names”)
print(“5. Exit”)

choice = input("Enter choice: ")

# Append Name
if choice == '1':
    name = input("Enter student name: ")
    students.append(name)
    print("Name added.")

# Display List
elif choice == '2':
    if students:
        print("Student List:", students)
    else:
        print("List is empty.")

# Count Students
elif choice == '3':
    print("Total students:", len(students))

# Display Last 3 Names
elif choice == '4':
    if len(students) >= 3:
        print("Last 3 names:", students[-3:])
    else:
        print("Less than 3 students:", students)

# Exit
elif choice == '5':
    break

else:
    print("Invalid choice!")