In this Python oops program, we will create a school management system using Python classes. The code demonstrates a basic implementation of a school management system using Python with the help of the below-given steps.
School management system using Python
Steps to solve the program
- The School class represents a school and has attributes like name, students, teachers, and courses. The __init__ method initializes these attributes.
- It also has methods to add and remove students, teachers, and courses from the school. These methods modify the respective lists by appending or removing the provided objects.
- The Student class represents a student and has attributes like name and grade. The __init__ method initializes these attributes.
- It also has methods to enroll and drop courses. The enroll_course method calls the add_student method of the Course class, and the drop_course method calls the remove_student method of the Course class.
- The Teacher class represents a teacher and has attributes like name and subject. The __init__ method initializes these attributes.
- It also has methods to assign and unassign courses. The assign_course method calls the add_teacher method of the Course class, and the unassign_course method calls the remove_teacher method of the Course class.
- The Course class represents a course and has attributes like name, students, and teacher. The __init__ method initializes these attributes.
- It also has methods to add and remove students, and add and remove a teacher for the course. These methods modify the respective attributes accordingly.The remaining part of the code demonstrates the usage of the class structure:
- An instance of the School class is created with the name “ABC School”.
- Instances of the Student class are created with different names and grades, and they are added to the school using the add_student method.
- Instances of the Teacher class are created with names and subjects, and they are added to the school using the add_teacher method.
- Instances of the Course class are created with names, and they are added to the school using the add_course method.
- Students are enrolled in courses using the enroll_course method, and teachers are assigned to courses using the assign_course method.
- Finally, the details of the school, students, teachers, and courses are printed using a series of loops.
class School:
def __init__(self, name):
self.name = name
self.students = []
self.teachers = []
self.courses = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, student):
if student in self.students:
self.students.remove(student)
def add_teacher(self, teacher):
self.teachers.append(teacher)
def remove_teacher(self, teacher):
if teacher in self.teachers:
self.teachers.remove(teacher)
def add_course(self, course):
self.courses.append(course)
def remove_course(self, course):
if course in self.courses:
self.courses.remove(course)
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def enroll_course(self, course):
course.add_student(self)
def drop_course(self, course):
course.remove_student(self)
class Teacher:
def __init__(self, name, subject):
self.name = name
self.subject = subject
def assign_course(self, course):
course.add_teacher(self)
def unassign_course(self, course):
course.remove_teacher(self)
class Course:
def __init__(self, name):
self.name = name
self.students = []
self.teacher = None
def add_student(self, student):
self.students.append(student)
def remove_student(self, student):
if student in self.students:
self.students.remove(student)
def add_teacher(self, teacher):
self.teacher = teacher
def remove_teacher(self, teacher):
self.teacher = None
# Create a School instance
school = School("ABC School")
# Create some students
student1 = Student("John", 10)
student2 = Student("Alice", 9)
student3 = Student("Bob", 11)
# Add students to the school
school.add_student(student1)
school.add_student(student2)
school.add_student(student3)
# Create some teachers
teacher1 = Teacher("Mr. Smith", "Math")
teacher2 = Teacher("Ms. Johnson", "English")
# Add teachers to the school
school.add_teacher(teacher1)
school.add_teacher(teacher2)
# Create some courses
course1 = Course("Mathematics")
course2 = Course("English")
# Add courses to the school
school.add_course(course1)
school.add_course(course2)
# Enroll students in courses
student1.enroll_course(course1)
student2.enroll_course(course2)
student3.enroll_course(course1)
# Assign teachers to courses
teacher1.assign_course(course1)
teacher2.assign_course(course2)
# Print the details of the school, students, teachers, and courses
print("School:", school.name)
print("Students:")
for student in school.students:
print(student.name, "- Grade", student.grade)
print("Teachers:")
for teacher in school.teachers:
print(teacher.name, "- Subject:", teacher.subject)
print("Courses:")
for course in school.courses:
print(course.name)
print("Teacher:", course.teacher.name)
print("Enrolled Students:")
for student in course.students:
print(student.name)
print("--------------------")
Output:
School: ABC School
Students:
John - Grade 10
Alice - Grade 9
Bob - Grade 11
Teachers:
Mr. Smith - Subject: Math
Ms. Johnson - Subject: English
Courses:
Mathematics
Teacher: Mr. Smith
Enrolled Students:
John
Bob
--------------------
English
Teacher: Ms. Johnson
Enrolled Students:
Alice
--------------------
Related Articles
Write a Python Class Structure for Employee Management Application.
Write a Python Class with @property decorator.
Write a Python Class structure with module-level Import.
Create 5 different Python Classes and access them via a single class object.
Create 5 Python classes and set up multilevel inheritance among all the classes.
Set Instance variable data with setattr and getattr methods.