Home > Articles > Open Source > Python

A Tutorial Introduction to the Python Programming Language

  • Print
  • + Share This
New Riders author Dave Beazley guides you through the basics of working with the Python programming language in this quick introduction. The goal is to illustrate Python's essential features without getting too bogged down in special rules or details.
This article was excerpted from Python Essential Reference, Second Edition, by David Beazley.
Like this article? We recommend

This article briefly covers basic Python concepts such as variables, expressions, control flow, functions, and input/output. This article isn't intended to provide comprehensive coverage, nor does it cover many of Python's more advanced features. However, experienced programmers should be able to extrapolate from the material provided here to create more advanced programs. Beginners are encouraged to try a few examples to get a feel for the language.

Running Python

Python programs are executed by an interpreter. On UNIX machines, the interpreter is started by typing python. On Windows and the Macintosh, the interpreter is launched as an application (either from the Start menu or by double-clicking the interpreter's icon). When the interpreter starts, a prompt appears at which you can start typing programs into a simple read-evaluation loop. For example, in the following output, the interpreter displays its copyright message and presents the user with the >>> prompt, at which the user types the familiar "Hello World" command:

Python 2.1 (#1, Apr 20 2001, 14:34:45)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.
>>> print "Hello World"
Hello World

Programs can also be placed in a file such as the following:

# helloworld.py
print "Hello World"

Python source files have a .py suffix. The # character denotes a comment that extends to the end of the line.

To execute the helloworld.py file, you provide the filename to the interpreter as follows:

% python helloworld.py
Hello World

On Windows, Python programs can be started by double-clicking a .py file. This launches the interpreter and runs the program in a console window. In this case, the console window disappears immediately after the program completes its execution (often before you can read its output). To prevent this problem, you should use an integrated development environment such as Idle or Pythonwin. An alternative approach is to launch the program using a .bat file containing a statement such as python -i helloworld.py that instructs the interpreter to enter interactive mode after program execution.

On the Macintosh, programs can be executed from the included integrated development environment. In addition, the BuildApplet utility (included in the distribution) turns a Python program into a document that automatically launches the interpreter when opened.

Within the interpreter, the execfile() function runs a program, as in the following example:

>>> execfile("helloworld.py")
Hello World

On UNIX, you can also invoke Python using #! in a shell script:

print "Hello World"

The interpreter runs until it reaches the end of the input file. If running interactively, you can exit by typing the EOF (end of file) character or by selecting Exit from a pull-down menu. On UNIX, EOF is Ctrl+D; on Windows, Ctrl+Z. A program can also exit by calling the sys.exit() function or raising the SystemExit exception. For example:

>>> import sys
>>> sys.exit()


>>> raise SystemExit
  • + Share This
  • 🔖 Save To Your Account