video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher for the second half of this programming part five unit.

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

You're also going to need a pen and paper to make some notes, but also to answer any questions that I give you in this lesson, it's a really good idea to try and remove as many distractions as you possibly can so that you can really make sure that you focus in this lesson.

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

In this lesson, you will traverse a list of elements, use list methods, create a function that returns a list and you'll import custom built functions.

Off we go.

Let's start off them by making a prediction.

What I'd like you to do is take a look at this programme and see if you can predict what might happen to the output when this programme is executed, pause the video while you have a think about that.

Let's take a look then.

So this is what the output would be if you executed this programme.

So it's saying Python is the best, and then it's just saying for the others, it's saying java is a programming language, C+ is a programming language, and yuby is a programming language.

And what's happened there is we've traversed through a list.

Now we've already traversed through a string before where we've gone through a Word.

So if we look at that Python code at the bottom, we've got their word equals Python, and then we've got for item in Word print item, and then the output would be all of those letters of the word Python on a new row like so.

So we've traversed through that string.

We can actually do the same thing, we can traverse through a list.

So for the item in words, so for each item in that list, I want you to do some instructions.

So traversing is just another word for moving through or travelling through.

So we're just going through in order from left to right there.

So now we're going to start looking at some list methods.

And the first list method is index.

The index method, we'll check each element in a list against the argument placed inside the brackets.

If the location is found, then the index location is returned.

So I've got a question for you then, what will be the output when this programme is executed based on what you've read there? So pause the video while you have a little think.

So this is what it would do.

It would print out, it will display for the user the number four.

It's very important, it will only return the index of the first match that it finds.

So if it's, if the same number appears later on, then it won't show that one, it'll just find that first one and return straight away.

So here's another list method that we can use, and it's the insert method.

So the insert method will insert an item at the location specified in the arguments.

So those bits in the brackets.

The items make room for the new item, and what I want you to do now is see if you can do another prediction.

So what will be the output when this programme is executed, based on what you've read there? Pause the video, while you have a think.

Let's take a look then.

So this is what will happen.

It's printing that whole list, but this time is inserted the value 10 at location 2, you can see the difference.

If we look at line one of that code and see what the original list was, you can see that now it's had have that extra value inserted there at that location.

So now you've got pop, pop is another less method.

The pop method is another way to remove an item from a list.

This will remove it based on the index location rather than the contents of the item, which you've seen before with remove.

So again, based on what you've read there, what do you think the output is going to be when this programme is executed? Pause the video while you have a think.

Here we go then.

So now what you can see, if you look at the difference between those two lists.

So we've got that on line one, we've got all of those items. We've got four or five, three, two, nine, ten, but when we look at it the second time when we print it, we can we've, after we've used that pop method, it has popped, it's removed the ultimate location.

So you rose to the bit in the brackets.

So now we could four his disappeared and we're just seeing five, three, two, nine, ten.

And then we've got another one called count.

The count method counts how many times it finds the value given in the arguments.

It returns a total number of occurrences.

So again, what do you think is going to be the output this time based on what you've learned there? Let's take a look.

So it will output two.

So it's looking for the number 10 there, because that is what is in those brackets.

And if you look at that list, you can see if you count them, there are two occurrences of the value 10.

So it's going to display two as the output, And now we've got reverse.

Now the clue is in the name there, by what it actually does, the reverse method reverses the list of items. So make a prediction, then pause the video while you make a prediction about what this is going to look like.

Yeah, it's just reversed it hasn't, it is taken that list of items and it's just flipped it around and reversed it to the other way round.

And now we've got sort.

So again, sort gives you a bit of a clue as to what that method is going to do.

So the sort methods sorts the list into ascending orders.

It's quite important to remember that it's ascending when you've just got, so with the brackets like that.

So what do you think the output is going to be, perhaps try and write it down and see if you can figure it out.

Let's take a look then.

So as you can see, it is sorted that list into ascending order.

So that means that the smallest number is going to be at the beginning and the largest number is going to be at the end.

Now, what you're going to do is you're going to take all of those things that I've just shown you that, and you are going to apply that to a challenge.

You're going to create a deck of cards.

Now, what I want you to do is really read those instructions carefully, make sure that you're looking at those codes snippet as examples to help you with it too.

And I want you to complete the "create a deck of cards" section of your worksheet, and you're going to create a deck of cards.

So see how you get up, pause the video while you have a go.

All right then.

So let's take a look at how you could have completed this task off.

We're going to replit.

I'm going to go through that task there now so that you can see how I would have solved the problem.

Now you might solve it in a slightly different way, and that's okay, as long as you've done exactly what the programme is meant to do, then you've done it correctly.

Don't worry if yours is slightly different.

And I am following through the worksheet in front of me down here.

So if you wondering why I'm looking down a little bit, it's because I've got the worksheet there to follow through so that you can see how I would have done it.

And if you were struggling with that, it hopefully gives you a little bit support.

But if not, it'll just confirm, yeah, you've done it how you should have.

So, we start off by saying the scenario is that you've got to create a deck of cards, 52 cards ignoring the jokers.

So we're not putting any jokers in this deck of cards.

And the first task is to use the lists that you've been given there in this replit, from the short link that you were given on the worksheet as your starting point, and then you needed to display each suit on the screen so that you can get a full loop working that traverses through that suits list.

So I'm going to start off by doing for suits in suits, print suit.

So hopefully what that will do is it'll traverse through this list here and it will display each item interns.

Let's just run it and see what happens.

Yeah, it has, it's gone through, it's got hearts, diamonds, clubs, spades, and the same list as above, the same output as appeared there.

But that in that order, there's traverse three.

So I've got it doing that first bit.

I've got it iterating through that list.

And then he says, you now have a fully that will execute for each suit in its suits.

So this is a good starting point.

Now you can start populating your deck list.

So instead of printing the suit, so at the moment I've got print suit there, change the line of code so that it appends to the deck list with the suit instead, complete the code snippet below and place it in your programme.

So I've been given a bit of code there too.

I've got the word upended in the question.

So that's given me a clue too.

So deck dot append and then suit, and does it tell me to test it? Print the deck, yes it does, print the deck list at the end of your programme to check that it worked, the output should look like this.

So, I'm going to just do it.

I'm not going to do it within the loop because that's not going to do what I want it to do.

I'm going to make sure that I'm outside the loop.

So print, and I'm going to print deck, and here we go, run.

Yeah, it's printed that new list now.

So deck has been populated with exactly the same as what is in suits at this point.

So again, we've got that starting point.

So when we programming, we're always trying to break the problem down into small manageable chunks.

You should never try and solve the entire problem straight away, cause that's where you start getting into trouble and you start getting stuck.

You can see here that this problem has been broken down into tiny little chunks.

Cause we started off by just doing that, that tiny little bit to populate just the suits.

And now we can start thinking about adding those other bits in.

So the next bit then task five, each suit has 13 cards.

That's quite important.

The cards start started one, and it's got ace in brackets at this one and finish at King, which is 13.

For each suit in the park, you will need to create 13 cards of the deck.

In order to do this, you will need to use a for loop inside a for loop.

So this is a nested for loop.

The code below we'll get this programme started.

So I've been given a bit of code to get me going as well.

It says complete the code by filling in the blanks and make the changes in your programme.

So I'm just going to, I'm just going to look down and see what's going on here.

So I've got another for loops.

I've got for card in deck, card in deck, yes, no, range.

For card in range.

Yeah, I think we're going to go with for card in range, and I'm going to put 13 there.

Like, so I think 13 is right.

Might need to 12, we can always check this later on copy.

So for card in range 13, and then it says deck to append suit, I'm assuming that that's the same bit of code that I've already written there.

So I'm going to give this a go.

So how do we test it? I have to print that deck at the bottom, which was deleted earlier.

So print deck.

So let's see what happens now.

What's it going to do this time? All right, okay.

So, it's printed a lot of hearts, a lot of diamonds, a lot of clubs and a lot of spades.

So the output from print deck should now look like this, and it's looking like it's right, but I'm not sure if I've got the exact amount of cards at the moment.

So task seven says, check that you have 52 cards by printing the len of the cards at the end of your programme, the output should look like this.

So let's just do print, len, deck, all right.

So when is it going to say 52? And we got it right? Yes I have, I've got it right, okay? Cause now I know I've got 13 hearts, 13 diamonds, 13 clubs, and 13 spades in there cause it's counted, double checked, that he's got it right.

So its range 13.

So even I get that mixed up sometimes, sometimes I have to just print it and test it to see which one it is.

So it's completely normal to question yourself, is it 12 or is it 13? It was 13 in this instance.

And really, it doesn't matter, cause if I'd got it wrong, I just go in and change it to 12.

You know, don't worry.

If you get these little things wrong, it's completely fine, it's completely normal.

So I'm going to move on to task eight then.

So, now that your programme is creating 52 cards, yes.

It doesn't that you need to add in the correct values before each suit, yeah.

Cause at the moment it's not looking like a deck of cards, it's just looking at like a lot of suits in there.

So the variable X is holding a value from one.

So, hold on.

The variable X is holding a value from one to 13.

See, now I think the instructions might be just slightly wrong.

I think it meant to say cards there.

So it's actually cards, that's holding it.

You so you can use this in your programme to add values to your suits.

Think carefully what you will need to be changed in the line of code below in order to complete this task.

Don't worry about ace, jack, King, queen, King at the moment.

This will be handled next.

So we've got there a clue as well.

Concatenation will need to be used for this task to be successful.

Look at old pieces of code where you have concatenated the string in order to help you with this.

Oh gosh.

See, now I've got to get my thinking hat on and think, well, what if I got a do because at the moment it's appending with the suit, but it's not appending it with the card.

Cause if you remember how for-loop works, that works, a for loop when we're using range in there.

So range is quite important function that we're using here.

So the range function creates a sequence based on the value that you've put in there.

So card is going to iterate through that sequence.

So at the moment it's going one, two, three, four, five, all the way up to, what it's going up, it's going to zero all the way up to 12.

And that's why it's showing 13.

So let's just see what happens if I do, if I do some concatenation and if I put card here now, pretty sure I'm going to get an error message, but let's just see what happens.

Yeah, I'm getting an error message.

So let's take a look.

Well that error message says, it says type error can only concatenate string not hint.

So what we've got to think, well, what is, what is string here? What's an integer and it's getting confused.

So have we got to make one of them strings? So suit, is suit an integer or is it string? Let's have a look it's string, isn't it? Because you can see by the speech marks that the string.

Now card is definitely an integer because it's being used to iterate through this range.

So what I've going to do is I've got to use casting to make sure that card is a string.

So I'm going to add that in that, I'm going to run it now, and I've still got an error message.

So now there's something else go wrong.

This is a footy error.

Now it doesn't like print len deck for some reason, it might be, do you know what? Sometimes you get this error and you start and you're looking at it and you go, well, there's nothing wrong with that, it's completely fine.

But actually the problem is, is the line before there's got confused.

So if sometimes you're looking at a line of code thinking, well, there's nothing wrong with that.

Take a look at the one before the one that was executed just before.

And you'll see that there's something not quite right with it.

Can you spot what's wrong with that? I haven't got another bracket at the end.

So now it should work.

Fingers crossed, yes it does.

So now I've got 52 and then I've got all of those, but it's going from zero to 12.

It's not doing one to 13 and that's because this sequence starts at zero and ends at 12.

So it's not quite right at this point in time.

So let's just have a look concatenation we need to be used, look at all pieces of code and he says, test your programme I printed the deck again.

It should've put like the example below.

If you're pro is an arrow check that you were converted the value X to a string, which a half, but it's not doing it a one to 13, it's doing zero to 12.

So his looking slightly different to what's on that worksheet.

So what I could do is I could just change this to one to 13 and I think that will fix the problem.

Let's just have a look.

So now I've got less cards.

So now I just need to put 14.

Let's try it again.

And I think I've done it this time.

So the reason that I needed to have 14 there instead of 13 is because that final number is used as a stopping point.

It's not part of the value in the sequence.

It's the stopping point.

So it's important to remember that if you're going to switch to range.

There were other ways that I could have done it.

I could have done 13 here.

And I think I could've done plus one here.

Let me just see what that would have done.

Yeah, so that would have solved it too when it that's another way I could have done it.

So when you've been doing this as well, you could have done all sorts of things to try and get it to work properly.

So I think I'm going to stick with one to 14.

That's definitely working.

So that's how, I'm happy with that.

But if you tried it slightly different ways and that's completely fine too, don't worry it.

So the next we said now is the time to add in ace, Jack, King, queen in place of one, 11, 12, 13.

It says use if and if else to place the letters before the suits instead of the number.

So I've got to have an if statement, so if card is equal to one, then it's going to, oh gosh, so now I've got, I need this bit of code in here, but it's not going to print the number, it's going to print ace.

So what it to say, ace plus ace, it's going to be an a isn't it, like that.

So if card is equal to one, blah, blah, blah.

So now I'm just going to put in an else there, just so that I can test that to see if it's actually worked.

So should do for everyone that's on there.

It should now have an A it's just like so hard A's should appear at the beginning.

So let's say or yeah, I'm getting there, I'm getting there now, okay.

So you've got A, and so now I just need to put an elif for.

So elif card is equal to which one was it? 11, then it's a Jack.

So it's exactly the same code again.

So I could just copy and paste it and I could put a J like, so, and then another one elif card is equal to 12.

Then I wanted to do queen.

come on.

And then elif card is equal to 13.

Same bit of code again, and this time with King.

So that should work now.

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

Excellent.

Yeah ace, two, three, four, four.

Jack, queen, King and then it starts at eight.

So I've got my pack of cards now.

So that's all ready.

It says, now that you have a deck of cards, you can perform lots of operation that would work with a card game, like snap, for example.

So here's a code snippet that you can use to shuffle at least incorporate this into your programme.

So I've got an input statement that I'm going to need at the very top.

So from random import shuffle, and I'm going to shuffle the list so I can have sub print deck as normal first time, then I'm going to shuffle deck and then I'm going to print deck again.

Oh, I can't spell print deck like that.

And hopefully I should have, yeah.

So now it's printing out the deck in the correct order and then it's printing out it in the shuffle order.

So I know it's working now.

And he also says he might have a to play game that requires each player to start with half of the shuffle deck, create two new lists for player one and player two, and populate each new list with half of the shuffle deck.

Here is some sample code used to slice a list of months to support you with this, okay.

So I've got to slice that list in half and put the first half in one list and another half in another list.

So I'm just going to scroll down a bit.

So I've got player one equals and then I've got my bit of code to slice it.

So I'm going to go with zero.

What's hard for 52, is it 26? Zero to 26 and again, I might have my numbers wrong.

It doesn't matter cause I'm going to test it and I'm going to figure it out later or I can stop and think, that's the other thing that I could do.

I could do it the other way round, if want to say player two, I'm going to do deck 27 to 52.

And I'm going to just print the len as well of those underneath, just so that I can print len player one.

And then I'm going to print len player two, just in case I've got those slices wrong.

So it's just do it.

I've got the slides wrong.

Have definitely got the slides wrong.

So the first one's right, but the second one isn't so.

Should go with 53.

It's not going to work.

Yeah, no, no, it hasn't worked.

Did I not Change it? Okay, let's change it to 53.

Run.

Nope, still not working.

That's weird, isn't it? Hmm, do do do doo do.

It Can't be 26.

Could it be 26 doing diff, the same cards, I'm going to just hashtag this side, and hashtag they side so that I can test this and just see what's going wrong here.

Cause I don't think there's something going right.

I'm going to print the list as well.

And this is all part of testing your programme and you'll come across these same problems yourself and it's just making sure it is accurate.

And some people sit and think or exactly what those numbers are going to do.

And do you know what would be a great idea? A great thing to do at this point is to go to the point and documentation and just check what values are returned when we use that slice so that you can get those numbers exactly right.

So you can do it through trial and error like I do right now.

If it goes to the documentation to figure out what it is, either way, you know, it's still going to get the right answer I hope.

It'd be quicker if you looked at the documentation though.

So let me just see what is happening.

So is it done ace, so it should be quite neat.

And it does, it's done it.

So let's just have a look, yeah, because that one starts with an ace.

Hold on, hold on.

I don't want to print the deck either anymore so I can really see what's going on, let's have a look.

So, starts with ace and it ends with Kings there, starts with ace there and it ends with Kings there.

So it has neatly split it up.

So I just needed to make sure that I went to zero to 26 and then I started at 26 to 52.

So we are now working correctly.

So now what I do is I don't want this anymore.

I still need all this anymore.

So now I should get shuffle deck.

Which I do.

Okay, brilliant.

So I've done everything on that task list and you'll see that the programmers don't get it right first time.

And it's completely normal to make little mistakes, to have to stop and think, to have to print things on the screen so that you can see what happened and you can test it and read things to make sure that it's working.

All of these things are totally normal.

And these are skills that you need to practise as well.

Because when you start developing your own programmes and you start thinking of your own problems to solve, you're going to need to do these things yourself.

So it's important that you see me go through those experiences so that you know that when you come through it and you start struggling, you know, it's completely normal, all programmers for getting into things and they'll go off and they'll find it.

They'll, they'll do a search on the internet for a bit of code and they'll bring it in or they'll just sit and they'll trial and error it until they get it right, okay? This is normal, don't panic.

We're now going to look at how we can return a list from a function.

So we already know that our function returns a value.

Sometimes we need a function to return more than one value and list is a method that we can use to return more than one value from a function.

So here is a function that populates a list with the letters of the alphabet.

And the function is called, the list is populated inside the function and then returned.

Now our deck of cards programme can be turned into a function so that we can call it whenever we need a new deck of cards.

So let's just go back to that code and just see how you can actually do that.

So here is where I left off before with my deck of cards programmes, I've got all of this stuff in all this testing and things down the bottom that I did.

Now, I'm just going to delete it.

I might keep print deck, actually that.

There just at the bottom.

Now you might not want to delete it from your programme, you might want to save another copy.

So if you want to get into the copy and replit the way I do it is I copy the code and then I click on new replit, and then I paste the code into the, I've got a second copy of that code.

So you might want to do that, but I mean, I don't necessarily need to do that.

So here is this structure for creating a deck of cards.

So this is the start code and then it all gets upended and this is just it printing at the bottom.

So all this code here can be placed into a function.

I'm going to call it deck of cards or yeah a deck of cards is a good name.

So I'm going to put def deck of cards, I'm not going to pass anything into it.

I'm just going to return by these outfits.

So I don't need anything in the parameters there.

And then I want to make sure that all that code is executed with in that function.

So I'm going to highlight it all, I'm going to press the Tab key on my keyboard.

And then, at the moment, if I run this programme, let's just take a look at what happens.

So what it's doing at this point is it is run it, trying to go through all of that.

And then we've got here, print deck.

Now what it's doing is it's trying to find deck, but because deck has been created within that function, it can't find it to call it.

So this is why we use that return bit.

So in my programme, I'm along here, I'm going to have to have a return somewhere.

So I'm going to put it right at the end, so I'm just going to make sure I've got my cursor lined up correctly.

So I want to make sure it's here outside of the for loop.

So now I'm going to have return, deck.

So now let's see what happens when it runs.

So we're still getting that error message.

And let's just take a look.

Why do you think we're still getting that error message? Can you remember what we've learned about function so far and how we have to use them, to get those returned values? Just have a little think about what might be missing here.

Well, we haven't actually called the function yet so we can call the function so it can go, let me just scroll up a little bit so that you can see.

So the function is called deck of cards.

So if I just have deck of cards here, will it work now? And it doesn't work now at this point.

And that is because yes, it's returned deck, but it's had no where to hold that value.

So it's just returned it and then done nothing with it.

So we need to actually make sure that value has been held somewhere.

So I can have another variable called deck, can call it something different if I want to, it make much difference, which just make sure I call the right thing down here when I print.

Yeah, I can tell that deck equals deck of cards.

So now that return value will be held in this new variable deck.

So let's see if it works now.

Okay, so now it's done it.

So we've got a function called deck of cards.

We've got here, return deck, it's returning that value.

We've given it somewhere to live or to be held.

So that value is now being held in deck.

And then when we print deck it's going to print that deck of cards that has been all created within that function.

So you're going to have a go doing that yourself now.

What I'd like you to do then is use the live coding demonstration to help you turn your deck of card code into a function.

So pause the video while you're have to do that.

Excellent, so, we're now going to look at creating a custom built function.

All of the functions that we create can be used in other programmes where required, we could just copy and paste the code each time, but this would be pretty inefficient to do.

Instead, we can save all functions in a separate Python file and import them into our programmes.

We have used these lines of code many times to input functions into our programmes.

We can do the same thing for our own custom built functions.

So you've seen all of these before from random import rantint, from time import sleep, from guizero import App.

So we can turn our deck of cards function into a custom built function that we can use whenever we need a deck of cards in any programme that we make.

So let's take a look at how you can actually do that in replit.

So here we are back in our old function code.

So now I can show you how to turn it into a custom-built function.

So at the moment, this function is being executed here within this programme.

But what can actually do is make my own custom built function so that I can just import in when I need it.

So this is how I do it.

I'm going to create a new Python file.

So over here with my main programme, I can actually go to the plus to add a file and I'm going to call my new Python file cards.

PY.

And now I've got two separate pipes and files.

If you look, I've got my main code and I've got my new Python code, it's just blank at the moment.

And if you were doing this in a different idea, different development environment, then usually as long as you have saved that Python file in the same location as your main post from file, then this will work as it is now, okay? So if you do decide to do this in a different development environment, it will do work just very slightly differently.

So I've made my extra Python file called cards and I've got my main Python file there.

And what I want to do is I want to take this whole function out because it works now, it works independently on its own, so I can actually take it out and put it into its own files so I can use it as a custom-built function.

And it'll start to make a bit more sense in a second.

So there's my whole function.

And I'm going to just go ahead and cut, so right-click cuts could do control X and that gets rid of it in my programme.

Now I can go over to here and I can paste that code in.

And for some reason you can't do right click paste.

So I'm just going to do control V to paste it.

I've now got a function in a separate place to my main code.

So here's my main code.

If I try to run this programme, now, let's just see what happens.

So it's now saying that deck cards is not defined cause it's trying to find this function, but we've actually moved that function now over to here into this other file.

So at the moment it cannot see this file because we haven't imported it into our main programme.

So just like we've got up here without, from random import shuffle, we can actually now go from cards, which is the name of this Python file here.

I'm going to import the function deck of cards, and now fingers crossed it should work.

Oops, I've just got a little bit.

Let's just, I think that's working now.

I don't know what that was a little error message that just came up.

So I'm just going to press run.

And now it is seeing that deck of cards again, but it's doing it because it's importing the function from this file over here.

So it's got from cards import deck of cards that is importing this deck of cards function from the cards file into my main programme.

So hopefully just by looking at that and seeing that maybe these other bits here, so this from random import shuffle is making you see how those might be working as well.

And it's little bit like another layer of obstruction, because what we can do is we can abstract away that function.

We don't need to know how it works anymore.

We just need to use it.

If ever, we need a deck of cards, we just need to call it.

So we don't need to see that line of code in more.

We can stick it over in a file over here and just use it whenever we want it.

And whenever we want a deck of cards in the future, we don't have to write that piece of code anymore.

We can just import it into our main programme and it'll work as long as it's in the same place as the Python file where it's saved.

So it's a really useful thing to do to learn how to do these custom-built functions yourself.

So you're going to have a go at this now.

Use the live coding demonstration, I've just done with you, to turn your deck of cards function into its own custom-built function.

Pause the video while you have a go.

Superb, so, final challenge for you.

Then what I want you to do is match the methods to their description.

So look at those methods on the left-hand side.

See if you can remember, that's quite a long time ago that at the beginning of this lesson, see if you can remember what those lists methods actually did.

So you can draw this out on a piece of paper, draw those lines between the, the method and the description.

Pause the video while you have a go at that.

Let's take a look there.

So index, was that bottom one returns that index location of the specified item, and then you've got insert index item so it adds a specified item at an identified location, pop, deletes the item at the identified location and count returns a total of the occurrences of an item, and then sort manipulates the list so that it appears in ascending order.

Well done, there's been a lot of new stuff this lesson, hasn't there? So hopefully you've had a good go.

And, and hopefully my demonstrations helped you a little bit if you needed a little bit of extra support.

And if you'd like to, please ask your parent or care to show your work on Instagram, Facebook, or Twitter, tagging @OakNational and #LearnWithOak, cause it's great to see what you've been getting up to, and I'll see you soon, see you again soon for the next lesson.