1 Why program? Get started with Python

Learning Objectives

  • Reflect on the advantages of learning to code.
  • Understand why Python is an ideal first language
  • Get started using Python.

Learning Outcomes

All must complete a “Hello World” program, use Python to ask a question and print the answer to the screen. (Level 5)

Most should complete the above but adding some meaningful comments to the code. Use Python to perform simple maths. (Level 6)

Some could complete the above, as well as adapting a program to ask the user for a question, then perform a calculation on the given number. Use detailed comments in code (Level 7)

Keywords

Words to learn: coding, program, input, output

Starter

Watch this video below which shows the importance of coding:

Now discuss and write down 4 reasons why everyone should know how to code. If you finish quickly, hit the back button on your browser and look why Python is an ideal first language to learn properly.


Main

Let’s get started with Python.  Some of you will have seen this before, but it’s a very good idea to revise the basics before we make the new Call Of Duty!
First make a folder called “Python” in your (H): drive.  Now click the windows button, then “All programs”,  then “ICT programs”, and finally, the “Python 3.4” folder.  In there you’ll see a program called “IDLE (Python GUI)”. Open it up and you should see this:
python2
**Important**  This first window that you can see is called the Python “Shell“. This takes care of running your programs.  It is not intended for writing your programs in!  If you start to type your code here, it will try to run each line you type, and soon give you some very interesting error messages!

Now you should click on “file” then “new window” and you’ll see something like this:
python3
You can now start to type programs into this new window – your programs will then run in the shell.

Try typing this (the traditional first program!!):
python4

Make sure you type exactly what you see…
Now click “Run”, then “Run Module”.  You’ll be asked where to save your program.  Make sure you call it “hello” and save it to your (H): drive (in your Python folder).

Now, your first program runs!  Well done!  The program outputs our message to the shell window.

Let’s add a bit to it:
python5 Now run this.  Note how each line of code gives a separate line of output.

You can try this with your own messages if you like…

 


Input

So far we’ve printed messages (actually called “Strings”) to the shell.  Now we’ll take some information from the user of our program:
python6

Run this, and you’ll see the first line is printed out, and the program waits at the second line-
veg=input() 
It will pause here until you type something and hit return.  Whatever you type will be saved in a block of memory with the label “veg”. (This is called a variable –  more on those next lesson!).
Now the program will run the third line where it prints whatever value is in “veg”  followed by the message “is a really tasty vegetable”.

Now answer these questions in your book/folder:

  1. I’ve just had a great idea for a program.  I’ll open up Python and type it straight into the shell.  Is this OK?  Why?
  2. What happens when I type (exactly):print “I love computing”)

    How do I make it work?
  3. Write a program that asks for a name, and outputs a welcome message, including the nameFor example:Computer:”What’s your name?“.
    I type “Sally“,
    Computer: “Welcome Sally“.Test your program before you write it down.
  4. Add to the program above so that it then asks “How are you?“, takes my reply (for example “great“), and responds with “I’m glad that you are [my reply]“.Extension:
  5. Write a program which asks for the user’s name, then age.  It stores each value (as above) then outputs:
    Your name is [user’s name] and you are [user’s age] years old! 

Python Does Maths for you!

Start a new program (click on “file”, then “new window”), and type:

print(5+5)

Now run this, saving it as “sums”.  Great!  Python solves the problem for us.  Try this:

print(48-6)

Python now does subtraction as well! This is really useful in more complicated programs, but there’s more…

Try these lines of code:
print(12/4)
print(6*7)

Questions (write the answers in sentences in your book/folder):

  1. in the sample code above, what did each of the symbols, + ,- ,/ ,* mean?
  2. Try this:
    print(2**2)
    Now change the numbers a little and see if you can guess what ” ** ” means (clue- think of power…).
  3. What about this?
    print (21//10)
    (clue- whole numbers)
  4. Finally, the strange one:
    print(13%4)  (Clue – leftovers!)Extension:
  5. Write a line of code which outputs “2+2=4“.  Sound easy?  You can’t use the number 4 in your code!!
  6. Now change the program so that it asks me my name, and then outputs
    “[my name], 2 plus 2 is obviously 4 !”Again, you can’t use number 4!!