Answer:
class Student:
def __init__(self, name, age, grades=None):
self.name = name
self.age = age
self.grades = grades if grades is not None else {}
def add_grade(self, subject, grade):
self.grades[subject] = grade
def get_grade(self, subject):
return self.grades.get(subject, None)
def get_grades(self):
return self.grades
def get_average_grade(self):
if not self.grades:
return None
total_grades = sum(self.grades.values())
num_grades = len(self.grades)
return total_grades / num_grades
def __str__(self):
return f"Student(name={self.name}, age={self.age}, grades={self.grades})"
# Example usage
student1 = Student("Alice", 18)
student1.add_grade("Math", 95)
student1.add_grade("English", 89)
student1.add_grade("Science", 92)
print(student1)
print("Grade in Math:", student1.get_grade("Math"))
print("All grades:", student1.get_grades())
print("Average grade:", student1.get_average_grade())
Step-by-step explanation:
We define a Student class, which will be the blueprint for creating student objects.
class Student:
The __init__ method is called when a new object is created. It initializes the object with the provided data: name, age, and grades. The grades parameter has a default value of None, which means that if no grades are provided when creating the object, the grades attribute will be set to an empty dictionary.
def __init__(self, name, age, grades=None):
self.name = name
self.age = age
self.grades = grades if grades is not None else {}
The add_grade method allows you to add a grade for a specific subject. It takes two parameters, subject and grade, and stores the grade in the grades dictionary with the subject as the key.
def add_grade(self, subject, grade):
self.grades[subject] = grade
The get_grade method returns the grade for a specific subject. If the subject is not present in the grades dictionary, it returns None.
def get_grade(self, subject):
return self.grades.get(subject, None)
The get_grades method returns the grades dictionary, which contains all the subject-grade pairs for the student.
def get_grades(self):
return self.grades
The get_average_grade method calculates and returns the average grade for the student. It sums all the grades in the grades dictionary, divides the total by the number of subjects, and returns the result. If there are no grades, it returns None.
def get_average_grade(self):
if not self.grades:
return None
total_grades = sum(self.grades.values())
num_grades = len(self.grades)
return total_grades / num_grades
The __str__ method is a special method that returns a string representation of the object. In this case, it returns a string with the format Student(name=..., age=..., grades=...)
def __str__(self):
return f"Student(name={self.name}, age={self.age}, grades={self.grades})"
We now have a usage example for the Student class. Using the various methods, we access and modify the student's data after creating a new student object with a name, age, and a few grades.
# Example usage
student1 = Student("Alice", 18)
student1.add_grade("Math", 95)
student1.add_grade("English", 89)
student1.add_grade("Science", 92)
print(student1)
print("Grade in Math:", student1.get_grade("Math"))
print("All grades:", student1.get_grades())
print("Average grade:", student1.get_average_grade())
The Student class's methods for managing and storing student data are demonstrated in this code, along with how to create instances of the class.
NOTE: You should be able to easily compile this in an online compiler or use what is necessary to do so yourself. Cheers.