video

Lesson video

In progress...

Loading...

Hi there, I'm Rebecca, you're computing teacher.

For this lesson, you're going to need your repeller account which you should have already set up with your parent or carers permission.

It's also a really good idea to have a pen and paper handy to answer any questions or make any notes that you need to in this lesson.

The final thing I'd like you to do is really try your best to remove as many distractions as you can so that you can really focus in this lesson.

Once you've got that ready, we can begin.

This lesson you will define a 2D list and an array and you'll use a 2D list in a programme.

To start the lesson thinking about these two things so first of all, do you notice anything new about the structure of the scores list that's been given you there on the left hand side? And then secondly, what do you think the output will be when this programme is executed? So pause the video while you think about those two questions.

What's the answer there.

So the scores list holds more than one list this is known as a two dimensional list.

And notice the extra square brackets that surround each list and then the usual square brackets surrounding the entire structure.

A 2D list is essentially a list of lists.

Scores zero will access the list of position zero which is why all of the items in that list have been displayed.

We will be exploring this further during the lesson.

So what is a two dimensional list? A list is used to hold multiple elements under one name.

A two dimensional list allows you to hold a list as one element.

Data in a 2D list can be held in rows and columns.

A 2D list can be used to hold the player names and scores for a game.

So row zero holds the player names and row one holds the players scores.

Each column then holds the data for each player.

Each new row is a new list.

If a new row is needed, then we add another list.

You can access each list in the 2D list in the same way that you can access a single element in a list.

So we've got that sample there scores zero.

This accesses the list at location zero.

And then this accesses the list at location one.

To access a single item in a single list then you need an extra bit of code and you can notice there you've now got two rows of square brackets there.

So you've got the row index and you've got the column index.

What will be the output of this programme when it is executed? Do you think pause the video while you have a little think.

Let's take a look.

So it would be Fred because it's in list zero and it's at position zero.

So the first value in the list or row that you wish to access is that first one the row index.

And then the column index is the second value is the index of the item in that list.

So what will be the output of this programme when it is executed bearing those things in mind? Let's take a look.

It's going to be six.

What will be the output of this programme when it is executed? Have a look.

Let's take a look it will be Wilma.

The same rules apply to 2D arrays and lists a single arrays and lists.

So a 2D array must be a predefined fixed size and each item held must be of the same data type.

But a 2D list can vary in size throughout the programme execution and can contain multiple data types.

What I want you to do then is I want you to use the word play section of your worksheet to practise navigating a 2D list.

So pause the video while you have a go with that.

Excellent, so let's take a look at the code that you could have used.

So this task was just to get you used to accessing and referencing items in those 2D lists.

So hopefully you've had a little go and you managed to do those tasks, okay.

But if you didn't, I'm just going to do a little demonstration of how you could do it.

And I've got a worksheet down here as I always say, that's what I'm looking at if you see me looking downwards.

So your first step was gave you a 2D list to copy into the environment into relique.

So I've done that to get me started it says write a line of code that will print all of the birds, so you see have to look down, they've got fish, then you've got birds, and then you've got the animals and mammals or mammals I guess you'd call them.

So what I'm going to do now is I am going to so I'm just going to print them.

So print animals and if you remember you need that square bracket.

Now it's asking me to print all of the.

what was it all of the birds.

So in that second one, so if you think about how lists work this would be list zero, this would be list one, and this would be list two.

So if I put in a one, then it should work when I press run, it should just display the birds which it does, so am right.

They say write a line of code that will print all the mammals.

So the mammals is on the second one and that's printing all of the mammals.

And then the next one, let's just have a look at what it says it says accessing single items within two dimensional list.

So we've done accessing a list within a list but how do we access just those single items. And now we've got to start using two lots of square brackets.

So using the same two dimensional list write a line code that will print Wren as output.

So I know I've got to have the two brackets so I'm going to put those in there.

And then I've got to think well which list is it? So Wren is in the second list so that's at location one.

And then this goes zero, one, two so it should be to this.

Let's just have a look.

Yeah, got it right? Because if we go down first so this one here, we look at the rows, so zero, one it was on row one and then it was at location two within that list that's why that's worked, right a line of code that will print Pollock as output.

So Pollock is in list zero there and it's at location one, so zero, and then one.

So hopefully that's going to work.

Yes, it did.

And then write a line of code that will print camel as output.

So camel is in the third list so that's at location zero, one, two and within that list, it's at location zero.

So I run that, and it's printed camel so I've done that right.

Then it says to start using randomization with it.

So it says import the random integer function from the random library.

So from random import randint, okay so I've done that write some code that will select a random animal from the animals list hint, you will need to generate two random numbers that from within the list range.

So what I can do is I can put num1 and I can have a num2, that'll be my two numbers.

And then I can put num1 and num2 here.

Like so and now I'm just going to think, well, what am I going to write for my random integer so it's randint and then I put my range randint and I a range and what's the range going to be? So we've got three lists, it's going to be zero, one and two, so it's going to be zero to two and then we've got three items in that list.

So it'll be zero, two again and like I said, with other demonstrations that I've done with you, don't worry if you get it a little bit wrong that's what the testing is for.

So this is random integers so it should work is it's not random range where you would need the three of that.

So let's just see 'cause you've got to run it a few times because if something's wrong it might not be wrong straightaway because camel is still within that range.

But if I was out the range it wouldn't do called lion or tiger probably or it would miss the last list off.

So it's definitely done that final list 'cause we've got lion.

Now I'm looking for something in that third column and I have got Wren, so I know it's working and it hasn't come up with an error message at this point, 'cause it's not been out of range.

So let's just put it out of range if I just put zero to three there.

Now it's not going to throw up an error message straight away.

You see that first one worked, but the second one it hasn't worked because it's out of range because probably that random number that was being held was a three and there isn't a location three it doesn't exist.

So that's where you would know you'd got it wrong with that.

So randint and random range can be a little bit confusing.

Randint is where you put the actual range 'cause there isn't stopping point there whereas with ran range that final value is used as a stop point.

So it's not used within the sequence.

So you possibly got that wrong, you might not have.

But if you did, that's why.

So now my programme is working and that is what you should have done for that activity.

Let's start looking then at some other things that you can do with lists.

So we're going to start looking at changing items in a 2D list.

So first of all, let's see how we do it in a 1D list.

So to replace an item in a one dimensional list we specify the location and provide the value that we wish to replace it with.

So you can see there that is happening on line three so we've put the name of the list in the square brackets we've set the location of the item that we want to replace, and then we have now assigned cow there instead of dog.

But we can do the same thing with a 2D list we just need to specify the location of the item.

So thinking back to that last activity where you were putting the two values in those square brackets, you just need to do the same thing here.

So replace item in row zero at index location two with plaice, then we can append a 2D list.

So if we want to append or add to a 1D list then we state the name of the list and use dot append to add the new item.

And that's what's happening there it says add cow to the word list.

If we use exactly the same code with the 2D list then it will just add another element to the end list of end of the list structure so have a look what it does.

So animals dot append trout can you see there that it is got those three lists that are there originally but then it's added an additional element at the end so hasn't quite worked, how you would expect it.

If we want to append a specific list within the 2D list then we specify which lists so you can see now that you've got those square brackets there around the zero because we want to append the fish section of the animals by adding trout to that list.

So what you're going to do now is you're going to use what I've just shown you there to create a password manager programme.

And I want you to use the password manager section of your worksheet to apply your knowledge of 2D lists.

So it's going to take a little bit of thinking read those instructions very, very carefully.

Don't worry if you get things wrong do a bit of trial and error bit of printing to display what's in the variables or the list so that you can see what's going on.

And always refer to those code snippets that you've been given or maybe even think back to past programmes that you've made as well to help you with it.

So off you go pause the video.

Great so let's take a look at the code you could have used.

So you've given some stock code which is just this here they might have taken you a little while to just get that right.

And it's quite important that you notice things like where the commas are that isn't a comma on that final one there.

There's just commas on those other ones.

So sometimes you get a syntax error because you've put a comma there or sometimes you forget to put that final square bracket at the end it just takes a little bit of practise just to get used to the structure and just make sure you keep looking at the examples on your worksheet and then looking back on the programme just to see where you might have gone wrong there.

So I've got my list structure there.

It then wants me to add some code in so it says the incomplete code below should add the item Raspbian to the first list in the 2D list complete it and test it out by printing the list underneath.

So what I've got to do is I've got to do passwords and then I've got a thing really wants me to do it to the first list in the 2D list so that's the one at location zero.

And I've got a dot append and I've got to append it with Raspbian like that.

And then if I print passwords, then hopefully it should display it in that first item and it does so my first list there says Raspbian and then the other two are blank it then says write two more lines of code that will add the username pi and the password raspberry.

Okay, so I'm going to put in but I'm just going to copy that code there two times 'cause I want to put something at location one something at location two.

So this one was going to be pi and this one was going to be raspberry.

So let's run it again now just make sure it has done it.

And yeah, it has, you can see, you've got the main list here and then you've got the lists within that list there.

So it's definitely looking right at the moment.

Now the next task was starting to get a bit more challenging now it's saying to incorporate a while loop.

So this is your basic code for adding a username and password.

So imagine this is the account.

This is the username and this is the password to this list.

And now I've got to incorporate a while loop and it says continue to ask the user if they would like to enter a new password.

So then you've got to start thinking well, what kind of Boolean variable am I going to need so I can have not finished equals true and then I could say, while not finished.

And I'm going to want not this exact code but something similar to it.

So while not finished, I want to add these things in but then I've got to get some user input as well.

So let's just have a look.

If they say yes, it should prompt for the account username and password.

If they say no, then the loop should terminate.

After the loop is terminated it should print the entire 2D list.

So what I've got to do now is if they have asked for it so the first time it's running they're definitely going to want to be doing it so there we go.

So if they say yes, it should prompt for the account, username and password.

So what I'm going to do is I'm going to say, enter an account name, like so and then I'm going to put a count as my variable input like that and then I'm going to do print enter a user name.

And then user name equals input and then another one enter a password.

Password equals input, okay? And then I'm going to want to append it with the variable now, instead of that value that I've put there.

So I can put account, I can put username, and I can put password.

So that's all working fine now I could run this but I know straight away that I'm going to get an error.

Do you know what that error is going to be? In fact, let's just run it and just see what happens.

Okay, so I'm just going to put gibberish in we're just while we're testing.

Can you see what's gone wrong? So it's got stuck in a loop, hasn't it? So I need to figure out how to get it out of that loop then.

So you could have a question here.

Huh would you have it here? Well let's just put it here, Would you like to enter account details? Now if I have let's just have a look.

So if I've got to record the answer as well answer equals input, like so.

So I'm going to have also a Y and an N there.

And then I've got here, if answer is equal to Y.

Then I'm going to want to run all of this on time.

But if else sorry, else, not finished becomes false because they have finished so not whoops.

not finished equals false and not should break the loop.

But let's just see what happens, it should double check it, would you like to enter account details and I've put in a capital Y, I've put no validation in there or anything like that.

So going to make sure you put a capital Y.

So if I put a capital Y, it should allow me to enter things.

And it does.

And then it asks me again, would you like to enter so I'm just going to do it one more time.

And it says, would you like to enter account details this time, I'm going to put an N Yeah, and it does it its loop terminates, and it prints those passwords on the screen, I think.

So the next bit was to access the item as well.

So I've got that part of work in what I've got to do now is going to access it.

So I've done that bit, and access an account username and password to incorporate extra functionality to your programme, after the user has entered all of their passwords it should no longer print them at the end.

So let's get rid of that.

In fact, now I'm going to keep it just for testing purposes.

So I'm just going to put a hashtag in front of it at the moment, oops, that's at the end onto the front, there we go.

Scroll down a little bit.

So it says ask the user which account they would like to reveal the password for, okay.

So, okay, and then allow the user to enter the account name.

I'm going to call this account name 'cause I've already got an account variable, okay and searched for the account name in the account list which is list zero, and reveal the username and password for their account based on the found location.

So I've got to find a snippet of code to help me with that.

I have found the snippet of code now so I'm just going to paste it in.

Now, this snippet of code isn't the exact answer I've got to use it as a guide and I've got to modify it so that it works with my programme.

So I've got a variable called location and I'm going to keep that as it is because that's quite handy to call it location.

It's looking at the words lists.

Well, my list isn't the words list is the passwords list.

You've then got, it's looking at that first list in the location, which is what we wanted it to do so I don't need to change that.

And then we want the index method and we are going to change this 'cause we're not going to look for word, we're going to look for the account name that the user has just typed in.

Now, I'm going to print location as well just so that you can have an idea of how this is actually going to work.

So I'm just going to do a bit of an example first.

So would you like to enter something? Yes, I would.

So I'm going to put Raspbian, pi, raspberry, I'm going to add in another account, Google red, blue okay.

would you like to add another one no, and it's what is done there, I've asked you to print the list which the whole list which you can see on the screen.

And if you look here, we've got three lists within a list.

So in our two dimensional list, so the first list, so the list that location zero holds the usernames.

The second list, which is at location one holds the sorry, that one holds the accounts the first one holds the accounts and then the one at location one it holds the usernames and the wallet location two hold the passwords.

So now it says which account would you like to reveal the password for and I'm going to type in Google this time.

And it's saying one, so it's return the value one because it is found Google at location one within location zero.

If you look here, we've said look up password zero, so look at least zero, which is this one and return the location of whatever I've just typed in.

So Google, and that's what it's going to return it return that location one.

So this is quite important for us this value.

'Cause if you think about it, if Google is at location one in that first list, then if we look at the usernames the Google username is also at location one.

And then Google password is also at location one.

So we can use that value to find the usernames and the passwords in that list.

So what I've going to do now is think about the next bit so they've typed in their account name I've got something working out the location of where it appears in this first list.

And now I need to just say what the username or password is back to the user.

So I can have it's good idea probably to have another variable.

So I'm going to have username first username and then I'm going to have password.

And now I've got to think well, what have I got to type here? It's going to be passwords 'cause I want to look at the passwords lists.

And then I've got to think, well, what do I put for my first and second location.

So this first one here is saying which list to look at.

So the the username is in list location one so I'm going to put one there, and the passwords are at location two.

So I'm going to put two there.

And then this is saying the location of where it will appear in that list.

So I just need to put the variable location here.

And I'm just going to run it just to make sure that that's accessing it properly.

So I'm just going to type something in oh, wow, wow I pressed the wrong thing Oops yes, type something in username, password.

Would you like to enter more details? No, oh, gosh, f-d-s-a-f is the account.

And it didn't bring up any error message.

So I know, it's highly likely I've got that be right.

But until I've done the next bit, I'm not going to know for sure.

But it didn't throw up any error messages so that was good.

So now I'm going to display it back to the user.

So I've got print, I'm going to use an F string and I'm going to have account and account name.

And then an F string again so user name.

And then another one for password.

So now hopefully, it should all work as expected.

So I'm going to type in properly this time so they can rust in.

username, pi, password, raspberry.

Yes, I want to add another one Google, red, blue.

No, I don't.

Which account Would you like to reveal the password for Google.

And it says account Google, username red, password blue.

So it's now working.

Lets see what you've learned then see if you can make prediction about this.

So if Jenny is the input, what will be the output when this programme is executed? Take a look at it and see what you think.

Pause the video while you have a look.

Let's take a look there.

So the answer is it should have revealed Jenny's score is six Brilliant well done so you've learned an awful lot this lesson about 2D lists and how to access items and change things.

And then you've also created a password programme to starting to build now there's computational thinking skills breaking down the problem and really start in to think about how you might go about tackling these problems a bit more independently.

And if you'd like to, please ask your parent or carer to share your work on Instagram, Facebook or Twitter tagging at OakNational and hashtag LearnWithOaks will be great to see what you've been getting up to.

And I'll see you again soon for next lesson.