Launching the Python interpreter

Python can be programmed via the interactive command line (aka the interpreter or IDE) but anything you code won't be saved. Once you close the session it all goes away. To save your program, it's easiest to just type it in a text file and save it in the foo.py format.

To use the interpreter, type python at the command prompt (*nix and Mac) or launch the Python IDE (Windows and Mac). If you’re using Windows and installed the Python .msi file, you should be able to also type Python on the command prompt. For those who aren’t used to opening Terminal or Command Prompt (same thing, different name on different operating systems), here’s how to do it.

Windows

Mac

Here are two versions of what you should see (the first is an example from the bash shell, the second example is from the MacPython IDE):


Generic Code Example:

cpe-70-95-108-53:~ codyjackson$ python

Python 2.3 (#1, Sep 13 2003, 00:49:11)

[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>>

Notice that there is no significant difference between the two. It doesn’t matter how you launch the Python interpreter; you’ll end up with the same session.

You may notice that the Python version used in the above examples is 2.3; the current version is 2.6. Don’t panic. The vast majority of what you will learn will work regardless of what version you’re using. My favorite Python book, Python How to Program, is based on version 2.2 but I still use it nearly every day as a reference while coding. For the most part, you won’t even notice what version is in use, unless you are using a library, function, or method for a specific version. Then, you simply add a checker to your code to identify what version the user has and notify him to upgrade or modify your code so it is backwards-compatible.

The >>> in the above code is the Python command prompt; your code is typed here and the result is printed on the following line, without a prompt. For example:


Generic Code Example:

>>>print "We are the knights who say, ‘Ni’."

We are the knights who say, ‘Ni’.

If you write a statement that doesn’t require any “processing” by Python, it will simply return you to the prompt, awaiting your next order. The next code example shows the user assigning the value “spam” to the variable can. Python doesn’t have to do anything with this, in regards to calculations or anything, so it accepts the statement and then waits for a new statement.


Generic Code Example:

>>>can = “spam”

>>>

By the way, Python was named after Monty Python, not the snake. Hence, much of the code you'll find on the 'net, tutorials, and books will have references to Monty Python sketches.

The standard Python interpreter can be used to test ideas before you put them in your code. This is a good way to hash out the logic required to make a particular function work correctly or see how a conditional loop will work. You can also use the interpreter as a simple calculator. This may sound geeky, but I often launch a Python session for use as a calculator because it’s often faster than clicking through Windows’ menus to use its calculator.

Here’s an example of the “calculator” capabilities:


>>> 2 + 2

4

>>> 4 * 4

16

>>> 5 ** 2

25

Python also has a math library that you can import to do trigonometric functions and many other higher math calculations. Importing libraries will be covered later in this book.

Launching Python programs

If you want to run a Python program, simply type python at the shell command prompt (not the IDE) followed by the program name.


Generic Code Example:

$python foo.py

Files saved with the .py extension are called modules and can be called individually at the command line or within a program like header files. More information on working with modules can be found in the Python documentation.

Depending on the program, certain arguments can be added to the command line when launching the program. This is similar to adding switches to a Windows DOS prompt command. The arguments tell the program what exactly it should do. For example, perhaps you have a Python program that can output it’s processed data to a file rather than to the screen. To invoke this function in the program you simply launch the program like so:


Generic Code Example:

$python foo.py -f

The “-f” argument is received by the program and calls a function that prints the data to a designated within the computer’s file system instead of printing it to the screen.