School Management System in Python

1. Introduction

Building a School Management System in Python is an excellent way to understand object-oriented programming, relationships between classes, and basic CRUD operations (Create, Read, Update, Delete). This system will support adding students, teachers, and courses and establishing relationships between teachers and courses. This tutorial covers the foundational steps to create such a system via console input.

School Management System in Python

2. Program Steps

1. Define classes for Students, Teachers, and Courses.

2. Implement methods for adding, displaying, updating, and deleting each entity.

3. Create functions to assign teachers to courses.

4. Develop a simple text-based UI for user interactions.

5. Ensure the system can handle all basic operations for managing school data.

3. Code Program

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

class Teacher:
    def __init__(self, name, subject):
        self.name = name
        self.subject = subject
        self.courses = []

class Course:
    def __init__(self, name):
        self.name = name
        self.teacher = None

students = []
teachers = []
courses = []

def add_student():
    name = input("Enter student's name: ")
    age = input("Enter student's age: ")
    students.append(Student(name, age))
    print("Student added successfully.")

def add_teacher():
    name = input("Enter teacher's name: ")
    subject = input("Enter teacher's subject: ")
    teachers.append(Teacher(name, subject))
    print("Teacher added successfully.")

def add_course():
    name = input("Enter course name: ")
    courses.append(Course(name))
    print("Course added successfully.")

def assign_teacher_to_course():
    course_name = input("Enter the course name to assign a teacher to: ")
    teacher_name = input("Enter the teacher's name: ")
    course = next((course for course in courses if course.name == course_name), None)
    teacher = next((teacher for teacher in teachers if teacher.name == teacher_name), None)
    if course and teacher:
        course.teacher = teacher
        teacher.courses.append(course)
        print(f"{teacher.name} has been assigned to {course.name}.")
    else:
        print("Course or teacher not found.")

def display_entities(entity_list):
    for entity in entity_list:
        if isinstance(entity, Student):
            print(f"Student Name: {entity.name}, Age: {entity.age}")
        elif isinstance(entity, Teacher):
            courses_taught = ", ".join(course.name for course in entity.courses) if entity.courses else "No courses assigned"
            print(f"Teacher Name: {entity.name}, Subject: {entity.subject}, Courses: {courses_taught}")
        elif isinstance(entity, Course):
            teacher_name = entity.teacher.name if entity.teacher else "No teacher assigned"
            print(f"Course Name: {entity.name}, Teacher: {teacher_name}")

# Replace the original display_entities function with this updated version in your system.


def main_menu():
    while True:
        print("\n--- School Management System ---")
        print("1. Add Student")
        print("2. Add Teacher")
        print("3. Add Course")
        print("4. Assign Teacher to Course")
        print("5. Display Students")
        print("6. Display Teachers")
        print("7. Display Courses")
        print("8. Exit")
        choice = input("Enter your choice: ")

        if choice == "1":
            add_student()
        elif choice == "2":
            add_teacher()
        elif choice == "3":
            add_course()
        elif choice == "4":
            assign_teacher_to_course()
        elif choice == "5":
            display_entities(students)
        elif choice == "6":
            display_entities(teachers)
        elif choice == "7":
            display_entities(courses)
        elif choice == "8":
            print("Exiting system.")
            break
        else:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    main_menu()

Output:

--- School Management System ---
1. Add Student
2. Add Teacher
3. Add Course
4. Assign Teacher to Course
5. Display Students
6. Display Teachers
7. Display Courses
8. Exit
Enter your choice: 1
Enter student's name: Alok
Enter student's age: 25
Student added successfully.
--- School Management System ---
1. Add Student
2. Add Teacher
3. Add Course
4. Assign Teacher to Course
5. Display Students
6. Display Teachers
7. Display Courses
8. Exit
Enter your choice: 2
Enter teacher's name: Ramesh
Enter teacher's subject: Java
Teacher added successfully.
--- School Management System ---
1. Add Student
2. Add Teacher
3. Add Course
4. Assign Teacher to Course
5. Display Students
6. Display Teachers
7. Display Courses
8. Exit
Enter your choice: 3
Enter course name: Java Programming
Course added successfully.
--- School Management System ---
1. Add Student
2. Add Teacher
3. Add Course
4. Assign Teacher to Course
5. Display Students
6. Display Teachers
7. Display Courses
8. Exit
Enter your choice: 4
Enter the course name to assign a teacher to: Java Programming
Enter the teacher's name: Ramesh
Ramesh has been assigned to Java Programming.
--- School Management System ---
1. Add Student
2. Add Teacher
3. Add Course
4. Assign Teacher to Course
5. Display Students
6. Display Teachers
7. Display Courses
8. Exit
Enter your choice: 5
Student Name: Alok, Age: 25
--- School Management System ---
1. Add Student
2. Add Teacher
3. Add Course
4. Assign Teacher to Course
5. Display Students
6. Display Teachers
7. Display Courses
8. Exit
Enter your choice: 6
Teacher Name: Ramesh, Subject: Java, Courses: Java Programming
--- School Management System ---
1. Add Student
2. Add Teacher
3. Add Course
4. Assign Teacher to Course
5. Display Students
6. Display Teachers
7. Display Courses
8. Exit
Enter your choice: 7
Course Name: Java Programming, Teacher: Ramesh
--- School Management System ---
1. Add Student
2. Add Teacher
3. Add Course
4. Assign Teacher to Course
5. Display Students
6. Display Teachers
7. Display Courses
8. Exit
Enter your choice: 8
Exiting system.

Explanation:

1. Class Definitions: The Student, Teacher, and Course classes are defined with basic attributes. The Teacher class includes a list to track assigned courses, and the Course class has an attribute to hold the assigned teacher.

2. CRUD Operations: Functions are implemented to add students, teachers, and courses to their respective lists. An additional function handles assigning teachers to courses, establishing a relationship between the two entities.

3. User Interface: A looped main menu offers options for performing operations. Users can add new entities, assign teachers to courses, and display lists of all entities.

4. Relationship Handling: The assign_teacher_to_course function demonstrates managing relationships by updating the teacher's list of courses and the course's assigned teacher.

5. Flexibility and Expansion: This foundational system is designed for easy expansion. More attributes and methods can be added to the classes to support advanced features like grade tracking, attendance, or more complex relationships.

6. Object-Oriented Approach: Utilizing classes and objects allows for clear, manageable code that can be extended to include more sophisticated school management features.

Comments