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.

2.1 What you should know


You should know how to edit programs in a text editor or IDLE, save them to disk (floppy or hard) and run them once
they have been saved

CHAPTER TWO


1.5 Using Python from the command line


If you don’t want to use Python from the command line, you don’t have too, just use IDLE. To get into interactive
mode just type python with out any arguments. To run a program create it with a text editor (Emacs has a good
python mode) and then run it with python program name

Tuesday, June 12, 2012

1.4 Creating and Running Programs


Go into IDLE if you are not already. Go to File then New Window. In this window type the following:
print "how are you"
First save the program. Go to File then Save. Save it as ‘how.py’. (If you want you can save it to some other
directory than the default.) Now that it is saved it can be run.
Next run the program by going to Run then Run Module (or if you have a older version of IDLE use Edit then
Run script). This will output how are you on the *Python Shell* window.
Confused still? Try this tutorial for IDLE at http://hkn.eecs.berkeley.edu/ dyoo/python/idle intro/index.html

1.3 Interactive Mode


Go into IDLE (also called the Python GUI). You should see a window that has some text like this:
1
Python 2.0 (#4, Dec 12 2000, 19:19:57)
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.
IDLE 0.6 -- press F1 for help
>>>
The >>> is Python way of telling you that you are in interactive mode. In interactive mode what you type is immediately
run. Try typing 1+1 in. Python will respond with 2. Interactive mode allows you to test out and see what Python
will do. If you ever feel you need to play with new Python statements go into interactive mode and try them out.

1.2 Installing Python


First you need to download the appropriate file for your computer from http://www.python.org/download. Go to the 2.0
link (or newer) and then get the windows installer if you use Windows or the rpm or source if you use Unix.
TheWindows installer will download to file. The file can then be run by double clicking on the icon that is downloaded.
The installation will then proceed.
If you get the Unix source make sure you compile in the tk extension if you want to use IDLE.