Records
A record is a data structure that can store different types of related data.
For example, if we want to store information about a pupil, we might need:
- their name
- their age
Instead of storing these separately, we group them together in one structure called a record.
from dataclasses import dataclass
@dataclass
class Pupil:
name: str = ""
age: int = 0
new_pupil = Pupil()
new_pupil.name = "Rachel"
new_pupil.age = 16
print(new_pupil.name)
print(new_pupil.age)
# Import a tool that helps us create records
from dataclasses import dataclass
# Define a record structure called Pupil
@dataclass
class Pupil:
# Field to store the pupil's name (text)
name: str = ""
# Field to store the pupil's age (whole number)
age: int = 0
# Create a new Pupil record
new_pupil = Pupil()
# Store "Rachel" in the name field of the record
new_pupil.name = "Rachel"
# Store 16 in the age field of the record
new_pupil.age = 16
# Display the value stored in the name field
print(new_pupil.name)
# Display the value stored in the age field
print(new_pupil.age)
Array of Records
An array of records is a data structure where each array element stores a record.
To access the data in an array of records you must specify the array name, index and record field.
from dataclasses import dataclass
@dataclass
class Pupil:
name: str = ""
age: int = 0
# Create an array of records
pupil_array = [Pupil() for i in range(3)]
# Input data into array of records
for i in range(3):
pupil_array[i].name = input("Enter name: ")
pupil_array[i].age = int(input("Enter age: "))
# Display array of records
for i in range(3):
print(pupil_array[i].name, pupil_array[i].age)
from dataclasses import dataclass
@dataclass
class Pupil:
name: str = ""
age: int = 0
def read_data():
# Create an array of records
pupils = [Pupil() for i in range(3)]
file = open("pupils.txt", "r")
for i in range(3):
pupils[i].name, pupils[i].age = file.readline().strip().split(",")
pupils[i].age = int(pupils[i].age)
file.close()
return pupils
def display_data(pupils):
# Display array of records
for i in range(3):
print(pupils[i].name, pupils[i].age)
# Main
pupils = read_data()
display_data(pupils)
from dataclasses import dataclass
@dataclass
class Pupil:
name: str = ""
age: int = 0
def read_data():
pupils = [Pupil() for i in range(3)]
file = open("pupils.txt", "r")
for i in range(3):
pupils[i].name, pupils[i].age = file.readline().strip().split(",")
pupils[i].age = int(pupils[i].age)
file.close()
return pupils
def find_oldest(pupils):
oldest = pupils[0].age
for i in range(1, len(pupils)):
if pupils[i].age > oldest:
oldest = pupils[i].age
print("The oldest pupil is", oldest, "years old.")
# Main
pupils = read_data()
find_oldest(pupils)
from dataclasses import dataclass
@dataclass
class Pupil:
name: str = ""
age: int = 0
def read_data():
pupils = [Pupil() for i in range(3)]
file = open("pupils.txt", "r")
for i in range(3):
pupils[i].name, pupils[i].age = file.readline().strip().split(",")
pupils[i].age = int(pupils[i].age)
file.close()
return pupils
def count_teens(pupils):
num_teens = 0
for i in range(len(pupils)):
if pupils[i].age >= 13:
num_teens = num_teens + 1
return num_teens
def write_data(num_teens):
file = open("teenagers.txt", "w")
file.write("There are " + str(num_teens) + " teenagers.")
file.close()
# Main
pupils = read_data()
num_teens = count_teens(pupils)
write_data(num_teens)