next up previous contents
Next: Basic Principles of Python Up: Introduction Previous: The very Basics of   Contents

Invoking Python

There are three ways to invoke python, each with its' own uses. The first way is to type ``python'' at the shell command prompt. This brings up the python interpreter with a message similar to this one:
Python 2.2.1 (#2, Aug 27 2002, 09:01:47) 
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
The three greater-than signs (»>) represent python's prompt; you type your commands after the prompt, and hit return for python to execute them. If you've typed an executable statement, python will execute it immediately and display the results of the statement on the screen. For example, if I use python's print statement to print the famous ``Hello, world'' greeting, I'll immediately see a response:
>>> print 'hello,world'
hello,world
The print statement automatically adds a newline at the end of the printed string. This is true regardless of how python is invoked. (You can suppress the newline by following the string to be printed with a comma.)

When using the python interpreter this way, it executes statements immediately, and, unless the value of an expression is assigned to a variable (See Section 6.1), python will display the value of that expression as soon as it's typed. This makes python a very handy calculator:

>>> cost = 27.00
>>> taxrate = .075
>>> cost * taxrate
2.025
>>> 16 + 25 + 92 * 3
317

When you use python interactively and wish to use a loop, you must, as always, indent the body of the loop consistently when you type your statements. Python can't execute your statements until the completion of the loop, and as a reminder, it changes its prompt from greater-than signs to periods. Here's a trivial loop that prints each letter of a word on a separate line -- notice the change in the prompt, and that python doesn't respond until you enter a completely blank line.

>>> word = 'python'
>>> for i in word:
...    print i
...
p
y
t
h
o
n
The need for a completely blank line is peculiar to the interactive use of python. In other settings, simply returning to the previous level of indentation informs python that you're closing the loop.

You can terminate an interactive session by entering the end-of-file character appropriate to your system (control-Z for Windows, control-D for Unix), or by entering

import sys
sys.exit()
or
raise SystemExit
at the python prompt.

For longer programs, you can compose your python code in the editor of your choice, and execute the program by either typing ``python'', followed by the name of the file containing your program, or by clicking on the file's icon, if you've associated the suffix of your python file with the python interpreter. The file extension most commonly used for python files is ``.py''. Under UNIX systems, a standard technique for running programs written in languages like python is to include a specially formed comment as the first line of the file, informing the shell where to find the interpreter for your program. Suppose that python is installed as /usr/local/bin/python on your system. (The UNIX command ``which python'' should tell you where python is installed if it's not in /usr/local/bin.) Then the first line of your python program, starting in column 1, should look like this:

#!/usr/local/bin/python
After creating a file, say myprogram.py, which contains the special comment as its first line, you would make the file executable (through the UNIX command ``chmod +x myprogram.py''), and then you could execute your program by simply typing ``myprogram.py'' at the UNIX prompt.

When you're running python interactively, you can instruct python to execute files containing python programs with the execfile function. Suppose that you are using python interactively, and wish to run the program you've stored in the file myprog.py. You could enter the following statement:

execfile("myprog.py")
The file name, since it is not an internal python symbol (like a variable name or keyword), must be surrounded by quotes.


next up previous contents
Next: Basic Principles of Python Up: Introduction Previous: The very Basics of   Contents
Phil Spector 2003-11-12