2. RUNNER RECORD
• A record is a special data structure which allows the programmer to
combine multiple data objects
• For example, a record might contain a string, an integer and a Boolean
• Think again about the runner example
• Instead of storing the name, bib number and elite status in three parallel
arrays, these could be stored in a single record
3. RUNNER RECORD
• The pseudocode for the runner record is shown below:
RECORD runner:
name: string
bib_number: integer
elite: Boolean
END RECORD
4. RUNNER RECORD
• To implement this code in Python we use a dictionary
runner = {
'name': 'John McGrain',
'bib_number': 1014,
'elite': False
}
5. RUNNER RECORD
• In Python, name, bib_number and elite are known as the dictionary keys
runner = {
'name': 'John McGrain',
'bib_number': 1014,
'elite': False
}
6. RUNNER RECORD
• We access dictionary keys like this:
print(runner[‘name’]) -> John McGrain
print(runner[‘bib_number’]) -> 1014
7. RUNNER RECORD
• We update the dictionary like this:
runner[‘name’] = ‘Billy Jones’
print(runner)
-> {'name': ‘Billy Jones’, 'bib_number': 1014,'elite':
False}
8. ARRAY OF RECORDS
• What if we want to store data about multiple runners?
• Then we create an array
• Instead of an array of integers, or an array of strings as we have seen
previously, we create and array of records
9. ARRAY OF RECORDS
• It would look like this:
runners =
[{'name': 'Ann', 'bib_number': 101, 'elite': False},
{'name': 'Jen', 'bib_number': 307, 'elite': True},
{'name': 'Tom', 'bib_number': 231, 'elite': False}]
• Notice we have 3 records each separated by a comma
• And these are all held within an array (we can tell by the square
brackets)
10. ARRAY OF RECORDS
• An array of records works in exactly the same way as any other array
print(runners[0])
-> {'name': 'Ann', 'bib_number': 101, 'elite': False}
print(runners[2])
-> {'name': 'Tom', 'bib_number': 231, 'elite': False}