video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher, and I'm going to be taking you through the Introduction to Python Programming Unit.

This is one of my favourite topics by the way, so I hope you're going to enjoy it as well.

For this lesson, you're going to need; a pen, paper, scissors, thinking, why on Earth do I need scissors for a programming lesson, and a ruler as well.

So two things you're probably thinking, why on Earth do I need those for this lesson? Well you'll find out very soon.

You're also going to need a Repl.

it account.

Now you will need to ask your parent or carer if it's okay for you to have a Repl.

it account and make sure you do get permission and get them to help you set that up, but I will be showing you how to do that this lesson as well.

So also don't forget, remove any distractions that you can too, just so that you can focus as best you can, and once you've done all of those things, we can begin.

In this lesson, you will define the term algorithm, you'll discover the Repl.

it environment and you'll write your first Python programmes.

So let's get started.

For this activity, you're going to need a piece of paper, a pen or pencil, a ruler and some scissors.

So I've got all those things ready and I'm going to get started because we've got to make two strips, one 18 centimetres long and one 12 centimetres long.

So I'm going to do that now.

I'm going to go to this side, get my ruler the right way round, and I'm going to do 18 centimetres like this.

And it doesn't really matter what the width is, but I'm going to choose three centimetres like so, and then three centimetres up here as well.

Like so.

Now it's important to try and be as neat as you can.

It's not always going to be perfect.

It would help actually, if you had squared paper for this but I haven't got squared paper.

So 18 centimetres, that's my first strip and my next stretch is 12 centimetres.

So I'm going to try and be as accurate as I can, 12 centimetres, make my little mark and then go to three centimetres across, and three centimetres across, and I'm going to draw my line and I'm going to draw my other line, now I've got the layout that I need.

Next I need to cut it out, and it's important to try and be as neat as you can.

I always mess this up a little bit because I can never do it perfectly.

So I'm going to try my best.

So that's my 18 centimetre strip.

I think I've done that slightly short, but oh well, and then my next one is my 12 centimetre one that's it and now I can put my piece of paper to one side 'cause I don't need that anymore, and now I've got my two strips that I need.

So my 12 centimetre and my 18 centimetre strip.

Now I'm going to start looking at the instructions that are on the slide deck, so it says you have two strips of paper, one is X centimetres long, the other is Y centimetres long.

So if we say this is X, what's been held in X at the moment is 18 centimetres because I've got an 18 centimetre strip, and then if we say, this is Y, what is being held in Y at the moment is 12 centimetres because it's 12 centimetres long.

So X is 18 and Y is 12.

So the instructions are, align the two strips at one end and I've done that.

So align two strips at one end.

Next instruction, cut the longer strip where the shorter strip ends.

So what I've got to do, I'm going to draw a line 'cause I want to try and be as neat as I can, I'm going to draw a line before I cut it, like so, and then I'm going to cut it with my scissors.

And it says, discard the part that you have cut off that is the same length as the shorter strip.

So this bit here, we need to discard 'cause we don't need that anymore.

And it says, repeat A and C until the strips are equal in length.

Now at the moment, they're not equal in length, so I've got to repeat it.

So I go to that one, I draw my line, try and be as neat as I possibly can.

And I've got to cut it, and then I discard the bit that I have cut off the bottom.

Now, are they an equal length now? They're just about equal, I've done that fairly neatly.

Let's just see though how accurate I am when I go to measure it, 'cause the next bit it says, measure that length and report it, it is the required result.

So let's measure it and see how many centimetres is.

I might have to measure both pieces of paper.

So this one is six and this one's six.

I've done that.

I'm quite impressed with my cutting there.

They're both six centimetres.

So those three numbers are really important.

Remember we had the longest strip, which is 18 centimetres, then we have the shorter strip that is 12 centimetres and we have been left with six centimetres.

So let's see what those three numbers actually mean.

What we just did there was Euclid's method.

Following those instructions solves a problem.

It calculates the highest common factor of X and Y, and if you remember, our X was 18 and our Y was 12 and then we were left with the number six.

So the highest common factor of 18 and 12 is six and six fits into 18 three times and it fits into 12 two times.

This algorithm was recorded by Euclid in around 300 BC.

That is a super long time ago, isn't it? This is the granddaddy of all algorithms because it is the oldest non-trivial algorithm that has survived to the present day.

Have you heard of the term algorithm before? An algorithm is a set of precise instructions expressing some sort of language either textual or visual.

So if you've ever had some flat pack furniture that you've had to build, you might find there that you've got some visual interpretations of an algorithm because you're following the pictures in order to build that piece of furniture.

So that's a bit of an example of a visual algorithm.

Understanding the language is necessary in order to execute the instructions.

Executing these instructions is meant to solve a problem, just like we did with Euclid's algorithm.

We created, we found out the highest common factor by following those instructions in the algorithm.

A programme is a set of precise instructions, just like an algorithm, expressed in a programming language.

Translating the programming language is necessary for a machine to be able to execute the instructions.

To execute a Python programme, you need a Python interpreter.

This is a programme that translates and executes your Python programme.

The Python interpreter does not necessarily run on your computer.

All programming languages have rules for syntax, or how statements can be assembled and all languages have rules for syntax as well, or how sentences can be assembled.

Programmes written in a programming language must follow it's syntax and speech or text in language must also follow a syntax or else we won't understand each other.

And programmes with syntax errors can not be translated and executed, but humans can infer meaning even in cases when syntax tools are violated.

So this is where they differ.

For example, tonight see you instead of see you tonight will probably still be understood.

In Scratch, Syntax errors are not possible.

Rules are enforced by the blocks and the way they fit together.

You can still make logical errors though and that is when your programme does not work the way it should.

Here is an example of some Python programming.

In Python you can, and you will, make syntax errors.

You will need to follow the syntax rules.

Syntax errors can be really, really frustrating and when you start learning a text-based language programme, this is quite common and it's going to happen all the time and the sooner you learn how to fix them, the better and just get over that hump, it will be much better for you.

So here's some examples of some syntax errors and don't be overwhelmed by these errors.

They are here to discourage the faint hearted and you can fix them.

So we're going to take our first steps in Python now.

What I'm going to do is I'm going to give you some live coding and this is just to show you the environment that we're going to be using, that Repl.

it account that I mentioned at the very beginning, and also to just show you some of those syntax errors that you might come across and how to fix them and how to spot them and things like that.

So I'm going to do a demonstration now for you to have a look at.

You can find Repl.

it by doing a Google search and you can see what I've typed there.

It's R, E, P, L.

it and if you do a search for that, then you'll find Repl.

it for you to use, and I need you to ask your parent or carer to help you set that up and also give their permission to allow you to have an account with Repl.

it as well.

Now, once you've signed up, you'll be able to see this screen, and this is your home screen and here you can access all of the programmes that you've made.

So I've got my Repls there and that's where you would find anything that you've done before and then all the way over here is a plus sign, and this is where you make your brand new programmes.

So I'm going to click on the plus sign now and it tends to always put Python at the top, but if you've had a look at some other ones, or if you try programming in a different language then it tends to remember what you've done and puts that language at the top.

So for me, I've got Python at the top there.

So I'm going to click on that.

It also gives you a random name to call it but I never think that's a good idea.

I always think it's a great idea to name your programme sensibly and usually your first ever programme is called Hello World.

So I'm going to call it Hello World to keep up tradition.

And then you click create Repl.

It takes a little while.

There we go.

And this is your development environment.

So this is where you can type your code, so this middle section here is for typing your code and then you can run, so you can execute your code here and then the output is displayed in this panel here.

So that is your development environment, it's translating it and executing it and the displays is there.

Now we've got some options here for you.

Now I don't like this with the white background and the black text.

I like to have the darker background just because I get migraines and having lots of white background on my screen isn't good for my eyes.

So what I tend to do is I tend to go to settings and I change the theme.

So if you look here, you've got light and you've got dark and if you change it to dark, then you get this mode here.

The other thing you've got is font size and now this is really, really handy for if you need it to be a larger size.

So you've got huge, which I think for me is a little bit too big, so I think I'm going to stick with a large.

Actually, no, I think we're going to stick with huge, just so that you can see everything clearly.

Let's give it a go, we might have to go a bit smaller later on as our programmes get a little bit larger.

So that's my settings then I can just go back up here and that takes me to it.

Now, this is where you type your Python code and this is where your Python gets executed.

So I'm going to type my first programme now which you're going to have a go at too and it is to print Hello World.

Let's just take a look.

So I've done that super, super fast.

So what I've done is I've used the print function which displays output on the screen and inside the parameters of the print function I have typed the words, Hello World.

So make a prediction, what do you think might happen when I run this code on the right-hand side, can you make a prediction? Let's have a go, let's run it and see what happens.

Wah hey, so it says, Hello World on the screen.

So that is my first ever programme which you're going to have a go as well soon.

So anything that I've put in here will be displayed as output, so I could put, hello Rebecca, and it would run it.

So it does exactly the same thing, but now with hello Rebecca instead.

Let's just put it back to Hello World.

Now let's talk about those syntax errors, okay? You're going to get loads of syntax errors, so it's time to get used to them.

And a lot of common syntax errors that happen are things like forgetting the speech marks, for example.

So if you don't have the speech marks there it thinks you're trying to use something called a variable and that variable won't exist at this point because you haven't got a variable called Hello World plus there's a space so it's going to get really confused.

So if I run it now, yeah, it's got very confused and it said, invalid syntax up here, it doesn't quite know, it doesn't understand what it's got to do with that so that's where it hasn't worked.

Another thing is some people, they put the open bracket but they forget to put the close bracket.

So let's just see what happens now.

So again, an unexpected error has happened.

And then the other thing that people do quite a lot is they put a capital P at the beginning.

So if I run it now you'll see, it's looking for the function print but with a capital P and that doesn't exist.

So it's not working, it's saying print is not defined.

So I have to change that back to the lower-case print.

So you're going to get all random problems like this just because of tiny little mistakes in the syntax.

So remember the syntax is those rules that the programming language needs so that it can interpret those instructions that you've given it correctly.

Otherwise you're going to get syntax errors.

So the more we do, the more I'll point out these common syntax errors and that should hopefully help you spot them and fix them yourself.

So you're going to have a go at doing this yourself in a minute.

So your task then is to go to Repl.

it, set up your account if you haven't already and have a go at displaying Hello World by yourself.

Go back and watch that video again if you need to and there's a little explorer task for you too, which is to edit code so then it says something else.

So pause the video while you have a go at that.

Awesome, so you took your first steps in Python and you've done your Hello World programme.

You will need to use print whenever you wish to display something to user.

The print function is similar to the say block in Scratch, if you've used Scratch before.

So now we're going to look at assignment in Python.

You will need an assignment statement when your programme is to use a name or an identifier to keep track of a value, and you've done this before in Scratch, possibly, where you've had a variable and you've set that variable to a certain value.

So I'm going to use live coding again to show you how to use assignment in Python.

So here's our new programme now that uses assignment.

Now here we've got something called a variable and a variable holds the value.

The identifier for this variable is user.

And what we've done is we've used assignment to assign the value Claude to the variable user.

So the word Claude is being held in the variable user.

You'll also notice that round the word Claude, I've also got quotation marks or speech marks, and this is just symbolising that this data type that I'm using is string or text.

Now if you look here, we've got that print function again that we used before, but instead we've got this new bit here now, this comma and user, and this user is pointing to the value that is held in the variable user.

So when I execute this programme, what do you think might happen? Let's run it and see.

I'm going to run it.

So it said, Hello Claude.

So that means that what it's done is it's used that print function, it's used that text, hello, but he's also accessed the value that's held in the variable user and it's displayed that as well.

If I change the value that is held in user to Rebecca, then what's going to happen now? Let's just have a look.

It changes and this value can change throughout the running of the programme.

So I could have user Rebecca, user Claude.

Now, it's a tricky one.

What do you think is going to happen this time? Let's have a look.

It's just said, Hello Claude.

And this is because, initially the variable held the value Rebecca, but then it changed because now the variable holds Claude which overwrites Rebecca 'cause we're pointing to a new value and we've only said print once and that's after both these two instructions have happened and it's just going to print whatever is currently held in the value user, which is currently Claude.

So it's time for you to have a go now.

Using that demonstration to help you, what I'd like you to do is edit the programme that you've already done, so that it now holds a value Claude in user and that it displays the username, so hello, Claude.

So have a go at doing that and if you want to use different name as well, you can go and do that.

So just pause your video while you have a go.

Brilliant, so you're now going to start having a look at extending the programme, and this is a bit more of an independent task, but don't worry I will go through it afterwards, after you've had a go.

This is all going to be on your worksheet, so I'm just going to go through it with you very quickly first.

So you're going to be extending the existing programme from the previous task by typing in two similar additional statements.

Note that line four is incomplete.

So you can see there on your screens that you've got some programme code from Python code and then line four has got a faded bit between the two brackets, and what it wants you to do is think about, what actually needs to go in there? So, first of all, I want you to extend the programme.

I want you to complete line four so that the output of the programme is Hello Claude and then my lucky number is 13.

So have a think about that, what you're going to need to do, what coding you need to reuse and just change a little bit so that you can do that line four properly and then make sure that your programme displays the value of the lucky variable, not just the number 13, and then have a go at it, run it.

Did it work? Did you get any error messages? And if you did get some error messages, that's absolutely fine, but I want you to write down how you fix them so you can start keeping a log of how you fix things so that when you come across problems in the future, then you'll know what to do.

Okay? So this is all on the worksheet.

So you can go to your worksheet now and find those tasks for you and just complete the first section and then resume it when you are done.

Fantastic, so let's just see if you got it right and if you didn't, don't worry.

This is your first lesson ever in Python, so don't worry if you made a few mistakes along the way.

On your screen now you can see the correct answer and let's just have a look at it.

So user is a variable.

It is assigned a string value and lucky is another variable and it is assigned an integer value.

So an integer is a different kind of data type and this is to do with whole numbers.

So one, two, three, four, five.

So when we say whole numbers we mean not decimal numbers, like 1.

6 or 5.

789.

We're talking about whole numbers without those decimal points, so one, two, three, okay? And hopefully you've got it right and you've reused what you saw with print Hello user, you've replicated that for the final line of code and you've just put my lucky number is, and then that comma and lucky.

Don't worry if you didn't.

If you didn't manage to do it, you can go back and try now, can't you, and see if you can get it to work at this point.

But yeah, don't worry if you didn't and try again.

It's always a good idea to just keep trying these things.

Next problem then is to change the programme, and change your programme so it now looks like this example, and again this is all on the worksheet.

So I've given you three lines of code there, just to have a look at what you've currently got and then to change it to this instead.

So see if you can figure out how to do that.

Again, you might throw up a few error messages, syntax errors as you start moving things around, things get a little bit more confusing, again, try and fix them and if you can't, I'll help you at the end.

And then before running your new programme, make a prediction about what might happen at line two and what the output might be.

So if you look there, you've got something new, I haven't shown you this yet.

So line two has got something called input with two brackets next to it.

So what could that do? That's an interesting one.

And then you've got also underneath, you've got that same line of code that you've already done which is print, hello and then user.

So I want you to make a prediction as well.

So change your code, make a prediction.

And then there's a few more tasks on there as well to get you thinking about this too.

So just pause the video now and get on with the next part of your worksheet.

So what did it do then, that input line? What did it actually do? And then how, what did it output as well? Hopefully you figured that out from the worksheet.

You will need the input function when your programme must receive keyboard input from the user.

So that's when you'll use that input function.

So you've got the print function and you've got the input function.

Those are two new things that you've learned this lesson and variables, how to assign to variables, so that's three things.

So again, this is the input function.

This is something you're probably going to use quite a lot.

When input is invoked, the programme pauses waiting for keyboard input.

So it will just sit there and wait until something is typed in and the enter key is pressed.

The text typed by the user is assigned to the user variable.

Remember that's that word, assigned, assignment, assign.

This is where the value of the variable that's held is either initialised, so the first thing, when you first set it up you assign a value or throughout the running of the programme also, we can change the value to become assign it again and put different values in there.

So that's a new word that we've be looking at today.

And we can refer to the value of user in the programme without knowing what it will be.

And that's why they're so handy having variables, 'cause we don't know what the user's going to type, we can't predict that, they could type anything.

So using that variable means that it'll just display whatever the user has typed in, and you'll see if you've been using Scratch before, this is just like having that ask block and then setting their answer to a variable name, like user.

So it's just very similar code to that.

It's just that now you've got to type it all in by yourself.

So here's a little recap.

What I want you to do is just have a look at your screen and I want you to locate the fragment or fragments of code that display a message for the user on the screen.

So can you do that now? Which one displays a message for the user on the screen? Shall we have a look? Okay, so anything that uses the print function is going to be displaying a message for the user on the screen.

And then we've got retrieve what the user types on the keyboard.

So which bit of code there retrieves what the user has typed on the keyboard, let's have a look.

Which one do you think is? Yeah, it's the input function.

That's where it prompts for keyboard instructions and it'll sit and it'll wait for those instructions and then assign a value to a variable.

So where is assignment happening in that programme? Let's have a look.

Yeah, so this is going to assign whatever that input was that was typed in from the keyboard, it's going to be assigned to the variable film.

So that was a quick recap, wasn't it? I hope you've learned a lot.

We've covered a lot in this lesson, haven't we? So I hope you've done really well.

If you'd like to, please ask your parent or carer to share your work on Instagram, Facebook or Twitter tagging @OakNational and hashtag LearnwithOak.

We'd really love to see those Hello World programmes because it's such an exciting thing to do, your first ever programme.

So it'd be great to see that and to see what you've been doing this lesson.

So please join me for next time.