warning

Content guidance

Contains external content.

Adult supervision recommended

video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher.

Now, in order to start this unit, you need to have a really good understanding of a sequence, selection and iteration in Python.

And if you're not sure what that is, then I suggest that you have a go at the introduction to Python unit before you start this one.

Don't worry I'll still be here when you're ready.

If you've done all of that though, and you are ready, then you're going to need a pen and paper, you're going to need a Repl.

it account.

And if you are under 13 you are going to need to make sure that you ask your parents or carer's permission before you set up a Repl.

it account.

And you're also going to need to make sure that you can remove any distractions out of the way that you possibly can so that you can really focus.

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

In this lesson you will brush up on your Python skills.

You'll use selection, which is if, elif, else statements to control the flow of programme execution, and you'll create lists and access of their items. Lets start with a matching task then.

What I'd like you to do is take a look at these five things on the left-hand side that you can use in Python, and then on the right hand side, I want you to much it up to the description.

So pause your video while you have a go that.

Excellent.

Right, let's look at the answers then.

Let's see if you've got any of them right.

We shall see.

So, input then, what's input? It is reading from the keyboard.

So whenever we want the user to enter something in we use the input function for that.

That's the next one then, print.

What does that do? Yeah, it displays output to the screen.

So every time we want something to be displayed for the user, we'll use the print function for that.

What's the next one then? It's while.

So while performs actions repeatedly, that's what it is.

It's a loop, isn't it? So let's look at the next one.

If, what does that do? It checks a condition to select which actions to perform, okay? Final one, what's that equal sign for? It assigns a value to a variable.

So it's used for assignment.

And if you're a member two equals are used in a condition, but one single equal sign is used for assignment.

Well done.

Let's take a look at this Python programme then.

And the question is when this programme is executed the user types in 12 and two on the keyboard, what will be displayed on the screen? Now think about the rules of operator precedence when you're looking at the expression on that.

So, the division is going to have to go before the addition.

So use that to help you answer the question.

Pause the video while you think about it.

Fantastic.

So here's the answer.

The answer was eight.

So why was it eight? Let's take a look, let's walk through this programme and see what actually it does.

So A would be assigned 12 and B would be assigned two because that's what it says in the question.

And then average equals B plus A divided by two.

And because we're looking at the order of operator precedence, we look at A divided by two first.

So A is 12.

So 12 divided by two, is six.

And then we're left with B plus six and B was two.

So two plus six is eight.

So what's going to be displayed on the screen, it's going to be whatever's held in AB dual average, and that would be eight.

So that's why that was right.

Let's look at the next one.

So this one still has those two inputs at the beginning and we're still doing A is 12 and B is two, but then there's some slight differences to the rest of the programme.

So take a look at that.

Pause the video while you do it, and think about what the answer might be.

Brilliant, so what's the answer? It was nothing.

So why is it nothing? So let's take a look at that programme again.

So A is assigned 12 and B is assigned two.

And then we've got a new variable called max and that is assigned the value of A, which is 12, so max is 12.

And then if we look there we've got an if statement and we've got a condition there B is greater the max.

So if we look at that condition where it is now B is two and max is 12.

So we're saying two is greater than 12.

Now that is false isn't it? So, because it's false, what it's going to do is is going to ignore those indented line lines of code and just go to the next line.

But if it was true, it would run whatever was indented in there.

If we wanted something to be displayed if that condition was false then we could use an elif or an else instead.

For this activity, what I'd like you to do is read through those statements on the right-hand side and identify where they appear in the programme code on the left-hand side.

You might want to use your pen and paper to help you with this 'cause it could probably get a little bit confusing after a little while.

So, have some time to think about that, pause your video while you do, and then come back when you're ready.

Lets look at the lesson.

So the first one was variables, and this is where those variables appear.

There's four variables and you've got A, B, average, and max, and this is where they are initialised.

This is where their first created but you might have also highlighted where they're referred to as well in expressions, and that's okay too.

But I just put there where they'd been initialised, and where they've been set up initially.

The next one then was assignment statement.

So this is the whole thing.

You've got the variable name with that single equals.

And then you've got an expression on the right-hand side that is being assigned to that variable Arithmetic expressions then are to do with calculation.

So if the calculation needs to happen as part of the assignment, then that is going to appear there.

So there's our arithmetic expression 'cause it's B plus A divided by two.

So it's going to do a calculation there.

Your next one is a Boolean expression or a condition.

Now a Boolean expression always evaluates as either true or false.

And then it nearly always appear as conditions.

So this is a selection statement here and the condition is B is greater than max.

So, we got that in a selection statement because we want some code to run if this condition is true.

So if the condition is true then the indented code will run, and if it's false then it'll move on to the following lines where the indents aren't.

And then finally a block of statements that may not be executed.

So, this may not be executed because if that condition was false, then it wouldn't be executed.

Let's take a deeper look into selection then and just remind ourselves of how it actually works.

You need a selection structure that's the if, elif or else when there are multiple branches.

And your programme needs to select which one of them to follow.

And you can use none, one or multiple elif blocks but you can only use one or no else blocks.

So just take a look at the left-hand side there where you can see if that condition is true then that block of statements runs, and then with an elif you can have another condition.

So elif this condition is true run this block of set statements, and then the else is used used as the other option.

So if none of those conditions are true then do this as well.

So that's just a little reminder.

Now let's extend this programme together just so that we can really just remind ourselves of how this work.

So to check the day and display whether it's a weekday or not.

So we're going to do some live coding now in Repl.

it that you can see how we can actually do this.

So I want to extend this programme so that it gives some feedback based on the number that I enter.

So for the days of the week, and then if you'll notice that table, a Monday is going to be a zero, a Tuesday will be one, a Wednesday will be two and so on.

So what I want to do is when I type in a number, it either says, it's the weekday, it's a weekday, or it's the weekend.

So what will I need for that? Am I going to need just an if on its own? Am I going to need an if with an else? Or am I going to need an if, elif, else? Which one of those will I need to have a think? I'm going to go with if else because I only need one condition so I can check if it's a weekday, and if it's a weekday, I can display a message.

And then if it's not a weekday it's just going to say it's a weekend.

So I don't need more than one condition there, I just need that one that's either going to be true or false.

So what I need to do then is I need to put in my if statements.

I'm going to put if and then I've got to think of what is the condition.

What needs to be true in order for it to be a week day.

So I've got to do some logical thinking in my head while I do this.

So Monday is zero, Tuesdays is one, Wednesday is two, Thursday is four, and no three, Thursdays is three.

I'm getting confused 'cause I did it on my fingers.

Thursdays is three and Friday is four.

So, if it's less than or equal to four then it's going to be a week day, okay? So less than.

So if day is less than or equal to four then it's going to be a week day.

So I need to use the print function.

It's a week day, like that.

And then for the opposite, it's going to be a weekend, isn't it? Because if that's not true, then it'll be the weekend, 'cause if its not a weekday it's going to be a weekend.

So now I'm going to put my else in, try to remember that syntax.

So else is all lower case with the colon at the end.

So else otherwise print it's the weekend.

And then I just need to test it to see if it actually works.

So what did I say? So, Friday is definitely four.

Let's just remember.

Let's just remind ourselves that Mondays is zero, Tuesdays is one, Wednesdays is two, Thursdays is three, Friday is four.

So if I put that it's Friday, so if I enter four, it should date.

So it's a weekday and it does.

And if I run it again, if it's a Monday which is zero, whoops, something's gone wrong there let's just have a look, zero, yup, it's a weekday.

And then what happens if I put five? It's the weekend, okay? So it's working.

Brilliant.

In order for you to have a go at these activities you're going to need to have a Repl.

it account.

Now you will need to ask your parent or carers permission if you're under 13 to set up that account.

And when you can set up an account you just need to go to Repl.

it So R-E-P-l.

it, and you press enter key and then you should be able to sign up for your account using your email address at that point, think of a username that's not going to reveal your identity, that's very important.

Now, here, this is my account.

So I go into my account and then when I want to create something myself then I just go to this plus sign in the top right-hand corner.

And it tends to remember your favourite language and puts that at the beginning there.

So if you've been using this for other programming languages then that might appear at the top.

I tend to use Python most of the times so that's why Python's there.

So you just select Python, and it comes up with a very strange name for you to use but you don't want to be using that, you want to use something meaningful.

So then you would do.

So if you were doing this day activity you might want to call it weekdays.

And then when you were happy with the name you just press create Repl.

it.

And then it'll go into your Repl.

it account.

Now also there's different settings that you can have for your screen.

So if I go over here, I always keep it in dark mode but if you prefer light mode, you can have it in light mode as well, but I just prefer dark.

And then there's different font sizes too, so I tend to keep it huge so that it's a bit more readable for you looking at the videos but you might want it to be slightly smaller if you want.

And there's other options as well for you to have a look up too.

So it's quite good 'cause you can personalise it to however you need it.

And then once you change your settings you just go back up there and then you're back where you need to be.

This is where you type your code.

So, I can just type my code here, and then I just run my code and appears on the right-hand side on that panel.

So that's just how you set up and use Repl.

it if you need to.

I'm going to give you some independent work to do next and that's going to involve using this worksheet.

So I'm just going to talk you through the worksheet just to help you get started.

Try not to get too overwhelmed by the amount of writing there is, and it's all there to help and support you through the activity.

The actual tasks they're quite short but there's a lot of reading there to help build you up and support you through it.

So that's why it might feel a little bit overwhelming.

So I'm just going to talk you through this worksheet just to help you out there.

So it says how long until the weekend? That's your first section.

This is a task that you're about to do.

And it starts off with this syntax checklist.

And these are really, really important because a lot of times when you're programming you're going to get lots and lots of syntax errors.

You've probably experienced this before.

And especially as you're learning at home or wherever you decided to learn, you're going to need to fix those syntax errors yourself because you haven't got a teacher there looking at your code constantly telling you oh just do this, just do this, just do this.

And also this syntax checklist is going to help you be a bit more independent with checking your own programmes and the better you get at this, the quicker you'll get better at programming because those little frustrations of a comma missing or a speech mark missing or a bracket, you're going to tend to do this less and less because you'll have fixed it yourself a lot.

So this checklist here is really useful.

So when you're doing this activity, if you suddenly find yourself getting those syntax errors when you're typing in your code just come back to this slide in the worksheet and go through that checklist and just make sure that you've done everything that's on there.

And that's why it's there to support you.

You've then got a task, just the very first task is literally just says one open this incomplete programme.

So you've got there a short link.

So in the highlighted there programming, sorry, programme ntc.

io/py-week-two, all you do is you just type that into your web browser and it will take you straight to this piece of code.

So then you can get started.

So you don't have to sit and type all of this code in by hand manually, you can just click on that short link and go to that short link and it will take you to that start programme straight away.

So that's just to help you out a little bit.

And then you've got a table there to look out.

Remember how tricky I found it to remember whether it is Monday zero, oh it gets a little bit confusing.

So that table is there to help you as well for you to refer back to when you do new programmes too.

Then you've got step two complete line five, so that the value of remaining is the number of days left until the weekend.

So, it can get a little bit confusing because this slide, this question is on a different page to the actual code.

There's the code, so you can find line five on there.

So just get used to just moving back and forth.

You'll also have it as line five in your Repl.

it as well, so you'll be able to see it there.

So, and then you've got step three add some insert this line wherever you think it should be.

So again, this time you are going to have to type it out yourself, unless you've got electronic version of this worksheet and you might be able to copy and paste it, but I don't think you will.

So you have to type it out and put that in.

That's where you probably going to make a syntax error, aren't you? So what do you do when you've got a syntax error? You go right back to the start where there's that syntax checklist, don't you? And then this one here is an example input output table.

And you're going to see lots of these throughout this unit.

And they're there to help you visualise what the user will experience when they're using your programme, and that will help you think, well what code then goes behind it? So that's why these tables are really useful.

So you've got there and input and output, so it says, use this example to check your programme given the input you see in this sample interaction, this is the output your programme should produce if it's working correctly.

So then it says the programme displays a prompt and waits for keyboard input, so it says, what day is it today? So if that first bit of your program's working when you press the run button, it should say what day is it today? And if that works, then you've got it right? And then the users be able to type a , and again, if that works, you've got that bit right.

And then finally, the programme identifies the type of day and displays the days remaining until the weekend.

And its allowed to say, it should say it's a weekday, four days until the weekend.

So, you can see why that table will help you think about the input and output and how you also test your programme too.

And then you've got more and more steps, more input and output tables to help you.

And there's an optional task too to push yourself a little bit further.

So if you find that quite easy and you want to stretch yourself a little bit more, then you could try that task too.

And then you'll see this slide which tells you to go back to the video.

So hopefully that'll just help you with your worksheet when you're actually doing it yourself.

Complete the first section of that worksheet then and come back when you're ready to carry on.

Let's look at the solution and those worksheet activities.

The first one was steps two to three and you had to calculate the remaining days and that needed to happen inside that if branch, that first one.

So if the condition was true, so if it was a weekday, you then needed to calculate how many days it was until the weekend, and you needed an arithmetic expression there for remaining, so it was five takeaway day.

And then it would print however many days it was until the weekend.

So did you get that right? If you didn't, don't worry what you could do is you could type in that code now and see if you can get it to work and that will hopefully help you figure out how it would actually work in the future.

So let's have a look at the next one.

Did you get this one right? Let's have a look.

So this one, you needed to add in another elif because we wanted a different one to handle Friday as a special case this was step four.

So we needed another condition there and that's why we needed to add in the elif because we've got our first condition that's checking if days less than four, because that's looking for Monday, Tuesday, Wednesday, Thursday and then we need that other one to check if it was Friday and Friday was four.

So if day's equal to four, then it's going to say, it's Friday just a day left until the weekend so it's giving that unique feedback to the user.

And then otherwise it's just going to say it is the weekend.

So we needed to add in an elif at that point on top of our if and or else.

So did you get that right? Again, if you didn't, don't worry, you can just copy that in now and hopefully that will help you figure out how it actually works properly just to support you with future stuff.

So the final one was the explorer task.

So this is where you can really challenge yourself and push yourself a little bit further.

if you're finding it no, not easy, but easy enough for you to have a go with the explorer task.

So hopefully you had a go and if you didn't you might want to have a go later.

So it was importing a module datetime and importing the function from that datetime.

And what it does is it automatically figures out whether it's a weekday or not, use it by pulling in today's date, and then it will automatically do the feedback to the user as well.

So the user doesn't have to actually type anything in at this point.

So that was quite an interesting one to have a go at if you'd like to.

Instead of having numbers for all of these days of the week, what we can do is we can use something called a list.

And this is what a list looks like on your screens now.

So we've got there what looks like a variable name it's an identifier and it's called days.

And what's being held with that identifier days is actually a list of items. So each of those items Monday, Tuesday, Wednesday, are all separate items, and they are held under an identifier days.

So the names for the days of the week can be stored in a list and take a look at the syntax there, you've got the square brackets to show the start and the end of the list, and you've got each list item is separated by a comma.

In this example, the list items are string literals and that's why they've got the quotation marks or speech marks surrounding each piece of data that's in that list.

So a list is a kind of data structure.

Data structures are organised collections of data.

And in this case.

In the case of lists, data is organised in a sequence with each item having a unique index to noting its position in the list.

So here is what it looks like in memory.

So you've got there the identifier days, and then you've got for Mondays zero, Tuesday is one, Wednesday is two, can you see why we did the other tasks before to help you so understand how these lists numbering works.

It always starts with a zero.

So it might seem like an odd thing to do and you've probably thought, why are we doing zero for Monday when we were doing that other task, but list items, they always begin with that index number zero.

So hopefully that sort of made you think, okay maybe your brain sort of get used to using lists.

So when we have an index item, when we have a list of items the index for that first item is always zero it's not one in Python.

So when this programme is executed then what will be displayed on the screen? You might want to take a little moment to read that code properly and then really think about it, but what do you think is going to be executed? Let's have a look.

So the answer is going to be Monday.

So what is doing that is it's using that identifier days, and then in the square brackets is saying the index number of whatever's in that list.

So you've got there a zero, so it's looking for Monday that first item in the list.

So have a look at this one then what do you think will be displayed now? Its going to be Tuesday because Tuesday is at location one in that list.

What we're going to do before them which ones aren't going to be? Its going to be Friday because Friday is at index location four in that list.

So then we can also use expressions.

So expressions can be used to access item lists as long as they evaluate to an integer.

So if you look there day equals three.

So we've held the value three in day, and then instead of putting a zero or two or three or whatever in those square brackets this time we've put day.

So what's it going to be? Let's have a look.

When this programme is executed what will be displayed on the screen, is going to go to item three, because three is being held in dates, so its going to go to item three and it's going to display Thursday.

And then you can also have an arithmetic expression in there too, so there you've got day minus one.

So thing, Ooh what will happen now when this programme is executed? So being held in day currently is three, but day minus one is what? It's two.

So it's going to go to location two and it's going to display Wednesday.

So what we're going to do now is we're going to take a look at this list, actually in Repl.

it in our Python programme and just see how it works.

Here's the programme then that uses that list.

Now you're going to recognise this programme, well half of it anyway, because the whole bottom half is what you've already done in that other worksheet.

So if you look there, you've got feedback based on what day of the week it is, whether it's Monday to Thursday or Friday, or if it's the weekend, you're going to get different feedback.

We've also put in those two lines of code that were the explorer tasks that's going to pull in what day of the week it currently is.

And then this is your new betsy you've got the identifier days and then we've got a list of items there.

And the list of items being held there is all the days of the week.

So let's just.

Before we look at this too much, let's just see what happens when we pull in this module here and function.

So I'm just going to pull print day, ignore what the rest of it's doing at the moment.

I'm just going to run the programme just so you can see what actually happens, what's being held in day when this programme is executed.

So if you look there that first line of code that has been output that print there, it's actually saying zero.

And that's because today, while I'm filming it is Monday now for you, it could be a different day, couldn't it? But when at the day that I'm filming, it is Monday.

So there it's got a zero.

So what's being held in day is that value.

So instead of typing in a zero or a one or a two of the day of the week now we've actually got a module that inputs the day of the week, but as a value starting at zero for Monday, just like our table did.

So that's how a bit of code is working.

So now when it looks at this, instead of entering a number in, its just using the automatic day that's been used using this module and function.

So its saying if days less than four and it's going to run that bit of code, isn't it? So just run it one more time now I've got rid of that print for day and it's saying it's Monday, five days until the weekend, and that's because day was zero so it's run this part of code here.

And how does the programme know that it's Monday? Its 'cause at the moment you've only got that value zero for Monday, but how does it know it's Monday? Let's look at this if we go to here, because we've got the zero is less than four, then this condition is true, it's going to go to this print statement here.

So let's just take a look at that.

So it says it's, and then it's got a comma so it's going to print as well whatever's being held here and it's got that identifier day for the list days.

And then in the square bracket it's got day and being held in day is zero.

So that's going to print on the screen that it's Monday because it's going to print the item that is at the location zero in this list.

So that's why it's saying it's Monday.

What I'd like you to do now then is take a look at the second part of your worksheet, that it's called as the seasons roll on by and have a go at those activities.

Remember all that support is there.

So if you've got work, for example, or syntax checklist or those input output tables, that's all there to support you so make sure you read it carefully, pause the video and come back when you're done.

Let's take a look at the solutions then to those programmes.

Now you might've noticed that this programme that we were looking at the seasons programme was quite similar to the days of the week programme because it's got a list of items so let's do date and time, and we want to print.

We want to access those items in that list.

So if we take a look at this one, this was your first activity.

So, in order to print June, and it had an index of five, so you had to do print months and then with the five and then July was six and August was seven.

And this might have been a little bit tricky to get your head around, just because when you're used to writing the date, you're probably used to writing for June a six when actually it was a five because it was shifted by one.

So that might have been a little bit confusing there.

So did you get it right? Don't worry if you didn't, you can always have a look back and you can copy this code and try and get it to work in that sense.

The other thing that has to do is the month provided by the user is reduced by one before it is used as an index because list item numbering is zero-based.

So this is where you had to do take away.

One is to have an arithmetic expression there within those square brackets to ensure that it went for the correct month and not the one further ahead, which would have been wrong.

And then finally the condition that checks if this is a winter month is more complex because the numbering for winter months is not consecutive.

So, that involved lots of elif statements there as well and thinking about the different conditions to figure out what the winter months actually were.

So that might have been a tricky one there.

And if you did find that little bit tricky, as I always say you can just copy this code in from the video and see if you can get it to work now and that will help you understand this a little bit further.

That's everything for this lesson.

So hopefully you've had a bit of a recap into some of the things that you can do in Python which is the sequence and your selection.

And also you've learned a little bit about lists snag and that there are data structure and they have indexes that begin at zero and then how to access them.

So hopefully you've recapped and refreshed but also learnt something new as well about lists in this lesson.

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

See you again for lesson two.