video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca your computing teacher for the programming part 1 unit.

Now for this lesson, you're going to need your repli.

it account that you should have set up in lesson 2 with your parent or carer's permission.

You also might want to have a pen and paper handy so that you make notes and answer any of the questions that I give you.

It's also a really good idea to remove as many distractions as you can so that you can really try and focus.

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

In this lesson, you will use meaningful identifiers, determine the need for variables, distinguish between declaration, initialisation as an assignment of variables, demonstrate appropriate use of naming conventions and output data.

I'd like you to start off by comparing these two user areas.

So take a good look at them and decide which person has the best user area and why? Pause the video while you think about that.

Well hopefully, you realise that the one on the right was the better user area, but why? So why is it important to name files correctly? That one on the left-hand side had all sorts of gibberish just random key presses, but the one on the right-hand side had proper names for each of those files.

But why is that important? It's important because if you want to look at those files again, if you want to look at all programmes that you've done and old solutions that you've come up with then you need to go back at your old programmes and you need to see what you've done.

Now, if you save all of your Python files as random key presses on your keyboard then it's going to be very very difficult for you to find any work that you've done before.

Giving something a sensible naming convention means that you can find that work again quite easily and quite quickly too.

So it's a really good idea when you're saving your Python files and any files for that matter to give them sensible names.

Now, if it was me, what I would do is I would have a folder for each of the lessons that you're doing.

So this lesson is all on variables.

So I would make a folder called variables and then any programmes that I created to do with that variables lesson, I would save in that folder and I'd give each file a sensible name, so based on whatever programme I was making at that time.

And it's a really good idea to get in a good habit of doing that.

We're going to be looking at naming conventions throughout the lesson.

In programming there are naming conventions that should be followed to make it easier to read and understand your code.

So just like with those file names we need to have sensible names for all of our other things in our programmes.

Python has an agreed set of conventions that programme should follow.

You will explore some of these during this lesson.

And you've got an example there on the screen and these are all variable names of which we're going to be looking at today.

Previously, you have written a programme in a sequence, used the print statement, used a subroutine, checked for errors in your code, and fixed common errors in your code.

This lesson, you will be introduced to a new programming construct known as a variable.

A variable holds a value in memory location that is needed for the execution of your programme.

A variable can hold one value at a time.

This value can change throughout the execution of the programme.

You might use a variable to keep track of the score for a game.

At the beginning of a game the score might be 0, just like we've got there in our example.

During gameplay, the player might earn a point.

So we want to add 1 to that point to what's already being held in score.

This would change the value held in score by increasing the value by 1.

A memory location needs to be allocated for the variable value before it can be used by the programme.

Declaring a variable means stating what data type will be used for the value.

Initialising a variable means setting the initial value.

I'm going to look at those two things in a bit more detail.

So Python doesn't have variable declaration only initialisation.

So I can't really show you an example in Python of what variable declaration looks like.

But here are some examples of variable declaration in visual basic and C.

So integer or int just means whole numbers, and both of these statements mean declare the variable age as an integer.

So you can look at that one for visual basic, the code that you would use to declare age as an integer is Dim Age as Integer, and then in a C you would just put int age with a semi-colon at the end.

Now initialisation is when the initial value is assigned to the variable.

Remember that the variables only hold one value at a time.

And this value can change throughout the execution of the programme.

Here are examples of variable initialisation.

This means hold the value 22 under the variable name age.

So in visual basic is just age equals 22 in a C its age equals 22 with a semi-colon and in Python its just age equals 22.

So fairly similar bits of code there for initialisation.

So let's take a look at some variable initialisation in Python.

Line 1 initialises the variable by calling it name and assigning the value Gerry at a memory location.

So that's, what's happening in at line 1.

Type this code into Python and run it to see what happens and then swap lines 1 and 2 around and read the error message that occurs.

So open up repli.

it, get a fresh Python file and just have a go typing that coded, pause the video while you have a go.

Excellent, so when you try to use a variable before it's been initialised, it will cause an error which hopefully you've just see when you were doing that in Python yourself.

Instructions are executed one after the other.

In this case, the error would be name, name is not defined and that's the error message that you should've just got when you did it the wrong way around.

Once a variable has been initialised with its first value, then it can be reassigned a new value throughout the execution of the code.

So think about this and what do you think might happen when this code is executed? Pause the video while you have a think about that.

Let's take a look then.

So it's going to output just Sam.

So were you right? Were you wrong? Did something surprise you about that? The programme will display Sam on the screen, this is because it was the last value that was assigned to name.

So even though we initialised name with Gerry because we then reassigned or assigned Sam to the variable name, when we actually went to print it it's only going to print the current value that's held in name.

Variables should always be identified in a meaningful way like the files from the starter activity.

So a, b, c, x, y are not helpful names for a variable.

If a variable is going to hold a name then call that variable name, all right? All programming languages have their own naming conventions.

A considerate programmer will read the documentation for the programming language to find out what the conventions are.

In Python, variable names should be written in lowercase with an underscore separating words if required.

And we'll be practising that in this lesson.

If you want to find more, find out more about that then you can pause the video now and just take a look at that short link, and it'll take you to the Python documentation.

So here's a recap then, 'cause there's quite a lot of new things that we just started there.

So declaration means to declare a variable as a certain data type.

So you saw those examples in visual basic and in C we want to make sure that we've declared that variable as a certain data type.

So that word declaration means setting it as a specific data type and Python doesn't have that only initialisation.

So initialisation means to set an initial value for a variable Python does have that.

Assignment means to a change the value held at the variable location.

And a variable must be initialised before it can be used.

Meaningful identifiers are essential, and also following the Python conventions with naming is also a very good idea.

Now you're going to have a go at creating your own programme now.

So you're going to use the worksheet to predict, run, investigate, and modify a silly sentences programme.

And the final task is to create your own silly story.

So have a go at that and then I'm going to go through the solutions with you after you've had a go.

Make sure you read it really really carefully while you're doing it.

Pause the video, well you have a go.

Excellent, how did you find that? Well, I'm going to go through this solution now with you.

So watch the demonstration to see how you could solve a silly sentences task, and hopefully that will help if you struggled a little bit there and you need a little bit of support as you going along then you can watch me going through it and hopefully that will help you figure somethings out.

And also we'll hopefully confirm or you got it right or you weren't quite sure and that kind of thing.

So watch the demonstration and I'll go through that with you now.

So here's the programme that you've just been using in your activity.

And I'm going to go through all that activity with you now, so if you did struggle with any of it along the way, then hopefully by watching me then it should help you and feel free to code along with me if you were struggling a little bit there and if not, if you didn't struggle, hopefully it'll just confirm that you got things right along the way.

So have a go and watch with me and see how you go on.

So, first of all, you're asked to make a prediction and run your programme and you might've been right, you might've been wrong, something might have surprised you with that, and that's completely normal this completely brand new code to you.

So you've probably not seen anything like this before so it's quite tricky to make a prediction about it but it's good to read it and see if you can make a prediction.

So when I run this programme, this is what happens.

The car was loud when it gently went to school and the zebra was giant when it aggressively went to school.

So that's what the output was.

That's quite some silly sentences in there.

So you're asked to make a prediction and then run and see if your prediction was correct.

Then you are asked to do some investigate tasks.

So for your first investigate task, you were asked, in which line is the variable objective initialised? Now, if you remember a variable is initialised when we first assign it at the very beginning.

So I just need to go to the very top of my code and look where adjective appears first.

And it's here it's line 3, so you should have got line 3 for that.

Then he says, in which line is the variable adverb first reassigned.

So it's not going to be this one because this is where it was initialised, so I need to find where it appears again, and that's down here on line 6.

So the answer would be line 6.

You've then got, when is the variable noun first used? Now that's quite a tricky question.

So where is it being referenced in our programme but not initialised or assigned? So if we look here, we've got print f, the noun was objective when it adverb went to school.

So actually it was used first on line 4, when it was used here.

So that might have been a tricky one to get.

Then he says, is there a difference between the code in line 4 and line 8? So let's take a look, I'm just going to expand the code a little bit so I can read it properly.

So line 4 has got print f and then a speech mark, the noun was adjective when it adverb went to school and then on line 8, I've got print f so same thing again and the noun was objective when it adverb went to school.

So they are exactly the same lines of code.

On line 4 remove the f after print and before the speech marks, that was my next one.

So on line 4 remove the f there, so I'm removing the f, run the code and write down what happens, so let's have a look.

So what's happened is it's gone the noun was objective when it adverb went to school.

So now it's not displaying what's being held in those variables, it's just displaying the entire print statement as it is.

And then it says to put the f back, so I'm going to put the f back and just make sure it's still working, yes, it is.

So the next one, what do you think the f is used for? So that's quite a tricky one really, but this is just what you think it's useful, so, it's your opinion.

But what actually is happening there that f is something called an f string.

And you can go to the Python documentation and find much more about f strings if you would like to.

But what it's basically doing is it's just telling our programme with that f is saying I'm going to display some variables within this statement, and those variables are going to be used using those curly brackets.

So that f is pretty important for this to work.

So then you had step 7 and step 7 says on line 4, remove the curly brackets that surround noun and run the code again, what happens? So I'm going to remove it, I've given you a little clue there about what might happen.

So I've removed it and then I'm going to go to run, and now it's just simply saying the noun was loud rather than what's being held in the variable.

'cause those curly brackets are really really important with x strings.

So when we want to do an f string, there we go, when we want to do an f string, we have to have the f there and we have to have the variables that we wanted to use surrounded by those curly brackets.

That's really important syntax that you're going to need to remember.

And then he said for step 8 why does it not display the cars zebra was loud giant when it gently aggressively went to school when the code is executed.

Now, hopefully you've remembered that a variable can only hold one value at a time.

So the first time that we reference these variables then what's being held in those variables is so for noun it'd be car, for adjective it's gently and for adverb it's loud.

So that's first time when we referenced those variables that is what's being held in those variables.

So that is what is displayed as output.

Now, the second time we have assigned new values to those variables.

So we've got zebra, aggressively, and giant.

So the second time this line of code is executed, we've got here noun is now looking at zebra, adjective is aggressively and adverb is giant.

Because different values have now been held there.

We can't hold more than one value in a variable.

So that's why it hasn't displayed both of those values.

Now, then you were asked to modify the programme.

So this is where you start making the programme a bit more your own.

Now you'll have done slightly different ones to me but I'm just going to do what I would do off the top of my head.

So step 1 was to change all values in both occurrences of noun, adjective, and adverb to something different.

So I would just think of a different noun so I could have plane, and then instead of gently I could have softly and instead of loud, I could have quiet, there we go.

And then for the next lot, I could have monkey, I could have calmly and then instead of giant I could have huge.

And then when I run this programme now the silly sentence has changed slightly.

So the plane was quiet when he softly went to school, the monkey was huge when it calmly went to school.

So I've got slightly different silly sentences there.

So then what else did it ask is to do then? So make a change to your code that will ensure that the second print statement displays a, oh no I've missed one, there we go.

I've missed one, it says, step 3.

Step 2 even I've missed step 2, I'm going to go back to step 2.

So create a new variable called proper noun and initialise it as London.

So I'm going to go over here and I got a proper noun, and I'm going to hold in that variable London.

And then it says, if I'm ready nothing's going to happen 'cause it would just held London in a proper noun we haven't referenced it.

And then it says, replace the word school with proper noun in both print statements.

So I just need to go over here and I need to put proper noun, and because I'm using an idea and it's got this helpful tips I just have to start writing it and then it carries it on for me.

I do the same thing, so prop, there we go and then it's finished it off for me.

So now when I run it, instead of saying school is going to say London instead.

Then it says for step 4, make a change to your code that will ensure that the second print statement displays a different proper noun.

So the moment we've initialised proper noun and then we've referenced that same value twice here before I want to reassign, if I want to assign again I need to copy that code and paste it there, I just did Control + C Control + V to copy and paste, and then I can put a different proper noun in so I can put Birmingham.

Like that and I can run it.

So the plane was quiet when it softly went to London, the monkey was huge when it calmly went to Birmingham.

So I've got some different silly sentences going on there.

And then the final modify step was to add a completely new silly sentence to the bottom of the code, you can use the same variables, but think of a different sentence to write.

So I need to have print, I need to have that f, which is really really important, and then the speech mark, and then I can put a and then I'm going to have noun a noun went adverb, I can't spell, I should just be using the helpful tip, there it is adverb a noun when adverb, now I'm going to put objective there to proper noun and then I'm just going to put adverb at the end.

I don't know what this is going to do, it's going to be very weird I think.

Now I'm just going to put a speech mark there, but if you notice you've possibly had this problem yourself.

If I put one speech mark there at the end, it adds it's trying to be helpful.

And then if you try and delete it, it gets rid of both of them, but I still need a speech mark there.

So it's a little bit annoying, 'cause I want to put a speech mark, but it won't let me put one.

So what I have to do is I have to go to the right one and delete that one and then it works.

And I know it's working now because of the syntax colouring that's using and I've got this white bracket there.

So if you had a problem like that similar that's how you should have fixed it.

So I'm just going to run it now, and my final sentence, a monkey went huge to Birmingham calmly, it doesn't even make any sense at all.

But I have practised using f strings and displaying those variables in a print statement.

So your final task was a make task.

Now I'm not going to show you how to do that because you can use this code here now and adapt it to make your own silly sentences or some silly stories that you want to come up with.

So hopefully you can use that code to do that, the important thing there is to just check your syntax, that's why we have these practise programmes so you can go back and you can see what that syntax was that was used 'cause you've going to remember the f, the curly bracket, the speech marks, the normal brackets.

It's quite a lot to remember.

So always go back to your old programmes that you've done to see what syntax you use there, and that's going to help you out.

So that's your demonstration, hopefully if you didn't quite get there you can use my tips and explanations to have a go yourself now.

But if you did get it all right first time, well done give yourself a pat on the back too.

Well done, you've just finished lesson 3.

So hopefully now you've got a better understanding of what variables are and how they work in our programmes.

And then also have display them as output using f strings.

You should also know those important words like initialisation, declaration, assignment quite key words that you need to be aware of.

That if you'd like to, please ask your parent or carer to share your work on Instagram, Facebook, or Twitter tag in @OakNational and #LearnwithOak, and perhaps you'd like to share your silly sentence programmes with me today.

And I'll see you soon for lesson 4.