video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher and well done for coming back to Lesson 4 of this Python unit.

For this lesson, you're going to need your Replit account and you should have set that up already with your parent or carer's permission.

I also recommend you having a pen and paper nearby if you need to make any notes where you can.

And where possible, remove as many distractions as you possibly can out of the way, so that you can really focus on this lesson.

Once you've done all of that, we can begin.

In this lesson you will use iteration and it's for loops this time to iterate over list items. And you'll practise using common operations on lists and strings.

Let's start with a question then.

Take a look at this code and see if you can make a prediction about what will be displayed as output when this programme is run.

Now, it does contain a new piece of code that you won't have seen before, or you might not have seen before.

So it might be tricky to make a prediction.

But what I want you to do is just try and think about it.

Don't worry if you get it wrong.

It's completely normal to get things wrong especially when you don't know what the code does.

So see if you can make a prediction about what the output might be.

Pause the video while you have a think about that.

So the answer is this, because if you look at line four, you've just got that print function and it's printing the word buy on the screen.

And then you've got a for loop.

And what that for loop is doing is iterating through the list of items and then it's displaying each item on the list.

So each time it loops around it'll display the next item on the list.

So it's a for loop and this is the basic structure and syntax of a for loop.

So you say for and then a variable name in list or it could be something else.

It could be string.

So for something in a something, do this block of statements.

That's basically what we're saying.

And for loops are convenient for iterating over the items of a list.

And yeah, you can also iterate over strings as well.

You can use for loops for other things as well, but that's where they're most convenient to be used for.

So here's another example.

Take a look at this code and see if you can make a prediction now about what the output might be when this programme is executed.

Pause the video while you think about it.

So that's the answer.

Were you right? Hopefully, you're starting to see a little bit more.

You might still be not quite sure about what for loops do, but what it's done is iterated through that list of items and it's displayed each item in that list on the screen, one-by-one line-by-line.

So what do you think this one's going do? Something a little bit different now, 'cause you've got an arithmetic expression there as well.

You're incrementing the variable count.

So what do you think is going to happen this time? Pause the video while you think about it.

So it's going to to display the value four.

So if you look, the count variable is initialised to zero and it's incremented by one for each item in the list.

So it's looking at each item and we haven't asked it to print this time.

We've asked it to count something this time.

So we've just given it a different instruction.

Let's take a look at the next one.

So now we've got something slightly trickier going on because we've got an if statement in there as well with another condition for us to think about.

So pause the video probably for a little bit longer this time while you think that through and try and make your prediction about what might happen.

So here's the output.

The output is two.

So it says the count variable is initialised to zero, just like it was last time, but it's only incremented by one when the item in the list is greater than three.

So that's why it's only two this time, because if you look at that list you've only got four and six that's are greater than three.

So the count is going to be two at the end of it.

Here's another one then.

Again, slightly trickier.

These are getting a little bit harder each time.

Take a look and see if you can make a prediction about what this code is going to to do.

So you have to think, try and remember what append does and think about how that's going to work in the programme.

Pause the video while you think about that.

So it's going to display that list at the end.

So just take a look.

Selection is initialised to an empty list and then the value of each dice roll in rolls that is greater than three is appended to selection.

So if you look there, you've got the variable dice and it says for dice in rolls so that's looking at the item on that list and every time dice or the value that's held in that item that it's looking at is greater than three, it's going to append that value to the list.

What we're going to do now is we're going to walk through that programme so that we can look at it in a little bit more detail and just step it through so that we can try and increase our understanding about how a for loop actually works.

So let's take a look at this programme.

First of all, it's going to initialise the rolls list with those four values.

So you can see our abstract diagram there of a list and you've got all those items at those locations.

We're then going to initialise the selection list.

And that's going to be blank, because we've just got square brackets, we've not added any values to it.

And that's so that we can refer to it later on.

And then, our for loop begins.

So for dice in rolls means it's going to start looking at the items in that list.

And we've made a variable called dice which is going to be used to iterate through those items. And with that variable dice, dice is going to hold the value of the list item that it is currently at.

And you'll see this, it'll start to make a little bit more sense, the more we get through.

So the first time we look at dice it is holding the first value in our list which is one.

We're then going to go onto this if statement.

So now it's saying, if dice greater than three.

So look at what's being held in dice at the moment and that value is one, and is one greater than three? Is that going to be true or false? It's going to be false.

So is line five going to execute? No, it's not.

So what it does now is because there's still items left in the list to iterate, it's going to go back and it's going to check that and see what happens next.

So it's already at list item zero and now it's going to move on to list item one.

And the value of list item one is now being held in dice.

So now we look at the condition again.

So we've got if dice greater than four.

So is that going to be true or false this time? It's going to be true this time.

So is line five going to execute? Yeah, line five is going to execute.

So we've got there, selection.

append dice.

So now, the value that's been held in dice is going to also be held in the list, the selection list at item location zero.

And we've still got more to go.

So we go back at the top and it checks are there more items in the list to look at? So yeah, there are, because it's moved along to the next one and it's now looking at item two.

And the value being held at item two is three.

So now that is being held in the dice variable as well.

So we go to, if dice greater than three is that going to be true or false? It's going to be false, because three isn't greater than three, is it? So is line five going to execute? No, it's not.

So it's going to go back up again and it's going to iterate again to the next item in that list.

So the next item contains the value six.

So the value six is also assigned to the dice variable.

And now, we can do that condition again.

So if dice greater than three is that true or false? It's true, isn't it? So line five is going to execute and the selection list is going to append with whatever value is currently being held in dice, which is six.

So now our list has two items. It has a four and it has a six.

Now it doesn't just break there It has to go back to the top to check if there are any more items in the list.

And because there aren't any more items in the list the loop is terminated and it goes to the next line of code that hasn't been indented.

And then we display that list to showings four and six.

We're now going to look at this novel.

So the novel is the 1939 novel "Gadsby" by Ernest Vincent Wright, is a lipogram, which means it's missing a letter.

A note at the end of the book claims, Not a word containing the letter E has appeared in this story of over 50,000 words.

And what we're going to do is we will make a programme to check this claim, to see if it's actually true or not.

So this is our start code and then we're going to use live coding to actually see if we can get it to work.

Now like a lot of these projects that we're doing in this Python unit, that first line of code, that import is something that has been designed for you just for this activity.

So it's not a piece of code that you can just put in any Python programme.

You'll need this starter programme if you're going to actually use it, the Replit one.

So let's take a look and see if we can get it to work.

Here is that programme in Replit then, and if you remember from Lesson 3, you'll notice that you can actually go to the files and actually have a look at them.

So this is the "Gadsby" text.

If I double-click on it, you can see the entire text there.

So you can see this is what we're actually going to be searching through.

And that's what this line of code does, it imports the text file into words.

And then, this second line of code is actually creating a list.

And each item in the list is going to be a word from that "Gadsby" text file.

And we can actually check that out by just printing word list like that.

And see, it's probably going to take a little while.

There we go.

So there's all of the words in the "Gadsby" book and there, each of them are now our list item.

So we don't need that anymore, I can get rid of that, and I'll just clear that screen.

So that's just to show you that, that line of code that second line of code there is generating a list of words and it's all of the words that are in that book.

Now the book claims to have 50,000 words.

So what can we do to find out if it actually does have 50,000 words? What code can we use to find out the length of the list? What do you think? So what we can do is we can use len can't we? So if I make a variable called length and then I use the len function and I do len word list, then that's going to calculate the length of the word and what I can do as well is I can just print length and then, I can test it out and see let's see if it is 50,000.

Let's run it.

Taking some time to to calculate the length of that.

Oh, there we go.

So it's actually over 50,000, is 51,957 words.

That's an awful lot of words.

So we've worked that out.

But now what we need to do is we need to figure out well, do any of those words contain an E? So we've got to think back to the code that we've used before, where we're checking if that string E is anywhere in there in those items. So first of all, we're going to have to iterate over the word list.

So think about the code that you just saw with that for loop.

How might I structure this code? So I need to do for word, or you could put item, if you wanted to.

So for word in word list and then I end it with that colon.

So that is my structure of the for loop there to get it started.

And then I've got to think, well, what actually needs to happen within this? So now we've got to think, well, how did I find out if a certain character was in a word, what did I do? I have to use that in operators.

So I'm going to need an if statement, I've got if so we're looking for an E.

So if E and then we're using the in operator again.

So if E in word, because what we want to do is we want to access the item that it's currently indexing through, if you remember.

So that's what this variable does here, it's looking at each item in the list, it's iterating through each item in the list and then we can use that within our programme to access it.

So if E in word, what do we want it to do? Should we just get it to print it? I mean, we could get it to count it, but let's just get it to print it.

So print word, and then we'll be able to see if there are any words that contain an E.

So let's run it now and see what happens.

Might take a little while.

Oh, didn't take too long.

And there we go.

So we've got one, two, three, four, five, six words that contain an E.

So they were right in saying that we're over 50,000 words but actually they were wrong, because there are all those words there that contain an E.

So what we've just done there is we've used a for loop to iterate over a massive list to do a little investigation.

Let's just recap then.

So we had that piece of code and you had those two important sections that we looked at there.

So first of all, we looked at the length of the word list, so how many words were actually in the book? And then, we iterated through all of those words to find out if there was a word with an E in it.

And there were, there were six words with an E in it.

Now what you're going to do next is you're going to take a look at your worksheet and you've got the section there, that's called English words.

And what you're going to do is perform a similar task to the one that I've just shown you.

And it's all to do with a different list this time.

So remember, when using these worksheets they're going to be quite overwhelming.

There's going to be lots of things on there but that's all there to support you and help you and make sure that you can complete the task.

So make sure you're reading it really carefully and looking up the examples that are there for you and the clues to help you succeed.

So take a good look at that, have a go at the exercise and then we'll look at the solutions when you're done.

Pause the video while you get on with that.

Let's look at Task 1 solution then.

So you see that section there of code that's highlighted and that's going to count words of a given length.

It's going to iterate over the N words and increase the count when the length of the current word is equal to the given length.

So did you get that right? If you didn't, don't worry, you can just copy that now and see if you can get it to work in your own programme.

Well done, if you did get it right though, that's good going.

Let's look at the next task then.

So for Task 2, those are the sections that you had to have a look at.

And it says, collect words that contain a given string.

That's what it should be doing.

Iterate over the N word and append to the collection when the current word contains the given sub-string and print every word in the collection.

So how did you get on with that one? How did you find that one? Was it challenging, too hard, okay? Hopefully, it's not too bad.

But yeah, again, if you did struggle with it you can always enter that code now into your Python programme and see if you can get it to work at this point so that you can start to figure out and understand how it actually worked.

But again, don't worry if you're not getting these exactly right first time, you're still learning, okay? So don't panic if you're getting a few of these wrong.

You can always go through these solutions afterwards and see if you can get it to work with some help.

Your next task is to look at creating a heartbeats programme.

And so far, you've used for to iterate over lists of textual data.

In this activity, you will iterate over lists of numbers.

So let's take a look at the start programme before you get started on that worksheet.

So here is the programme that you're going to be asked to look at when you look at your worksheet.

And this is what it looks like.

So it's, again, it's looking for an NCCE folder and it's importing from the module, it's importing the load and plot functions.

So that's quite important.

And you can see you've got the NCCE folder there and what it's looking at, it's looking at this text file here.

And we've got there, heartbeat data equals load 100.

So if we actually look at the data, there is a lot more than 100 values in there.

Wow, that's an awful lot of values.

It's a huge text file.

So just to get started, we're only going to look at the first 100 values that are in that text file.

And then, what this can do is it's going to plot a graph based on heartbeats when it is run.

So if I go to run, takes a little while, just wait for that, there we go.

So it's done it now.

And then, if I go to heartbeats.

png what it's done is it's made a graph of that data.

So all of those numbers, those first 100 in that list you've got there some data, and that's just showing the first heartbeat that's on here.

So if I go back to main, you'll see that I can actually change the value.

So if I didn't want to look at the first 100, if I wanted to look at the first 200, if I run it again, just takes a little time.

Then I can go to the graph again, and you can see now that it's looked at the first 200 items on that list there, the first 200 bits of data from that heartbeat, and you can still only see one heartbeat on there, but it's just plotting a graph every time based on the value that I've put in here.

And if I wanted to see that just as data, rather than on a graph, I can just do print heartbeat data like so, and then, I can run it and you'll see down here it should print all 100 values.

And it has, because you've got a list there, heartbeat data list, and I've just printed the heartbeat data list, and those are all of the values in that list.

And you can see some are minus and occasionally you'll see a positive value.

There we go.

There's the positive values, you have a look.

And when it goes above one, if you take a look on here there's.

Sorry, above zero, if you look here, when it goes above zero, we know that a heartbeat is going to be detected.

So your programme that you've got to make is to use that list, if I just get rid of that, use that heartbeat data list, you've got to iterate over it and find out when there's a positive value in there, because when there's a positive value, then a heartbeat will be detected.

So take a look at the worksheet, follow it through carefully.

And remember, the solutions will be there at the end if you are struggling, but do try lots of different things to see if you can figure out what bit of code you're going to need in order to do that.

And don't forget, read that worksheet really, really carefully.

Pause the video while you complete that section of your worksheet and then, come back when you're done.

On the worksheet then you were given some of this code for you.

So the for value in heartbeat data, so that it'll iterate through that heartbeat data list.

And it'll look at each value that's held in that each item in that list.

And then, it also gave you that little bit of code at the bottom, previous equals value.

So it made a new variable and assigned the current value that it was on in the list.

And this is one way you could have solved it.

There's two ways really that you could solve this problem.

So you might have decided to do a nested if.

So that it looked at if the value is greater than zero.

So first of all, it had to check that.

So the current value that it's on in that iteration, if that is greater than zero then it's going to do another check.

So if you look, we've now got another selection statement within that nested inside.

So if the previous is less than zero so that previous value was assigned at the end of each iteration in that loop with the current value, and it'll check if that was less than zero, because if those two conditions are met then it's going to print heartbeat detected, because it's gone above and below zero, making a heartbeat.

So if we take a look at the other option, it is this one.

So if value is greater than zero and previous less than zero, then print heartbeat, because that's also going to show heartbeat as well.

So there's two different ways you can do it.

You can do a nested if, so you can have an if with an if inside it or you could use an and, and check both conditions within that one selection statement.

So that's just two ways that you can do it.

Let's take a look then at this for loop.

So what's this for, let's just have a little bit of a recap, see what we've learned in this lesson.

So what do you think will be displayed on the screen when this programme is executed? Hopefully, this is looking a little bit easier now.

What do you think? Pause the video while you think about that? So the answer is, and it's what it's done is it's iterated through every character in that string, and it's displayed every character on a new line, because each line is a new print.

So well done if you got that right.

And then note, you can use for to iterate over anything that has individual elements.

So in this case for iterates over the characters of a string and we've also used it to iterate over list items. Let's take a look at this one then.

So this was, this is an Explorer task.

See if you could think about this, this is a tricky one.

Okay, I'm warning you now, It's a tricky one.

So it's a little bit of a head-scratcher.

What do you think will be displayed on the screen when this programme is executed? And if you notice, you've got a for loop with a for loop indented inside it, okay? So try and make a prediction.

It's an Explorer task.

It's meant to be a big challenge, this one.

So see if you can have a think about it.

Pause the video while you make your prediction.

So there's the answer.

Done exactly the same thing.

So it's broken it down, 'cause if you take a look at it you've actually got, it's not a one string anymore, it's not a piece of text.

What it's done is if you look at the list it's still got the word lipogram, but the string has been broken down into items in that list.

So it's looking for the syllable.

So li, po, gramme.

If you don't know what a syllable is, it's the sounds that the word makes, So, li, po, gramme.

And then, it's done for each syllable in syllables.

So for that, for the list syllables it's going to look at each syllable.

And then, you've got another for loop within it.

So now it's going to look at just L-I in that syllable.

So it's going to iterate through that and then, it's go to the next one, po, and then, it's going to put P-O, and then, it's going to look at gramme, and then, it's going to look at G-R-A-M.

So it's actually done exactly the same thing but in a different way.

Mind blowned, has it blowned? You can always copy and paste this code into Python and just see if actually works, 'cause I bet, you don't believe me.

It's true, it does work.

Nesting for loops within another for loop is quite a tricky thing to do.

And as you get more experienced with programming maybe when you start doing GCSE programming you'll start to use that a little bit more, but hopefully, it wasn't too bad and it made a little bit of sense.

So this has been a very, very packed lesson and it's given you all the grounding to start looking at a bit of a mini-project in Lesson 5 and 6.

Hopefully, it's prepared you for that.

And I'd really love to see what you've been up to this lesson.

So if you'd like to, please ask your parent or carer to share your work on Instagram, Facebook, or Twitter tagging @OakNational and #LearnwithOak, 'cause I'd really love to see what you've been up to.

And I look forward to sharing the next lesson with you which is Lesson 5.