Python Practice › OOP Programs › Program 17
School management system
Write a small school management system with classes for students and a school.
Output
['Asha', 'Meena'] Topper: Asha
Approach
- A
Studentclass holds data; aSchoolclass manages a list of students.
Solution
class Student: def __init__(self, name, grade, marks): self.name, self.grade, self.marks = name, grade, marks class School: def __init__(self, name): self.name = name self.students = [] def admit(self, student): self.students.append(student) def by_grade(self, grade): return [s.name for s in self.students if s.grade == grade] def topper(self): return max(self.students, key=lambda s: s.marks).name school = School("Sunrise Public School")for args in [("Asha", 5, 92), ("Ravi", 6, 81), ("Meena", 5, 88)]: school.admit(Student(*args))print(school.by_grade(5))print("Topper:", school.topper())Output
['Asha', 'Meena'] Topper: Asha
Line by line
Lines 10-11
admit adds a student to the school’s list.
Lines 13-17
Queries are methods on the School, so the rest of the program does not need to know how students are stored.
Learn the concept
New to this topic? Read the lesson, then come back.