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
- 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.
- 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. - The code creates an object course of the Course class with the name “Python” and the teacher’s name “Mr. Dipesh Yadav”.
- Two students, “John Walker” and “Jade Smith”, are added to the course using the add_student method.
- 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.
- The remove_student method is called to remove the student “Jade Smith” from the course.
- The print_details method is called again to print the updated course details.
- Thus, we have created a Python class real world example.
Output:
Related Articles
Create a Python class called Shape with a method to calculate the area of the shape.
Create a Python class called Employee with attributes name and salary.
Create a Python class called Manager that inherits from the Employee class.
Create a Python class called Customer with attributes name and balance.
Create a Python class called VIPCustomer that inherits from the Customer class.
Create a Python class called Phone with attributes brand, model, and storage.