Lesson video

In progress...

Loading...

Hello, my name is Mrs. Holborow and welcome to Computing.

I'm so pleased you've decided to join me for the lesson today.

In today's lesson, we are going to be investigating how records store data and we'll learn how to create dictionaries in Python.

Welcome to today's lesson from the unit Programming: Dictionaries and Data Files.

This lesson is called Records and Dictionaries, and by the end of today's lesson, you will be able to create a record using a dictionary in Python.

Shall we make a start? We will be exploring these keywords in today's lesson: record.

Record: a collection of attributes for a single entity.

Entity.

Entity: an entity is a single object, place, person or thing.

Attribute.

Attribute: properties or characteristics of an entity.

Dictionary.

Dictionary: a data structure that involves creating data pairings that can be located using a key.

Key.

Key: used to identify each attribute held in the dictionary.

Look out for these keywords throughout today's lesson.

Today's lesson is broken down into three parts.

We'll start by describing how records store data.

We'll then use a dictionary to represent a record.

And then finally we'll use a dictionary with a list to represent records.

Let's make a start by describing how records store data.

Data structures are used to store data in an organised and accessible way.

A list is a data structure.

Another example of a data structure is a record.

You might have heard of the word record if you've ever created a database before.

A record allows you to store a collection of attributes for a single entity.

Here is an example of a record that holds the data for a player.

So we have player, we have username, which is "rockstar9", password, "6goatsEating" and top score, 5,328.

The player is the entity.

Username, password and top score are attributes, and then "rockstar9", "6goatsEating" and 5,328 is the data.

An entity can be any object, place, person or thing.

Attributes are properties or characteristics of that entity.

Attributes are sometimes referred to as fields.

Time to check your understanding.

In this record, a password is A: an entity, B: an attribute, or C :data? Pause the video whilst you have a think.

Did you select B, an attribute? Well done.

In this record, "rockstar9" is A: an entity, B: an attribute, or C: data? Pause the video whilst you have a think.

Did you select data? Well done.

A database will hold many records for a particular entity.

So here you can see we have multiple players.

We have "rockstar9" from our previous example.

We have dinomouse and sharkfish.

Izzy has a question.

"What other entities could be stored in a record?" Maybe pause the video whilst you have a think.

Ah, Laura's got some great ideas.

Laura says, "Lists of customers, books, games, stock for a shop and cars." Did you think of any different examples? A record can be represented in pseudocode like this example.

So here we have RECORD Player, username, which is going to be string value, password, which is string value, top score, which is going to be an integer.

And then we ENDRECORD.

Remember, player is the entity.

Username, password and top score are the attributes.

A record is what's known as a static data structure.

You must determine the attributes and data types for each entity during declaration.

A static data structure does not change in size.

These will then be fixed for each record used in the database.

So string and integer are the data types for the attributes.

True or false: A record is a dynamic data structure? Pause the video whilst you have a think.

Did you select false? Well done.

A record is a static data structure, which means its size is fixed at declaration.

Okay, we're moving on to our first task of today's lesson, Task A.

I'd like you to describe how records are used to store data, and I'd like you to use these words in your answer: record, entity and attribute.

Pause the video whilst you have a go at the question.

How did you get on with your question? Great work.

Let's have a look at a sample answer together.

You were asked to describe how records are used to store data.

A record is a data structure that holds data, about one entity.

An entity can be any object, place, person or thing.

A record allows you to store a collection of attributes about a single entity.

For example, you can store the attributes of the make, model and registration number for a car.

Did you have a similar answer? Remember, if you want to pause your video here and go back and add any detail to your answer, you can do that now.

So we've described how records store data.

Let's move on to Use a dictionary to represent a record.

Python does not have a native data structure for a record.

Instead, you can use a different data structure called a dictionary, to represent a record.

A dictionary has some of the features of a record.

Here is an example of a dictionary data structure being used as a record.

So we've used the same example that we saw earlier on in the lesson.

So we have player is equal to, and then we have our open curly brackets and we have username, which has the value "rockstar", password, which has the value "6goatsEating" and score, which has the value 5,328.

And then we close our curly brackets.

Remember, player is the entity, username, password, and score are the attributes and "rockstar", "6goatsEating" and 5,328 are the data.

Note how curly brackets are used for dictionaries, whereas lists use square brackets.

A dictionary works differently to a list.

In a list, items are accessed by their indices, their index location.

In a dictionary, you access the data using a key.

In this example, the keys are username, password, and score.

If you want to know the data associated with the username key in the player record, you use this line of code: print(player["Username"]) What do you think will be the output from this programme? Maybe pause the video whilst you have a think.

That's right.

The output will be "rockstar" because we want the data associated with the key username, and "rockstar" is associated to username in this dictionary.

To modify the data paired with the key, you can use this line of code.

So player["Password"] = and then the new value.

So this time it's "7goatsEating".

What do you think will be the output from this programme? Maybe pause the video whilst you have a think.

That's right.

The programme will output the new value of the password, which is "7goatsEating".

Well done.

Time to check your understanding.

Which type of brackets do you use when defining a dictionary in Python? Is it A: the square brackets, B: brackets, or C: curly brackets? Pause the video whilst you have a think.

That's right, we use the curly brackets to define a dictionary in Python.

Well done.

What will be the output from the following programme? A: 5,328, B: 8,756, or C: score.

Pause the video whilst you have a think.

Did you select B? Well done.

The output from the programme will be 8,756 and that's because the value of score is updated on line 4 of the code.

Okay, we are moving on to our second task of today's lesson and you're doing a fantastic job so far, so well done.

For Task B, I'd like you to use the dictionary data structure example to create a record for an entity of your choice.

Example entities could be a game, a book, a film, or a car.

Your record should have at least three attributes.

Once you have created your dictionary data structure, create several print statements that will print out each attribute from your dictionary with a suitable heading.

An example for a car record is given below.

So we have Make: Ford, Model: C Max, Colour: red.

And then for part three, modify the dictionary so that it contains data for a new record.

So hint here: the code to modify the number held in the attribute score was player["Score"] = and then the new value, which in this case is 8,756.

Pause the video here whilst you complete the tasks.

How did you get on? Did you manage to create your dictionaries? Well done.

You were asked to use a dictionary data structure to create a record for an entity of your choice.

Here's an example for a car.

So we have cars = and then we have {"Make": "Ford", "Model": "C Max" "Colour": "Red"}.

Notice the curly brackets around the dictionary data structure and the colons to separate the attributes from the data.

You were then asked to create several print statements that will print each attribute from your dictionary with a suitable heading.

So we've added lines 4, 5, and 6 in the code here with some print statements.

So let's have a look at the one on line 4.

Print, open bracket, f for the F string "Make": with a colon.

And then we have the curly bracket because we're printing out an item that's held in the value, {cars.

And then we have square brackets, speech marks, ["Make"]} close speech marks, close square brackets, close curly brackets, close our speech marks, close our brackets, so that will print out, Make.

And then "Ford", which is the value held in the attribute "Make".

You were then asked to modify the dictionary so that it contained data for a new car or your new example.

So we have here on lines 4, 5 and 6 some code that will update those values.

So now the Make is Skoda, the Model is Fabia and the Colour is Blue.

Okay, we are moving on to our final part of today's lesson and you've done a fantastic job so far, so well done.

We are now going to use a dictionary with a list to represent records.

A database can hold multiple records as seen in this example.

The database players has three records.

So we have three usernames: rockstar9, dinomouse, and sharkfish.

If you want to use multiple records in a database, then you can add multiple dictionaries to a list.

So you can see here on line 13, I have a list which I've set to players and then I've said = [, remember we use square brackets for lists.

And then I have player1, player2, player3], So this is putting each dictionary into an item on my list.

Note that each dictionary follows exactly the same fixed structure.

You've just changed the data and the names for each dictionary.

What do you think will happen when this programme is executed? Pause the video whilst you have a think.

Did you make a prediction about what would happen when the programme was executed? Let's run the programme together and see how it works.

So I've opened the programme in the Raspberry Pi code editor and I'm going to hit Run.

The programme asked me to enter a username.

The programme asked me to enter a password.

The programme asked me to enter a score.

The programme then asked me if I'd like to add another player.

It asked me to enter another username and another password and a score.

And it repeatedly asks me if I want to add another player until I answer the question with No.

When I enter No, you can see that the results that I've typed in have been added to the dictionary data structure.

So Laura correctly made the prediction here.

"It will repeatedly add dictionaries to the player's list until the user responds 'N' which represents No to the question 'Would you like to add another player? Yes/No.

'" Time to check your understanding What data structure is being created on line 13 of this programme? Look carefully at the code and pause your video whilst you have a think.

That's right.

A dictionary is being created on line 13.

Remember, you can spot the dictionary because of the curly brackets.

Where is the player's list initialised? Pause the video whilst you have a think.

Did you spot it? The player's list is initiated on line 1 of the programme, right at the top.

Where is the new data added to the player's list? Pause the video whilst you have a think.

That's right.

New data is added to the player's list on line 17.

We have the line players.

append(player).

Okay, we are moving on to our final task of today's lesson and you've done a fantastic job so far, so well done.

I'd like you to open the starter programme at oak.

link/add-players.

The programmer has realised that they have left out an attribute from the record.

They need to have an attribute for the highest_score.

Modify the programme so that the user can input the highest_score, which is then added to the dictionary or record.

The programme needs to display the dictionary that is located at position 0 of the list.

At the bottom of the programme, write a line of code that will display this.

For part four, modify the programme so that the user can choose which record they wish to access by typing in the index.

Pause the video here whilst you complete the activities.

How did you get on with your code? I'm sure you managed to do it.

Well done.

Let's have a look at the modified code.

So we were asked to add in the attribute for highest score.

So on lines 11 and 12, we've got a print statement to ask the user for the highest score.

Line 12, we're storing that input as highest_score.

And then we have our dictionary structure, which is started on line 14, but on line 17 we've added the new attribute here.

So we have highest score and then we've set that to the value highest_score.

For part three, you were asked to print out the value that was held in position 0.

So you should have added a line of code that looks like this.

So print, open brackets, players and then in square brackets [0] because that's the first item in the list, close the square bracket, close the bracket.

And then for part four, you were asked to give the user the option to print out the record of their choice.

So we've got three lines of code to do this.

First, a print statement which asks the user which record they want to print out.

And then on line 28 we have record_no is equal to the input from the user, which is an integer value.

And then on line 29, we are using the same print statement that we used on line 26.

But this time, rather than putting a number in the square brackets, we are putting the variable record_no, which obviously holds the integer value that the user has entered.

If you want to see the full working solution, you can go to oak.

link/add-players-solution.

You can also make any amendments to your code if you didn't quite get it working correctly.

Okay, we've come to the end of the lesson: Records and dictionaries.

Let's summarise what we've learned together.

Data structures are used to store data in an organised and accessible way.

A record is an example of a data structure.

A record allows you to store a collection of attributes for a single entity.

Python does not have a native data structure for a record.

Instead, you can use a different data structure called a dictionary to represent a record.

You've worked really hard today and I hope you've enjoyed the lesson.

See you again soon.

Bye.