Python Practice › OOP Programs › Program 36
Course with students
Create Course with name, teacher and students, with methods to add and remove students and print the course.
Output
Python by Ms. Rao: Asha, Meena
Approach
- Keep students in a list.
Solution
class Course: def __init__(self, name, teacher): self.name, self.teacher, self.students = name, teacher, [] def add(self, student): if student not in self.students: self.students.append(student) def remove(self, student): if student in self.students: self.students.remove(student) def show(self): print(f"{self.name} by {self.teacher}: {', '.join(self.students)}") c = Course("Python", "Ms. Rao")for s in ["Asha", "Ravi", "Meena"]: c.add(s)c.remove("Ravi")c.show()Output
Python by Ms. Rao: Asha, Meena
Learn the concept
New to this topic? Read the lesson, then come back.