36. Problem to create a Python class real world example

In this Python oops program, we will create a Python class real world example. Python class called Course with attributes name, teacher, and students.

Python class real world example

Steps to solve the program
  1. The Course class has a constructor method __init__ that takes two parameters: name and teacher. It initializes the instance variables self.name, self.teacher, and self.students. The self.students variable is initialized as an empty list.
  2. The class also has three methods:
    The add_student method appends a student to the self.students list.
    The remove_student method removes a student from the self.students list if the student is present.
    The print_details method prints the course name, teacher’s name, and the list of students enrolled in the course.
  3. The code creates an object course of the Course class with the name “Python” and the teacher’s name “Mr. Dipesh Yadav”.
  4. Two students, “John Walker” and “Jade Smith”, are added to the course using the add_student method.
  5. The print_details method is called on the course object, which prints the course details, including the course name, teacher’s name, and the list of enrolled students.
  6. The remove_student method is called to remove the student “Jade Smith” from the course.
  7. The print_details method is called again to print the updated course details.
  8. Thus, we have created a Python class real world example.

Output:

Related Articles

Create a Python class called StudentRecord with attributes name, age, and grades.

Create a Python class called Shape with a method to calculate the area of the shape.

Leave a Comment