video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher and well done for coming back for lesson two.

You're going to need your Repl.

it account for this lesson, which you should have already set up with your parent or carer's permission.

You're also going to need a pen and paper to make notes if you need to and to remove as many distractions as you possibly can, so that you can really focus on this lesson.

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

In this lesson, you will perform operations on lists and you'll brush up on your Python skills, so mainly looking at selection this lesson.

Let's warm you up a little bit then and try to remember what we were learning in lesson one about lists.

So take a look at this question.

It says, "What will be displayed on the screen when this programme is executed?" So pause the video while you take a good look at the code and try to make a prediction about what the output will be.

Let's take a look then.

So that line of code there, line six, it says, object equals planets and then you've got the variable position there in those square brackets.

The value currently held in position was three.

So it's going to, remember that the index location of a list always begins at zero.

So location three was Mars.

So that was the answer.

The correct answer was Mars.

So hopefully, if you didn't get it right, now, you know, because probably because of the index location maybe you got that mixed up, but that's the answer.

So let's look at the next one then.

It says, "The index of an item.

What do you think will be displayed on the screen when this programme is executed?" Now, you've not seen this before so I'm not expecting you to know the answer, but try and make a prediction about what might happen based on what you know about indexes.

So have a think.

So the index location of Venus is one.

So what do you think is going to be returned from this? Let's have a look.

So the number will be displayed, the number of one will be displayed and this is the index of the value, Venus.

So you can use that dot notation.

index and then whatever your search term is in the quotations and that will help you find the index location of an item that's in that list.

These are other operations that you can carry out on lists and there's quite a lot of them, so I'm not expecting you to remember them all just from this list, but we'll look at a few of those in this lesson and as we move forward with Python programming.

Now, you're going to be happy to go at this worksheet.

So I just want to talk you through it a little bit, just so that you understand how it works.

So a lot of these worksheets contain quite a lot of reading, but the actual task is quite short, but all the reading is there to support you to hopefully make sure that you can carry on with the task independently.

So I'm just going to go through it with you a little bit before we get started.

So this is the section on the solar system that we're looking at and it starts off by showing you operations on lists, which is from the slideshow that I was showing you a few minutes ago.

All of those operations are here.

So if you're asked to do something in the task, then you might need to refer back to these operations, so they're all there and it explains what they all do.

And then you've got a worked example as well.

So worked examples are really good, because they show you example code that you can use and you can refer back to them just to see the correct syntax that has been used and the right order that's being used for that code, so that's really useful to look back on.

Plus, you've got there that coding system for zero is Mercury, one is Venus.

So that might help you as well, just to help you visualise what's happening with those lists.

Then you've got your task one, which is about the planets and it gives you your introduction there and this step one is just to open up the worksheet.

So you've got there the short link.

So just type that into your browser and it'll take you to Repl.

it and then you will be able to edit it in your account and that's the programme that you'll be using there that's on the screen and then steps two to five just give you things to do.

And you've got that example input and output there, so you'll see those tables with example input and output quite a lot in these worksheets in these units, 'cause that gives you an idea of what the user will experience when they're using the programme and what's happening.

And then your job is to do the coding behind it, but it'll just help you visualise and it also helps you with testing.

So try not to get too overwhelmed by how long these worksheets are.

I know they are quite long, but they are there to support you and guide you throughout the tasks, and then hopefully if you do get stuck, you can just go back and look at those worked examples or those operations on lists' part, so you can get little code snippets to help you along the way.

Pause the video while you complete the solar system section of your worksheet.

Let's take a look at the solutions then to the worksheet that you just did and if you did struggle a little bit, then hopefully this will help to guide you to what certain things meant and how they actually were working behind the scenes.

So you might've got the answer and the code right, but maybe not quite understood how it works, so hopefully this explanation will help you.

So that first one was to add the three planets to the list and you had to select the correct operation to do that.

So you had to add Uranus, Neptune and Pluto in that order.

So this was the code that you needed to use, which was.

append.

So you put the name of the list, which was planets and.

append just means add to the end of the list and then whatever you want to add, you put in the brackets, because this is a string, you'd put the speech marks there as well.

But how does that work? Let's have a look.

So this was the original list that you can see here, but when it comes to planets.

append, when it comes to that line six of code, it's going to add Uranus to location six and then the same with those other two.

So because line seven is executed next, then Neptune is going to go to index location seven and Pluto was the final one, so that's going to go to location eight.

So you had another one, poor Pluto was demoted in 2006, it was removed from the list of planets and classified as a dwarf planet.

What sad times that was? So it says, remove Pluto from the list and it gave you where to do that as well in the code.

So the challenge, continue with your existing programme, remove planet from the planets list.

So you had to go through and look and find the operation to remove something.

So that was the code.

So again, you put the name of the list and then you put.

remove, 'cause that was the operation that you were going to perform, and then in the brackets, you say what item you would like to remove, which was Pluto.

And this is how it works.

So when you run this programme, we're at line 11 now, so if you notice some of the code that was higher above.

We're looking at line nine in the top now, there's still code before it, but we're just trying to focus on the bit that we're on.

So planets.

remove there, Pluto from the list and it gets taken from the list and it was at the bottom of the list, if you just look there, it was at the bottom of the list.

So it was just removed from the end of the list.

Now, the next one, in 2006 some sub planetary objects were classified as dwarf planets and your challenge was to create a list containing names of dwarf planets.

So those were the dwarf planets that you needed to create a list for and hopefully you were able to look at the syntax that we used for the planets list and you're able to copy that syntax for the new one, which was the dwarf planet's and then you created your list.

And if you have problems with the syntax, then you should've gone back to that worked example or just looked above in your code, at your old code, and just seen how it was structured with the square brackets, with the commas, with the speech marks, so that you could see exactly how it worked.

So that is how you should have done it there.

In 2008, Huamea was recognised as the official name of Ataecina, so your challenge was to continue with your existing programme, modify the second item of the list and assign the value, Haumea to it.

So what line of code did you choose? Let's have a look.

That's the line of code that it should've been.

So what that's doing is it's assigning a new value to index location one and so you put the list name and then the square brackets, the index location and then you've just assigned it a new value.

So this is what it would look like if we were wanting to look at it as a list abstractly.

So you've got the original list there and then it runs that line of code and then it replaces with Haumea, like so.

Now, Pluto should also be added to the list chronologically, it was the second to be discovered.

So this time you've got an insert, so it's not replacing, it's not assigning just over what was originally there, it's actually inserting it this time.

So it wants all those values in that list, those items in the list to remain, but it wants Pluto to be inserted at the correct location.

So what line of code could we have used for that? Let's have a look.

So we could've used insert for that.

So the operation insert actually contains two arguments there.

So if you've got there, you put the list named, dwarves.

insert, because that's the operation that you want to perform and then the first argument is one, which is the index location, and then the second argument is Pluto, because that's the value that we want to insert at location one and this is what it looks like.

So there's the original list.

It goes to run that line of code and it insert Pluto at location one, and did you notice there that all of the three that were passed in the list have been incremented along? So they now got a new location that's been incremented by one.

You're going to practise using some more list operations by creating a dice battle game.

So I'm going to go through the rules and I'm going to give you some examples as well.

It's a two-player game.

You've got an attacker and you've got a defender.

The attacker rolls three dice and the defender rolls two dice and then each player sorts their dice into a descending order, which means that they put the highest number first and then there's a check to compare the players' dice rolls.

So you've got the highest roll.

You look at the highest one that they've got and whoever's got the lowest number there, loses a point and then you look at the middle one and whoever's there, the lowest number there, loses a point.

And if there's a tie, then the defender is the person that actually wins that round, so the attacker will lose a point.

So here's some examples, hopefully to help make it a little bit clearer, let's take a look.

So there you've got, the top row is the attacker's dice, that's the three dice and the bottom row is the defender, so you've got two dice there.

So first of all, their rolled and this was a example roll.

And then they're sorted, so you can see that from top to bottom or left to right, you can see the highest number is now on the left and then it gets smaller and smaller as you go along the list, and then you go through and you compare.

So look at those two dice initially.

So the attacker's got six and the defender has got five.

So that means that the defender loses a point and then if you look at the next one, the attacker's got four and the defender has got three.

So again, the defender loses a point.

So here's another example.

You've got another roll there, they roll the dice and then it's sorted into descending order.

So with the highest one first, and then if you look there, we've got compare the player's highest roll, so let's have a look.

So at this point, they both rolled a five for their highest number, but because it's a tie, that means that the defender wins that round.

So the attacker loses a point and then on the next one, what do you think it's going to be? Yeah, the defender loses the point on that one, because the attacker had the highest dice roll.

So that's how it works and what you're going to do, is you're going to have a go at completing this game, but before we do that, let's just have a little look at the programme in Python, just to see sort of how the start programme actually works.

When you look at your worksheet, you'll see that you'll be given an instruction to open the programme at the NCCA short link.

And you'll see that when you do that, you'll open this programme here.

So I just wanted to say a couple of things about it.

First of all, you've got line one, from the module dice import the function dice rolls, and this is not a standard Python function that you can import into any programme.

The start programme that you've been given already has that function installed there.

So it's been written especially for this task, just to remove some of the complexity so that you can focus on the actual game part, and what this function does, if you take a look here, you've got attacker = dicerolls and defender = dicerolls.

So the argument that's being passed there is being used to generate a list where the first list is going to have three random numbers and the second list is going to have two random numbers.

So that's what that argument that's being passed there.

So if I just run that, you can see that we've got there player's rolls, attacker, there's those three random dice rolls and then the defender, those two random dice rolls.

The programme doesn't work correctly yet, because you'll notice there you've got hashtag doubt and some instructions that you're going to have to modify to make sure that you complete, because at the moment it's not sorted it, 'cause it's just replicating exactly the same thing.

So your worksheet is going to step you through making this a working dice battle game.

Pause the video then while you go through the second part of the worksheet to do the dice battle game.

And define that, was it a little bit tricky? Well, don't worry if you didn't quite get there, 'cause I'm going to give you the solutions now so you can go through and you can copy those solutions into your programme to hopefully help you understand it a little bit more.

If not, you can just check if you've got the correct answers that I've got here in my code, and hopefully you getting used to now looking at the worksheet to get your support too, so you can look back at the worked examples and the code snippets to help you with your work.

So let's look at this one then.

So the first one was, sort the attacker's dice, sort the defender's dice, and hopefully you took a look at those lists operations to get the code that you needed and this was the code.

It says, you can also sort in ascending order and then reverse, so that's another way that you could do it.

And this was the other one, it says, compare the two player's highest rolls, the rolls have been sorted, so you know that the highest rolls are first.

So if you look there, you should've used an if statement to look at that first item in each list and compare them.

And then that was the first item or item zero.

And then the next one was to look at the second dice, which would've been at location one and that's going to compare it and deduct the points where necessary.

Let's look at some other operations then that you can do with lists.

So you might want to measure the length of a list.

So the built-in len function returns the length or number of items of a list and the syntax that you would use there is, len and then the name of the list in brackets.

And if you look there on the right-hand side, you've got some examples.

So you might want to find out how many dwarf planets there are in that list that you've got.

So you'd put len.

You've then got another one, so if seats are greater than the guest list, then you can say insufficient seats, so that's another example.

The while, the length of a list rolls is less than 100, keep appending a list rolls.

So their just some examples.

You'll find that you'll use this as and when you need it, but len is a really useful tool for finding the length of a list of items. Now here's an example of it being used in a programme.

What will be displayed on the screen when this programme is executed, what do you think? Have a think.

So the answer is eight, which is the number of items in the list of planets.

So if you look, if count them, there are eight items in there.

So try not to confuse that with the whole index starting at zero thing, 'cause Neptune is at location seven.

So you might confuse that and think actually there's seven items, but no, it's the total number of items in that list that it's going to return there.

You've also got the in operator or list memberships.

So the in operator checks if a value is equal to any item in a list, expressions formed with in, evaluate to either true or false.

And then the syntax there is item in list.

So you've got an example there.

So if chips is in supplies, then remove chips from the list and then you've also got, if not word in sensored: print, you can use that word, or invited myname in guests, then you're invited.

So the in operator's really useful for finding a specific item in a list and seeing if it's there and if it's not, you could use that term, you could add it in if it wasn't there.

So here's an example then, what will be displayed on the screen when this programme is executed, what do you think? So there's the answer, so the answer is false, which is the value of the expression, Pluto in planets.

There is no item equal to Pluto in the list of Planets, so it's going to return false from that.

You can use this in literature as well.

We're going to have a look at a programme in a minute.

So this code retrieves the entire text of Bram Stoker's "Dracula" and stores it as a list of words in a word list.

And no, again, just like with that module we were using before, the text from module and the words function are not standard Python components.

The list has been created for you, specifically for this lesson.

So we're going to look at this line of code.

What do you think it's going to do? So length = len.

What does line three compute, what do you think? So it says how many words there are in the book, did you get that right? It's a good guess if you did.

Now, what does line five compute? What do you think? Contains vampire in wordlist, what do you think that's going to do? Let's have a look.

So whether or not vampire appears in the book, it's either going to be true or false.

So how can you modify the programme to compute the number of times that vampire appears in the book? Now, you might want to pause while you have a think about that.

So what we're going to do now, is we're going to have a look and see if you've got the right answer, to see if you were thinking the right lines, let's have a look.

You've seen an awful lot of operations that can be formed on lists in this lesson.

So let's see how many you can remember.

So pause the video while you write down as many as you can remember as possible.

All right, here we go then, here's my list.

So you've got adding items, appending, inserting, you've got reversing so you can sort and you can also reverse a list as well, you can delete items by popping and removing and searching, so there's all sorts of things you can do there, we have list membership, finding the index of an item and counting occurrences.

So that's quite a lot of things to cover in one lesson.

Hopefully it wasn't too overwhelming and it's just given you a flavour of all those different things.

And you're not going to use every single one in all the programmes that you make, but it's just useful to know that those operations are there for when you need them.

So well done this lesson, that was pretty challenging, wasn't it? There's quite a lot going on.

So give yourself a pat on the back, 'cause you've worked really, really hard.

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 we'd really love to see what you've been up to in this lesson.

So hopefully I shall see you seen for lesson three.