blob: f86fc39d26c2ae74e969c3033370a96f40dfaa3e [file] [log] [blame]
Guido van Rossum37953781992-04-06 14:04:04 +00001\documentstyle[twoside,11pt,myformat]{report}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002
3\title{\bf
Guido van Rossum6fc178f1991-08-16 09:13:42 +00004 Python Tutorial
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00005}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00006
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00007\author{
8 Guido van Rossum \\
9 Dept. CST, CWI, Kruislaan 413 \\
10 1098 SJ Amsterdam, The Netherlands \\
11 E-mail: {\tt guido@cwi.nl}
12}
13
14\begin{document}
15
16\pagenumbering{roman}
17
18\maketitle
19
20\begin{abstract}
21
22\noindent
Guido van Rossum4410c751991-06-04 20:22:18 +000023Python is a simple, yet powerful programming language that bridges the
Guido van Rossum6fc178f1991-08-16 09:13:42 +000024gap between C and shell programming, and is thus ideally suited for
25``throw-away programming''
26and rapid prototyping. Its syntax is put
27together from constructs borrowed from a variety of other languages;
28most prominent are influences from ABC, C, Modula-3 and Icon.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000029
Guido van Rossum4410c751991-06-04 20:22:18 +000030The Python interpreter is easily extended with new functions and data
Guido van Rossum6fc178f1991-08-16 09:13:42 +000031types implemented in C. Python is also suitable as an extension
32language for highly customizable C applications such as editors or
33window managers.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000034
Guido van Rossum4410c751991-06-04 20:22:18 +000035Python is available for various operating systems, amongst which
Guido van Rossum6fc178f1991-08-16 09:13:42 +000036several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
37and MS-DOS.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000038
Guido van Rossum6fc178f1991-08-16 09:13:42 +000039This tutorial introduces the reader informally to the basic concepts
40and features of the Python language and system. It helps to have a
41Python interpreter handy for hands-on experience, but as the examples
42are self-contained, the tutorial can be read off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000043
Guido van Rossum481ae681991-11-25 17:28:03 +000044For a description of standard objects and modules, see the {\em Python
45Library Reference} document. The {\em Python Reference Manual} gives
46a more formal definition of the language.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000047
48\end{abstract}
49
50\pagebreak
Guido van Rossumcdc93551992-02-11 15:53:13 +000051{
52\parskip = 0mm
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000053\tableofcontents
Guido van Rossumcdc93551992-02-11 15:53:13 +000054}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000055
56\pagebreak
57
58\pagenumbering{arabic}
59
Guido van Rossum5e0759d1992-08-07 16:06:24 +000060
Guido van Rossum6fc178f1991-08-16 09:13:42 +000061\chapter{Whetting Your Appetite}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000062
Guido van Rossum6fc178f1991-08-16 09:13:42 +000063If you ever wrote a large shell script, you probably know this
64feeling: you'd love to add yet another feature, but it's already so
65slow, and so big, and so complicated; or the feature involves a system
66call or other funcion that is only accessible from C \ldots Usually
67the problem at hand isn't serious enough to warrant rewriting the
68script in C; perhaps because the problem requires variable-length
69strings or other data types (like sorted lists of file names) that are
70easy in the shell but lots of work to implement in C; or perhaps just
71because you're not sufficiently familiar with C.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000072
Guido van Rossum6fc178f1991-08-16 09:13:42 +000073In such cases, Python may be just the language for you. Python is
74simple to use, but it is a real programming language, offering much
75more structure and support for large programs than the shell has. On
76the other hand, it also offers much more error checking than C, and,
77being a {\em very-high-level language}, it has high-level data types
78built in, such as flexible arrays and dictionaries that would cost you
79days to implement efficiently in C. Because of its more general data
80types Python is applicable to a much larger problem domain than {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +000081Awk} or even {\em Perl}, yet many things are at least as easy in
82Python as in those languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000083
Guido van Rossum6fc178f1991-08-16 09:13:42 +000084Python allows you to split up your program in modules that can be
85reused in other Python programs. It comes with a large collection of
Guido van Rossuma8d754e1992-01-07 16:44:35 +000086standard modules that you can use as the basis of your programs --- or
87as examples to start learning to program in Python. There are also
88built-in modules that provide things like file I/O, system calls,
89sockets, and even a generic interface to window systems (STDWIN).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000090
Guido van Rossuma8d754e1992-01-07 16:44:35 +000091Python is an interpreted language, which can save you considerable time
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000092during program development because no compilation and linking is
Guido van Rossum6fc178f1991-08-16 09:13:42 +000093necessary. The interpreter can be used interactively, which makes it
94easy to experiment with features of the language, to write throw-away
95programs, or to test functions during bottom-up program development.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000096It is also a handy desk calculator.
97
Guido van Rossum6fc178f1991-08-16 09:13:42 +000098Python allows writing very compact and readable programs. Programs
99written in Python are typically much shorter than equivalent C
100programs, for several reasons:
101\begin{itemize}
102\item
103the high-level data types allow you to express complex operations in a
104single statement;
105\item
106statement grouping is done by indentation instead of begin/end
107brackets;
108\item
109no variable or argument declarations are necessary.
110\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000111
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000112Python is {\em extensible}: if you know how to program in C it is easy
113to add a new built-in
114function or
115module to the interpreter, either to
116perform critical operations at maximum speed, or to link Python
117programs to libraries that may only be available in binary form (such
118as a vendor-specific graphics library). Once you are really hooked,
119you can link the Python interpreter into an application written in C
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000120and use it as an extension or command language for that application.
121
122By the way, the language is named after the BBC show ``Monty
123Python's Flying Circus'' and has nothing to do with nasty reptiles...
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000124
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000125\section{Where From Here}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000126
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000127Now that you are all excited about Python, you'll want to examine it
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000128in some more detail. Since the best way to learn a language is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000129using it, you are invited here to do so.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000130
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000131In the next chapter, the mechanics of using the interpreter are
132explained. This is rather mundane information, but essential for
133trying out the examples shown later.
134
Guido van Rossum4410c751991-06-04 20:22:18 +0000135The rest of the tutorial introduces various features of the Python
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000136language and system though examples, beginning with simple
137expressions, statements and data types, through functions and modules,
138and finally touching upon advanced concepts like exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000139
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000140When you're through with the turtorial (or just getting bored), you
141should read the Library Reference, which gives complete (though terse)
142reference material about built-in and standard types, functions and
143modules that can save you a lot of time when writing Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000144
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000145
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000146\chapter{Using the Python Interpreter}
147
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000148\section{Invoking the Interpreter}
149
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000150The Python interpreter is usually installed as {\tt /usr/local/bin/python}
151on those machines where it is available; putting {\tt /usr/local/bin} in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000152your {\UNIX} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000153typing the command
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000154
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000155\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000156python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000157\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000158%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000159to the shell. Since the choice of the directory where the interpreter
160lives is an installation option, other places are possible; check with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000161your local Python guru or system administrator. (E.g., {\tt
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000162/usr/local/python} is a popular alternative location.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000163
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000164The interpreter operates somewhat like the {\UNIX} shell: when called
165with standard input connected to a tty device, it reads and executes
166commands interactively; when called with a file name argument or with
167a file as standard input, it reads and executes a {\em script} from
168that file.
169
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000170A third way of starting the interpreter is
171``{\tt python -c command [arg] ...}'', which
172executes the statement(s) in {\tt command}, analogous to the shell's
173{\tt -c} option. Since Python statements often contain spaces or other
174characters that are special to the shell, it is best to quote {\tt
175command} in its entirety with double quotes.
176
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000177Note that there is a difference between ``{\tt python file}'' and
178``{\tt python $<$file}''. In the latter case, input requests from the
Guido van Rossum573805a1992-03-06 10:56:03 +0000179program, such as calls to {\tt input()} and {\tt raw_input()}, are
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000180satisfied from {\em file}. Since this file has already been read
181until the end by the parser before the program starts executing, the
182program will encounter EOF immediately. In the former case (which is
183usually what you want) they are satisfied from whatever file or device
184is connected to standard input of the Python interpreter.
185
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000186\subsection{Argument Passing}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000187
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000188When known to the interpreter, the script name and additional
189arguments thereafter are passed to the script in the variable {\tt
190sys.argv}, which is a list of strings. Its length is at least one;
191when no script and no arguments are given, {\tt sys.argv[0]} is an
192empty string. When the script name is given as {\tt '-'} (meaning
193standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c
194command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options
195found after {\tt -c command} are not consumed by the Python
196interpreter's option processing but left in {\tt sys.argv} for the
197command to handle.
198
199\subsection{Interactive Mode}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000200
Guido van Rossumdd010801991-06-07 14:31:11 +0000201When commands are read from a tty, the interpreter is said to be in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000202{\em interactive\ mode}. In this mode it prompts for the next command
203with the {\em primary\ prompt}, usually three greater-than signs ({\tt
204>>>}); for continuation lines it prompts with the {\em secondary\
205prompt}, by default three dots ({\tt ...}). Typing an EOF (Control-D)
206at the primary prompt causes the interpreter to exit with a zero exit
207status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000208
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000209The interpreter prints a welcome message stating its version number
210and a copyright notice before printing the first prompt, e.g.:
211
212\bcode\begin{verbatim}
213python
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000214Python 0.9.7 (Aug 28 1992).
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000215Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
216>>>
217\end{verbatim}\ecode
218
219\section{The Interpreter and its Environment}
220
221\subsection{Error Handling}
222
223When an error occurs, the interpreter prints an error
224message and a stack trace. In interactive mode, it then returns to
225the primary prompt; when input came from a file, it exits with a
226nonzero exit status after printing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000227the stack trace. (Exceptions handled by an {\tt except} clause in a
228{\tt try} statement are not errors in this context.) Some errors are
229unconditionally fatal and cause an exit with a nonzero exit; this
230applies to internal inconsistencies and some cases of running out of
231memory. All error messages are written to the standard error stream;
232normal output from the executed commands is written to standard
233output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000235Typing the interrupt character (usually Control-C or DEL) to the
236primary or secondary prompt cancels the input and returns to the
237primary prompt.%
238\footnote{
239 A problem with the GNU Readline package may prevent this.
240}
241Typing an interrupt while a command is executing raises the {\tt
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000242KeyboardInterrupt} exception, which may be handled by a {\tt try}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000243statement.
244
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000245\subsection{The Module Search Path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000246
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000247When a module named {\tt foo} is imported, the interpreter searches
248for a file named {\tt foo.py} in the list of directories specified by
249the environment variable {\tt PYTHONPATH}. It has the same syntax as
250the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000251directory names. When {\tt PYTHONPATH} is not set, or when the file
252is not found there, the search continues in an installation-dependent
253default path, usually {\tt .:/usr/local/lib/python}.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000254
255Actually, modules are searched in the list of directories given by the
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000256variable {\tt sys.path} which is initialized from {\tt PYTHONPATH} and
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000257the installation-dependent default. This allows Python programs that
258know what they're doing to modify or replace the module search path.
259See the section on Standard Modules later.
260
261\subsection{``Compiled'' Python files}
262
263As an important speed-up of the start-up time for short programs that
264use a lot of standard modules, if a file called {\tt foo.pyc} exists
265in the directory where {\tt foo.py} is found, this is assumed to
266contain an already-``compiled'' version of the module {\tt foo}. The
267modification time of the version of {\tt foo.py} used to create {\tt
268foo.pyc} is recorded in {\tt foo.pyc}, and the file is ignored if
269these don't match.
270
271Whenever {\tt foo.py} is successfully compiled, an attempt is made to
272write the compiled version to {\tt foo.pyc}. It is not an error if
273this attempt fails; if for any reason the file is not written
274completely, the resulting {\tt foo.pyc} file will be recognized as
275invalid and thus ignored later.
276
277\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000278
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000279On BSD'ish {\UNIX} systems, Python scripts can be made directly
280executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000281
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000282\bcode\begin{verbatim}
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000283#! /usr/local/bin/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000284\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000285%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000286(assuming that's the name of the interpreter) at the beginning of the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000287script and giving the file an executable mode. The {\tt \#!} must be
288the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000289
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000290\subsection{The Interactive Startup File}
291
292When you use Python interactively, it is frequently handy to have some
293standard commands executed every time the interpreter is started. You
294can do this by setting an environment variable named {\tt
295PYTHONSTARTUP} to the name of a file containing your start-up
296commands. This is similar to the {\tt /profile} feature of the UNIX
297shells.
298
299This file is only read in interactive sessions, not when Python reads
300commands from a script, and not when {\tt /dev/tty} is given as the
301explicit source of commands (which otherwise behaves like an
302interactive session). It is executed in the same name space where
303interactive commands are executed, so that objects that it defines or
304imports can be used without qualification in the interactive session.
305
306If you want to read an additional start-up file from the current
307directory, you can program this in the global start-up file, e.g.
308\verb\execfile('.pythonrc')\. If you want to use the startup file
309in a script, you must write this explicitly in the script, e.g.
310\verb\import os;\ \verb\execfile(os.environ['PYTHONSTARTUP'])\.
311
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000312\section{Interactive Input Editing and History Substitution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000313
Guido van Rossum4410c751991-06-04 20:22:18 +0000314Some versions of the Python interpreter support editing of the current
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000315input line and history substitution, similar to facilities found in
316the Korn shell and the GNU Bash shell. This is implemented using the
317{\em GNU\ Readline} library, which supports Emacs-style and vi-style
318editing. This library has its own documentation which I won't
319duplicate here; however, the basics are easily explained.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000320
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000321Perhaps the quickest check to see whether command line editing is
322supported is typing Control-P to the first Python prompt you get. If
323it beeps, you have command line editing. If nothing appears to
324happen, or if \verb/^P/ is echoed, you can skip the rest of this
325section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000326
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000327\subsection{Line Editing}
328
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000329If supported, input line editing is active whenever the interpreter
330prints a primary or secondary prompt. The current line can be edited
331using the conventional Emacs control characters. The most important
332of these are: C-A (Control-A) moves the cursor to the beginning of the
333line, C-E to the end, C-B moves it one position to the left, C-F to
334the right. Backspace erases the character to the left of the cursor,
335C-D the character to its right. C-K kills (erases) the rest of the
336line to the right of the cursor, C-Y yanks back the last killed
337string. C-underscore undoes the last change you made; it can be
338repeated for cumulative effect.
339
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000340\subsection{History Substitution}
341
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000342History substitution works as follows. All non-empty input lines
343issued are saved in a history buffer, and when a new prompt is given
344you are positioned on a new line at the bottom of this buffer. C-P
345moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000346Any line in the history buffer can be edited; an asterisk appears in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000347front of the prompt to mark a line as modified. Pressing the Return
348key passes the current line to the interpreter. C-R starts an
349incremental reverse search; C-S starts a forward search.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000350
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000351\subsection{Key Bindings}
352
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000353The key bindings and some other parameters of the Readline library can
354be customized by placing commands in an initialization file called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000355{\tt \$HOME/.inputrc}. Key bindings have the form
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000356
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000357\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000358key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000359\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000360%
361or
362
363\bcode\begin{verbatim}
364"string": function-name
365\end{verbatim}\ecode
366%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000367and options can be set with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000368
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000369\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000370set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000371\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000372%
373For example:
374
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000375\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000376# I prefer vi-style editing:
377set editing-mode vi
378# Edit using a single line:
379set horizontal-scroll-mode On
380# Rebind some keys:
381Meta-h: backward-kill-word
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000382"\C-u": universal-argument
383"\C-x\C-r": re-read-init-file
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000384\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000385%
Guido van Rossum4410c751991-06-04 20:22:18 +0000386Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000387instead of Readline's default filename completion function. If you
388insist, you can override this by putting
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000389
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000390\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000391TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000392\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000393%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000394in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000395indented continuation lines...)
396
397\subsection{Commentary}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000398
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000399This facility is an enormous step forward compared to previous
400versions of the interpreter; however, some wishes are left: It would
401be nice if the proper indentation were suggested on continuation lines
402(the parser knows if an indent token is required next). The
403completion mechanism might use the interpreter's symbol table. A
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000404command to check (or even suggest) matching parentheses, quotes etc.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000405would also be useful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000406
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000407
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000408\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000409
410In the following examples, input and output are distinguished by the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000411presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
412the example, you must type everything after the prompt, when the
413prompt appears; lines that do not begin with a prompt are output from
414the interpreter.%
415\footnote{
416 I'd prefer to use different fonts to distinguish input
417 from output, but the amount of LaTeX hacking that would require
418 is currently beyond my ability.
419}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000420Note that a secondary prompt on a line by itself in an example means
421you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000422
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000423\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000424
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000425Let's try some simple Python commands. Start the interpreter and wait
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000426for the primary prompt, {\tt >>>}. (It shouldn't take long.)
427
428\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000429
430The interpreter acts as a simple calculator: you can type an
431expression at it and it will write the value. Expression syntax is
432straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000433work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000434can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000435
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000436\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000437>>> # This is a comment
438>>> 2+2
4394
440>>>
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000441>>> (50-5*6)/4
4425
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000443>>> # Division truncates towards zero:
444>>> 7/3
4452
446>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000447\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000448%
449Like in C, the equal sign ({\tt =}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000450variable. The value of an assignment is not written:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000451
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000452\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000453>>> width = 20
454>>> height = 5*9
455>>> width * height
456900
457>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000458\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000459%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000460A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000461
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000462\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000463>>> # Zero x, y and z
464>>> x = y = z = 0
465>>>
466\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000467%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000468There is full support for floating point; operators with mixed type
469operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000470
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000471\bcode\begin{verbatim}
472>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004733.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000474>>> 7.0 / 2
4753.5
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000476>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000477\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000478
479\subsection{Strings}
480
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000481Besides numbers, Python can also manipulate strings, enclosed in
482single quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000483
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000484\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000485>>> 'foo bar'
486'foo bar'
487>>> 'doesn\'t'
488'doesn\'t'
489>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000490\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000491%
492Strings are written the same way as they are typed for input: inside
493quotes and with quotes and other funny characters escaped by
494backslashes, to show the precise value. (The {\tt print} statement,
495described later, can be used to write strings without quotes or
496escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000497
498Strings can be concatenated (glued together) with the {\tt +}
499operator, and repeated with {\tt *}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000500
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000501\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000502>>> word = 'Help' + 'A'
503>>> word
504'HelpA'
505>>> '<' + word*5 + '>'
506'<HelpAHelpAHelpAHelpAHelpA>'
507>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000508\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000509%
510Strings can be subscripted (indexed); like in C, the first character of
511a string has subscript (index) 0.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000512
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000513There is no separate character type; a character is simply a string of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000514size one. Like in Icon, substrings can be specified with the {\em
515slice} notation: two indices separated by a colon.
516
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000517\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000518>>> word[4]
519'A'
520>>> word[0:2]
521'He'
522>>> word[2:4]
523'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000524>>>
525\end{verbatim}\ecode
526%
527Slice indices have useful defaults; an omitted first index defaults to
528zero, an omitted second index defaults to the size of the string being
529sliced.
530
531\bcode\begin{verbatim}
532>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000533'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000534>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000535'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000536>>>
537\end{verbatim}\ecode
538%
539Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
540equals \verb\s\.
541
542\bcode\begin{verbatim}
543>>> word[:2] + word[2:]
544'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000545>>> word[:3] + word[3:]
546'HelpA'
547>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000548\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000549%
550Degenerate slice indices are handled gracefully: an index that is too
551large is replaced by the string size, an upper bound smaller than the
552lower bound returns an empty string.
553
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000554\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000555>>> word[1:100]
556'elpA'
557>>> word[10:]
558''
559>>> word[2:1]
560''
561>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000562\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000563%
564Indices may be negative numbers, to start counting from the right.
565For example:
566
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000567\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000568>>> word[-1] # The last character
569'A'
570>>> word[-2] # The last-but-one character
571'p'
572>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000573'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000574>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000575'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000576>>>
577\end{verbatim}\ecode
578%
579But note that -0 is really the same as 0, so it does not count from
580the right!
581
582\bcode\begin{verbatim}
583>>> word[-0] # (since -0 equals 0)
584'H'
585>>>
586\end{verbatim}\ecode
587%
588Out-of-range negative slice indices are truncated, but don't try this
589for single-element (non-slice) indices:
590
591\bcode\begin{verbatim}
592>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000593'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000594>>> word[-10] # error
595Unhandled exception: IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000596>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000597\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000598%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000599The best way to remember how slices work is to think of the indices as
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000600pointing {\em between} characters, with the left edge of the first
601character numbered 0. Then the right edge of the last character of a
602string of {\tt n} characters has index {\tt n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000603
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000604\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000605 +---+---+---+---+---+
606 | H | e | l | p | A |
607 +---+---+---+---+---+
608 0 1 2 3 4 5
609-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000610\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000611%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000612The first row of numbers gives the position of the indices 0...5 in
613the string; the second row gives the corresponding negative indices.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000614The slice from \verb\i\ to \verb\j\ consists of all characters between
615the edges labeled \verb\i\ and \verb\j\, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000616
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000617For nonnegative indices, the length of a slice is the difference of
618the indices, if both are within bounds, e.g., the length of
619\verb\word[1:3]\ is 2.
620
621The built-in function {\tt len()} returns the length of a string:
622
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000623\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000624>>> s = 'supercalifragilisticexpialidocious'
625>>> len(s)
62634
627>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000628\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000629
630\subsection{Lists}
631
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000632Python knows a number of {\em compound} data types, used to group
633together other values. The most versatile is the {\em list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000634can be written as a list of comma-separated values (items) between
635square brackets. List items need not all have the same type.
636
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000637\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000638>>> a = ['foo', 'bar', 100, 1234]
639>>> a
640['foo', 'bar', 100, 1234]
641>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000642\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000643%
644Like string indices, list indices start at 0, and lists can be sliced,
645concatenated and so on:
646
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000647\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000648>>> a[0]
649'foo'
650>>> a[3]
6511234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000652>>> a[-2]
653100
654>>> a[1:-1]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000655['bar', 100]
656>>> a[:2] + ['bletch', 2*2]
657['foo', 'bar', 'bletch', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000658>>> 3*a[:3] + ['Boe!']
659['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000660>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000661\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000662%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000663Unlike strings, which are {\em immutable}, it is possible to change
664individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000665
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000666\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000667>>> a
668['foo', 'bar', 100, 1234]
669>>> a[2] = a[2] + 23
670>>> a
671['foo', 'bar', 123, 1234]
672>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000673\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000674%
675Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000676of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000677
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000678\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000679>>> # Replace some items:
680>>> a[0:2] = [1, 12]
681>>> a
682[1, 12, 123, 1234]
683>>> # Remove some:
684>>> a[0:2] = []
685>>> a
686[123, 1234]
687>>> # Insert some:
688>>> a[1:1] = ['bletch', 'xyzzy']
689>>> a
690[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000691>>> a[:0] = a # Insert (a copy of) itself at the beginning
692>>> a
693[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000694>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000695\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000696%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000697The built-in function {\tt len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000698
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000699\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000700>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00007018
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000702>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000703\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000704%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000705It is possible to nest lists (create lists containing other lists),
706for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000707
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000708\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000709>>> q = [2, 3]
710>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000711>>> len(p)
7123
713>>> p[1]
714[2, 3]
715>>> p[1][0]
7162
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000717>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000718>>> p
719[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000720>>> q
721[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000722>>>
723\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000724%
725Note that in the last example, {\tt p[1]} and {\tt q} really refer to
726the same object! We'll come back to {\em object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000727
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000728\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000729
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000730Of course, we can use Python for more complicated tasks than adding
731two and two together. For instance, we can write an initial
732subsequence of the {\em Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000733
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000734\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000735>>> # Fibonacci series:
736>>> # the sum of two elements defines the next
737>>> a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000738>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000739... print b
740... a, b = b, a+b
741...
7421
7431
7442
7453
7465
7478
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000748>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000749\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000750%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000751This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000752
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000753\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000754
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000755\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000756The first line contains a {\em multiple assignment}: the variables
757{\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
758last line this is used again, demonstrating that the expressions on
759the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000760assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000761
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000762\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000763The {\tt while} loop executes as long as the condition (here: {\tt b <
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000764100}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000765true; zero is false. The condition may also be a string or list value,
766in fact any sequence; anything with a non-zero length is true, empty
767sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000768comparison. The standard comparison operators are written the same as
769in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000770
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000771\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000772The {\em body} of the loop is {\em indented}: indentation is Python's
773way of grouping statements. Python does not (yet!) provide an
774intelligent input line editing facility, so you have to type a tab or
775space(s) for each indented line. In practice you will prepare more
776complicated input for Python with a text editor; most text editors have
777an auto-indent facility. When a compound statement is entered
778interactively, it must be followed by a blank line to indicate
779completion (since the parser cannot guess when you have typed the last
780line).
781
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000782\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000783The {\tt print} statement writes the value of the expression(s) it is
784given. It differs from just writing the expression you want to write
785(as we did earlier in the calculator examples) in the way it handles
786multiple expressions and strings. Strings are written without quotes,
787and a space is inserted between items, so you can format things nicely,
788like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000789
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000790\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000791>>> i = 256*256
792>>> print 'The value of i is', i
793The value of i is 65536
794>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000795\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000796%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000797A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000798
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000799\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000800>>> a, b = 0, 1
801>>> while b < 1000:
802... print b,
803... a, b = b, a+b
804...
8051 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
806>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000807\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000808%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000809Note that the interpreter inserts a newline before it prints the next
810prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000811
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000812\end{itemize}
813
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000814
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000815\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000816
Guido van Rossum4410c751991-06-04 20:22:18 +0000817Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000818usual control flow statements known from other languages, with some
819twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000820
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000821\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000822
823Perhaps the most well-known statement type is the {\tt if} statement.
824For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000825
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000826\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000827>>> if x < 0:
828... x = 0
829... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000830... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000831... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000832... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000833... print 'Single'
834... else:
835... print 'More'
836...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000837\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000838%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000839There can be zero or more {\tt elif} parts, and the {\tt else} part is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000840optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
841useful to avoid excessive indentation. An {\tt if...elif...elif...}
842sequence is a substitute for the {\em switch} or {\em case} statements
843found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000844
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000845\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000846
Guido van Rossum4410c751991-06-04 20:22:18 +0000847The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000848used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000849arithmetic progression of numbers (like in Pascal), or leaving the user
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000850completely free in the iteration test and step (as C), Python's {\tt
851for} statement iterates over the items of any sequence (e.g., a list
852or a string), in the order that they appear in the sequence. For
853example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000854
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000855\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000856>>> # Measure some strings:
857>>> a = ['cat', 'window', 'defenestrate']
858>>> for x in a:
859... print x, len(x)
860...
861cat 3
862window 6
863defenestrate 12
864>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000865\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000866%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000867It is not safe to modify the sequence being iterated over in the loop
868(this can only happen for mutable sequence types, i.e., lists). If
869you need to modify the list you are iterating over, e.g., duplicate
870selected items, you must iterate over a copy. The slice notation
871makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000872
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000873\bcode\begin{verbatim}
874>>> for x in a[:]: # make a slice copy of the entire list
875... if len(x) > 6: a.insert(0, x)
876...
877>>> a
878['defenestrate', 'cat', 'window', 'defenestrate']
879>>>
880\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000881
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000882\section{The {\tt range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000883
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000884If you do need to iterate over a sequence of numbers, the built-in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000885function {\tt range()} comes in handy. It generates lists containing
886arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000887
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000888\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000889>>> range(10)
890[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
891>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000892\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000893%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000894The given end point is never part of the generated list; {\tt range(10)}
895generates a list of 10 values, exactly the legal indices for items of a
896sequence of length 10. It is possible to let the range start at another
897number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000898
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000899\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000900>>> range(5, 10)
901[5, 6, 7, 8, 9]
902>>> range(0, 10, 3)
903[0, 3, 6, 9]
904>>> range(-10, -100, -30)
905[-10, -40, -70]
906>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000907\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000908%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000909To iterate over the indices of a sequence, combine {\tt range()} and
910{\tt len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000911
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000912\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000913>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000914>>> for i in range(len(a)):
915... print i, a[i]
916...
9170 Mary
9181 had
9192 a
9203 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00009214 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000922>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000923\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000924
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000925\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000926
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000927The {\tt break} statement, like in C, breaks out of the smallest
928enclosing {\tt for} or {\tt while} loop.
929
930The {\tt continue} statement, also borrowed from C, continues with the
931next iteration of the loop.
932
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000933Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000934loop terminates through exhaustion of the list (with {\tt for}) or when
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000935the condition becomes false (with {\tt while}), but not when the loop is
936terminated by a {\tt break} statement. This is exemplified by the
937following loop, which searches for a list item of value 0:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000938
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000939\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000940>>> for n in range(2, 10):
941... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000942... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000943... print n, 'equals', x, '*', n/x
944... break
945... else:
946... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000947...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009482 is a prime number
9493 is a prime number
9504 equals 2 * 2
9515 is a prime number
9526 equals 2 * 3
9537 is a prime number
9548 equals 2 * 4
9559 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000956>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000957\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000958
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000959\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000960
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000961The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000962It can be used when a statement is required syntactically but the
963program requires no action.
964For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000965
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000966\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000967>>> while 1:
968... pass # Busy-wait for keyboard interrupt
969...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000970\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000971
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000972\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000973
974We can create a function that writes the Fibonacci series to an
975arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000976
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000977\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000978>>> def fib(n): # write Fibonacci series up to n
979... a, b = 0, 1
980... while b <= n:
981... print b,
982... a, b = b, a+b
983...
984>>> # Now call the function we just defined:
985>>> fib(2000)
9861 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
987>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000988\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000989%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000990The keyword {\tt def} introduces a function {\em definition}. It must
991be followed by the function name and the parenthesized list of formal
992parameters. The statements that form the body of the function starts at
993the next line, indented by a tab stop.
994
995The {\em execution} of a function introduces a new symbol table used
996for the local variables of the function. More precisely, all variable
997assignments in a function store the value in the local symbol table;
998whereas
999 variable references first look in the local symbol table, then
1000in the global symbol table, and then in the table of built-in names.
1001Thus,
1002global variables cannot be directly assigned to from within a
1003function, although they may be referenced.
1004
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001005The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001006the local symbol table of the called function when it is called; thus,
1007arguments are passed using {\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001008\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001009 Actually, {\em call by object reference} would be a better
1010 description, since if a mutable object is passed, the caller
1011 will see any changes the callee makes to it (e.g., items
1012 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001013}
1014When a function calls another function, a new local symbol table is
1015created for that call.
1016
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001017A function definition introduces the function name in the
1018current
1019symbol table. The value
1020of the function name
1021has a type that is recognized by the interpreter as a user-defined
1022function. This value can be assigned to another name which can then
1023also be used as a function. This serves as a general renaming
1024mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001025
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001026\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001027>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001028<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001029>>> f = fib
1030>>> f(100)
10311 1 2 3 5 8 13 21 34 55 89
1032>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001033\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001034%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001035You might object that {\tt fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001036Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001037value. In fact, technically speaking, procedures do return a value,
1038albeit a rather boring one. This value is called {\tt None} (it's a
1039built-in name). Writing the value {\tt None} is normally suppressed by
1040the interpreter if it would be the only value written. You can see it
1041if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001042
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001043\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001044>>> print fib(0)
1045None
1046>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001047\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001048%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001049It is simple to write a function that returns a list of the numbers of
1050the Fibonacci series, instead of printing it:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001051
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001052\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001053>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001054... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001055... a, b = 0, 1
1056... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001057... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001058... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001059... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001060...
1061>>> f100 = fib2(100) # call it
1062>>> f100 # write the result
1063[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1064>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001065\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001066%
Guido van Rossum4410c751991-06-04 20:22:18 +00001067This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001068
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001069\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001070
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001071\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001072The {\tt return} statement returns with a value from a function. {\tt
1073return} without an expression argument is used to return from the middle
1074of a procedure (falling off the end also returns from a proceduce), in
1075which case the {\tt None} value is returned.
1076
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001077\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001078The statement {\tt result.append(b)} calls a {\em method} of the list
1079object {\tt result}. A method is a function that `belongs' to an
1080object and is named {\tt obj.methodname}, where {\tt obj} is some
1081object (this may be an expression), and {\tt methodname} is the name
1082of a method that is defined by the object's type. Different types
1083define different methods. Methods of different types may have the
1084same name without causing ambiguity. (It is possible to define your
1085own object types and methods, using {\em classes}. This is an
1086advanced feature that is not discussed in this tutorial.)
1087The method {\tt append} shown in the example, is defined for
1088list objects; it adds a new element at the end of the list. In this
1089example
1090it is equivalent to {\tt result = result + [b]}, but more efficient.
1091
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001092\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001093
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001094
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001095\chapter{Odds and Ends}
1096
1097This chapter describes some things you've learned about already in
1098more detail, and adds some new things as well.
1099
1100\section{More on Lists}
1101
1102The list data type has some more methods. Here are all of the methods
1103of lists objects:
1104
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001105\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001106
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001107\item[{\tt insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001108Insert an item at a given position. The first argument is the index of
1109the element before which to insert, so {\tt a.insert(0, x)} inserts at
1110the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
1111{\tt a.append(x)}.
1112
1113\item[{\tt append(x)}]
1114Equivalent to {\tt a.insert(len(a), x)}.
1115
1116\item[{\tt index(x)}]
1117Return the index in the list of the first item whose value is {\tt x}.
1118It is an error if there is no such item.
1119
1120\item[{\tt remove(x)}]
1121Remove the first item from the list whose value is {\tt x}.
1122It is an error if there is no such item.
1123
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001124\item[{\tt sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001125Sort the items of the list, in place.
1126
1127\item[{\tt reverse()}]
1128Reverse the elements of the list, in place.
1129
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001130\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001131
1132An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001133
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001134\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001135>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001136>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001137>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001138>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001139[66.6, 333, -1, 333, 1, 1234.5, 333]
1140>>> a.index(333)
11411
1142>>> a.remove(333)
1143>>> a
1144[66.6, -1, 333, 1, 1234.5, 333]
1145>>> a.reverse()
1146>>> a
1147[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001148>>> a.sort()
1149>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001150[-1, 1, 66.6, 333, 333, 1234.5]
1151>>>
1152\end{verbatim}\ecode
1153
1154\section{The {\tt del} statement}
1155
1156There is a way to remove an item from a list given its index instead
1157of its value: the {\tt del} statement. This can also be used to
1158remove slices from a list (which we did earlier by assignment of an
1159empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001160
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001161\bcode\begin{verbatim}
1162>>> a
1163[-1, 1, 66.6, 333, 333, 1234.5]
1164>>> del a[0]
1165>>> a
1166[1, 66.6, 333, 333, 1234.5]
1167>>> del a[2:4]
1168>>> a
1169[1, 66.6, 1234.5]
1170>>>
1171\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001172%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001173{\tt del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001174
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001175\bcode\begin{verbatim}
1176>>> del a
1177>>>
1178\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001179%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001180Referencing the name {\tt a} hereafter is an error (at least until
1181another value is assigned to it). We'll find other uses for {\tt del}
1182later.
1183
1184\section{Tuples and Sequences}
1185
1186We saw that lists and strings have many common properties, e.g.,
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001187indexinging and slicing operations. They are two examples of {\em
1188sequence} data types. Since Python is an evolving language, other
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001189sequence data types may be added. There is also another standard
1190sequence data type: the {\em tuple}.
1191
1192A tuple consists of a number of values separated by commas, for
1193instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001194
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001195\bcode\begin{verbatim}
1196>>> t = 12345, 54321, 'hello!'
1197>>> t[0]
119812345
1199>>> t
1200(12345, 54321, 'hello!')
1201>>> # Tuples may be nested:
1202>>> u = t, (1, 2, 3, 4, 5)
1203>>> u
1204((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1205>>>
1206\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001207%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001208As you see, on output tuples are alway enclosed in parentheses, so
1209that nested tuples are interpreted correctly; they may be input with
1210or without surrounding parentheses, although often parentheses are
1211necessary anyway (if the tuple is part of a larger expression).
1212
1213Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1214from a database, etc. Tuples, like strings, are immutable: it is not
1215possible to assign to the individual items of a tuple (you can
1216simulate much of the same effect with slicing and concatenation,
1217though).
1218
1219A special problem is the construction of tuples containing 0 or 1
1220items: the syntax has some extra quirks to accomodate these. Empty
1221tuples are constructed by an empty pair of parentheses; a tuple with
1222one item is constructed by following a value with a comma
1223(it is not sufficient to enclose a single value in parentheses).
1224Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001225
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001226\bcode\begin{verbatim}
1227>>> empty = ()
1228>>> singleton = 'hello', # <-- note trailing comma
1229>>> len(empty)
12300
1231>>> len(singleton)
12321
1233>>> singleton
1234('hello',)
1235>>>
1236\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001237%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001238The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
1239tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
1240are packed together in a tuple. The reverse operation is also
1241possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001242
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001243\bcode\begin{verbatim}
1244>>> x, y, z = t
1245>>>
1246\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001247%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001248This is called, appropriately enough, {\em tuple unpacking}. Tuple
1249unpacking requires that the list of variables on the left has the same
1250number of elements as the length of the tuple. Note that multiple
1251assignment is really just a combination of tuple packing and tuple
1252unpacking!
1253
1254Occasionally, the corresponding operation on lists is useful: {\em list
1255unpacking}. This is supported by enclosing the list of variables in
1256square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001257
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001258\bcode\begin{verbatim}
1259>>> a = ['foo', 'bar', 100, 1234]
1260>>> [a1, a2, a3, a4] = a
1261>>>
1262\end{verbatim}\ecode
1263
1264\section{Dictionaries}
1265
1266Another useful data type built into Python is the {\em dictionary}.
1267Dictionaries are sometimes found in other languages as ``associative
1268memories'' or ``associative arrays''. Unlike sequences, which are
1269indexed by a range of numbers, dictionaries are indexed by {\em keys},
1270which are strings. It is best to think of a dictionary as an unordered set of
1271{\em key:value} pairs, with the requirement that the keys are unique
1272(within one dictionary).
1273A pair of braces creates an empty dictionary: \verb/{}/.
1274Placing a comma-separated list of key:value pairs within the
1275braces adds initial key:value pairs to the dictionary; this is also the
1276way dictionaries are written on output.
1277
1278The main operations on a dictionary are storing a value with some key
1279and extracting the value given the key. It is also possible to delete
1280a key:value pair
1281with {\tt del}.
1282If you store using a key that is already in use, the old value
1283associated with that key is forgotten. It is an error to extract a
1284value using a non-existant key.
1285
1286The {\tt keys()} method of a dictionary object returns a list of all the
1287keys used in the dictionary, in random order (if you want it sorted,
1288just apply the {\tt sort()} method to the list of keys). To check
1289whether a single key is in the dictionary, use the \verb/has_key()/
1290method of the dictionary.
1291
1292Here is a small example using a dictionary:
1293
1294\bcode\begin{verbatim}
1295>>> tel = {'jack': 4098, 'sape': 4139}
1296>>> tel['guido'] = 4127
1297>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001298{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001299>>> tel['jack']
13004098
1301>>> del tel['sape']
1302>>> tel['irv'] = 4127
1303>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001304{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001305>>> tel.keys()
1306['guido', 'irv', 'jack']
1307>>> tel.has_key('guido')
13081
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001309>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001310\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001311
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001312\section{More on Conditions}
1313
1314The conditions used in {\tt while} and {\tt if} statements above can
1315contain other operators besides comparisons.
1316
1317The comparison operators {\tt in} and {\tt not in} check whether a value
1318occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
1319is not} compare whether two objects are really the same object; this
1320only matters for mutable objects like lists. All comparison operators
1321have the same priority, which is lower than that of all numerical
1322operators.
1323
1324Comparisons can be chained: e.g., {\tt a < b = c} tests whether {\tt a}
1325is less than {\tt b} and moreover {\tt b} equals {\tt c}.
1326
1327Comparisons may be combined by the Boolean operators {\tt and} and {\tt
1328or}, and the outcome of a comparison (or of any other Boolean
1329expression) may be negated with {\tt not}. These all have lower
1330priorities than comparison operators again; between them, {\tt not} has
1331the highest priority, and {\tt or} the lowest, so that
1332{\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
1333course, parentheses can be used to express the desired composition.
1334
1335The Boolean operators {\tt and} and {\tt or} are so-called {\em
1336shortcut} operators: their arguments are evaluated from left to right,
1337and evaluation stops as soon as the outcome is determined. E.g., if
1338{\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
1339does not evaluate the expression C. In general, the return value of a
1340shortcut operator, when used as a general value and not as a Boolean, is
1341the last evaluated argument.
1342
1343It is possible to assign the result of a comparison or other Boolean
1344expression to a variable, but you must enclose the entire Boolean
1345expression in parentheses. This is necessary because otherwise an
1346assignment like \verb/a = b = c/ would be ambiguous: does it assign the
1347value of {\tt c} to {\tt a} and {\tt b}, or does it compare {\tt b} to
1348{\tt c} and assign the outcome (0 or 1) to {\tt a}? As it is, the first
1349meaning is what you get, and to get the latter you have to write
1350\verb/a = (b = c)/. (In Python, unlike C, assignment cannot occur
1351inside expressions.)
1352
1353\section{Comparing Sequences and Other Types}
1354
1355Sequence objects may be compared to other objects with the same
1356sequence type. The comparison uses {\em lexicographical} ordering:
1357first the first two items are compared, and if they differ this
1358determines the outcome of the comparison; if they are equal, the next
1359two items are compared, and so on, until either sequence is exhausted.
1360If two items to be compared are themselves sequences of the same type,
1361the lexiographical comparison is carried out recursively. If all
1362items of two sequences compare equal, the sequences are considered
1363equal. If one sequence is an initial subsequence of the other, the
1364shorted sequence is the smaller one. Lexicographical ordering for
1365strings uses the ASCII ordering for individual characters. Some
1366examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001367
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001368\bcode\begin{verbatim}
1369(1, 2, 3) < (1, 2, 4)
1370[1, 2, 3] < [1, 2, 4]
1371'ABC' < 'C' < 'Pascal' < 'Python'
1372(1, 2, 3, 4) < (1, 2, 4)
1373(1, 2) < (1, 2, -1)
1374(1, 2, 3) = (1.0, 2.0, 3.0)
1375(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1376\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001377%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001378Note that comparing objects of different types is legal. The outcome
1379is deterministic but arbitrary: the types are ordered by their name.
1380Thus, a list is always smaller than a string, a string is always
1381smaller than a tuple, etc. Mixed numeric types are compared according
1382to their numeric value, so 0 equals 0.0, etc.%
1383\footnote{
1384 The rules for comparing objects of different types should
1385 not be relied upon; they may change in a future version of
1386 the language.
1387}
1388
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001389
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001390\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001391
Guido van Rossum4410c751991-06-04 20:22:18 +00001392If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001393definitions you have made (functions and variables) are lost.
1394Therefore, if you want to write a somewhat longer program, you are
1395better off using a text editor to prepare the input for the interpreter
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001396and run it with that file as input instead. This is known as creating a
1397{\em script}. As your program gets longer, you may want to split it
1398into several files for easier maintenance. You may also want to use a
1399handy function that you've written in several programs without copying
1400its definition into each program.
1401
Guido van Rossum4410c751991-06-04 20:22:18 +00001402To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001403them in a script or in an interactive instance of the interpreter.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001404Such a file is called a {\em module}; definitions from a module can be
1405{\em imported} into other modules or into the {\em main} module (the
1406collection of variables that you have access to in a script
1407executed at the top level
1408and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001409
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001410A module is a file containing Python definitions and statements. The
1411file name is the module name with the suffix {\tt .py} appended. For
1412instance, use your favorite text editor to create a file called {\tt
1413fibo.py} in the current directory with the following contents:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001414
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001415\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001416# Fibonacci numbers module
1417
1418def fib(n): # write Fibonacci series up to n
1419 a, b = 0, 1
1420 while b <= n:
1421 print b,
1422 a, b = b, a+b
1423
1424def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001425 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001426 a, b = 0, 1
1427 while b <= n:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001428 result.append(b)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001429 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001430 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001431\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001432%
Guido van Rossum4410c751991-06-04 20:22:18 +00001433Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001434following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001435
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001436\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001437>>> import fibo
1438>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001439\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001440%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001441This does not enter the names of the functions defined in
1442{\tt fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001443directly in the current symbol table; it only enters the module name
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001444{\tt fibo}
1445there.
1446Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001447
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001448\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001449>>> fibo.fib(1000)
14501 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1451>>> fibo.fib2(100)
1452[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1453>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001454\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001455%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001456If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001457
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001458\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001459>>> fib = fibo.fib
1460>>> fib(500)
14611 1 2 3 5 8 13 21 34 55 89 144 233 377
1462>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001463\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001464
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001465\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001466
1467A module can contain executable statements as well as function
1468definitions.
1469These statements are intended to initialize the module.
1470They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001471{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001472time the module is imported somewhere.%
1473\footnote{
1474 In fact function definitions are also `statements' that are
1475 `executed'; the execution enters the function name in the
1476 module's global symbol table.
1477}
1478
1479Each module has its own private symbol table, which is used as the
1480global symbol table by all functions defined in the module.
1481Thus, the author of a module can use global variables in the module
1482without worrying about accidental clashes with a user's global
1483variables.
1484On the other hand, if you know what you are doing you can touch a
1485module's global variables with the same notation used to refer to its
1486functions,
1487{\tt modname.itemname}.
1488
1489Modules can import other modules.
1490It is customary but not required to place all
1491{\tt import}
1492statements at the beginning of a module (or script, for that matter).
1493The imported module names are placed in the importing module's global
1494symbol table.
1495
1496There is a variant of the
1497{\tt import}
1498statement that imports names from a module directly into the importing
1499module's symbol table.
1500For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001501
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001502\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001503>>> from fibo import fib, fib2
1504>>> fib(500)
15051 1 2 3 5 8 13 21 34 55 89 144 233 377
1506>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001507\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001508%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001509This does not introduce the module name from which the imports are taken
1510in the local symbol table (so in the example, {\tt fibo} is not
1511defined).
1512
1513There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001514
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001515\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001516>>> from fibo import *
1517>>> fib(500)
15181 1 2 3 5 8 13 21 34 55 89 144 233 377
1519>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001520\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001521%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001522This imports all names except those beginning with an underscore
Guido van Rossum573805a1992-03-06 10:56:03 +00001523({\tt _}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001524
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001525\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001526
Guido van Rossum4410c751991-06-04 20:22:18 +00001527Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001528document (Python Library Reference). Some modules are built into the
1529interpreter; these provide access to operations that are not part of the
1530core of the language but are nevertheless built in, either for
1531efficiency or to provide access to operating system primitives such as
1532system calls. The set of such modules is a configuration option; e.g.,
1533the {\tt amoeba} module is only provided on systems that somehow support
1534Amoeba primitives. One particular module deserves some attention: {\tt
1535sys}, which is built into every Python interpreter. The variables {\tt
1536sys.ps1} and {\tt sys.ps2} define the strings used as primary and
1537secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001538
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001539\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001540>>> import sys
1541>>> sys.ps1
1542'>>> '
1543>>> sys.ps2
1544'... '
1545>>> sys.ps1 = 'C> '
1546C> print 'Yuck!'
1547Yuck!
1548C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001549\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001550%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001551These two variables are only defined if the interpreter is in
1552interactive mode.
1553
1554The variable
1555{\tt sys.path}
1556is a list of strings that determine the interpreter's search path for
1557modules.
1558It is initialized to a default path taken from the environment variable
1559{\tt PYTHONPATH},
1560or from a built-in default if
1561{\tt PYTHONPATH}
1562is not set.
1563You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001564
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001565\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001566>>> import sys
1567>>> sys.path.append('/ufs/guido/lib/python')
1568>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001569\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001570
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001571\section{The {\tt dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001572
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001573The built-in function {\tt dir} is used to find out which names a module
1574defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001575
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001576\bcode\begin{verbatim}
1577>>> import fibo, sys
1578>>> dir(fibo)
1579['fib', 'fib2']
1580>>> dir(sys)
1581['argv', 'exit', 'modules', 'path', 'ps1', 'ps2', 'stderr', 'stdin', 'stdout']
1582>>>
1583\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001584%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001585Without arguments, {\tt dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001586
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001587\bcode\begin{verbatim}
1588>>> a = [1, 2, 3, 4, 5]
1589>>> import fibo, sys
1590>>> fib = fibo.fib
1591>>> dir()
1592['a', 'fib', 'fibo', 'sys']
1593>>>
1594\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001595%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001596Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001597
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001598{\tt dir()} does not list the names of built-in functions and variables.
1599If you want a list of those, they are defined in the standard module
1600{\tt builtin}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001601
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001602\bcode\begin{verbatim}
1603>>> import builtin
1604>>> dir(builtin)
1605['EOFError', 'KeyboardInterrupt', 'MemoryError', 'NameError', 'None', 'Runti
1606meError', 'SystemError', 'TypeError', 'abs', 'chr', 'dir', 'divmod', 'eval',
1607 'exec', 'float', 'input', 'int', 'len', 'long', 'max', 'min', 'open', 'ord'
1608, 'pow', 'range', 'raw_input', 'reload', 'type']
1609>>>
1610\end{verbatim}\ecode
1611
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001612
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001613\chapter{Output Formatting}
1614
1615So far we've encountered two ways of writing values: {\em expression
1616statements} and the {\tt print} statement. (A third way is using the
1617{\tt write} method of file objects; the standard output file can be
1618referenced as {\tt sys.stdout}. See the Library Reference for more
1619information on this.)
1620
1621Often you'll want more control over the formatting of your output than
1622simply printing space-separated values. The key to nice formatting in
1623Python is to do all the string handling yourself; using string slicing
1624and concatenation operations you can create any lay-out you can imagine.
1625The standard module {\tt string} contains some useful operations for
1626padding strings to a given column width; these will be discussed shortly.
1627
1628One question remains, of course: how do you convert values to strings?
1629Luckily, Python has a way to convert any value to a string: just write
1630the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001631
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001632\bcode\begin{verbatim}
1633>>> x = 10 * 3.14
1634>>> y = 200*200
1635>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1636>>> print s
1637The value of x is 31.4, and y is 40000...
1638>>> # Reverse quotes work on other types besides numbers:
1639>>> p = [x, y]
1640>>> ps = `p`
1641>>> ps
1642'[31.4, 40000]'
1643>>> # Converting a string adds string quotes and backslashes:
1644>>> hello = 'hello, world\n'
1645>>> hellos = `hello`
1646>>> print hellos
1647'hello, world\012'
1648>>> # The argument of reverse quotes may be a tuple:
1649>>> `x, y, ('foo', 'bar')`
1650'(31.4, 40000, (\'foo\', \'bar\'))'
1651>>>
1652\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001653%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001654Here is how you write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001655
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001656\bcode\begin{verbatim}
1657>>> import string
1658>>> for x in range(1, 11):
1659... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1660... # Note trailing comma on previous line
1661... print string.rjust(`x*x*x`, 4)
1662...
1663 1 1 1
1664 2 4 8
1665 3 9 27
1666 4 16 64
1667 5 25 125
1668 6 36 216
1669 7 49 343
1670 8 64 512
1671 9 81 729
167210 100 1000
1673>>>
1674\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001675%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001676(Note that one space between each column was added by the way {\tt print}
1677works: it always adds spaces between its arguments.)
1678
1679This example demonstrates the function {\tt string.rjust()}, which
1680right-justifies a string in a field of a given width by padding it with
1681spaces on the left. There are similar functions {\tt string.ljust()}
1682and {\tt string.center()}. These functions do not write anything, they
1683just return a new string. If the input string is too long, they don't
1684truncate it, but return it unchanged; this will mess up your column
1685lay-out but that's usually better than the alternative, which would be
1686lying about a value. (If you really want truncation you can always add
1687a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1688
1689There is another function, {\tt string.zfill}, which pads a numeric
1690string on the left with zeros. It understands about plus and minus
1691signs:%
1692\footnote{
1693 Better facilities for formatting floating point numbers are
1694 lacking at this moment.
1695}
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001696
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001697\bcode\begin{verbatim}
1698>>> string.zfill('12', 5)
1699'00012'
1700>>> string.zfill('-3.14', 7)
1701'-003.14'
1702>>> string.zfill('3.14159265359', 5)
1703'3.14159265359'
1704>>>
1705\end{verbatim}\ecode
1706
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001707
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001708\chapter{Errors and Exceptions}
1709
1710Until now error messages haven't been more than mentioned, but if you
1711have tried out the examples you have probably seen some. There are
1712(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1713and {\em exceptions}.
1714
1715\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001716
1717Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001718kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001719
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001720\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001721>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001722Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001723while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001724 ^
1725Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001726>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001727\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001728%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001729The parser repeats the offending line and displays a little `arrow'
1730pointing at the earliest point in the line where the error was detected.
1731The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001732{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001733the arrow: in the example, the error is detected at the keyword
1734{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001735File name and line number are printed so you know where to look in case
1736the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001737
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001738\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001739
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001740Even if a statement or expression is syntactically correct, it may
1741cause an error when an attempt is made to execute it.
1742Errors detected during execution are called {\em exceptions} and are
1743not unconditionally fatal: you will soon learn how to handle them in
1744Python programs. Most exceptions are not handled by programs,
1745however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001746
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001747\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001748>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001749Unhandled exception: run-time error: integer division by zero
1750Stack backtrace (innermost last):
1751 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001752>>> 4 + foo*3
1753Unhandled exception: undefined name: foo
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001754Stack backtrace (innermost last):
1755 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001756>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001757Unhandled exception: type error: illegal argument type for built-in operation
1758Stack backtrace (innermost last):
1759 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001760>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001761\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001762%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001763The first line of the error message indicates what happened.
1764Exceptions come in different types, and the type is printed as part of
1765the message: the types in the example are
1766{\tt run-time error},
1767{\tt undefined name}
1768and
1769{\tt type error}.
1770The rest of the line is a detail whose interpretation depends on the
1771exception type.
1772
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001773The rest of the error message shows the context where the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001774exception happened.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001775In general it contains a stack backtrace listing source lines; however,
1776it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001777
1778Here is a summary of the most common exceptions:
1779\begin{itemize}
1780\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001781{\em Run-time\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001782are generally caused by wrong data used by the program; this can be the
1783programmer's fault or caused by bad input.
1784The detail states the cause of the error in more detail.
1785\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001786{\em Undefined\ name}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001787errors are more serious: these are usually caused by misspelled
1788identifiers.%
1789\footnote{
1790 The parser does not check whether names used in a program are at
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001791 all defined elsewhere in the program; such checks are
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001792 postponed until run-time. The same holds for type checking.
1793}
1794The detail is the offending identifier.
1795\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001796{\em Type\ errors} are also pretty serious: this is another case of
1797using wrong data (or better, using data the wrong way), but here the
1798error can be gleaned from the object type(s) alone. The detail shows
1799in what context the error was detected.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001800\end{itemize}
1801
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001802\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001803
1804It is possible to write programs that handle selected exceptions.
1805Look at the following example, which prints a table of inverses of
1806some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001807
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001808\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001809>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001810>>> for x in numbers:
1811... print x,
1812... try:
1813... print 1.0 / x
1814... except RuntimeError:
1815... print '*** has no inverse ***'
1816...
18170.3333 3.00030003
18182.5 0.4
18190 *** has no inverse ***
182010 0.1
1821>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001822\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001823%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001824The {\tt try} statement works as follows.
1825\begin{itemize}
1826\item
1827First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001828{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001829(the statement(s) between the {\tt try} and {\tt except} keywords) is
1830executed.
1831\item
1832If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001833{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001834is skipped and execution of the {\tt try} statement is finished.
1835\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001836If an exception occurs during execution of the try clause,
1837the rest of the clause is skipped. Then if
1838its type matches the exception named after the {\tt except} keyword,
1839the rest of the try clause is skipped, the except clause is executed,
1840and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001841\item
1842If an exception occurs which does not match the exception named in the
1843except clause, it is passed on to outer try statements; if no handler is
1844found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001845{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001846and execution stops with a message as shown above.
1847\end{itemize}
1848A {\tt try} statement may have more than one except clause, to specify
1849handlers for different exceptions.
1850At most one handler will be executed.
1851Handlers only handle exceptions that occur in the corresponding try
1852clause, not in other handlers of the same {\tt try} statement.
1853An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001854e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001855
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001856\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001857... except (RuntimeError, TypeError, NameError):
1858... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001859\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001860%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001861The last except clause may omit the exception name(s), to serve as a
1862wildcard.
1863Use this with extreme caution!
1864
1865When an exception occurs, it may have an associated value, also known as
1866the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001867{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001868The presence and type of the argument depend on the exception type.
1869For exception types which have an argument, the except clause may
1870specify a variable after the exception name (or list) to receive the
1871argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001872
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001873\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001874>>> try:
1875... foo()
1876... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001877... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001878...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001879name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001880>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001881\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001882%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001883If an exception has an argument, it is printed as the third part
1884(`detail') of the message for unhandled exceptions.
1885
1886Standard exception names are built-in identifiers (not reserved
1887keywords).
1888These are in fact string objects whose
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001889{\em object\ identity}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001890(not their value!) identifies the exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001891The string is printed as the second part of the message for unhandled
1892exceptions.
1893Their names and values are:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001894
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001895\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001896EOFError 'end-of-file read'
1897KeyboardInterrupt 'keyboard interrupt'
1898MemoryError 'out of memory' *
1899NameError 'undefined name' *
1900RuntimeError 'run-time error' *
1901SystemError 'system error' *
1902TypeError 'type error' *
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001903\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001904%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001905The meanings should be clear enough.
1906Those exceptions with a {\tt *} in the third column have an argument.
1907
1908Exception handlers don't just handle exceptions if they occur
1909immediately in the try clause, but also if they occur inside functions
1910that are called (even indirectly) in the try clause.
1911For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001912
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001913\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001914>>> def this_fails():
1915... x = 1/0
1916...
1917>>> try:
1918... this_fails()
1919... except RuntimeError, detail:
1920... print 'Handling run-time error:', detail
1921...
Guido van Rossum67fa1601991-04-23 14:14:57 +00001922Handling run-time error: integer division by zero
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001923>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001924\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001925
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001926\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001927
1928The {\tt raise} statement allows the programmer to force a specified
1929exception to occur.
1930For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001931
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001932\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001933>>> raise NameError, 'Hi There!'
1934Unhandled exception: undefined name: Hi There!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001935Stack backtrace (innermost last):
1936 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001937>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001938\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001939%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001940The first argument to {\tt raise} names the exception to be raised.
1941The optional second argument specifies the exception's argument.
1942
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001943\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001944
1945Programs may name their own exceptions by assigning a string to a
1946variable.
1947For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001948
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001949\bcode\begin{verbatim}
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001950>>> my_exc = 'Nobody likes me'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001951>>> try:
1952... raise my_exc, 2*2
1953... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001954... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001955...
1956My exception occured, value: 4
1957>>> raise my_exc, 1
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001958Nobody likes me: 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001959Stack backtrace (innermost last):
1960 File "<stdin>", line 7
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001961>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001962\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001963%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001964Many standard modules use this to report errors that may occur in
1965functions they define.
1966
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001967\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001968
1969The {\tt try} statement has another optional clause which is intended to
1970define clean-up actions that must be executed under all circumstances.
1971For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001972
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001973\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001974>>> try:
1975... raise KeyboardInterrupt
1976... finally:
1977... print 'Goodbye, world!'
1978...
1979Goodbye, world!
1980Unhandled exception: keyboard interrupt
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001981Stack backtrace (innermost last):
1982 File "<stdin>", line 2
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001983>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001984\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001985%
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001986A {\tt finally} clause is executed whether or not an exception has
1987occurred in the {\tt try} clause. When an exception has occurred, it
1988is re-raised after the {\tt finally} clauses is executed. The
1989{\tt finally} clause is also executed ``on the way out'' when the
1990{\tt try} statement is left via a {\tt break} or {\tt return}
1991statement.
1992
1993A {\tt try} statement must either have one or more {\tt except}
1994clauses or one {\tt finally} clause, but not both.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001995
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001996
1997\chapter{Classes}
1998
1999Python's class mechanism adds classes to the language with a minimum
2000of new syntax and semantics. It is a mixture of the class mechanisms
2001found in C++ and Modula-3. As is true for modules, classes in Python
2002do not put an absolute barrier between definition and user, but rather
2003rely on the politeness of the user not to ``break into the
2004definition.'' The most important features of classes are retained
2005with full power, however: the class inheritance mechanism allows
2006multiple base classes, a derived class can override any methods of its
2007base class(es), a method can call the method of a base class with the
2008same name. Objects can contain an arbitrary amount of private data.
2009
2010In C++ terminology, all class members (including the data members) are
2011{\em public}, and all member functions are {\em virtual}. There are
2012no special constructors or desctructors. As in Modula-3, there are no
2013shorthands for referencing the object's members from its methods: the
2014method function is declared with an explicit first argument
2015representing the object, which is provided implicitly by the call. As
2016in Smalltalk, classes themselves are objects, albeit in the wider
2017sense of the word: in Python, all data types are objects. This
2018provides semantics for importing and renaming. But, just like in C++
2019or Modula-3, built-in types cannot be used as base classes for
2020extension by the user. Also, like in Modula-3 but unlike in C++, the
2021built-in operators with special syntax (arithmetic operators,
2022subscriptong etc.) cannot be redefined for class members.
2023
2024
2025\section{A word about terminology}
2026
2027Lacking universally accepted terminology to talk about classes, I'll
2028make occasional use of Smalltalk and C++ terms. (I'd use Modula-3
2029terms, since its object-oriented semantics are closer to those of
2030Python than C++, but I expect that few readers have heard of it...)
2031
2032I also have to warn you that there's a terminological pitfall for
2033object-oriented readers: the word ``object'' in Python does not
2034necessarily mean a class instance. Like C++ and Modula-3, and unlike
2035Smalltalk, not all types in Python are classes: the basic built-in
2036types like integers and lists aren't, and even somewhat more exotic
2037types like files aren't. However, {\em all} Python types share a little
2038bit of common semantics that is best described by using the word
2039object.
2040
2041Objects have individuality, and multiple names (in multiple scopes)
2042can be bound to the same object. This is known as aliasing in other
2043languages. This is usually not appreciated on a first glance at
2044Python, and can be safely ignored when dealing with immutable basic
2045types (numbers, strings, tuples). However, aliasing has an
2046(intended!) effect on the semantics of Python code involving mutable
2047objects such as lists, dictionaries, and most types representing
2048entities outside the program (files, windows, etc.). This is usually
2049used to the benefit of the program, since aliases behave like pointers
2050in some respects. For example, passing an object is cheap since only
2051a pointer is passed by the implementation; and if a function modifies
2052an object passed as an argument, the caller will see the change --- this
2053obviates the need for two different argument passing mechanisms as in
2054Pascal.
2055
2056
2057\section{Python scopes and name spaces}
2058
2059Before introducing classes, I first have to tell you something about
2060Python's scope rules. Class definitions play some neat tricks with
2061name spaces, and you need to know how scopes and name spaces work to
2062fully understand what's going on. Incidentally, knowledge about this
2063subject is useful for any advanced Python programmer.
2064
2065Let's begin with some definitions.
2066
2067A {\em name space} is a mapping from names to objects. Most name
2068spaces are currently implemented as Python dictionaries, but that's
2069normally not noticeable in any way (except for performance), and it
2070may change in the future. Examples of name spaces are: the set of
2071built-in names (functions such as \verb\abs()\, and built-in exception
2072names); the global names in a module; and the local names in a
2073function invocation. In a sense the set of attributes of an object
2074also form a name space. The important things to know about name
2075spaces is that there is absolutely no relation between names in
2076different name spaces; for instance, two different modules may both
2077define a function ``maximize'' without confusion --- users of the
2078modules must prefix it with the module name.
2079
2080By the way, I use the word {\em attribute} for any name following a
2081dot --- for example, in the expression \verb\z.real\, \verb\real\ is
2082an attribute of the object \verb\z\. Strictly speaking, references to
2083names in modules are attribute references: in the expression
2084\verb\modname.funcname\, \verb\modname\ is a module object and
2085\verb\funcname\ is an attribute of it. In this case there happens to
2086be a straightforward mapping between the module's attributes and the
2087global names defined in the module: they share the same name space!%
2088\footnote{
2089 Except for one thing. Module objects have a secret read-only
2090 attribute called {\tt __dict__} which returns the dictionary
2091 used to implement the module's name space; the name
2092 {\tt __dict__} is an attribute but not a global name.
2093 Obviously, using this violates the abstraction of name space
2094 implementation, and should be restricted to things like
2095 post-mortem debuggers...
2096}
2097
2098Attributes may be read-only or writable. In the latter case,
2099assignment to attributes is possible. Module attributes are writable:
2100you can write \verb\modname.the_answer = 42\. Writable attributes may
2101also be deleted with the del statement, e.g.
2102\verb\del modname.the_answer\.
2103
2104Name spaces are created at different moments and have different
2105lifetimes. The name space containing the built-in names is created
2106when the Python interpreter starts up, and is never deleted. The
2107global name space for a module is created when the module definition
2108is read in; normally, module name spaces also last until the
2109interpreter quits. The statements executed by the top-level
2110invocation of the interpreter, either read from a script file or
2111interactively, are considered part of a module called \verb\__main__\,
2112so they have their own global name space. (The built-in names
2113actually also live in a module; this is called \verb\builtin\,
2114although it should really have been called \verb\__builtin__\.)
2115
2116The local name space for a function is created when the function is
2117called, and deleted when the function returns or raises an exception
2118that is not handled within the function. (Actually, forgetting would
2119be a better way to describe what actually happens.) Of course,
2120recursive invocations each have their own local name space.
2121
2122A {\em scope} is a textual region of a Python program where a name space
2123is directly accessible. ``Directly accessible'' here means that an
2124unqualified reference to a name attempts to find the name in the name
2125space.
2126
2127Although scopes are determined statically, they are used dynamically.
2128At any time during execution, exactly three nested scopes are in use
2129(i.e., exactly three name spaces are directly accessible): the
2130innermost scope, which is searched first, contains the local names,
2131the middle scope, searched next, contains the current module's global
2132names, and the outermost scope (searched last) is the name space
2133containing built-in names.
2134
2135Usually, the local scope references the local names of the (textually)
2136current function. Outside functions, the the local scope references
2137the same name space as the global scope: the module's name space.
2138Class definitions place yet another name space in the local scope.
2139
2140It is important to realize that scopes are determined textually: the
2141global scope of a function defined in a module is that module's name
2142space, no matter from where or by what alias the function is called.
2143On the other hand, the actual search for names is done dynamically, at
2144run time --- however, the the language definition is evolving towards
2145static name resolution, at ``compile'' time, so don't rely on dynamic
2146name resolution! (In fact, local variables are already determined
2147statically.)
2148
2149A special quirk of Python is that assignments always go into the
2150innermost scope. Assignments do not copy data --- they just
2151bind names to objects. The same is true for deletions: the statement
2152\verb\del x\ removes the binding of x from the name space referenced by the
2153local scope. In fact, all operations that introduce new names use the
2154local scope: in particular, import statements and function definitions
2155bind the module or function name in the local scope. (The
2156\verb\global\ statement can be used to indicate that particular
2157variables live in the global scope.)
2158
2159
2160\section{A first look at classes}
2161
2162Classes introduce a little bit of new syntax, three new object types,
2163and some new semantics.
2164
2165
2166\subsection{Class definition syntax}
2167
2168The simplest form of class definition looks like this:
2169
2170\begin{verbatim}
2171 class ClassName:
2172 <statement-1>
2173 .
2174 .
2175 .
2176 <statement-N>
2177\end{verbatim}
2178
2179Class definitions, like function definitions (\verb\def\ statements)
2180must be executed before they have any effect. (You could conceivably
2181place a class definition in a branch of an \verb\if\ statement, or
2182inside a function.)
2183
2184In practice, the statements inside a class definition will usually be
2185function definitions, but other statements are allowed, and sometimes
2186useful --- we'll come back to this later. The function definitions
2187inside a class normally have a peculiar form of argument list,
2188dictated by the calling conventions for methods --- again, this is
2189explained later.
2190
2191When a class definition is entered, a new name space is created, and
2192used as the local scope --- thus, all assignments to local variables
2193go into this new name space. In particular, function definitions bind
2194the name of the new function here.
2195
2196When a class definition is left normally (via the end), a {\em class
2197object} is created. This is basically a wrapper around the contents
2198of the name space created by the class definition; we'll learn more
2199about class objects in the next section. The original local scope
2200(the one in effect just before the class definitions was entered) is
2201reinstated, and the class object is bound here to class name given in
2202the class definition header (ClassName in the example).
2203
2204
2205\subsection{Class objects}
2206
2207Class objects support two kinds of operations: attribute references
2208and instantiation.
2209
2210{\em Attribute references} use the standard syntax used for all
2211attribute references in Python: \verb\obj.name\. Valid attribute
2212names are all the names that were in the class's name space when the
2213class object was created. So, if the class definition looked like
2214this:
2215
2216\begin{verbatim}
2217 class MyClass:
2218 i = 12345
2219 def f(x):
2220 return 'hello world'
2221\end{verbatim}
2222
2223then \verb\MyClass.i\ and \verb\MyClass.f\ are valid attribute
2224references, returning an integer and a function object, respectively.
2225Class attributes can also be assigned to, so you can change the
2226value of \verb\MyClass.i\ by assignment.
2227
2228Class {\em instantiation} uses function notation. Just pretend that
2229the class object is a parameterless function that returns a new
2230instance of the class. For example, (assuming the above class):
2231
2232\begin{verbatim}
2233 x = MyClass()
2234\end{verbatim}
2235
2236creates a new {\em instance} of the class and assigns this object to
2237the local variable \verb\x\.
2238
2239
2240\subsection{Instance objects}
2241
2242Now what can we do with instance objects? The only operations
2243understood by instance objects are attribute references. There are
2244two kinds of valid attribute names.
2245
2246The first I'll call {\em data attributes}. These correspond to
2247``instance variables'' in Smalltalk, and to ``data members'' in C++.
2248Data attributes need not be declared; like local variables, they
2249spring into existence when they are first assigned to. For example,
2250if \verb\x\ in the instance of \verb\MyClass\ created above, the
2251following piece of code will print the value 16, without leaving a
2252trace:
2253
2254\begin{verbatim}
2255 x.counter = 1
2256 while x.counter < 10:
2257 x.counter = x.counter * 2
2258 print x.counter
2259 del x.counter
2260\end{verbatim}
2261
2262The second kind of attribute references understood by instance objects
2263are {\em methods}. A method is a function that ``belongs to'' an
2264object. (In Python, the term method is not unique to class instances:
2265other object types can have methods as well, e.g., list objects have
2266methods called append, insert, remove, sort, and so on. However,
2267below, we'll use the term method exclusively to mean methods of class
2268instance objects, unless explicitly stated otherwise.)
2269
2270Valid method names of an instance object depend on its class. By
2271definition, all attributes of a class that are (user-defined) function
2272objects define corresponding methods of its instances. So in our
2273example, \verb\x.f\ is a valid method reference, since
2274\verb\MyClass.f\ is a function, but \verb\x.i\ is not, since
2275\verb\MyClass.i\ is not. But \verb\x.f\ is not the
2276same thing as \verb\MyClass.f\ --- it is a {\em method object}, not a
2277function object.
2278
2279
2280\subsection{Method objects}
2281
2282Usually, a method is called immediately, e.g.:
2283
2284\begin{verbatim}
2285 x.f()
2286\end{verbatim}
2287
2288In our example, this will return the string \verb\'hello world'\.
2289However, it is not necessary to call a method right away: \verb\x.f\
2290is a method object, and can be stored away and called at a later
2291moment, for example:
2292
2293\begin{verbatim}
2294 xf = x.f
2295 while 1:
2296 print xf()
2297\end{verbatim}
2298
2299will continue to print \verb\hello world\ until the end of time.
2300
2301What exactly happens when a method is called? You may have noticed
2302that \verb\x.f()\ was called without an argument above, even though
2303the function definition for \verb\f\ specified an argument. What
2304happened to the argument? Surely Python raises an exception when a
2305function that requires an argument is called without any --- even if
2306the argument isn't actually used...
2307
2308Actually, you may have guessed the answer: the special thing about
2309methods is that the object is passed as the first argument of the
2310function. In our example, the call \verb\x.f()\ is exactly equivalent
2311to \verb\MyClass.f(x)\. In general, calling a method with a list of
2312{\em n} arguments is equivalent to calling the corresponding function
2313with an argument list that is created by inserting the method's object
2314before the first argument.
2315
2316If you still don't understand how methods work, a look at the
2317implementation can perhaps clarify matters. When an instance
2318attribute is referenced that isn't a data attribute, its class is
2319searched. If the name denotes a valid class attribute that is a
2320function object, a method object is created by packing (pointers to)
2321the instance object and the function object just found together in an
2322abstract object: this is the method object. When the method object is
2323called with an argument list, it is unpacked again, a new argument
2324list is constructed from the instance object and the original argument
2325list, and the function object is called with this new argument list.
2326
2327
2328\section{Random remarks}
2329
2330
2331[These should perhaps be placed more carefully...]
2332
2333
2334Data attributes override method attributes with the same name; to
2335avoid accidental name conflicts, which may cause hard-to-find bugs in
2336large programs, it is wise to use some kind of convention that
2337minimizes the chance of conflicts, e.g., capitalize method names,
2338prefix data attribute names with a small unique string (perhaps just
2339an undescore), or use verbs for methods and nouns for data attributes.
2340
2341
2342Data attributes may be referenced by methods as well as by ordinary
2343users (``clients'') of an object. In other words, classes are not
2344usable to implement pure abstract data types. In fact, nothing in
2345Python makes it possible to enforce data hiding --- it is all based
2346upon convention. (On the other hand, the Python implementation,
2347written in C, can completely hide implementation details and control
2348access to an object if necessary; this can be used by extensions to
2349Python written in C.)
2350
2351
2352Clients should use data attributes with care --- clients may mess up
2353invariants maintained by the methods by stamping on their data
2354attributes. Note that clients may add data attributes of their own to
2355an instance object without affecting the validity of the methods, as
2356long as name conflicts are avoided --- again, a naming convention can
2357save a lot of headaches here.
2358
2359
2360There is no shorthand for referencing data attributes (or other
2361methods!) from within methods. I find that this actually increases
2362the readability of methods: there is no chance of confusing local
2363variables and instance variables when glancing through a method.
2364
2365
2366Conventionally, the first argument of methods is often called
2367\verb\self\. This is nothing more than a convention: the name
2368\verb\self\ has absolutely no special meaning to Python. (Note,
2369however, that by not following the convention your code may be less
2370readable by other Python programmers, and it is also conceivable that
2371a {\em class browser} program be written which relies upon such a
2372convention.)
2373
2374
2375Any function object that is a class attribute defines a method for
2376instances of that class. It is not necessary that the function
2377definition is textually enclosed in the class definition: assigning a
2378function object to a local variable in the class is also ok. For
2379example:
2380
2381\begin{verbatim}
2382 # Function defined outside the class
2383 def f1(self, x, y):
2384 return min(x, x+y)
2385
2386 class C:
2387 f = f1
2388 def g(self):
2389 return 'hello world'
2390 h = g
2391\end{verbatim}
2392
2393Now \verb\f\, \verb\g\ and \verb\h\ are all attributes of class
2394\verb\C\ that refer to function objects, and consequently they are all
2395methods of instances of \verb\C\ --- \verb\h\ being exactly equivalent
2396to \verb\g\. Note that this practice usually only serves to confuse
2397the reader of a program.
2398
2399
2400Methods may call other methods by using method attributes of the
2401\verb\self\ argument, e.g.:
2402
2403\begin{verbatim}
2404 class Bag:
2405 def empty(self):
2406 self.data = []
2407 def add(self, x):
2408 self.data.append(x)
2409 def addtwice(self, x):
Guido van Rossum084b0b21992-08-14 09:19:56 +00002410 self.add(x)
2411 self.add(x)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002412\end{verbatim}
2413
2414
2415The instantiation operation (``calling'' a class object) creates an
2416empty object. Many classes like to create objects in a known initial
2417state. There is no special syntax to enforce this, but a convention
2418works almost as well: add a method named \verb\init\ to the class,
2419which initializes the instance (by assigning to some important data
2420attributes) and returns the instance itself. For example, class
2421\verb\Bag\ above could have the following method:
2422
2423\begin{verbatim}
2424 def init(self):
2425 self.empty()
2426 return self
2427\end{verbatim}
2428
2429The client can then create and initialize an instance in one
2430statement, as follows:
2431
2432\begin{verbatim}
2433 x = Bag().init()
2434\end{verbatim}
2435
2436Of course, the \verb\init\ method may have arguments for greater
2437flexibility.
2438
2439Warning: a common mistake is to forget the \verb\return self\ at the
2440end of an init method!
2441
2442
2443Methods may reference global names in the same way as ordinary
2444functions. The global scope associated with a method is the module
2445containing the class definition. (The class itself is never used as a
2446global scope!) While one rarely encounters a good reason for using
2447global data in a method, there are many legitimate uses of the global
2448scope: for one thing, functions and modules imported into the global
2449scope can be used by methods, as well as functions and classes defined
2450in it. Usually, the class containing the method is itself defined in
2451this global scope, and in the next section we'll find some good
2452reasons why a method would want to reference its own class!
2453
2454
2455\section{Inheritance}
2456
2457Of course, a language feature would not be worthy of the name ``class''
2458without supporting inheritance. The syntax for a derived class
2459definition looks as follows:
2460
2461\begin{verbatim}
2462 class DerivedClassName(BaseClassName):
2463 <statement-1>
2464 .
2465 .
2466 .
2467 <statement-N>
2468\end{verbatim}
2469
2470The name \verb\BaseClassName\ must be defined in a scope containing
2471the derived class definition. Instead of a base class name, an
2472expression is also allowed. This is useful when the base class is
2473defined in another module, e.g.,
2474
2475\begin{verbatim}
2476 class DerivedClassName(modname.BaseClassName):
2477\end{verbatim}
2478
2479Execution of a derived class definition proceeds the same as for a
2480base class. When the class object is constructed, the base class is
2481remembered. This is used for resolving attribute references: if a
2482requested attribute is not found in the class, it is searched in the
2483base class. This rule is applied recursively if the base class itself
2484is derived from some other class.
2485
2486There's nothing special about instantiation of derived classes:
2487\verb\DerivedClassName()\ creates a new instance of the class. Method
2488references are resolved as follows: the corresponding class attribute
2489is searched, descending down the chain of base classes if necessary,
2490and the method reference is valid if this yields a function object.
2491
2492Derived classes may override methods of their base classes. Because
2493methods have no special privileges when calling other methods of the
2494same object, a method of a base class that calls another method
2495defined in the same base class, may in fact end up calling a method of
2496a derived class that overrides it. (For C++ programmers: all methods
2497in Python are ``virtual functions''.)
2498
2499An overriding method in a derived class may in fact want to extend
2500rather than simply replace the base class method of the same name.
2501There is a simple way to call the base class method directly: just
2502call \verb\BaseClassName.methodname(self, arguments)\. This is
2503occasionally useful to clients as well. (Note that this only works if
2504the base class is defined or imported directly in the global scope.)
2505
2506
2507\subsection{Multiple inheritance}
2508
2509Poython supports a limited form of multiple inheritance as well. A
2510class definition with multiple base classes looks as follows:
2511
2512\begin{verbatim}
2513 class DerivedClassName(Base1, Base2, Base3):
2514 <statement-1>
2515 .
2516 .
2517 .
2518 <statement-N>
2519\end{verbatim}
2520
2521The only rule necessary to explain the semantics is the resolution
2522rule used for class attribute references. This is depth-first,
2523left-to-right. Thus, if an attribute is not found in
2524\verb\DerivedClassName\, it is searched in \verb\Base1\, then
2525(recursively) in the base classes of \verb\Base1\, and only if it is
2526not found there, it is searched in \verb\Base2\, and so on.
2527
2528(To some people breadth first --- searching \verb\Base2\ and
2529\verb\Base3\ before the base classes of \verb\Base1\ --- looks more
2530natural. However, this would require you to know whether a particular
2531attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in
2532one of its base classes before you can figure out the consequences of
2533a name conflict with an attribute of \verb\Base2\. The depth-first
2534rule makes no differences between direct and inherited attributes of
2535\verb\Base1\.)
2536
2537It is clear that indiscriminate use of multiple inheritance is a
2538maintenance nightmare, given the reliance in Python on conventions to
2539avoid accidental name conflicts. A well-known problem with multiple
2540inheritance is a class derived from two classes that happen to have a
2541common base class. While it is easy enough to figure out what happens
2542in this case (the instance will have a single copy of ``instance
2543variables'' or data attributes used by the common base class), it is
2544not clear that these semantics are in any way useful.
2545
2546
2547\section{Odds and ends}
2548
2549Sometimes it is useful to have a data type similar to the Pascal
2550``record'' or C ``struct'', bundling together a couple of named data
2551items. An empty class definition will do nicely, e.g.:
2552
2553\begin{verbatim}
2554 class Employee:
2555 pass
2556
2557 john = Employee() # Create an empty employee record
2558
2559 # Fill the fields of the record
2560 john.name = 'John Doe'
2561 john.dept = 'computer lab'
2562 john.salary = 1000
2563\end{verbatim}
2564
2565
2566A piece of Python code that expects a particular abstract data type
2567can often be passed a class that emulates the methods of that data
2568type instead. For instance, if you have a function that formats some
2569data from a file object, you can define a class with methods
2570\verb\read()\ and \verb\readline()\ that gets the data from a string
2571buffer instead, and pass it as an argument. (Unfortunately, this
2572technique has its limitations: a class can't define operations that
2573are accessed by special syntax such as sequence subscripting or
2574arithmetic operators, and assigning such a ``pseudo-file'' to
2575\verb\sys.stdin\ will not cause the interpreter to read further input
2576from it.)
2577
2578
2579Instance method objects have attributes, too: \verb\m.im_self\ is the
2580object of which the method is an instance, and \verb\m.im_func\ is the
2581function object corresponding to the method.
2582
2583
2584XXX Mention bw compat hacks.
2585
2586
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002587\end{document}