Write a Python class Student with attributes name (a string) and marks (a list of integers), set when an object is created, and a method average() that returns the mean of the marks.
[5]
- Class header
class Student:(1) - Constructor
__init__withselfand parameters for name and marks(1) - Assigns both attributes using
self.nameandself.marks(1) - Method
averagedefined inside the class withselfas its parameter(1) - Returns
sum(self.marks) / len(self.marks)(or equivalent loop)(1) - Answer:
class Student: def __init__(self, name, marks): self.name = name self.marks = marks def average(self): return sum(self.marks) / len(self.marks) - Examiner tip: Attributes need the self. prefix; without it the values are lost when __init__ ends.