Python, part 4


Basic input and output.  Most programs need input and output--
most usually, input from the keyboard and output to the screen.
But there are also other forms of input--from a mouse cursor,
from a data file on a hard disk, from a remote computer--e.g.
you're visiting www.newkids.com on your web browser and
expect nice pictures and text to appear on your browser sent by
the new kids on the block web server--etc.  There are other forms
of output--to a laser printer, to a data file on your hard drive,
outputting requests for the photo on Donny at newkids, etc.
In this course, we'll be concentrating on input from the keyboard
and output to the screen. 

OUTPUT.  We've seen what happens with
    print "Hello World!"
and you may have tried something like
    print "Hello, Ludwig!"
    print "You're a great guy!"
which gives you two lines of output.  Adding a comma after the
first print statement here--i.e  print "Hello, Ludwig!",--
a small and subtle difference (it goes after the quotes) will result
in the next print --print "You're a great guy!" --printing on the
same line as the first print.  You can also use the newline escape
sequence--i.e. a line feed character--to get a new line.  This
uses a backslash-n  (i.e. \n) which is special, and tells the computer
to go to a new line--e.g.
    print "Hello, Ludwig!\nYou're a great guy!"
which prints
       Hello, Ludwig!
       You're a great guy!
It's easy to print out the value of a variable--the name of the
variable must not be in quotes.  Anything in quotes gets printed
just as you ask.  e.g.

      age = 22
      print "The value of age is:", age
note the comma between the string in quotes and the variable name.
      print "I am ", age, "years old!"
prints the I am part (note the space I've left after the "am") followed
by the value of age, followed by the years old part.  You can mix the
values of variables with quoted strings.

INPUT.  We need to be able to ask the user for input, and to put those
values into variables.
    <variable> = input ("<prompt>")
The prompt lets the user know that he/she is expected to type in input
at the keyboard (with a return)--this input is stored in the indicated
variable.  e.g.
     age = input("Please type in your age:")
The computer prints out the indicated prompt on the screen and waits
for the user to type in input (with a return).  This kind of input is
useful for numeric values.  If you want to input character strings, then
things are a bit different:
     <variable> = raw_input("<prompt>")
e.g.
       name = raw_input("Please type in your name:")
The user type Ludwig Snarf with a return, and the variable now holds
the character string "Ludwig Snarf"