Managing student records is a frequent requirement in educational institutions. Today, we'll design a basic Student Management System using Python, one of the most versatile languages available. This beginner-friendly tutorial will guide you through creating a system with essential CRUD operations (Create, Read, Update, and Delete).
Objective
To develop a simple Python program that allows the user to:
- Add a student's details
- Display all students
- Update student details
- Delete a student
Implementation
Let's start by defining our student data model.
class Student:
def __init__(self, roll_no, name, marks):
self.roll_no = roll_no
self.name = name
self.marks = marks
This simple Student class will help us encapsulate and manage student data.
Storage For the sake of simplicity, we'll use a list to store our student data:
students = []
CRUD Operations
Create:
def add_student():
roll_no = int(input("Enter roll number: "))
name = input("Enter name: ")
marks = float(input("Enter marks: "))
student = Student(roll_no, name, marks)
students.append(student)
print(f"Student {name} added successfully!")
Read:
def display_students():
for student in students:
print(f"Roll No: {student.roll_no}, Name: {student.name}, Marks: {student.marks}")
Update:
def update_student():
roll_no = int(input("Enter the roll number of the student to update: "))
for student in students:
if student.roll_no == roll_no:
student.name = input("Enter new name: ")
student.marks = float(input("Enter new marks: "))
print("Student details updated!")
break
else:
print("Student not found!")
Delete:
def delete_student():
roll_no = int(input("Enter the roll number of the student to delete: "))
for index, student in enumerate(students):
if student.roll_no == roll_no:
del students[index]
print(f"Student with roll number {roll_no} deleted!")
break
else:
print("Student not found!")
User Interaction:
We will use a simple loop to allow user interaction:
def main():
while True:
choice = input("""
1. Add student
2. Display students
3. Update student
4. Delete student
5. Exit
Enter your choice: """)
if choice == '1':
add_student()
elif choice == '2':
display_students()
elif choice == '3':
update_student()
elif choice == '4':
delete_student()
elif choice == '5':
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Code Explanation
We began by defining a Student class to represent student data.
The four functions (add_student, display_students, update_student, delete_student) implement our CRUD operations.
The main() function drives our application, presenting a menu to the user and responding to their choices.
Complete Source Code of Student Management System with Output
class Student:
def __init__(self, roll_no, name, marks):
self.roll_no = roll_no
self.name = name
self.marks = marks
students = []
def add_student():
roll_no = int(input("Enter roll number: "))
name = input("Enter name: ")
marks = float(input("Enter marks: "))
student = Student(roll_no, name, marks)
students.append(student)
print(f"Student {name} added successfully!")
def display_students():
for student in students:
print(f"Roll No: {student.roll_no}, Name: {student.name}, Marks: {student.marks}")
def update_student():
roll_no = int(input("Enter the roll number of the student to update: "))
for student in students:
if student.roll_no == roll_no:
student.name = input("Enter new name: ")
student.marks = float(input("Enter new marks: "))
print("Student details updated!")
break
else:
print("Student not found!")
def delete_student():
roll_no = int(input("Enter the roll number of the student to delete: "))
for index, student in enumerate(students):
if student.roll_no == roll_no:
del students[index]
print(f"Student with roll number {roll_no} deleted!")
break
else:
print("Student not found!")
def main():
while True:
choice = input(""" 1. Add student
2. Display students
3. Update student
4. Delete student
5. Exit
Enter your choice: """)
if choice == '1':
add_student()
elif choice == '2':
display_students()
elif choice == '3':
update_student()
elif choice == '4':
delete_student()
elif choice == '5':
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
1. Add student
2. Display students
3. Update student
4. Delete student
5. Exit
Enter your choice: 1
Enter roll number: 100
Enter name: Ramesh
Enter marks: 90
Student Ramesh added successfully!
1. Add student
2. Display students
3. Update student
4. Delete student
5. Exit
Enter your choice: 2
Roll No: 100, Name: Ramesh, Marks: 90.0
1. Add student
2. Display students
3. Update student
4. Delete student
5. Exit
Enter your choice: 3
Enter the roll number of the student to update: 100
Enter new name: Ram
Enter new marks: 70
Student details updated!
1. Add student
2. Display students
3. Update student
4. Delete student
5. Exit
Enter your choice: 2
Roll No: 100, Name: Ram, Marks: 70.0
1. Add student
2. Display students
3. Update student
4. Delete student
5. Exit
Enter your choice: 4
Enter the roll number of the student to delete: 4
Student not found!
1. Add student
2. Display students
3. Update student
4. Delete student
5. Exit
Enter your choice: 5
Conclusion
This Python-based Student Management System offers a fundamental introduction to CRUD operations. It's simple and extensible. If you're up for a challenge, consider enhancing it by adding data validation, storing student data in a file, or incorporating more advanced features! Happy coding!