Working with Records
Lesson Files
What you need to know
You must be able to describe, exemplify and implement records
Explanation
So far, we’ve used only one type of data structure to store multiple values: the array.
Remember, an array is like a list of related variables that all share the same data type. For example, an array could store a list of numbers or a list of names, but not both.
While arrays can only hold one type of data, records allow us to store different types of data together in a single structure. For example, one record can store a name (string), an age (integer), and a status (Boolean) all in one place.
Just like in a database, records help us organize related information more efficiently.
By using records, we can keep all related data about a single person or object together in one structure, and we can use real-world names for each category (called fields) to make the data easier to understand and manage.
This is especially useful because most systems we work with, like databases, organize data in this way.
Record Structures
A record structure is like creating your own custom data type.
As a programmer, you define a record with a name that represents something real, like "Student" or "Product" and specify different categories (fields) for storing information.
Each field has a name and a specific data type (e.g., string, integer, Boolean), so the program knows how to store and handle each piece of data.
This way, when the program creates actual records, it knows exactly what kind of information to expect in each field.
Creating a Single Record Structure
Part One
1 2 3 |
|
Line 1 - from dataclasses import Dataclass This line is like getting a special helper from a toolbox. It brings in something called dataclass, which makes it easier to create a class that stores information.
Line 3 - @dataclass This is a special tag that tells Python, "I want to make the next class a dataclass." This means Python will automatically help us with things like creating the class and keeping track of data inside it.
Part Two
1 2 3 4 5 6 7 8 |
|
Line 5 - class SchoolReg Here, we are creating a class called SchoolReg. A class is like a blueprint or recipe. In this case, it helps us create something to store information about a school registration, like a student's name, age, and registration number.
Line 6 - Name: str = "" Inside the class, we create a variable called Name. This will hold the student's name, and it's a string, which means it's a word or group of letters (like "Peter"). The empty quotes " " mean that we are starting with no name yet.
Line 7 - Age: int = 0 This is another variable called Age. It will hold the student's age, and it's an integer (which means a whole number like 17). Right now, we start it at 0.
Line 8 - Reg: str = "" This variable is called Reg, which stands for "registration number." It’s also a string (a group of letters or numbers). At first, it's an empty string "", but we will fill it in later.
Part Three
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Line 10 - pupilRecord = SchoolReg() Now we create an object called pupilRecord from the SchoolReg class. Think of the class as a cookie cutter, and this object is a cookie made from it. This object will store a student's name, age, and registration number.
Line 11 - pupilRecord.Name = "Peter" Here, we set the Name of the pupilRecord object to "Peter". Before it was empty (""), but now it's filled with the name "Peter".
Line 12 - pupilRecord.Age = 17 Next, we set the Age for pupilRecord to 17. Before, it was 0, but now we know Peter is 17 years old.
__Line 13 - pupilRecord.Reg = "r1"_ Here, we set the registration number (Reg) to "r1". Now, the student Peter has a registration number "r1".
Part Four
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Line 15 - print(pupilRecord.Name) This line tells the computer to print (show) the name of the student, which is "Peter". The computer will display "Peter" on the screen.
Line 16 - print(pupilRecord.Age) This prints the student's age, which is 17. The computer will show 17 on the screen.
Line 17 - print(pupilRecord.Reg) Finally, this prints the student's registration number, which is "r1". The computer will show "r1".