A Record allows us to define a structure that can contain one or more variables (attributes) with each one potentially having a different data type.
As we are using Python, we will create a record by defining a class. The basic structure of a class can be seen below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class MySubjects(object): # this function creates the object and initialises any attributes def __init__(self, subjectName, noOfPeriods, teacherName): self.subjectName = subjectName self.numberOfPeriods = noOfPeriods self.teacherName = teacherName # when called this function (method) will return the value of subject name def getSubjectName(self): return self.subjectName # when called this function (method) will set the attribute sunjectName to the value of the parameter passed def setSubjectName(self, subjectName): self.subjectName = subjectName
To create an object of type ‘MySubjects’ we do the following code:
subjects = MySubjects(subjectName, noOfPeriods, teacherName)
Task 1: Copy the program above and create three objects of ‘MySubjects’ and display all of the subject names by calling the appropriate method. Create get and set methods for the other attributes.
Task 2: Change the above program so that it can store a list of five subjects. You will get the user to enter five subjects and store them in a list (array).
Task 3: Make two more useful improvements to the program.