video

Lesson video

In progress...

Loading...

Hello and welcome back to Programming Part 5: Strings and Lists.

Now we've dealt with strings now, so now it's time to move on to lists.

So I'm Ben, I'm your computing teacher for this lesson and this is my final lesson with you before I hand you back over to my colleague, Rebecca Franks.

So, let's make the most of that, let's get started.

All you'll need for this lesson is your computer and a web browser.

You'll also need access to the Replit account, so please do ask your parents and carers for permission before accessing that, okay? Then if you can clear away any distractions that you might have, if you've got a nice quiet place to work, that'd be great.

If you could turn off your mobile phone then do that.

And it's always helpful to have a pen and paper up and ready, okay? So once you're all set and you've done all that and you're ready, let's get started.

Okay, so in this lesson, you're all going to complete all those things that you can see on the screen.

So you'll be able to define a data structure.

You'll be able to define a list and an array.

You will be able to describe the differences between lists and arrays.

You'll be able to use a list in a programme.

And then you'll also be able to append to a list.

Wow, what a lot of things, okay? But don't worry about it, you got this, I'm here with you I'm sure you'll be absolutely fine, okay? So let's get started then.

Now, you know the drill by now, we like to start off with a prediction, okay? So, let's have a look at the code that we've got here.

So, words equal square brackets, house, mountain, sheep.

Print words, square brackets two, okay? Close square brackets, close brackets.

Now, before you just rush into an answer here, okay, and work out what will be the output of print when this programme is executed, think back to those string operations that we learned about.

For example, if we were to say print a string, get square bracket zero, what would that actually do? Okay, so that might help you with that because we've looked at index positions.

So what do you think it will be? Do you think it will be number one, mountain, number two, sheep, number three, house comma mountain, or number four, U, okay what do you think? So give me an answer, shout it out.

So it was either one, two or three or four.

So three, two, one, it is number two, it was sheep, okay? So why was that? Now, the idea is that it's index location at position two.

So we got print words at the index position two.

Now, thinking about our string things, you might, string operators, sorry, you might have, you might've thought that it would have been looking at the first string, okay? So H and O and U, and therefore it'd be U because that's the second position of the first string.

But when we're looking at it in this context, when each one is separated by a comma and we've got quotation marks around it, we're looking for the item for the second position but that's what we're going to explore in this lesson.

So let's start off by talking about data structures.

So in computer science, we often need to manage large amounts of data.

Now we need to know how this data is stored and the types of operations that we can perform on it.

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

Now data structures can be static or dynamic.

So, static data structures reserve memory locations for a set amount of data, and that's really important.

So their size cannot change, whereas as you can imagine what the answer for dynamic data set is, is that it's a lot more flexible because memory capacity is not fixed and therefore the size can change, okay? So, an array.

Now, an array holds multiple items under one name as we can see there, you've got digits and then you've got zero to nine there.

Now an array is a static data structure and if you think back to the previous slide, what we mean by that is that it is therefore a fixed size, okay, and it can only contain data from the same data type.

So contents can be changed, but they can't be added or deleted.

So we can overwrite data that's in there, but we can't add on at the end of it or delete an item from there but we might overwrite it with maybe if this was a list of integers, maybe we'd just replace it with zero, I don't know, okay? Now the structure must remain fixed.

Now more commonly in Python at least, we use things called lists, okay? Now a list is used as a dynamic data structure.

So this means that the size can change.

It can also contain data of different types.

So in the array example we looked at before, it was just integers whereas you can see in this example, we have a two strings and an integer and that's absolutely fine with a list.

So this means that you can add and delete items as well as perform other operations on a list that are different to an array, okay? So in Python, we define a list, as we see here in the example.

So what you do is you put your variable then equals and then you define it using your square brackets and then you've got your items, okay? So if there's a string, you put quotation marks around it, if it's an integer, you don't need to, okay? And then you separate each value with a comma.

So this list is dynamic and it can be changed.

We can perform many types of operations on a list in Python.

And we'll explore those over the next few lessons.

So in Python, we can use a list as an array, if we need to.

We just need to make sure we follow certain rules.

And those rules are that each element is of the same data type and that we don't add or delete items from the list, okay.

So let's have a look at this example, so this is a list and it's a list of days, okay? So you can see that each item is separated by a comma and we've got Monday to Sunday.

So here's a list that we use to store days of the week.

Now, in this example, the list items are string literals, okay? And what I mean by string literals is that they are pieces of text, so they need to be in quotation marks.

So, when a programme is executed, this is what the list will look like in memory, okay? Because they're separated out like that.

So, let's note that just like we did with our string, each item number starts from zero, 'cause the list is zero-based.

So each item has a unique index position starting from first item always being zero, okay? And the, obviously the unique index position denotes its position in that list, okay? So let's kind of make that crystal clear and go through some examples.

So now we've added code to the left-hand side.

Here you can say print days up to position zero there, okay? So, this is, when this programme is executed, what will be displayed on the screen? So days at position zero.

So what's at position zero, okay? Hopefully you can see that it is Monday, okay? 'cause that is a position zero.

Next one then.

What about days at position one? What's the position one in my list? It is Tuesday, okay, you're getting the idea.

So let's make it a little bit harder.

What about days at position four? Okay, it is Friday.

What about days at position seven? What about that? What do you think will happen, okay? Well the answer is it would give me an IndexError, okay? Now, if you look at the, my list, my list only has, although it has seven positions, we're starting from zero, so zero to six, okay? So print days at position seven, there is no position seven, seven.

So if you ever come across this error, don't be worried about it.

So an IndexError happens when is trying to perform an action for an index position that doesn't actually exist or it can't find, okay? So if you ever see that, don't worry.

It just means you've got the wrong index.

So what do you think would happen here? So expressions can be used to access items in a lists as long as they evaluate to an integer.

So, previously we've been putting integers into here but what I'm doing here is I'm passing a variable into here, okay? So let's have a look at this one.

So our variable value is three, okay? So when it's executed, we're passing the value of a day into there, which is three, and that's linked to position three in my list, therefore the output would be Thursday.

What about this one then, slightly trickier, but you should be able to work it out.

So what do you reckon? Print days at position day minus one, okay? So what's day minus one? Well day is three, minus one becomes two.

And then what's at position two, it is Wednesday.

Okay, perfect.

Right, so now you've got all of that.

What I'd like to do is have a go at the first task on the worksheet.

So I'd like to head over to task one on your worksheet, to create a Simon Says programme, okay? And like always, use your worksheets, okay, to help you because there are some code snippets on there which will remind you about all the things that we've just learned about there and also helps you put to practise the skills that you need to be able to complete the Simon Says projects, okay? So have a go at that, have fun with it, and then once you're done unpause and I'll be here when you get back.

Okay, so how did you get on with that? So what I thought we'd do, as always, is we'll go through the solution to the problem, okay? But again, like always, then if you completed it, then definitely listen to this just to make sure that you did the same.

But if you didn't, then stop the video at the point where I've maybe helped you become a little bit unstuck, okay? And then go back to your solution, see if you can add to it and correct mistakes or get, you know, just get it unstuck, anyway.

So let's go through with the answer then.

So, ignoring the input, we'll come to that in just a minute.

First of all, we needed a list, all right? So we had Simon Says equals, square brackets, and then inside the square brackets I'm putting all my Simon Says instructions.

Now, first of all, the key things here is I'm separating each value with a comment.

And because they're all strings, they must be in between the quotation marks, okay? Then we've got intros, so we've got the intros that always says Simon Says, okay? So that's one value or blank, okay? Because obviously when we play Simon Says, we've either start with Simon Says, or we don't have an intro at all, okay? So, that gives me the two options there.

Then what we do is we enter into a loop, okay? So for X in range 10, for example here, okay? So for X in range 10, because we want to play 10 times.

First of all, we're going to have a value called a, sorry, a variable, sorry, called index.

And then that's going to generate a random position to run a number between zero and four.

So why is it between zero and four, okay? Well, because I've got one, two, three, four, five items in my list, but remember, we start with position zero.

So this is position zero, this is position one, this is position two, this is position three, and this is position four.

So it's going to generate a random number that we can use to find in the position of this.

So a random number, so it's going to get a random instruction, okay? So it's between zero and four and then we're storing that in the index.

And then what we're doing is we're going to instruction equals Simon Says and then the index position.

So we've now going to instruction this thing and generate a random number and pick around that.

Use that random number to pick something from this list randomly, okay? Then what we've got is we've got intro equals choice of intros, okay? So that means it can select something from there, okay? Then we'll just put in a print statement in it so it's going to use the intro and it's going to use the instruction and then sleep for three seconds, okay? So let's see if this works, run this.

So hands on head, so I'm not going to do that 'cause Simon hasn't told me to do it.

Simon Says, hands on head, so it's use same one.

Simon Says, a left hand up, I've done it yeah.

So Simon Says right-hand up, and then Simon Says hands on head, done that.

Simon Says right hand up, and then the programme stops, okay? I think I did that, didn't I, got it right.

I wasn't, oh no, it hasn't finished yet, right.

Right hand up, hands on shoulders, it did it 10 times, Simon says hands on head, ugh, there we go, okay.

Oh, that was so close, but maybe it got a bit carried away, okay? Right, so anyway, so you've done that.

Why don't you test it out with people in your house with you, see if you can get them to play the game with your programme, okay? So if, like I say, if you've solve that problem already, let's move back to slides, a big well done.

If you haven't, then hope this helped you get a little bit unstuck, so, maybe pause your video, minimise this window and see if you can go back and solve your problems, okay? Right, let's head back to the slides then.

Okay, so let's now do a bit of a recap of what we've learned so far.

So my first question, really easy.

Are lists static or dynamic.

Okay, so three, two, one, they are dynamic well done, okay? Next one, what makes a list dynamic? So what are the features of a dynamic data structure? Well, lists are dynamic because you can add and delete items and you can use things with different data types, okay? So that's what we're going to explore now.

We're going to explore those operations that we can complete on a list by looking at adding something to it, now we call that appending.

So lists are dynamic because we're going to add new items to them during the execution of our programmes.

Now, adding a new item to the end of a list is known as appending, okay? So it's appending the item to a list.

When you hear appending in this case, it means adding, okay? So for example, here is an empty shopping list, okay? So, shopping equals open bracket, open square brackets and close square rockets.

So we're, therefore we're defining an empty list and we've named it shopping, okay? Now we can add new items to the end of the shopping list by using the append operation.

So you can see there, we've got, it starts off with an empty list and we've got shopping dot append and then now in circular brackets, we're putting bread, okay? 'Cause that's the argument that that one takes.

So when we print the list, we can see that the first is empty and then it holds the item, bread, okay? So let's move on to this code.

We've got shopping equals empty, so it's an empty list.

We're printing shopping.

So you can see that it's empty there, okay? Then we're doing shopping dot append bread.

So we're adding bread to it, print that same list again.

And you can see now it has an item in it, okay? So, again, you can see there, if we want to then add another item, we can do shopping dot append and then cheese, okay, and then if we print it again, you can see it's now got two items. And it's adding onto the end of the list.

We can also remove items from a list by using the remove operator.

And you can see that the first, first, the less contains bread and then the item bread is removed from the list, okay? So my, I have bread, cheese, milk and then shopping dot remove bread and if you look at it now, print it again, I've only got cheese and milk left.

So, after bread is removed from the list, what index location holds the value cheese, okay? So what is the index location of cheese? So the answer was zero, okay? So the index position refers to the position in the list and there's not a direct link to the value held in it, okay? So, we can see that cheese was at position one, but because we deleted bread, if we look at positions zero, it is now cheese, okay? So, what I'd like to do is have a go at appending and removing to a list.

So I'd like to head over now to Task 2 on your worksheet to practise that appending and removing items from a list.

So again, pause the video, have fun with it, enjoy the tasks, and when you're done, you can unpause the video and we'll go through the solution.

Okay, so as ever, we're going to go through the solution to the problem that you just solved.

And of course, again, as ever, if you solved it, then please do listen to what I've got to say, but if you didn't solve it, and you just got a little bit stuck, then just listen to what I've got to say and if anything that I say really helps you get unstuck them, pause the video straight away, minimise this and just have a go yourself, okay? So what I do is I start off with an empty shopping list.

So I start off with my variable shopping, and I create, initialise this empty list, okay? So I've got square brackets, close square brackets, that initialises my list.

So then I've got items_left equals true, so that's a boolean operator, so we then use that.

So while items_left, now items_left is true, therefore saying while true, okay? So that's going to keep iterating until that condition becomes false.

So my first question here is, it says all print statements, it says, would you like to edit your shopping list, Y or N, okay? Now what I'll do here is, I've got a variable answer.

I'm asking for the input, but like I've done on previous projects, I'm using dot upper.

So what that's going to do, if they put lowercase Y or lowercase N, then it's going to convert it to upper case and I don't need to worry about whether or not it's uppercase or lowercase, because I've done that converting for them and I can guarantee from now on whatever value is an answer is going to be an upper case.

So I'm going to say if the answer is no, so equals no, so double equals, if that equals N, a capital N, because obviously it's going to be uppercase, then what we do is we break our loop or we change the condition, sorry.

So items_left equals false.

And therefore when we iterate around again, okay? When we check this condition, that's now evaluating to false, okay? So therefore we remove out the list and we get to print shopping.

Let me just run through this programme again and add these things.

Do I want to add to my shopping list? Yes, I do.

Would you like to add or remove? I'd like to add and I'm going to put my in cake, okay? Would you like to carry on? Yes.

Would you like to add or remove? I'm going to put my candles in.

Okay, would you like to edit your list? Yes, because I'm going to remove the candles, R, I'm going to type it right, this time no typos, okay? So hit enter, and there we go, so that seems to have worked.

Would you like to edit your shopping list? No, so so far I've added cake, I've added candles, but then I removed candles, so that should leave me just with cake.

So let's see what happens when I put no.

That should, therefore, when I type in N, it should change this to false, therefore when we iterate around again, it's going to, that's going to evaluate to false so we're going to exit the loop and then we're going to move on to print the shopping list.

So, and there we go, it's cake, perfect.

Now the error that I came across before, I promise you it wasn't a deliberate error.

However, it might make you want to expand on this programme a little bit.

Think like how can I handle that error? I don't want to just have that error.

I'd like to just be handled off and crash out the programme.

So let's just run this again really quickly.

Yes, I'd like to do that.

I'd like to add an item, and I'd like to add cake, okay? Write a yes, I want to remove an item.

And I'm just going to make a spelling mistake.

Look what happens here.

So what we've got is a value error.

So that's the bit that's really important, value error.

So maybe if you were to use try and capture, try and except a value error, you might be able to handle that rather than it ruining the programme.

So maybe have a go at that if you want to, okay? So, like I say, if you managed all that, then great and well done, okay? So we're going to head back over to the slides now.

But if you want to pause just to either correct your programme or just make it a little bit better by maybe adding the try caption, maybe putting it into a sub routine as well.

That might be something else you can do, okay? So, let's head over back over to the slides now.

Okay, so, so far we've looked at lists and also we've looked at one type of operation that we can, or two types of operation that we can use on them such as append and remove, but there are lots, there are many of operations that you can perform on lists, okay? For example, we can do append as we saw there, we can use something called insert, which then we put into it.

The item that you want to insert but you can specify the index position that you want to insert it at.

There's pop which means a pop and then the index.

So remove an item at an index position.

You can remove an item by specifying the actual item you want to remove.

You can find the index position for an item.

You can use the count to get occurrences of the item.

You can use reverse which is, which reverses list, puts it in reverse order, and the list dot sort that sorts the list into an order, okay? Either ascending or descending, that kind of thing.

Okay, so there are lots of things that you can develop.

Okay, so here's some examples.

So numbers dot append 42.

Cities dot insert two Oslo and so on like that, okay? So you can see is all those things that we talked about there, but in like a real practical purpose using real values.

Okay, so that's all for this lesson.

And as I mentioned at the beginning of the unit, this is your last lesson with me before I pass you over to my colleague, Rebecca Franks, okay? Where you'll be in safe hands I promise.

So, I hope you've really enjoyed the last section of lessons.

I've really enjoyed teaching it.

And I would really love to see the progress that you've made.

You should feel really confident and really proud of what you've achieved.

So if you'd like to share that with me, that'd be absolutely wonderful.

So please ask your parents or carer to share your work on Instagram, Facebook or Twitter tagging @OakNational and using the hashtag #LearnwithOak.

So I hope that you enjoy the remainder of your programming lessons, and good luck with it.

So I'll see you soon.