Python2owes much to ABC [#!ABC!#], a language developed at CWI as a programming language for non-expert computer users. Python borrows freely from ABC's syntax and data types, but adds modules, exceptions and classes, extensibility, and the ability to call system functions. The concepts of modules, exceptions and (to some extent) classes are influenced strongly by their occurrence in Modula-3 [#!Modula-3!#].
Although Python resembles ABC in many ways, there is a a clear difference in application domain. ABC is intended to be the only programming language for those who use a computer as a tool, but occasionally need to write a program. For this reason, ABC is not just a programming language but also a programming environment, which comes with an integrated syntax-directed editor and some source manipulation commands. Python, on the other hand, aims to be a tool for professional (system) programmers, for whom having a choice of languages with different feature sets makes it possible to choose `the right tool for the job.' The features added to Python make it more useful than ABC in an environment where access to system functions (such as file and directory manipulations) are common. They also support the building of larger systems and libraries. The Python implementation offers little in the way of a programming environment, but is designed to integrate seamlessly with existing programming environments (e.g. UNIX and Emacs).
Perhaps the best introduction to Python is a short example. The following is a complete Python program to list the contents of a UNIX directory.
import sys, posix def ls(dirname): # Print sorted directory contents names = posix.listdir(dirname) names.sort() for name in names: if name[0] != '.': print name ls(sys.argv[1])The largest part of this program, in the middle starting with def, is a function definition. It defines a function named ls with a single parameter called dirname. (Comments in Python start with `#' and extend to the end of the line.) The function body is indented: Python uses indentation for statement grouping instead of braces or begin/end keywords. This is shorter to type and avoids frustrating mismatches between the perception of grouping by the user and the parser. Python accepts one statement per line; long statements may be broken in pieces using the standard backslash convention. If the body of a compound statement is a single, simple statement, it may be placed on the same line as the head.
The first statement of the function body calls the function listdir defined in the module posix. This function returns a list of strings representing the contents of the directory name passed as a string argument, here the argument dirname. If dirname were not a valid directory name, or perhaps not even a string, listdir would raise an exception and the next statement would never be reached. (Exceptions can be caught in Python; see later.) Assuming listdir returns normally, its result is assigned to the local variable names.
The second statement calls the method sort of the variable names. This method is defined for all lists in Python and does the obvious thing: the elements of the list are reordered according to their natural ordering relationship. Since in our example the list contains strings, they are sorted in ascending order.
The last two lines of the function contain a loop that prints all elements of the list whose first character isn't a period. In each iteration, the for statement assigns an element of the list to the local variable name. The print statement is intended for simple-minded output; more elaborate formatting is possible with Python's string handling functions.
The other two parts of the program are easily explained. The first line is an import statement that tells the interpreter to import the modules sys and posix. As it happens these are both built into the interpreter. Importing a module (built-in or otherwise) only makes the module name available in the current scope; functions and data defined in the module are accessed through the dot notation as in posix.listdir. The scope rules of Python are such that the imported module name posix is also available in the function ls (this will be discussed in more detail later).
Finally, the last line of the program calls the ls function with a definite argument. It must be last since Python objects must be defined before they can be used; in particular, the function ls must be defined before it can be called. The argument to ls is sys.argv[1], which happens to be the Python equivalent of $1 in a shell script or argv[1] in a C program's main function.