Python Practice › OOP Programs › Program 36

Course with students

EasyOOP Programs · 36 of 48

Create Course with name, teacher and students, with methods to add and remove students and print the course.

Advertisement
Output
Python by Ms. Rao: Asha, Meena

Approach

  1. Keep students in a list.

Solution

Python
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.

More oop programs

← All 48 OOP Programs

Leave a Comment