Wednesday, June 13, 2012

2.2 Printing

Programming tutorials since the beginning of time have started with a little program called Hello,World! So here it is:
print "Hello, World!"
If you are using the command line to run programs then type it in with a text editor, save it as ‘hello.py’ and run it with
“python hello.py”
Otherwise go into IDLE, create a new window, and create the program as in section 1.4.
When this program is run here’s what it prints:
Hello, World!
Now I’m not going to tell you this every time, but when I show you a program I recommend that you type it in and run
it. I learn better when I type it in and you probably do too.
Now here is a more complicated program:
print "Jack and Jill went up a hill"
print "to fetch a pail of water;"
print "Jack fell down, and broke his crown,"
print "and Jill came tumbling after."
When you run this program it prints out:
Jack and Jill went up a hill
to fetch a pail of water;
Jack fell down, and broke his crown,
and Jill came tumbling after.

When the computer runs this program it first sees the line:
print "Jack and Jill went up a hill"
so the computer prints:
Jack and Jill went up a hill
Then the computer goes down to the next line and sees:
print "to fetch a pail of water;"
So the computer prints to the screen:
to fetch a pail of water;
The computer keeps looking at each line, follows the command and then goes on to the next line. The computer keeps
running commands until it reaches the end of the program.

No comments:

Post a Comment