Computing
Computer Science LearnITWithMrC ⛯ Year 7 Year 8 Year 9 GCSE
Responsive image

Lesson Video

22

Lesson Tasks

  • Watch the Lesson video
    Make notes if needed.
  • Open your Learning Journal
    Complete Task 1 in your Learning Journal
  • Complete the learning activities
    Make sure you complete the book tasks in your Unit Booklet
  • Complete End of Task Assessment
    Update your learning objectives

What do I need to Learn?

0 results forGuest
I need to learn how to understand and use a record as a data structures.

Key Terms

Data structure Array List Record Tuple Field Variable Identifier

Task 1 - Getting organised Click to see more


Task: Learning Journal

Open your Learning Journal by clicking on the image below

Good notes will help you organise and process data and information

Task 1: Fill out your learning Journal.

Open your Learning Journal and complete the task below .


Task 2 - What are Records? Click to see more


What are Records?

A Record is a data structure that groups together related data items. These are more complex than arrays, as you can store more than one type of data together under one identifier. For example, with a game, it would be useful to set up a data structure which collects a player's login and their score in one structure. Creating records will vary in different languages. Python uses a data structure called a 'dictionary' that has some features of the record structure.

  • Earlier in this topic we looked at arrays. An array is a collection of data items stored under one identifier, so that data items can be processed easily.
  • When we group data items together so they can be treated as a set of data, we refer to this as a data structure.
  • A dictionary stores data items in pairs, with each pair consisting of a key and a value.
  • It functions just like a printed dictionary, where you can look up a word (the key) and find its definition (the value).
  • Like an array/list, a dictionary is mutable, meaning that its value can be changed.
  • A dictionary is written using the curly brackets{ }, with each key value pair being separated by commas.
Example 1, in the code written below, the dictionary is called studentMarks and contains a number of student names and the mark they obtained in a test:

#Student Marks Dictionary #The first line defines a new dictionary: studentMarks = {"Owen":7, "William":9, "Libby":5, "Evie":8} #The following code will return all three records in the dictionary: print(studentMarks) >>> {"Mark":7, "William":9, "Libby":5, "Evie":8} #The following code will return Libbys mark in the dictionary: print(studentMarks["Libby"]) >>> 5

  • To look up the mark obtained by a student (the key), write the name of the dictionary followed by the key in square brackets [ ].
  • You cannot index a dictionary in the same way as a list by using an index number. An item can only be accessed through its key.

Task: Records

Complete the following using records in python.

  1. Create the record above using python
  2. Write a statement in python to print the mark obtained by Evie?
  3. Extension

    1. Print out each element in the record one at a time using a loop.

  • When you are finished screenshot your code into your Learning Journal


  • Task 3 - Dictionary Methods Click to see more

    Example 2, in the code written below, the dictionary is called players, that defines a player's name, their login and their score as one record:

    • We have two players, Katie and Patrick. We will use a dictionary to record the player names; Katie and Patrick with their logins and scores.
    • Katie's login is kat10 and her score is 124.
    • Patrick's login is pat00 and his score is 99.
    • The first line defines the dictionary data structure with two records of a player's name, login and score.
    • Be careful with the precise use of the syntax such as : and {} curly brackets with each key value pair being separated by commas:

    #Players Dictionary #The first line defines a new dictionary: players = {"Katie": {"login":"kat10", "score":124 }, "Patrick": {"login":"pat00", "score":99 }} #The next line adds a new record to the dictionary: players["Tom"] = {"login":"tom13", "score":121} #The following code will return all three records in the dictionary: print(players) >>> {'Katie': {'login': 'kat10', 'score': 124}, 'Patrick': {'login': 'pat00', 'score': 99}, 'Tom': {'login': 'tom13', 'score': 121}} #And this line of code would remove Katie's score: del players["Katie"] print(players) >>> {'Patrick': {'login': 'pat00', 'score': 99}, 'Tom': {'login': 'tom13', 'score': 121}}

    Dictionary Methods Table

    • The table below shows some of the most useful built-in dictionary methods:

    Dictionary_Methods_Table.png Basic Dictionary Methods

    Task : Dictionary Methods

    In the code below are examples of several dictionary methods, try them to see the results.

    Example 1: Look up a value

    #Student Marks Dictionary - Example 1: Look up a value studentMarks = {"Owen":7, "William":9, "Libby":5, "Keira":8} name= input("Enter a student name to look up: ") if name in studentMarks: print("mark: ", studentMarks[name]) else: print("Name not found")

    Example 2: Add a new key-value pair

    #Student Marks Dictionary - Example 2: Add a new key-value pair studentMarks = {"Owen":7, "William":9, "Libby":5, "Keira":8} name= input("Enter a student name to look add: ") if name in studentMarks: print(""Name is already in the marks record"") else: mark = input("Enter mark: ") studentMarks[name] = mark

    Example 3: Edit a value

    #Student Marks Dictionary - Example 3: Edit a value studentMarks = {"Owen":7, "William":9, "Libby":5, "Keira":8} name= input("Enter a student name to edit their mark: ") if name in studentMarks: mark = input("Enter mark: ") studentMarks[name] = mark else: print("Name not found")

    Example 4: Delete a key-value pair

    #Student Marks Dictionary - Example 4: Delete a key-value pair studentMarks = {"Owen":7, "William":9, "Libby":5, "Keira":8} name= input("Enter a student name to delete: ") if name in studentMarks: del studentMarks[name] print("Student Deleted") else: print("Name not found")

    1. Using the same methods as above can you create a menu to ask a user if they want to:
      • 1. Look up a student
      • 2. Add a new student
      • 3. Edit a student
      • 4. Delete a student
      • Turn the code above into functions to help you.
      • Show examples of the menu working.

  • When you are finished screenshot your code into your Learning Journal


  • Task 4 - Extension Task Click to see more

    Task: Coding your own records

    1. Write a program in python which stores a dictionary of names and 2-digit telephone extensions.
    2. Display a menu of options from which the user can choose:
      • A. Look up a telephone number.
      • B. Add a new name and telephone number.
      • C. Edit a telephone number.
      • D. Delete an entry.
      • E. Print phone directory in name sequence.
      • F. Quit

  • When you are finished screenshot your code into your Learning Journal

  • Task 5 - Book Task Click to see more

    Task: Complex Data Types

    Open your student workbook at page 54 Read through the notes and complete the following task:
    1. Task 29


    Task 6 - Lesson Review/Homework Click to see more


    Summing it all up

    Lets look at the learning outcomes and decide which one best describes our current level of understanding :

    Tick the one you feel is closest to your level

    Learning Outcomes

    • I have a basic understanding of how with a little help from my teacher
    • I can show my teacher that without their help.
    • independently and I can also explain it to others and can complete any extension tasks I am given.

    🠜 Now update your learning objectivesClick on the Assessment image