Python Revisited

Learning Objectives

  • Get re-accustomed to using Python.
  • Explore input/output at a very basic level.
  • Look at mathematical functions

Learning Outcomes

All must complete a “Hello World” program, use Python to ask a question and print the answer to screen (input/output)  Make sure code is well commented.

Most should complete the above, but adding some meaningful comments to the code. Use Python to perform simple mathematical functions.

Some could complete the above, as well as adapting a programs to include a higher level of formatting control (using escape sequences)

 

Starter

Your Teacher will explain the structure of the controlled assignment, and explain what you will be expected to do.

Main

Let’s get back into Python.  You’ve all seen this before, but it is a very good idea to revisit the basics, as these skills are used at ALL levels of the controlled assessment.

First make a folder called “Python_Revision” 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**  Remember that this first window that you 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, the interpreter will 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 simple 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…

 

As we’ve already mentioned, you will constantly need to output messages to the screen throughout your CA tasks!!


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”. (remember this is called a variable –  more on these later!).
Next the program will run the third line where it prints whatever value is in the variable labelled “veg”  followed by the message “is a really tasty vegetable”.

**IMPORTANT REMINDER** Don’t copy code and paste it into IDLE (or any other IDE for that matter) Chances are that characters and symbols will not be quite what you wanted, and your code won’t run!  Why do you think this is?

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 name (for 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!!

 

Escape Sequences and Formatting Text:

Have a go at running this code:

print(“\tQuestion what goes woof\t\tdogs\t\t\trabbits”)

print(“\n\nwhat kind of snake is good at maths?\n\nAn adder\n\n”)

print(“\n\nGoodbye\n\n”)

What happens when then interpreter reaches the ” \ ” ?
It treats the next character as a none-printable character.  In other words, the letter immediately after “\” will give the interpreter a special instruction on HOW to display the text which follows.  This is an Escape Sequence.

Here are the common ones:

ESCAPE WHAT IT DOES.
\\ Backslash (\)
\' Single-quote (‘)
\" Double-quote (“)
\a ASCII bell (BEL)
\b ASCII backspace (BS)
\f ASCII formfeed (FF)
\n ASCII linefeed (LF)
\N{name} Character named name in the Unicode database (Unicode only)
\r Carriage Return (CR)
\t Horizontal Tab (TAB)
\uxxxx Character with 16-bit hex value xxxx (Unicode only)
\Uxxxxxxxx Character with 32-bit hex value xxxxxxxx (Unicode only)
\v ASCII vertical tab (VT)
\ooo Character with octal value ooo
\xhh Character with hex value hh

Here’s a tiny piece of fun code to try out:

while True:
    for i in ["/","-","|","\\","|"]:
        print "%s\r" % i,