The next few activities can just be done in the IDLE Python shell (rather than saving them as programs) because they are very simple.

One of the simplest programs you can write is this:

    print "Hello World"

"Hello World" is known as a string.  A string should always be enclosed in either single or double quote marks (it doesn't matter which).

You can join strings together (it's called concatenation) using the + sign.  Try this:

You should get the following output

 

Can you think of a way to get a space in between Hello and world?  

What is the difference between these two print statements? (Try them to find out!)

 

Now try this:

Getting Input from the User

raw_input is a way to get the user to give some information.  Try the following:

(You can still type this in the IDLE shell window, although you will be asked to type your name before you get to put in the second line)

 
raw_input is a way of asking the user to type in a string. 

The string that the user enters is stored in a variable (or storage container) called name.  There are some rules about what you name your variables.  The main one is that they must start with a letter or an underscore_  After that you can have numbers, but you can never have punctuation or spaces in your variable names.

The = sign is known as an assignment operator (Don't confuse it with an equals sign as in maths).  Read it as name becomes NOT name equals

Greeting Two People

Write a program that asks two people for their names, stores them in variables called name1 and name2, then says Hello to both of them. 


Integers and Strings - Now for a Surprise!

Let's ask the user to put in two numbers and we will add them together.  (Should be simple enough for a computer eh?)

It seems my computer is so stupid that it thinks 3+5= 35

 
The problem lies in the fact that raw_input will always give us back a string, no matter what sort of data the user puts in.  So the computer does not think that the 3 and 5 I put in are numbers - it treats them just as if they were letters from the alphabet.

Strings and numbers are different types of infomation in Python.

To read in an integer use

n=int(raw_input('Type in a number'))

 

 Try this:

If you try to make your print statement  print 'Total is '+x+y you will get the following error: 

That's because you can't join (concatenate) strings and integers in your print statements.  You can fix it by turning the integer back into a string using str()

Write a program that will ask the user their name and age in years, then print out their age in days on their next birthday. (For this activity we will ignore leap years!)

If you are having trouble, click here to download a solution.