Skip to content

Arrays of Records

What you need to know

You must be able to describe, exemplify and implement arrays of records

Explanation

In Python, an array of records is like a list of boxes, where each box holds important information about a person, place, or thing.

Each "box" (record) has labelled parts for details, like a name, age, or ID number.

For example, if we have three students, we can store their name, age, and registration number in three separate boxes.

These boxes are part of a record, and we can easily look inside each one to see or change the details.

Array of Records in Python (basic method)

Part One

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    from dataclasses import dataclass

    @dataclass

    class SchoolReg:
        Name : str = ""
        Age : int = 0
        Reg : str = ""

    pupilRecord = [SchoolReg() for x in range (0,3)]

Info

Lines 1 - 8 are covered in Working with Records.

Line 10 - pupilRecord = [SchoolReg() for x in range(0,3)] This line creates a list called pupilRecord. A list is like a box where we can store many things. In this case, we are creating two empty student records because the range (0,3) means it will create three spots, one for pupilRecord[0], one for pupilRecord[1] and one for pupilRecord[2].

In short, this line says:

"Make 3 empty student records using the SchoolReg class."

Part Two

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    from dataclasses import dataclass

    @dataclass

    class SchoolReg:
        Name : str = ""
        Age : int = 0
        Reg : str = ""

    pupilRecord = [SchoolReg() for x in range (0,3)]

    pupilRecord[0].Name = "Peter"
    pupilRecord[0].Age = 17
    pupilRecord[0].Reg = "r1"

Line 12 - pupilRecord[0].Name = "Peter" Here, we set the Name of the first student (pupilRecord[0]) to "Peter". Before, it was empty, but now it holds the name "Peter."

Line 13 - pupilRecord[0].Age = 17 Next, we set the Age of the first student (pupilRecord[0]) to 17. Before it was 0, and now it’s set to 17 years old.

Line 14 - pupilRecord[0].Reg = "r1" Here, we set the registration number (Reg) for the first student to "r1". Now Peter has the registration number "r1".

Part Three

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
    from dataclasses import dataclass

    @dataclass

    class SchoolReg:
        Name : str = ""
        Age : int = 0
        Reg : str = ""

    pupilRecord = [SchoolReg() for x in range (0,3)]

    pupilRecord[0].Name = "Peter"
    pupilRecord[0].Age = 17
    pupilRecord[0].Reg = "r1"

    print (pupilRecord[0].Name) 
    print (pupilRecord[0].Age) 
    print (pupilRecord[0].Reg) 

Line 16 - print(pupilRecord[0].Name) This line tells the computer to print the name of the first student in the list, which is Peter.

Line 17 - print(pupilRecord[0].Age) This prints the age of the first student in the list, which is 17.

Line 18 - print(pupilRecord[0].Reg) Finally, this prints the registration number of the first student, which is "r1".

Array of Records (efficient method)

Part One

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
    from dataclasses import dataclass

    @dataclass

    class SchoolReg:
        Name : str = ""
        Age : int = 0
        Reg : str = ""

    pupilRecord = [SchoolReg() for x in range (0,3)]

    for x in range(len(pupilRecord)):
       pupilRecord[x].Name = str(input("Enter Name: "))
       pupilRecord[x].Age = int(input("Enter Age: "))
       pupilRecord[x].Reg = str(input("Enter Reg: "))

    for x in range(len(pupilRecord)):
        print ("Name: " , pupilRecord[x].Name , "Age: " , pupilRecord[x].Age , "Reg: " , pupilRecord[x].Reg )

Info

Lines 1 - 8 are covered in Working with Records.

Line 12 - for x in range(len(pupilRecord)): The range(len(pupilRecord)) makes the loop go through each student one at a time (three students in this case).

Line 13 - pupilRecord[x].Name = str(input("Enter Name: ")) This line asks the user to type a name for each student using the input() function. Whatever the user types in is stored in the Name variable for each student in the list. For example, the first time the loop runs, it asks for the name of pupilRecord[0].

Line 14 - pupilRecord[x].Age = int(input("Enter Age: ")) This line asks the user to type the student's age. The input() function is used again, and the number typed is stored as the student's age. It’s converted into an integer (whole number). For example, the first time, it will store the age for pupilRecord[0].

Line 15 - pupilRecord[x].Reg = str(input("Enter Reg: ")) This line asks the user to type the student's registration number. The registration number is stored as a string (letters and numbers) for each student. For example, the first time, it stores the registration number for pupilRecord[0].

Part Two

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
    from dataclasses import dataclass

    @dataclass

    class SchoolReg:
        Name : str = ""
        Age : int = 0
        Reg : str = ""

    pupilRecord = [SchoolReg() for x in range (0,3)]

    for x in range(len(pupilRecord)):
       pupilRecord[x].Name = str(input("Enter Name: "))
       pupilRecord[x].Age = int(input("Enter Age: "))
       pupilRecord[x].Reg = str(input("Enter Reg: "))

    for x in range(len(pupilRecord)):
        print ("Name: " , pupilRecord[x].Name , "Age: " , pupilRecord[x].Age , "Reg: " , pupilRecord[x].Reg )

Line 17 - pupilRecord = [SchoolReg() for x in range(0,3)] This is another loop that goes through each student record in the pupilRecord list only this time it’s going to print the details of every student one at a time.

Line 18 - print("Name: ", pupilRecord[x].Name, "Age: ", pupilRecord[x].Age, "Reg: ", pupilRecord[x].Reg) This line prints out the name, age, and registration number for each student in the list.

Example

    Name: Peter Age: 17 Reg: r1

Array of Records (with Files)

Part One

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    from dataclasses import dataclass

    @dataclass

    class SchoolReg:
        Name : str = ""
        Age : int = 0
        Reg : str = ""

    pupilRecord = [SchoolReg() for x in range (0,3)]

    file = open("School-Reg.csv", "r")

Info

Lines 1 - 10 are covered in Working with Records.

Line 12 - file = open("School-Reg.csv", "r") This line opens a file called "School-Reg.csv" in read mode so that we can read from it.

Part Two

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
    from dataclasses import dataclass

    @dataclass

    class SchoolReg:
        Name : str = ""
        Age : int = 0
        Reg : str = ""

    pupilRecord = [SchoolReg() for x in range (0,3)]

    file = open("School-Reg.csv", "r")

    for i in range(len(pupilRecord)):
        data = file.readline()
        data = data.strip("\n")
        data = data.split(",")
        pupilRecord[i] = SchoolReg(data[0], int(data[1]), data[2])

Line 14 A loop that will go through each of the three student records in the pupilRecord list one by one.

Line 15 - data = file.readline() This line reads one line of data from the file. Each time the loop runs, it reads the next line from the file.

Line 16 - data = data.strip("\n") This removes the newline character (\n), which is an invisible character at the end of each line in a file. It's like cleaning up the data so there are no extra spaces or jumps to the next line.

Line 17 - data = data.split(",") This splits the line of data at each comma. Since it's a CSV file, the data for each student is separated by commas. After splitting, we get a list of pieces of information, like the student's name, age, and registration number.

For example, if the line was "Peter,17,r1", after splitting it, we would get:

``` text
    data[0] = "Peter"
    data[1] = "17"
    data[2] = "r1"
```

Line 19 - pupilRecord[i] = SchoolReg(data[0], int(data[1]), data[2]) This line takes the pieces of data from the file and creates a SchoolReg object for each student. It fills in the student's name (data[0]), age (data[1]), and registration number (data[2]). The int() function converts the age from a string to a number.

Part Three

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    from dataclasses import dataclass
    import CSV

    @dataclass

    class SchoolReg:
        Name : str = ""
        Age : int = 0
        Reg : str = ""

    pupilRecord = [SchoolReg() for x in range (0,3)]

    file = open("School-Reg.csv", "r")

    for i in range(len(pupilRecord)):
        data = file.readline()
        data = data.strip("\n")
        data = data.split(",")
        pupilRecord[i] = SchoolReg(data[0], int(data[1]), data[2])

    for x in range(len(pupilRecord)):
        print ("Name: " , pupilRecord[x].Name , "Age: " , pupilRecord[x].Age , "Reg: " , pupilRecord[x].Reg )

    file.close()

Line 21 - for x in range(len(pupilRecord)): This is another loop. This time, it goes through each student's record in the list and prints out their information.

Line 22 - print("Name: ", pupilRecord[x].Name, "Age: ", pupilRecord[x].Age, "Reg: ", pupilRecord[x].Reg) This line prints out the student's name, age, and registration number.

Line 23 - file.close() This line closes the file after we are finished reading from it.