Using Python for software development

Python is a ``scripting language'' that has a superficial resemblance to Visual Basic. Unlike Visual Basic, it is intended for serious systems development, especially where a program must communicate with other programs and the operating system. Python provides heterogeneous, dynamically growing data structures likes lists and hash tables, as part of its core language. It also has a huge library of string-manipulation functions, like Perl. Python programs tend to be small and are often developed with a text editor and a command window.

This note explains how to use a text editor and command window to write and test Python scripts. Once a program (script) is working reasonably correctly, it can be used by others by clicking on its icon. But development and testing must be done within a command window. (If you try to test a Python program by clicking on its icon and the program generates an error, the answer window appears and disappears almost immediately, leaving you no time to read the error messages.)

Python works best when used with a Unix-like OS (or within Cygwin on Windows), but you can use it within a Windows Command-Prompt window, too. Read the web page, Using Python from a command window if you are a Windows user.

If you use Cygwin on Windows, it is simplest to run Python within a Cygwin window; just verify that Python is included in your Cygwin installation. (Type which python or just python to see. If not, return to http://www.cygwin.com/ and click on the ``Install or Update Now'' link to add Python to your installation.)

If you use Mac OS-X or Linux, start a command window and type which python or python to see if Python can be started from a command window. (This should work!) If this fails to locate Python, and if you are certain you have installed it, then you must locate python.exe on your computer and use the set PATH command to add the directory path to python.exe to your OS search path.

Starting a Python program from a command window

Say that you have installed Python, and when you start a command window and type python, you successfully start the Python interpreter, that is, you see something like this:
Python 2.6 (#1, May 18 2007, 07:40:45) 
Type "help", "copyright", "credits" or "license" for more information.
>>> 
You are ready to go. Here is a simple development methodology for Python scripts:
  1. Start a text editor, and type a Python program in it. (Call the program test.py) Save test.py in a folder where you keep your work.
  2. Open a command window, and cd to the folder where test.py is saved. Then, type this command to run the script:
    python test.py
    
    This loads the Python interpreter, which in turn loads test.py and parses and performs its commands. The results, including errors, are displayed in the command window.
  3. Using the editor, which is still open and ready for use, alter test.py as desired and save it. Using the command window, which is still open and ready for use, repeat python test.py. (Often, you need merely press the up-arrow key and then Enter to repeat.) Repeat this step until the program is completed satisfactorily.

Testing an individual function

You can place one or more Python functions into a file and test just the functions without a main program to start things. For example, we might place these two functions in the file, F.py:
def f(x) :
    y = x + 1
    return y

def g(a):
   b = f(a +1)
   print "answer =", b
Then, we load the functions by typing in the command window:
python -i F.py
The -i means ``interactive'', meaning you can interact with the functions. Here is a sample test session:
$ python -i F.py
>>> f(3)
4
>>> g(9)
answer = 11
>>> f("boo")
Traceback (most recent call last):
  File "", line 1, in ?
  File "F.py", line 3, in f
    y = x + 1
TypeError: cannot concatenate 'str' and 'int' objects
>>> 
When you are finished testing, type exit.

Additional reading

After you have some initial success, you might wish to consult the CIS200 Lecture Notes cited above. There is also a terse language summary page at the bottom of that same web page that you can use as quick syntax/semantics reference.