Loading...
Hi, I'm Rebecca, your computing teacher.
And well done for coming back for lesson three of this Python unit.
For this lesson, you're going to need your Replit account, which you should have already set up with your parent or carer's permission.
You're going to need a pen and paper to make any notes that you might need to and also to remove as many distractions as you possibly can so that you can really focus in this lesson.
Once you've done all of that, we can begin.
In this lesson, you will use iteration or while statements to control the flow of programme execution.
You'll practise using common operations on lists and you'll perform operations on strings.
Let's start by looking at some code then.
Assume that passwords is a list of common passwords sorted according to popularity.
The more common passwords are earlier in the list.
Now, note that this programme here that has got a module there that is importing, so from ncce.
data, import passwords.
So it's importing a module, it's for importing a function from that module.
That module is a module that's been created for this task.
It's not one that you can just use in any Python programme.
So just bear that in mind when you're doing this.
What I'd like you to do is based on your knowledge of what I've told you about this list of passwords, I want you to fill in the gaps with the gap with an index so that the programme displays the most common password.
So think about what index location is going to have the most common password.
So you might want to pause the video while you think about that and I'll reveal the answer in a few seconds.
Off you go.
Let's look at the answer then.
So the answer was zero.
So if you look at the question again, it says the more common passwords are earlier in the list.
So the most common password is going to be right at the beginning of that list.
And it's going to be at location zero.
And hopefully that's reminded you that when we have lists, they have index locations and those index locations always begin at zero.
So it's important to remember that one.
Let's take a look at this one then.
So, same programme, just a little bit more has been added to it.
And what I want you to do is fill in the gaps so that the programme also displays the second and 10th most common password.
So think about that index order now.
Now what you know about the zero and think about what the second and 10th.
So just pause the video while you write those numbers down.
Awesome.
Let's have a look.
So the first one was one because that would be the second location, wouldn't it? I know it gets a bit confusing but it's because zero would be the first location and one would be the second location.
So that means that the next one is going to be nine.
So it's one less than 10 to find the 10th place.
Let's look at the next one then.
So again, assume passwords is a list of common passwords sorted according to popularity.
So we assume that as just as before when it says fill in the gap so that the programme displays the number of passwords contained in the list.
So you're going to need to remember that operation that needs to be performed on a list so that it returns how many passwords or how many items are in that list.
So see if you can remember, give yourself a little time, pause the video while you think about that.
Let's take a look.
What does it do? So it was len we were looking for there.
So len is the operation that you can perform on a list and it will return the value of how many items are in that list.
So, well done if you got that right.
Let's take a look at this one then.
So it says, fill in the gap with a condition this time, that checks if the list of passwords contains an item equal to password.
So take a good look at that programme.
What's it doing? It's saying password to check and then the user is typing in a password to check.
And then we're going to check if whatever they've typed in is in the list, whether it's a common password or not.
And if the condition is true, it's going to say common password.
And if it's false, then it's going to say not a common password.
So pause the video while you think, well what condition is going to be there where that blank space is on that if statement.
And you've got four to choose from.
So pause the video while you think about that.
Let's take a look.
So it was, if password in passwords.
So you've got a list of passwords.
We're checking if whatever's been held in passwords or whatever the user typed in is actually appearing in that list somewhere.
So that's really useful in operator there.
It's a really useful one for checking things like that.
Here's another one for you then.
We're going to find out how popular the password is that the user entered.
So we need to figure out which bit of code is missing from this programme.
It says, fill in the gap so that rank is the position of password in the list of passwords.
So let's take a look at this programme.
It's just a little bit longer than the previous one.
So it's still asking the user to enter this password.
It's checking if the password is in the list of passwords.
If it is, it's going to say common password but then it's also going to find out the rank.
And if you remember that list of items, that list of passwords is in rank order, isn't it? So the most common one is at location zero and the least common one is right at the end.
So what it needs to do is it needs to find out the location of the password that the user has just typed in.
So you've got four options there.
Take a look at those four options and think, well, what bit of code am I going to need in that gap there? Pause the video while you think about that.
Let's take a look at the answer then.
So it was B, so passwords.
index, and then within those brackets, the argument that we were passing there is the password.
So what that's going to do is it's going to return the index location, the value, for wherever that password is in that list.
What we're going to do now is we're going to do a demonstration in Replit and you're going to see this Python programme and I'm going to go through it with you and explain how it works.
We can take a look at this programme then a closer look now and if you want to look at this yourself, then you can just use that short link that is on the video and just type that in and you'll be able to access this too.
It does take a little while to load when you run it 'cause it got a few things installed, but once you there, you'll get the same screen as me.
So just to remind you, this one here has been written for you already.
This is not something that you can just import into any Python programme.
This is for this specific programme that's here.
And we've also got another import there which is going to mask my typing so that you can't see what I'm typing.
So it's going to turn all my letters into asterixis.
So it's going to put a mask over the top.
So it's going to say password check which should already is 'cause it's running.
And then it's going to want me to type in my password.
And then it's going to check that password from this huge list of common passwords.
And it's going to see what rank order it is.
And then if it's not a common password, it's going to say not a common password.
So I'm going to start with the really obvious ones like one, two, three, four, five, six.
So if I type in one, two, three, four, five, six, and press the enter key.
Okay.
So it's the most common password.
That was a good guess, wasn't it? So, because it says zero location, that means it's the most common password, isn't it? So if I run it again, I can try a different one.
So I know that QWERTY is another popular one.
So I'm going to go QWERTY.
See what rank order that is.
So that one's at location three.
So it's the fourth most popular password.
What about the second one is, who knows? Maybe password.
I have password.
That was a good guess.
So password is another one.
I'm going to put my actual password in now and I'm not going to tell you what it is.
Let's just see what rank mine is and if it's in there or not.
I can't even remember it while I'm talking and everything.
This is my super skill, well, let's see.
Not a common password.
So I'm obviously doing something right.
So this is quite a useful programme, isn't it? But it's using lists.
It's bringing back the in that index.
And it's checking whether what has been typed in is located in the list.
So it's performing some operations on that list and it's useful because it's telling us whether it's a common password or not.
And it's telling us the rank orders.
So, if you want to have a go with that as well, you can use the short link on the video and try it yourself as well.
We're going to look at another piece of code now and this one's to do with forming a band.
And that doesn't mean the next Bowie band or pop band or anything like that, it means a band with instruments.
So this programme reads the name of an instrument and adds it to a list.
This action needs to be performed repeatedly in order to add all the instruments required to form a band.
This approach won't get as far.
So how can we get our programmes to perform actions repeatedly? So have a think about that.
What do you think? What do we need to use? Let's take a look.
So you're going to need an iterative structure.
So a while loop is the one that you probably know already about.
And a while loop can be used when your programme needs to repeat actions while a condition is true.
So what we're going to do now is we're going to modify this programme so that it uses iteration or while loop in order to repeat the same actions until it's finished.
So let's take a look at that in Python.
Let's take a look at this programme then.
This is our start code that we've got and we need to turn this into an intuitive statement.
So which three lines of code needed to be repeated? Can you remember that from the last part of the video? I always find it difficult to remember things when I switch between two things.
So you might want to just pause and just look back and remind yourself, but if not, try and think what those three things are.
Maybe have a look at that list of code statements there and see if you can figure out which three lines of code are going to need to be repeated.
So pause the video while you think about that.
So the three lines of code that need repeating are these three here.
I was trying to slip them there, there we go.
So those three 'cause we're going to pick an instrument.
We're going to type the instrument here.
And then the list band is going to be appended with whatever's been held in instruments.
So whatever I typed it.
So we know that those are the three parts of code that need to be repeated.
So we know that's going to need to go into a while loop.
So it's a good idea to pick those three points out and then just indent it in.
Now I do that using the Tab key.
So I highlight everything and then I put the Tab key 'cause I find that the quickest way to do it, that's how I do it.
And then we're going to need that while at the beginning.
So while.
But now we've got to figure out, well, what's the condition going to be? So we could start off by maybe thinking, well, once I've entered three instruments, then it's going to end, the loops going to terminate.
So I've got to think of a condition that needs to be true in order to keep entering information into that list but then false when it gets to three.
So what operation or function can we use to check the length of the list? Can you remember? It is the len function or the len operation.
So we can use len to check the length of the band and then we can also check, in our condition, we can check that it's less than three.
So let's have a look.
Len , and it's less than three.
And what goes at the end? Yeah, it's a colon.
So make sure you put more spaces, so it looks a bit neater.
So what I've done here is I've said while the length of band, so the length of the list or the amount of items in that list is less than three, I want you to keep asking this question.
So now we're going to run the programme and just see if that actually works.
Does it let me type three and let's see.
So run the pro, let's form a band, pick an instrument, oboe, clarinet, piano.
And at that point, it displayed the list back to me and the programme is terminated.
So if you look there, the while loop terminated once it was three because it didn't run those instructions again.
And then after the while loop terminated, it printed band.
So it printed the whole list to us.
So that seems to work now, what if I wanted to do it for 10 instruments, what would I need to change on here? Let's have a look.
Well, what do I need to change on here if I wanted to just have 10 instruments and then the loop terminates? I change that there to a 10.
And then that works in that way.
So I can change that back to a three.
So that's one way that I could do it.
Another way is maybe I want to make sure that I only have the list once it's got the word guitar in it, I want the loop to terminate because I really want the guitar in my band.
So people can type in all sorts of instruments but once that guitar is in my band, I want the loop to terminate.
So what kind of condition am I going to need to use here in order for that once that guitar is in there, it's going to terminate the loop.
So think about the in operator, it's going to be something like that.
It's going to be looking in the list.
It's going to be looking for guitar.
So what conditioning needs to be true for me to keep adding items into that list? Have a think.
Bit of a tricky one, isn't it? So what I'm looking for is while not guitar in band.
So what we're saying here is while that list doesn't contain the item guitar, keep executing these instructions.
So let's see if that works.
So I'm going to put oboe again, clarinet, piano and then I'm going to put guitar.
Just remember that I did it all in lowercase.
I need to make sure I put it lowercase.
And that time the loop terminated.
So it kept running while guitar wasn't in that list, but then as soon as it was in that list, the loop terminated because it went to check that condition again and the condition was then false, so the loop terminated.
Just to recap then, we created a programme that uses iteration which was a while loop, in order to repeatedly read the name of instruments and add them to a list.
In this case, so the first one we did, the first condition that we used, the process is repeated for as long as the length of the list has not reached three.
The other condition that we did it said in this case, the process is repeated for as long as the list does not contain a guitar.
So we've done two while loop, two for while loop, two different conditions.
And those instructions were repeated in different ways based on the condition that we had used in that while loop.
So let's walk through, let's see what actually happened in this programme.
So while guitar not in band, let's take a look at it.
So first of all, we've got let's form a band, that's our first statement there.
So the output was let's form a band.
And then a band list was created.
And at the moment, it's got nothing in it.
It's just got the two square brackets.
So that's just initialising it and setting it up before we refer to it later.
So that's the band with nothing in it.
And then while guitar not in band.
So this was true.
So these lines of code run, so pick an instrument.
And then you typed in the instrument and then like that.
So maybe the user typed violin.
And then at that point, the variable instrument held the value violin and then we have band or append instrument.
So whatever's being held in instrument will now be also held in band and at index zero.
Now the loop hasn't terminated yet.
Let's just go back and check that condition first.
So we're going back to that condition.
And again, it's true because band does not contain guitar at this point.
So pick an instrument happens again, and that says output on the screen and then instrument equals input, so it's waiting for that input, the user types bass this time.
And then now the value bass is held in the variable instrument.
And then when we go to the next line of code, I've got band or append instrument.
So the value of being held in instrument is then also held in the list band and at the next index location.
So the next one was location one.
And then we checked that condition again.
So guitar not in band.
Well, it's not, is it? It's not there.
And then we've got there true because the condition was true.
And then it says, pick an instrument.
So picking an instrument, we type in guitar this time.
And then guitar is now held in instrument.
And then at this point, we've got that code again, band or append with whatever's being held in instrument.
Now, the value guitar is also held in the list band at the next location.
So it's the index location two.
And then we check the condition again.
So now at this point, is the condition true or false? Let's take a look.
So it's now false.
So because the condition is false, the loop terminates and it now moves to the next line of code that's not been indented which was print band and now it's just printing the whole list.
So every item that is in that list.
What I'd like you to do now then is using all of what we've picked up there, you're going to have a go at the city hopping worksheet.
I'm just going to go for a little demonstration of the worksheet before you get going.
These worksheets can look a little bit overwhelming when you first look at them because there's quite a lot of slides in them.
But actually the task is fairly short.
It's a little bit challenging though this one but there's lots of support there to help you as well.
So I'm just going to go through some of that support with you.
So if you take a look, you've got that first slide which gives you the worked example that we were using in the lesson so that you can refer back to it and look at the way the code is structured.
Look at what it actually did.
And see maybe you can use that to help you out with the tasks that you're given.
You've also got the second worked example, which is, again, you can look at the condition thing, could I use any of this to help me with the activity, that I'd be enough to do? So these two worked examples you can go back to and refer to when you're doing the task.
You then got your task and it asks you to open this programme.
So it gives you the short link that you'll type in in your browser, in the address bars, you type that short link in, and then that will take you to the programme that I've made for you.
And it's there in the NCCE account.
But then as soon as you start editing it, it should switch to your own account.
If it doesn't, you might need to log in to make those changes.
But usually it remembers who logged in last time and just lets you in.
So hopefully, that should just work straight away and just sign in, that's fine.
So again, just read all of the steps that are there.
So it tells you on there a little bit about the programme, so you can read that carefully.
You've then got some steps to remove things.
So read it carefully.
So look at it, run it, then remove this line of code.
You've then got a bit of what we call a Parsons Puzzle.
A Parsons Puzzle is when you're given instructions for programme code and it's all being jumbled up, and your job is to put it in the right order.
So step four is to take a look at , it's on the next slide and have a look at it.
There's also a tip there.
There's always that tip there to help you, so read the tip.
And these are the lines of code that you need to get in the right order.
They are in no particular order.
And also, there's no indents there.
So you're going to have to think, well, what needs to be indented too.
So you're going to need to copy those lines of code into your programme and figure out where they need to go and what order they need to be and the indents.
And then this gives you some example code.
So you've got input and output.
So it says, this is how the programme should work if it works as expected.
So the programme displays an initial message.
So city hopping random planner.
And then after compiling a list of five random cities, the programme displays the itinerary.
So that's what the output is back.
So that just gives you a little bit of an idea of how it should appear for the user when they're using the programme.
And also you can use that to help you test your programme too to see if it actually works as it's expected.
So then you've got two further tasks to extend it and to modify it as well.
So that is what I'd like you to do.
And then you'll see this slide to resume back when you're done.
Now, I'd like you to pause the video while you get on with that worksheet.
Off you go.
Let's take a look at those solutions then.
Your first task was to open that programme and take a look at it and run it, but also add in those new lines of code that were all jumbled up that you had to put in the right order and put the indents in the right place to get it to actually work.
So this was the answer.
This is what you should have achieved at the end of task one, which is, those bits of code that you got randomly select a city from the list of cities and then add the city to the end of the trip list.
So hopefully you managed to get that working.
And if you didn't, you can always use this code now to correct what you have done and just try it and see if you can get it to work.
For task two, you had to remove the city from the list of cities so it can not be selected again.
Now that little bit of code there that you needed to use was that remove operation.
So you've got there remove, and then whatever is being held in city gets removed from the list.
So hopefully, you got that bit of code right.
If you didn't, again, just go and try it now in your programme and see if you can get it to work.
And then for task three, you have to modify the condition.
And then these actions are repeated for as long as London has not been included in the trip.
So you needed to change that condition so that as soon as London was entered into the list, the loop terminated and moved to the next lines of code.
So in and also in the end, London is also inside of that, the first item in the trip to make it around trips, so you start at London, then you go to the other cities and then you end at London because it's a circular route.
So well done.
If you might have to get all of that done, don't worry if you didn't 'cause it was quite a challenging thing that you had to do.
You can always go back and take that block of code now that's on your screen.
Type that into Python, see if you can get it to work and run it so that you can start building your understanding a bit further.
What we're going to do now then is we're going to start looking at the operations that can also be performed on string.
Now, string is just text, isn't it? Now a list is a list of items but string is just a list of characters, isn't it? With Python, you can actually perform a lot of similar things that you can do with list as the operations that you can do with lists, you can also do with string.
So I'm just going to go through some of those things with you now.
So for example, the built in len function that you've already used on the list returns a length or the number of items of a list.
So then the syntax is Len and then whatever name of the list is.
So when you've got an example there, so you've got a numbers list and len numbers would return to three because there were three items in that list.
So let's see how len would work with string.
So the len function returns the length or the number of characters of a string.
So the syntax is pretty much exactly the same, except but this time you put the name of the string in rather than the name of the list in.
So if you had language equals Python and then you had len and then language, then it would return six because there are six characters in the word Python.
We also have the in operator.
So the in operator checks if a value is equal to any item in a list and expressions formed within evaluate to either true or false, and the syntax is item in list.
And then you've got an example there, so you've got a variable of a list of numbers and three items in it, and it says, found 32 in numbers.
So using that in operator, we are checking is 32 in numbers? And 32 isn't in numbers so it's currently false.
And then you can also use the in operator on string as well.
So exactly the same thing, expressions formed within evaluator either true or false.
But this time, the syntax is string in strings.
And we're looking for a group of characters in another group of characters.
That's basically what we're doing.
And then you've got an example there.
It says language equals Python.
So you've got a variable language and held in that variable is the word Python with a capital P.
And then you've got contains equals ty.
So a string of characters there, ty, in language.
So does language have a string of characters that are ty? So ty right next to each other in that order.
Is that going to be true or false do you think? It's going to be false? It does contain a T and a Y, but not in that order.
It's very important to remember that.
Let's look at this next one then, it says, check = language in "pythonic".
So look at the variable language, see what's being held in that.
So what's being held in that is Python with a capital P in pythonic.
So, is Python with a capital P, that string of characters, is it in the string pythonic? What do you think? Let's have a look.
It's false again.
So, it checks whether it's a capital or a lowercase because Python in language has got a capital P, that string can not be found in pythonic because pythonic has got a lowercase p.
We can also use index with both lists and string.
So let's just remind ourselves of how it works in list.
So you've got our syntax there, the name of the list and then you put the index value in those square brackets.
And then your example is, you've got a numbers list and then item equals numbers.
And then in that bracket, we've got a one.
So that's returning the value 76 because that is the value at location one, remember, lists start with zero.
So location one is the second location which is holding the value 76.
Now with lists as well, we can also assign new values to those items so we can use numbers to replace whatever's held at location two with the value 32.
So zero, one, two.
As for that 11 would be replaced with 32 when that line of code is executed.
It's a little bit different with string.
Let's have a look.
So a character in a string can be accessed through its index.
So similar code again, you've got the name of that string and then the index in the square brackets.
And then we've got an example here that says, language is Python, character equals language one.
So we can access the first character or the second character actually, Y, by using the index.
But some caution here, you can't actually replace any characters, so that won't work with string.
You can use on the left-hand side, that numbers[2] = 32.
You can replace items in a list using that index but you cannot do that in string.
It's going to bring up an error message if you try to do that.
So, bit of caution there.
You're going to have a go at the second part of your worksheet next, which is the city guessing part.
And let's just take a look at what it asks you to do.
You'll start with an existing programme and you're going to be extending it.
You're going to be given this programme here.
Now, again, this contains NCCE programmes or bits of code that you're going to import into it.
And I'm just going to show you how you can use that to how you test the programme.
So let's just go and have a look at this programme in Replit so that I can show you.
Now, when this programme opens, it's going to look something like this.
If you take a look, you can actually go over to the left-hand side here and you can see that NCCE folder that it's calling.
You've got the data module.
You can have a look at that if you want to.
And there's also the text file module there that's being used.
But also, you've got something called a cities.
txt.
And inside that is all of the cities that are being used for this game.
And it might be really useful to know this because when you're testing your programme, you might need to know what those cities are to help you test your programme later on.
So that's why I'm showing you that, you can take a look at that.
Normally, you would just ignore that because it's not really relevant, but with this testing, you're going to need to go to those cities and use it for testing.
We'll throw one.
And then if you go back to, you want to just go back to the main programme, then you just press Main and that'll take you back to the main programme.
So when you're testing, don't forget, you can look at those cities lists in the NCCE folder there, cities just to help you test your code.
You now are going to use the worksheet to complete the city guessing game.
Now, remember just like before, there's going to be lots of slides in there for you to read through but it's all there to support you so read it carefully, go back all the way to those worked examples if you need to.
They're there to help you.
And just make sure you read everything and just try your best.
And then we'll go through the solutions in a minute.
Let's take a look at the solutions then.
If you'll notice, my example on the screen is starting at line 11 of the code.
Obviously, the code you were given at the beginning, it was the start code and then you had to add sections to it.
So firstly, you had to have the first letter of the city.
So obviously that's going to be at location zero.
So if it's not equal to it, then you're giving them that clue.
The first letter is and it would tell them what the first letter is.
So it's giving them a hint.
The second hint was the length of the city.
So telling them how many characters were in the name of that city.
And then finally, you had to have additional letter contained in city.
So giving them the second letter as well.
So if you didn't quite get there again, don't worry, you can just go back, use this code to help you and hopefully get it to work now in your programme and see if you can understand it a bit better.
So you can try that.
If you did get it right first time, absolutely, well done.
That's really, really good going.
But always don't worry too much, we find these things a little bit challenging.
You're always going to get the help there at the end and you can go back and try it yourself and try to familiarise yourself with the solution to that problem.
That is everything for this lesson.
It's been quite a journey part lesson, hasn't it? Have you found it challenging? It should always be a little bit challenging but not too challenging because hopefully, I've given you enough support to help you along the way.
Would really love to see what you've been getting up to this lesson.
And 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 getting up to.
See you again for next lesson.