35. Problem to create a Python class example

In this Python oops program, we will create a Python class example. Python class called StudentRecord with attributes name, age, and grades.

Python class example

Steps to solve the program
  1. The StudentRecord class has a constructor method __init__ that takes three parameters: name, age, and grades. It initializes the instance variables self.name, self.age, and self.grades with the provided values.
  2. The class also has a method called calculate_average_grade which calculates the average grade of the student. It sums up all the grades in the self.grades list, divides it by the number of grades, and returns the average grade.
  3. The print_details method prints the student’s name, age, and average grade by calling the calculate_average_grade method.
  4. The code creates an object student of the StudentRecord class with the name “John Snow”, age 20, and a list of grades [80, 90, 85, 95].
  5. The print_details method is called on the student object, which prints the student’s details, including their name, age, and average grade.
  6. Thus, we have created a Python class example.

Output:

Related Articles

Create a Python class called ElectricCar that inherits from the Car class.

Create a Python class called Course with attributes name, teacher, and students.

Leave a Comment