KS2: Starting with Python 2:

This is the second lesson in teaching pupils how to use Python.

Aim: In this lesson they will learn to write an interactive program (which is a lot more fun) and also learn some more syntax and some extra notation.

Activity: It is probably better to demonstrate the first part of the lesson on a white board.

You need to start off with IDLE running i.e you are going to write your programme inside the Python ‘shell’.

If you are running this lesson straight after the first Python lesson, then you will already have this open. If not, you will need to open it again (this is explained in the first lesson). Don’t worry if your first Hello, world! programme is still visible.

Look at the top toolbar. The first tab on the left should say IDLE. The second tab says file. Click on this and choose New File, just as you would with most software you are familiar with. This will open a new window but you can see that the Python ‘shell’ is still running in the background (in the other window). Sometimes we can go back to this shell screen and use it as a scribbling pad, to try out new instructions very quickly rather than screwing up the programme we are working on. More about that later.

One new symbol you will need to learn is the #.  In Python, the hashtag is something you use anywhere in the programme that you want to write comments to yourself.  The # and anything that comes after it in the same line will not be read by the computer, they are just there to help you remember things. So your first comment might be giving your programme a name. Or you could make comments such as…

#remember to put the brackets in

#this is where I imported some code from another programme

#this is the chunk of programme that I used to make the person move forward

#making a loop

You get the idea – these are called comments. Basically they are just notes to yourself so that if you look at your programme months later, maybe because you want to re-use a nifty bit of code, it is easy to find things. This is really important if your programmes are quite long.  It also helps other people who might have to work on your programme.

So the first thing we are going to type is what we are going to call the programme we are going to write, which is about asking people their name. So we could call it:

yourname#YourName.py

#MyName.py

#LearningAboutHashtags.py

#SecondLessonInPython.py

#JellyAndIcecream.py

Anything at all, in fact!

It obviously makes sense to call it something related to what it does and it is good practice to put .py at the end so that it tells you that this file is a Python programme. I have called my programme YourName.py and it has a hashtag in front of it so it appears in red.  Your programme should now look like the one above – and you can see the Python shell screen in the background.

At this point it is probably better to let the children work in pairs or threes, depending on the amount of computers you have, and copy what you have just done.

Now they are going to type in the following commands. You can type in these yourself on the white board and they can copy.  At this stage I would not explain what all the bits of code mean – that can come later.

#YourName.py

name=input(“What is your name?\n”)

print(“Hello,”,name)

yournameMake sure you do not put any spaces between the symbols.

It should now look something like this.

When they have done this, they should save their file by going to the top menu bar, clicking on file and choosing Save As then typing in the name of the file.

The fun bit comes next. Ask the children to click on Run on the top menu and select the Run Module option.

Then look at the Python shell in the background and see what it says. It should say, in blue, What is your name?

Then get one child in each group to type in their name underneath the question. Press return and see what happens.  It should say Hello, followed by their name.helloJenny

Go back to the IDLE screen, click on Run Module again.  This will show up on the Python shell screen as a restart of the programme.

Let each child in the group have a turn. They will have to

  • create a new file and give it a name,
  • type in # followed by a comment (the name of the programme is useful)
  • type in the lines of code above
  • save their programme
  • run their programme (it will appear in the Python shell, not in IDLE)
  • ask another child to type in their name

You might like to ask some question about what they think the different colours in the code mean but at this stage, it doesn’t matter too much.

 

jen hughes

View my other posts

Leave a Reply

Your email address will not be published. Required fields are marked *