5
Most read
9
Most read
10
Most read
RECORDS IN PYTHON
Higher Computing Science
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
RUNNER RECORD
• The pseudocode for the runner record is shown below:
RECORD runner:
name: string
bib_number: integer
elite: Boolean
END RECORD
RUNNER RECORD
• To implement this code in Python we use a dictionary
runner = {
'name': 'John McGrain',
'bib_number': 1014,
'elite': False
}
RUNNER RECORD
• In Python, name, bib_number and elite are known as the dictionary keys
runner = {
'name': 'John McGrain',
'bib_number': 1014,
'elite': False
}
RUNNER RECORD
• We access dictionary keys like this:
print(runner[‘name’]) -> John McGrain
print(runner[‘bib_number’]) -> 1014
RUNNER RECORD
• We update the dictionary like this:
runner[‘name’] = ‘Billy Jones’
print(runner)
-> {'name': ‘Billy Jones’, 'bib_number': 1014,'elite':
False}
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
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)
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}

Records in Python

  • 1.
    RECORDS IN PYTHON HigherComputing Science
  • 2.
    RUNNER RECORD • Arecord 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 • Thepseudocode for the runner record is shown below: RECORD runner: name: string bib_number: integer elite: Boolean END RECORD
  • 4.
    RUNNER RECORD • Toimplement this code in Python we use a dictionary runner = { 'name': 'John McGrain', 'bib_number': 1014, 'elite': False }
  • 5.
    RUNNER RECORD • InPython, name, bib_number and elite are known as the dictionary keys runner = { 'name': 'John McGrain', 'bib_number': 1014, 'elite': False }
  • 6.
    RUNNER RECORD • Weaccess dictionary keys like this: print(runner[‘name’]) -> John McGrain print(runner[‘bib_number’]) -> 1014
  • 7.
    RUNNER RECORD • Weupdate 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}