blob: 55ff36f77b824255a1e5e9c314e4d4aa8b760d07 [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
Guido van Rossum6938f061994-08-01 12:22:53 +00003\title{Python Tutorial}
4
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00005\author{
Guido van Rossum6938f061994-08-01 12:22:53 +00006 Guido van Rossum \\
7 Dept. CST, CWI, P.O. Box 94079 \\
8 1090 GB Amsterdam, The Netherlands \\
9 E-mail: {\tt guido@cwi.nl}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000010}
11
Guido van Rossum16d6e711994-08-08 12:30:22 +000012\date{14 July 1994 \\ Release 1.0.3} % XXX update before release!
Guido van Rossum83eb9621993-11-23 16:28:45 +000013
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000014\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
Guido van Rossum6938f061994-08-01 12:22:53 +000066call or other function that is only accessible from C \ldots Usually
Guido van Rossum6fc178f1991-08-16 09:13:42 +000067the 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,
Guido van Rossum6938f061994-08-01 12:22:53 +0000138and finally touching upon advanced concepts like exceptions
139and user-defined classes.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000140
Guido van Rossum6938f061994-08-01 12:22:53 +0000141When you're through with the tutorial (or just getting bored), you
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000142should read the Library Reference, which gives complete (though terse)
143reference material about built-in and standard types, functions and
144modules that can save you a lot of time when writing Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000145
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000146
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000147\chapter{Using the Python Interpreter}
148
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000149\section{Invoking the Interpreter}
150
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000151The Python interpreter is usually installed as {\tt /usr/local/bin/python}
152on those machines where it is available; putting {\tt /usr/local/bin} in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000153your {\UNIX} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000154typing the command
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000155
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000156\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000157python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000158\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000159%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000160to the shell. Since the choice of the directory where the interpreter
161lives is an installation option, other places are possible; check with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000162your local Python guru or system administrator. (E.g., {\tt
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000163/usr/local/python} is a popular alternative location.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000164
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000165The interpreter operates somewhat like the {\UNIX} shell: when called
166with standard input connected to a tty device, it reads and executes
167commands interactively; when called with a file name argument or with
168a file as standard input, it reads and executes a {\em script} from
169that file.
170
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000171A third way of starting the interpreter is
172``{\tt python -c command [arg] ...}'', which
173executes the statement(s) in {\tt command}, analogous to the shell's
174{\tt -c} option. Since Python statements often contain spaces or other
175characters that are special to the shell, it is best to quote {\tt
176command} in its entirety with double quotes.
177
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000178Note that there is a difference between ``{\tt python file}'' and
179``{\tt python $<$file}''. In the latter case, input requests from the
Guido van Rossum573805a1992-03-06 10:56:03 +0000180program, such as calls to {\tt input()} and {\tt raw_input()}, are
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000181satisfied from {\em file}. Since this file has already been read
182until the end by the parser before the program starts executing, the
183program will encounter EOF immediately. In the former case (which is
184usually what you want) they are satisfied from whatever file or device
185is connected to standard input of the Python interpreter.
186
Guido van Rossumb2c65561993-05-12 08:53:36 +0000187When a script file is used, it is sometimes useful to be able to run
188the script and enter interactive mode afterwards. This can be done by
189passing {\tt -i} before the script. (This does not work if the script
190is read from standard input, for the same reason as explained in the
191previous paragraph.)
192
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000193\subsection{Argument Passing}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000194
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000195When known to the interpreter, the script name and additional
196arguments thereafter are passed to the script in the variable {\tt
197sys.argv}, which is a list of strings. Its length is at least one;
198when no script and no arguments are given, {\tt sys.argv[0]} is an
199empty string. When the script name is given as {\tt '-'} (meaning
200standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c
201command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options
202found after {\tt -c command} are not consumed by the Python
203interpreter's option processing but left in {\tt sys.argv} for the
204command to handle.
205
206\subsection{Interactive Mode}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000207
Guido van Rossumdd010801991-06-07 14:31:11 +0000208When commands are read from a tty, the interpreter is said to be in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000209{\em interactive\ mode}. In this mode it prompts for the next command
210with the {\em primary\ prompt}, usually three greater-than signs ({\tt
211>>>}); for continuation lines it prompts with the {\em secondary\
212prompt}, by default three dots ({\tt ...}). Typing an EOF (Control-D)
213at the primary prompt causes the interpreter to exit with a zero exit
214status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000215
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000216The interpreter prints a welcome message stating its version number
217and a copyright notice before printing the first prompt, e.g.:
218
219\bcode\begin{verbatim}
220python
Guido van Rossum6938f061994-08-01 12:22:53 +0000221Python 1.0.3 (Jul 14 1994)
222Copyright 1991-1994 Stichting Mathematisch Centrum, Amsterdam
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000223>>>
224\end{verbatim}\ecode
225
226\section{The Interpreter and its Environment}
227
228\subsection{Error Handling}
229
230When an error occurs, the interpreter prints an error
231message and a stack trace. In interactive mode, it then returns to
232the primary prompt; when input came from a file, it exits with a
233nonzero exit status after printing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000234the stack trace. (Exceptions handled by an {\tt except} clause in a
235{\tt try} statement are not errors in this context.) Some errors are
236unconditionally fatal and cause an exit with a nonzero exit; this
237applies to internal inconsistencies and some cases of running out of
238memory. All error messages are written to the standard error stream;
239normal output from the executed commands is written to standard
240output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000241
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000242Typing the interrupt character (usually Control-C or DEL) to the
243primary or secondary prompt cancels the input and returns to the
244primary prompt.%
245\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +0000246 A problem with the GNU Readline package may prevent this.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000247}
248Typing an interrupt while a command is executing raises the {\tt
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000249KeyboardInterrupt} exception, which may be handled by a {\tt try}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000250statement.
251
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000252\subsection{The Module Search Path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000253
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000254When a module named {\tt foo} is imported, the interpreter searches
255for a file named {\tt foo.py} in the list of directories specified by
256the environment variable {\tt PYTHONPATH}. It has the same syntax as
257the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000258directory names. When {\tt PYTHONPATH} is not set, or when the file
259is not found there, the search continues in an installation-dependent
260default path, usually {\tt .:/usr/local/lib/python}.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000261
262Actually, modules are searched in the list of directories given by the
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000263variable {\tt sys.path} which is initialized from {\tt PYTHONPATH} and
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000264the installation-dependent default. This allows Python programs that
265know what they're doing to modify or replace the module search path.
266See the section on Standard Modules later.
267
268\subsection{``Compiled'' Python files}
269
270As an important speed-up of the start-up time for short programs that
271use a lot of standard modules, if a file called {\tt foo.pyc} exists
272in the directory where {\tt foo.py} is found, this is assumed to
273contain an already-``compiled'' version of the module {\tt foo}. The
274modification time of the version of {\tt foo.py} used to create {\tt
275foo.pyc} is recorded in {\tt foo.pyc}, and the file is ignored if
276these don't match.
277
278Whenever {\tt foo.py} is successfully compiled, an attempt is made to
279write the compiled version to {\tt foo.pyc}. It is not an error if
280this attempt fails; if for any reason the file is not written
281completely, the resulting {\tt foo.pyc} file will be recognized as
282invalid and thus ignored later.
283
284\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000285
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000286On BSD'ish {\UNIX} systems, Python scripts can be made directly
287executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000288
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000289\bcode\begin{verbatim}
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000290#! /usr/local/bin/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000291\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000292%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000293(assuming that's the name of the interpreter) at the beginning of the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000294script and giving the file an executable mode. The {\tt \#!} must be
295the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000296
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000297\subsection{The Interactive Startup File}
298
299When you use Python interactively, it is frequently handy to have some
300standard commands executed every time the interpreter is started. You
301can do this by setting an environment variable named {\tt
302PYTHONSTARTUP} to the name of a file containing your start-up
Guido van Rossum6938f061994-08-01 12:22:53 +0000303commands. This is similar to the {\tt .profile} feature of the UNIX
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000304shells.
305
306This file is only read in interactive sessions, not when Python reads
307commands from a script, and not when {\tt /dev/tty} is given as the
308explicit source of commands (which otherwise behaves like an
309interactive session). It is executed in the same name space where
310interactive commands are executed, so that objects that it defines or
311imports can be used without qualification in the interactive session.
Guido van Rossum7b3c8a11992-09-08 09:20:13 +0000312You can also change the prompts {\tt sys.ps1} and {\tt sys.ps2} in
313this file.
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000314
315If you want to read an additional start-up file from the current
316directory, you can program this in the global start-up file, e.g.
317\verb\execfile('.pythonrc')\. If you want to use the startup file
318in a script, you must write this explicitly in the script, e.g.
319\verb\import os;\ \verb\execfile(os.environ['PYTHONSTARTUP'])\.
320
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000321\section{Interactive Input Editing and History Substitution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000322
Guido van Rossum4410c751991-06-04 20:22:18 +0000323Some versions of the Python interpreter support editing of the current
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000324input line and history substitution, similar to facilities found in
325the Korn shell and the GNU Bash shell. This is implemented using the
326{\em GNU\ Readline} library, which supports Emacs-style and vi-style
327editing. This library has its own documentation which I won't
328duplicate here; however, the basics are easily explained.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000329
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000330Perhaps the quickest check to see whether command line editing is
331supported is typing Control-P to the first Python prompt you get. If
332it beeps, you have command line editing. If nothing appears to
333happen, or if \verb/^P/ is echoed, you can skip the rest of this
334section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000335
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000336\subsection{Line Editing}
337
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000338If supported, input line editing is active whenever the interpreter
339prints a primary or secondary prompt. The current line can be edited
340using the conventional Emacs control characters. The most important
341of these are: C-A (Control-A) moves the cursor to the beginning of the
342line, C-E to the end, C-B moves it one position to the left, C-F to
343the right. Backspace erases the character to the left of the cursor,
344C-D the character to its right. C-K kills (erases) the rest of the
345line to the right of the cursor, C-Y yanks back the last killed
346string. C-underscore undoes the last change you made; it can be
347repeated for cumulative effect.
348
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000349\subsection{History Substitution}
350
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000351History substitution works as follows. All non-empty input lines
352issued are saved in a history buffer, and when a new prompt is given
353you are positioned on a new line at the bottom of this buffer. C-P
354moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000355Any line in the history buffer can be edited; an asterisk appears in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000356front of the prompt to mark a line as modified. Pressing the Return
357key passes the current line to the interpreter. C-R starts an
358incremental reverse search; C-S starts a forward search.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000359
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000360\subsection{Key Bindings}
361
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000362The key bindings and some other parameters of the Readline library can
363be customized by placing commands in an initialization file called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000364{\tt \$HOME/.inputrc}. Key bindings have the form
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000365
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000366\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000367key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000368\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000369%
370or
371
372\bcode\begin{verbatim}
373"string": function-name
374\end{verbatim}\ecode
375%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000376and options can be set with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000377
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000378\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000379set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000380\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000381%
382For example:
383
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000384\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000385# I prefer vi-style editing:
386set editing-mode vi
387# Edit using a single line:
388set horizontal-scroll-mode On
389# Rebind some keys:
390Meta-h: backward-kill-word
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000391"\C-u": universal-argument
392"\C-x\C-r": re-read-init-file
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000393\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000394%
Guido van Rossum4410c751991-06-04 20:22:18 +0000395Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000396instead of Readline's default filename completion function. If you
397insist, you can override this by putting
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000398
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000399\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000400TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000401\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000402%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000403in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000404indented continuation lines...)
405
406\subsection{Commentary}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000407
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000408This facility is an enormous step forward compared to previous
409versions of the interpreter; however, some wishes are left: It would
410be nice if the proper indentation were suggested on continuation lines
411(the parser knows if an indent token is required next). The
412completion mechanism might use the interpreter's symbol table. A
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000413command to check (or even suggest) matching parentheses, quotes etc.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000414would also be useful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000415
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000416
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000417\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000418
419In the following examples, input and output are distinguished by the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000420presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
421the example, you must type everything after the prompt, when the
422prompt appears; lines that do not begin with a prompt are output from
423the interpreter.%
424\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +0000425 I'd prefer to use different fonts to distinguish input
426 from output, but the amount of LaTeX hacking that would require
427 is currently beyond my ability.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000428}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000429Note that a secondary prompt on a line by itself in an example means
430you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000431
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000432\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000433
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000434Let's try some simple Python commands. Start the interpreter and wait
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000435for the primary prompt, {\tt >>>}. (It shouldn't take long.)
436
437\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000438
439The interpreter acts as a simple calculator: you can type an
440expression at it and it will write the value. Expression syntax is
441straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000442work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000443can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000444
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000445\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000446>>> 2+2
4474
Guido van Rossum6938f061994-08-01 12:22:53 +0000448>>> # This is a comment
449... 2+2
4504
451>>> 2+2 # and a comment on the same line as code
4524
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000453>>> (50-5*6)/4
4545
Guido van Rossum6938f061994-08-01 12:22:53 +0000455>>> # Integer division returns the floor:
456... 7/3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004572
Guido van Rossum6938f061994-08-01 12:22:53 +0000458>>> 7/-3
459-3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000460>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000461\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000462%
463Like in C, the equal sign ({\tt =}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000464variable. The value of an assignment is not written:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000465
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000466\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000467>>> width = 20
468>>> height = 5*9
469>>> width * height
470900
471>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000472\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000473%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000474A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000475
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000476\bcode\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000477>>> x = y = z = 0 # Zero x, y and z
478>>> x
4790
480>>> y
4810
482>>> z
4830
484>>>
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000485\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000486%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000487There is full support for floating point; operators with mixed type
488operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000489
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000490\bcode\begin{verbatim}
491>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004923.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000493>>> 7.0 / 2
4943.5
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000495>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000496\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000497
498\subsection{Strings}
499
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000500Besides numbers, Python can also manipulate strings, enclosed in
Guido van Rossum6938f061994-08-01 12:22:53 +0000501single quotes or double quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000502
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000503\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000504>>> 'foo bar'
505'foo bar'
506>>> 'doesn\'t'
Guido van Rossum6938f061994-08-01 12:22:53 +0000507"doesn't"
508>>> "doesn't"
509"doesn't"
510>>> '"Yes," he said.'
511'"Yes," he said.'
512>>> "\"Yes,\" he said."
513'"Yes," he said.'
514>>> '"Isn\'t," she said.'
515'"Isn\'t," she said.'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000516>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000517\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000518%
519Strings are written the same way as they are typed for input: inside
Guido van Rossum6938f061994-08-01 12:22:53 +0000520quotes and with quotes and other funny characters escaped by backslashes,
521to show the precise value. The string is enclosed in double quotes if
522the string contains a single quote and no double quotes, else it's
523enclosed in single quotes. (The {\tt print} statement, described later,
524can be used to write strings without quotes or escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000525
526Strings can be concatenated (glued together) with the {\tt +}
527operator, and repeated with {\tt *}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000528
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000529\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000530>>> word = 'Help' + 'A'
531>>> word
532'HelpA'
533>>> '<' + word*5 + '>'
534'<HelpAHelpAHelpAHelpAHelpA>'
535>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000536\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000537%
538Strings can be subscripted (indexed); like in C, the first character of
539a string has subscript (index) 0.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000540
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000541There is no separate character type; a character is simply a string of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000542size one. Like in Icon, substrings can be specified with the {\em
543slice} notation: two indices separated by a colon.
544
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000545\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000546>>> word[4]
547'A'
548>>> word[0:2]
549'He'
550>>> word[2:4]
551'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000552>>>
553\end{verbatim}\ecode
554%
555Slice indices have useful defaults; an omitted first index defaults to
556zero, an omitted second index defaults to the size of the string being
557sliced.
558
559\bcode\begin{verbatim}
560>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000561'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000562>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000563'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000564>>>
565\end{verbatim}\ecode
566%
567Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
568equals \verb\s\.
569
570\bcode\begin{verbatim}
571>>> word[:2] + word[2:]
572'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000573>>> word[:3] + word[3:]
574'HelpA'
575>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000576\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000577%
578Degenerate slice indices are handled gracefully: an index that is too
579large is replaced by the string size, an upper bound smaller than the
580lower bound returns an empty string.
581
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000582\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000583>>> word[1:100]
584'elpA'
585>>> word[10:]
586''
587>>> word[2:1]
588''
589>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000590\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000591%
592Indices may be negative numbers, to start counting from the right.
593For example:
594
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000595\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000596>>> word[-1] # The last character
597'A'
598>>> word[-2] # The last-but-one character
599'p'
600>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000601'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000602>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000603'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000604>>>
605\end{verbatim}\ecode
606%
607But note that -0 is really the same as 0, so it does not count from
608the right!
609
610\bcode\begin{verbatim}
611>>> word[-0] # (since -0 equals 0)
612'H'
613>>>
614\end{verbatim}\ecode
615%
616Out-of-range negative slice indices are truncated, but don't try this
617for single-element (non-slice) indices:
618
619\bcode\begin{verbatim}
620>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000621'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000622>>> word[-10] # error
Guido van Rossum6938f061994-08-01 12:22:53 +0000623Traceback (innermost last):
624 File "<stdin>", line 1
625IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000626>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000627\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000628%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000629The best way to remember how slices work is to think of the indices as
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000630pointing {\em between} characters, with the left edge of the first
631character numbered 0. Then the right edge of the last character of a
632string of {\tt n} characters has index {\tt n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000633
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000634\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000635 +---+---+---+---+---+
636 | H | e | l | p | A |
637 +---+---+---+---+---+
638 0 1 2 3 4 5
639-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000640\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000641%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000642The first row of numbers gives the position of the indices 0...5 in
643the string; the second row gives the corresponding negative indices.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000644The slice from \verb\i\ to \verb\j\ consists of all characters between
645the edges labeled \verb\i\ and \verb\j\, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000646
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000647For nonnegative indices, the length of a slice is the difference of
648the indices, if both are within bounds, e.g., the length of
649\verb\word[1:3]\ is 2.
650
651The built-in function {\tt len()} returns the length of a string:
652
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000653\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000654>>> s = 'supercalifragilisticexpialidocious'
655>>> len(s)
65634
657>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000658\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000659
660\subsection{Lists}
661
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000662Python knows a number of {\em compound} data types, used to group
663together other values. The most versatile is the {\em list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000664can be written as a list of comma-separated values (items) between
665square brackets. List items need not all have the same type.
666
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000667\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000668>>> a = ['foo', 'bar', 100, 1234]
669>>> a
670['foo', 'bar', 100, 1234]
671>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000672\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000673%
674Like string indices, list indices start at 0, and lists can be sliced,
675concatenated and so on:
676
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000677\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000678>>> a[0]
679'foo'
680>>> a[3]
6811234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000682>>> a[-2]
683100
684>>> a[1:-1]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000685['bar', 100]
686>>> a[:2] + ['bletch', 2*2]
687['foo', 'bar', 'bletch', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000688>>> 3*a[:3] + ['Boe!']
689['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000690>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000691\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000692%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000693Unlike strings, which are {\em immutable}, it is possible to change
694individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000695
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000696\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000697>>> a
698['foo', 'bar', 100, 1234]
699>>> a[2] = a[2] + 23
700>>> a
701['foo', 'bar', 123, 1234]
702>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000703\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000704%
705Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000706of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000707
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000708\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000709>>> # Replace some items:
Guido van Rossum6938f061994-08-01 12:22:53 +0000710... a[0:2] = [1, 12]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000711>>> a
712[1, 12, 123, 1234]
713>>> # Remove some:
Guido van Rossum6938f061994-08-01 12:22:53 +0000714... a[0:2] = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000715>>> a
716[123, 1234]
717>>> # Insert some:
Guido van Rossum6938f061994-08-01 12:22:53 +0000718... a[1:1] = ['bletch', 'xyzzy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000719>>> a
720[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000721>>> a[:0] = a # Insert (a copy of) itself at the beginning
722>>> a
723[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000724>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000725\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000726%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000727The built-in function {\tt len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000728
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000729\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000730>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00007318
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000732>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000733\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000734%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000735It is possible to nest lists (create lists containing other lists),
736for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000737
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000738\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000739>>> q = [2, 3]
740>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000741>>> len(p)
7423
743>>> p[1]
744[2, 3]
745>>> p[1][0]
7462
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000747>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000748>>> p
749[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000750>>> q
751[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000752>>>
753\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000754%
755Note that in the last example, {\tt p[1]} and {\tt q} really refer to
756the same object! We'll come back to {\em object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000757
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000758\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000759
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000760Of course, we can use Python for more complicated tasks than adding
761two and two together. For instance, we can write an initial
762subsequence of the {\em Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000763
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000764\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000765>>> # Fibonacci series:
Guido van Rossum6938f061994-08-01 12:22:53 +0000766... # the sum of two elements defines the next
767... a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000768>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000769... print b
770... a, b = b, a+b
771...
7721
7731
7742
7753
7765
7778
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000778>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000779\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000780%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000781This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000782
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000783\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000784
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000785\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000786The first line contains a {\em multiple assignment}: the variables
787{\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
788last line this is used again, demonstrating that the expressions on
789the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000790assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000791
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000792\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000793The {\tt while} loop executes as long as the condition (here: {\tt b <
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000794100}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000795true; zero is false. The condition may also be a string or list value,
796in fact any sequence; anything with a non-zero length is true, empty
797sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000798comparison. The standard comparison operators are written the same as
799in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000800
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000801\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000802The {\em body} of the loop is {\em indented}: indentation is Python's
803way of grouping statements. Python does not (yet!) provide an
804intelligent input line editing facility, so you have to type a tab or
805space(s) for each indented line. In practice you will prepare more
806complicated input for Python with a text editor; most text editors have
807an auto-indent facility. When a compound statement is entered
808interactively, it must be followed by a blank line to indicate
809completion (since the parser cannot guess when you have typed the last
810line).
811
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000812\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000813The {\tt print} statement writes the value of the expression(s) it is
814given. It differs from just writing the expression you want to write
815(as we did earlier in the calculator examples) in the way it handles
816multiple expressions and strings. Strings are written without quotes,
817and a space is inserted between items, so you can format things nicely,
818like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000819
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000820\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000821>>> i = 256*256
822>>> print 'The value of i is', i
823The value of i is 65536
824>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000825\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000826%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000827A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000828
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000829\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000830>>> a, b = 0, 1
831>>> while b < 1000:
832... print b,
833... a, b = b, a+b
834...
8351 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
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 +0000839Note that the interpreter inserts a newline before it prints the next
840prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000841
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000842\end{itemize}
843
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000844
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000845\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000846
Guido van Rossum4410c751991-06-04 20:22:18 +0000847Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000848usual control flow statements known from other languages, with some
849twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000850
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000851\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000852
853Perhaps the most well-known statement type is the {\tt if} statement.
854For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000855
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000856\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000857>>> if x < 0:
858... x = 0
859... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000860... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000861... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000862... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000863... print 'Single'
864... else:
865... print 'More'
866...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000867\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000868%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000869There can be zero or more {\tt elif} parts, and the {\tt else} part is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000870optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
871useful to avoid excessive indentation. An {\tt if...elif...elif...}
872sequence is a substitute for the {\em switch} or {\em case} statements
873found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000874
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000875\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000876
Guido van Rossum4410c751991-06-04 20:22:18 +0000877The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000878used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000879arithmetic progression of numbers (like in Pascal), or leaving the user
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000880completely free in the iteration test and step (as C), Python's {\tt
881for} statement iterates over the items of any sequence (e.g., a list
882or a string), in the order that they appear in the sequence. For
883example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000884
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000885\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000886>>> # Measure some strings:
Guido van Rossum6938f061994-08-01 12:22:53 +0000887... a = ['cat', 'window', 'defenestrate']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000888>>> for x in a:
889... print x, len(x)
890...
891cat 3
892window 6
893defenestrate 12
894>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000895\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000896%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000897It is not safe to modify the sequence being iterated over in the loop
898(this can only happen for mutable sequence types, i.e., lists). If
899you need to modify the list you are iterating over, e.g., duplicate
900selected items, you must iterate over a copy. The slice notation
901makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000902
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000903\bcode\begin{verbatim}
904>>> for x in a[:]: # make a slice copy of the entire list
905... if len(x) > 6: a.insert(0, x)
906...
907>>> a
908['defenestrate', 'cat', 'window', 'defenestrate']
909>>>
910\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000911
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000912\section{The {\tt range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000913
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000914If you do need to iterate over a sequence of numbers, the built-in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000915function {\tt range()} comes in handy. It generates lists containing
916arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000917
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000918\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000919>>> range(10)
920[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
921>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000922\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000923%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000924The given end point is never part of the generated list; {\tt range(10)}
925generates a list of 10 values, exactly the legal indices for items of a
926sequence of length 10. It is possible to let the range start at another
927number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000928
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000929\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000930>>> range(5, 10)
931[5, 6, 7, 8, 9]
932>>> range(0, 10, 3)
933[0, 3, 6, 9]
934>>> range(-10, -100, -30)
935[-10, -40, -70]
936>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000937\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000938%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000939To iterate over the indices of a sequence, combine {\tt range()} and
940{\tt len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000941
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000942\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000943>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000944>>> for i in range(len(a)):
945... print i, a[i]
946...
9470 Mary
9481 had
9492 a
9503 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00009514 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000952>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000953\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000954
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000955\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000956
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000957The {\tt break} statement, like in C, breaks out of the smallest
958enclosing {\tt for} or {\tt while} loop.
959
960The {\tt continue} statement, also borrowed from C, continues with the
961next iteration of the loop.
962
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000963Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000964loop terminates through exhaustion of the list (with {\tt for}) or when
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000965the condition becomes false (with {\tt while}), but not when the loop is
966terminated by a {\tt break} statement. This is exemplified by the
967following loop, which searches for a list item of value 0:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000968
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000969\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000970>>> for n in range(2, 10):
971... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000972... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000973... print n, 'equals', x, '*', n/x
974... break
975... else:
976... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000977...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009782 is a prime number
9793 is a prime number
9804 equals 2 * 2
9815 is a prime number
9826 equals 2 * 3
9837 is a prime number
9848 equals 2 * 4
9859 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000986>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000987\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000988
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000989\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000990
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000991The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000992It can be used when a statement is required syntactically but the
993program requires no action.
994For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000995
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000996\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000997>>> while 1:
998... pass # Busy-wait for keyboard interrupt
999...
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001000\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001001
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001002\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001003
1004We can create a function that writes the Fibonacci series to an
1005arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001006
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001007\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001008>>> def fib(n): # write Fibonacci series up to n
1009... a, b = 0, 1
1010... while b <= n:
1011... print b,
1012... a, b = b, a+b
1013...
1014>>> # Now call the function we just defined:
Guido van Rossum6938f061994-08-01 12:22:53 +00001015... fib(2000)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000010161 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
1017>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001018\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001019%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001020The keyword {\tt def} introduces a function {\em definition}. It must
1021be followed by the function name and the parenthesized list of formal
1022parameters. The statements that form the body of the function starts at
1023the next line, indented by a tab stop.
1024
1025The {\em execution} of a function introduces a new symbol table used
1026for the local variables of the function. More precisely, all variable
1027assignments in a function store the value in the local symbol table;
1028whereas
Guido van Rossum6938f061994-08-01 12:22:53 +00001029variable references first look in the local symbol table, then
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001030in the global symbol table, and then in the table of built-in names.
1031Thus,
1032global variables cannot be directly assigned to from within a
Guido van Rossum6938f061994-08-01 12:22:53 +00001033function (unless named in a {\tt global} statement), although
1034they may be referenced.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001035
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001036The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001037the local symbol table of the called function when it is called; thus,
1038arguments are passed using {\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001039\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00001040 Actually, {\em call by object reference} would be a better
1041 description, since if a mutable object is passed, the caller
1042 will see any changes the callee makes to it (e.g., items
1043 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001044}
1045When a function calls another function, a new local symbol table is
1046created for that call.
1047
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001048A function definition introduces the function name in the
1049current
1050symbol table. The value
1051of the function name
1052has a type that is recognized by the interpreter as a user-defined
1053function. This value can be assigned to another name which can then
1054also be used as a function. This serves as a general renaming
1055mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001056
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001057\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001058>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001059<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001060>>> f = fib
1061>>> f(100)
10621 1 2 3 5 8 13 21 34 55 89
1063>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001064\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001065%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001066You might object that {\tt fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001067Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001068value. In fact, technically speaking, procedures do return a value,
1069albeit a rather boring one. This value is called {\tt None} (it's a
1070built-in name). Writing the value {\tt None} is normally suppressed by
1071the interpreter if it would be the only value written. You can see it
1072if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001073
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001074\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001075>>> print fib(0)
1076None
1077>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001078\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001079%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001080It is simple to write a function that returns a list of the numbers of
1081the Fibonacci series, instead of printing it:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001082
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001083\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001084>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001085... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001086... a, b = 0, 1
1087... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001088... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001089... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001090... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001091...
1092>>> f100 = fib2(100) # call it
1093>>> f100 # write the result
1094[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1095>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001096\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001097%
Guido van Rossum4410c751991-06-04 20:22:18 +00001098This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001099
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001100\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001101
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001102\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001103The {\tt return} statement returns with a value from a function. {\tt
1104return} without an expression argument is used to return from the middle
Guido van Rossum6938f061994-08-01 12:22:53 +00001105of a procedure (falling off the end also returns from a procedure), in
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001106which case the {\tt None} value is returned.
1107
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001108\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001109The statement {\tt result.append(b)} calls a {\em method} of the list
1110object {\tt result}. A method is a function that `belongs' to an
1111object and is named {\tt obj.methodname}, where {\tt obj} is some
1112object (this may be an expression), and {\tt methodname} is the name
1113of a method that is defined by the object's type. Different types
1114define different methods. Methods of different types may have the
1115same name without causing ambiguity. (It is possible to define your
Guido van Rossum6938f061994-08-01 12:22:53 +00001116own object types and methods, using {\em classes}, as discussed later
1117in this tutorial.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001118The method {\tt append} shown in the example, is defined for
1119list objects; it adds a new element at the end of the list. In this
1120example
1121it is equivalent to {\tt result = result + [b]}, but more efficient.
1122
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001123\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001124
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001125
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001126\chapter{Odds and Ends}
1127
1128This chapter describes some things you've learned about already in
1129more detail, and adds some new things as well.
1130
1131\section{More on Lists}
1132
1133The list data type has some more methods. Here are all of the methods
1134of lists objects:
1135
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001136\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001137
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001138\item[{\tt insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001139Insert an item at a given position. The first argument is the index of
1140the element before which to insert, so {\tt a.insert(0, x)} inserts at
1141the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
1142{\tt a.append(x)}.
1143
1144\item[{\tt append(x)}]
1145Equivalent to {\tt a.insert(len(a), x)}.
1146
1147\item[{\tt index(x)}]
1148Return the index in the list of the first item whose value is {\tt x}.
1149It is an error if there is no such item.
1150
1151\item[{\tt remove(x)}]
1152Remove the first item from the list whose value is {\tt x}.
1153It is an error if there is no such item.
1154
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001155\item[{\tt sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001156Sort the items of the list, in place.
1157
1158\item[{\tt reverse()}]
1159Reverse the elements of the list, in place.
1160
Guido van Rossum6938f061994-08-01 12:22:53 +00001161\item[{\tt count(x)}]
1162Return the number of times {\tt x} appears in the list.
1163
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001164\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001165
1166An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001167
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001168\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001169>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossum6938f061994-08-01 12:22:53 +00001170>>> print a.count(333), a.count(66.6), a.count('x')
11712 1 0
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001172>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001173>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001174>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001175[66.6, 333, -1, 333, 1, 1234.5, 333]
1176>>> a.index(333)
11771
1178>>> a.remove(333)
1179>>> a
1180[66.6, -1, 333, 1, 1234.5, 333]
1181>>> a.reverse()
1182>>> a
1183[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001184>>> a.sort()
1185>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001186[-1, 1, 66.6, 333, 333, 1234.5]
1187>>>
1188\end{verbatim}\ecode
1189
1190\section{The {\tt del} statement}
1191
1192There is a way to remove an item from a list given its index instead
1193of its value: the {\tt del} statement. This can also be used to
1194remove slices from a list (which we did earlier by assignment of an
1195empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001196
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001197\bcode\begin{verbatim}
1198>>> a
1199[-1, 1, 66.6, 333, 333, 1234.5]
1200>>> del a[0]
1201>>> a
1202[1, 66.6, 333, 333, 1234.5]
1203>>> del a[2:4]
1204>>> a
1205[1, 66.6, 1234.5]
1206>>>
1207\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001208%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001209{\tt del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001210
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001211\bcode\begin{verbatim}
1212>>> del a
1213>>>
1214\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001215%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001216Referencing the name {\tt a} hereafter is an error (at least until
1217another value is assigned to it). We'll find other uses for {\tt del}
1218later.
1219
1220\section{Tuples and Sequences}
1221
1222We saw that lists and strings have many common properties, e.g.,
Guido van Rossum6938f061994-08-01 12:22:53 +00001223indexing and slicing operations. They are two examples of {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001224sequence} data types. Since Python is an evolving language, other
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001225sequence data types may be added. There is also another standard
1226sequence data type: the {\em tuple}.
1227
1228A tuple consists of a number of values separated by commas, for
1229instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001230
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001231\bcode\begin{verbatim}
1232>>> t = 12345, 54321, 'hello!'
1233>>> t[0]
123412345
1235>>> t
1236(12345, 54321, 'hello!')
1237>>> # Tuples may be nested:
Guido van Rossum6938f061994-08-01 12:22:53 +00001238... u = t, (1, 2, 3, 4, 5)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001239>>> u
1240((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1241>>>
1242\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001243%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001244As you see, on output tuples are alway enclosed in parentheses, so
1245that nested tuples are interpreted correctly; they may be input with
1246or without surrounding parentheses, although often parentheses are
1247necessary anyway (if the tuple is part of a larger expression).
1248
1249Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1250from a database, etc. Tuples, like strings, are immutable: it is not
1251possible to assign to the individual items of a tuple (you can
1252simulate much of the same effect with slicing and concatenation,
1253though).
1254
1255A special problem is the construction of tuples containing 0 or 1
Guido van Rossum6938f061994-08-01 12:22:53 +00001256items: the syntax has some extra quirks to accommodate these. Empty
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001257tuples are constructed by an empty pair of parentheses; a tuple with
1258one item is constructed by following a value with a comma
1259(it is not sufficient to enclose a single value in parentheses).
1260Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001261
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001262\bcode\begin{verbatim}
1263>>> empty = ()
1264>>> singleton = 'hello', # <-- note trailing comma
1265>>> len(empty)
12660
1267>>> len(singleton)
12681
1269>>> singleton
1270('hello',)
1271>>>
1272\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001273%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001274The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
1275tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
1276are packed together in a tuple. The reverse operation is also
1277possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001278
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001279\bcode\begin{verbatim}
1280>>> x, y, z = t
1281>>>
1282\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001283%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001284This is called, appropriately enough, {\em tuple unpacking}. Tuple
1285unpacking requires that the list of variables on the left has the same
1286number of elements as the length of the tuple. Note that multiple
1287assignment is really just a combination of tuple packing and tuple
1288unpacking!
1289
1290Occasionally, the corresponding operation on lists is useful: {\em list
1291unpacking}. This is supported by enclosing the list of variables in
1292square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001293
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001294\bcode\begin{verbatim}
1295>>> a = ['foo', 'bar', 100, 1234]
1296>>> [a1, a2, a3, a4] = a
1297>>>
1298\end{verbatim}\ecode
1299
1300\section{Dictionaries}
1301
1302Another useful data type built into Python is the {\em dictionary}.
1303Dictionaries are sometimes found in other languages as ``associative
1304memories'' or ``associative arrays''. Unlike sequences, which are
1305indexed by a range of numbers, dictionaries are indexed by {\em keys},
Guido van Rossum6938f061994-08-01 12:22:53 +00001306which are strings (the use of non-string values as keys
1307is supported, but beyond the scope of this tutorial).
1308It is best to think of a dictionary as an unordered set of
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001309{\em key:value} pairs, with the requirement that the keys are unique
1310(within one dictionary).
1311A pair of braces creates an empty dictionary: \verb/{}/.
1312Placing a comma-separated list of key:value pairs within the
1313braces adds initial key:value pairs to the dictionary; this is also the
1314way dictionaries are written on output.
1315
1316The main operations on a dictionary are storing a value with some key
1317and extracting the value given the key. It is also possible to delete
1318a key:value pair
1319with {\tt del}.
1320If you store using a key that is already in use, the old value
1321associated with that key is forgotten. It is an error to extract a
Guido van Rossum6938f061994-08-01 12:22:53 +00001322value using a non-existent key.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001323
1324The {\tt keys()} method of a dictionary object returns a list of all the
1325keys used in the dictionary, in random order (if you want it sorted,
1326just apply the {\tt sort()} method to the list of keys). To check
1327whether a single key is in the dictionary, use the \verb/has_key()/
1328method of the dictionary.
1329
1330Here is a small example using a dictionary:
1331
1332\bcode\begin{verbatim}
1333>>> tel = {'jack': 4098, 'sape': 4139}
1334>>> tel['guido'] = 4127
1335>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001336{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001337>>> tel['jack']
13384098
1339>>> del tel['sape']
1340>>> tel['irv'] = 4127
1341>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001342{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001343>>> tel.keys()
1344['guido', 'irv', 'jack']
1345>>> tel.has_key('guido')
13461
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001347>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001348\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001349
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001350\section{More on Conditions}
1351
1352The conditions used in {\tt while} and {\tt if} statements above can
1353contain other operators besides comparisons.
1354
1355The comparison operators {\tt in} and {\tt not in} check whether a value
1356occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
1357is not} compare whether two objects are really the same object; this
1358only matters for mutable objects like lists. All comparison operators
1359have the same priority, which is lower than that of all numerical
1360operators.
1361
1362Comparisons can be chained: e.g., {\tt a < b = c} tests whether {\tt a}
1363is less than {\tt b} and moreover {\tt b} equals {\tt c}.
1364
1365Comparisons may be combined by the Boolean operators {\tt and} and {\tt
1366or}, and the outcome of a comparison (or of any other Boolean
1367expression) may be negated with {\tt not}. These all have lower
1368priorities than comparison operators again; between them, {\tt not} has
1369the highest priority, and {\tt or} the lowest, so that
1370{\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
1371course, parentheses can be used to express the desired composition.
1372
1373The Boolean operators {\tt and} and {\tt or} are so-called {\em
1374shortcut} operators: their arguments are evaluated from left to right,
1375and evaluation stops as soon as the outcome is determined. E.g., if
1376{\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
1377does not evaluate the expression C. In general, the return value of a
1378shortcut operator, when used as a general value and not as a Boolean, is
1379the last evaluated argument.
1380
1381It is possible to assign the result of a comparison or other Boolean
Guido van Rossum6938f061994-08-01 12:22:53 +00001382expression to a variable. For example,
1383
1384\bcode\begin{verbatim}
1385>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
1386>>> non_null = string1 or string2 or string3
1387>>> non_null
1388'Trondheim'
1389>>>
1390\end{verbatim}\ecode
1391%
1392Note that in Python, unlike C, assignment cannot occur inside expressions.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001393
1394\section{Comparing Sequences and Other Types}
1395
1396Sequence objects may be compared to other objects with the same
1397sequence type. The comparison uses {\em lexicographical} ordering:
1398first the first two items are compared, and if they differ this
1399determines the outcome of the comparison; if they are equal, the next
1400two items are compared, and so on, until either sequence is exhausted.
1401If two items to be compared are themselves sequences of the same type,
Guido van Rossum6938f061994-08-01 12:22:53 +00001402the lexicographical comparison is carried out recursively. If all
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001403items of two sequences compare equal, the sequences are considered
1404equal. If one sequence is an initial subsequence of the other, the
1405shorted sequence is the smaller one. Lexicographical ordering for
1406strings uses the ASCII ordering for individual characters. Some
1407examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001408
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001409\bcode\begin{verbatim}
1410(1, 2, 3) < (1, 2, 4)
1411[1, 2, 3] < [1, 2, 4]
1412'ABC' < 'C' < 'Pascal' < 'Python'
1413(1, 2, 3, 4) < (1, 2, 4)
1414(1, 2) < (1, 2, -1)
1415(1, 2, 3) = (1.0, 2.0, 3.0)
1416(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1417\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001418%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001419Note that comparing objects of different types is legal. The outcome
1420is deterministic but arbitrary: the types are ordered by their name.
1421Thus, a list is always smaller than a string, a string is always
1422smaller than a tuple, etc. Mixed numeric types are compared according
1423to their numeric value, so 0 equals 0.0, etc.%
1424\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00001425 The rules for comparing objects of different types should
1426 not be relied upon; they may change in a future version of
1427 the language.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001428}
1429
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001430
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001431\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001432
Guido van Rossum4410c751991-06-04 20:22:18 +00001433If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001434definitions you have made (functions and variables) are lost.
1435Therefore, if you want to write a somewhat longer program, you are
1436better off using a text editor to prepare the input for the interpreter
Guido van Rossum16d6e711994-08-08 12:30:22 +00001437and running it with that file as input instead. This is known as creating a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001438{\em script}. As your program gets longer, you may want to split it
1439into several files for easier maintenance. You may also want to use a
1440handy function that you've written in several programs without copying
1441its definition into each program.
1442
Guido van Rossum4410c751991-06-04 20:22:18 +00001443To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001444them in a script or in an interactive instance of the interpreter.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001445Such a file is called a {\em module}; definitions from a module can be
1446{\em imported} into other modules or into the {\em main} module (the
1447collection of variables that you have access to in a script
1448executed at the top level
1449and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001450
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001451A module is a file containing Python definitions and statements. The
Guido van Rossum6938f061994-08-01 12:22:53 +00001452file name is the module name with the suffix {\tt .py} appended. Within
1453a module, the module's name (as a string) is available as the value of
1454the global variable {\tt __name__}. For instance, use your favorite text
1455editor to create a file called {\tt fibo.py} in the current directory
1456with the following contents:
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# Fibonacci numbers module
1460
1461def fib(n): # write Fibonacci series up to n
1462 a, b = 0, 1
1463 while b <= n:
1464 print b,
1465 a, b = b, a+b
1466
1467def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001468 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001469 a, b = 0, 1
1470 while b <= n:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001471 result.append(b)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001472 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001473 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001474\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001475%
Guido van Rossum4410c751991-06-04 20:22:18 +00001476Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001477following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001478
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001479\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001480>>> import fibo
1481>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001482\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001483%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001484This does not enter the names of the functions defined in
1485{\tt fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001486directly in the current symbol table; it only enters the module name
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001487{\tt fibo}
1488there.
1489Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001490
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001491\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001492>>> fibo.fib(1000)
14931 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1494>>> fibo.fib2(100)
1495[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Guido van Rossum6938f061994-08-01 12:22:53 +00001496>>> fibo.__name__
1497'fibo'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001498>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001499\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001500%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001501If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001502
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001503\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001504>>> fib = fibo.fib
1505>>> fib(500)
15061 1 2 3 5 8 13 21 34 55 89 144 233 377
1507>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001508\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001509
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001510\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001511
1512A module can contain executable statements as well as function
1513definitions.
1514These statements are intended to initialize the module.
1515They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001516{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001517time the module is imported somewhere.%
1518\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00001519 In fact function definitions are also `statements' that are
1520 `executed'; the execution enters the function name in the
1521 module's global symbol table.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001522}
1523
1524Each module has its own private symbol table, which is used as the
1525global symbol table by all functions defined in the module.
1526Thus, the author of a module can use global variables in the module
1527without worrying about accidental clashes with a user's global
1528variables.
1529On the other hand, if you know what you are doing you can touch a
1530module's global variables with the same notation used to refer to its
1531functions,
1532{\tt modname.itemname}.
1533
1534Modules can import other modules.
1535It is customary but not required to place all
1536{\tt import}
1537statements at the beginning of a module (or script, for that matter).
1538The imported module names are placed in the importing module's global
1539symbol table.
1540
1541There is a variant of the
1542{\tt import}
1543statement that imports names from a module directly into the importing
1544module's symbol table.
1545For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001546
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001547\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001548>>> from fibo import fib, fib2
1549>>> fib(500)
15501 1 2 3 5 8 13 21 34 55 89 144 233 377
1551>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001552\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001553%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001554This does not introduce the module name from which the imports are taken
1555in the local symbol table (so in the example, {\tt fibo} is not
1556defined).
1557
1558There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001559
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001560\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001561>>> from fibo import *
1562>>> fib(500)
15631 1 2 3 5 8 13 21 34 55 89 144 233 377
1564>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001565\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001566%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001567This imports all names except those beginning with an underscore
Guido van Rossum573805a1992-03-06 10:56:03 +00001568({\tt _}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001569
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001570\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001571
Guido van Rossum4410c751991-06-04 20:22:18 +00001572Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001573document (Python Library Reference). Some modules are built into the
1574interpreter; these provide access to operations that are not part of the
1575core of the language but are nevertheless built in, either for
1576efficiency or to provide access to operating system primitives such as
1577system calls. The set of such modules is a configuration option; e.g.,
1578the {\tt amoeba} module is only provided on systems that somehow support
1579Amoeba primitives. One particular module deserves some attention: {\tt
1580sys}, which is built into every Python interpreter. The variables {\tt
1581sys.ps1} and {\tt sys.ps2} define the strings used as primary and
1582secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001583
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001584\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001585>>> import sys
1586>>> sys.ps1
1587'>>> '
1588>>> sys.ps2
1589'... '
1590>>> sys.ps1 = 'C> '
1591C> print 'Yuck!'
1592Yuck!
1593C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001594\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001595%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001596These two variables are only defined if the interpreter is in
1597interactive mode.
1598
1599The variable
1600{\tt sys.path}
1601is a list of strings that determine the interpreter's search path for
1602modules.
1603It is initialized to a default path taken from the environment variable
1604{\tt PYTHONPATH},
1605or from a built-in default if
1606{\tt PYTHONPATH}
1607is not set.
1608You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001609
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001610\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001611>>> import sys
1612>>> sys.path.append('/ufs/guido/lib/python')
1613>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001614\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001615
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001616\section{The {\tt dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001617
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001618The built-in function {\tt dir} is used to find out which names a module
1619defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001620
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001621\bcode\begin{verbatim}
1622>>> import fibo, sys
1623>>> dir(fibo)
Guido van Rossum6938f061994-08-01 12:22:53 +00001624['__name__', 'fib', 'fib2']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001625>>> dir(sys)
Guido van Rossum6938f061994-08-01 12:22:53 +00001626['__name__', 'argv', 'builtin_module_names', 'copyright', 'exit',
1627'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace',
1628'stderr', 'stdin', 'stdout', 'version']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001629>>>
1630\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001631%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001632Without arguments, {\tt dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001633
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001634\bcode\begin{verbatim}
1635>>> a = [1, 2, 3, 4, 5]
1636>>> import fibo, sys
1637>>> fib = fibo.fib
1638>>> dir()
Guido van Rossum6938f061994-08-01 12:22:53 +00001639['__name__', 'a', 'fib', 'fibo', 'sys']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001640>>>
1641\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001642%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001643Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001644
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001645{\tt dir()} does not list the names of built-in functions and variables.
1646If you want a list of those, they are defined in the standard module
Guido van Rossum4bd023f1993-10-27 13:49:20 +00001647{\tt __builtin__}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001648
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001649\bcode\begin{verbatim}
Guido van Rossum4bd023f1993-10-27 13:49:20 +00001650>>> import __builtin__
1651>>> dir(__builtin__)
Guido van Rossum6938f061994-08-01 12:22:53 +00001652['AccessError', 'AttributeError', 'ConflictError', 'EOFError', 'IOError',
1653'ImportError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
1654'MemoryError', 'NameError', 'None', 'OverflowError', 'RuntimeError',
1655'SyntaxError', 'SystemError', 'SystemExit', 'TypeError', 'ValueError',
1656'ZeroDivisionError', '__name__', 'abs', 'apply', 'chr', 'cmp', 'coerce',
1657'compile', 'dir', 'divmod', 'eval', 'execfile', 'filter', 'float',
1658'getattr', 'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'len', 'long',
1659'map', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'range', 'raw_input',
1660'reduce', 'reload', 'repr', 'round', 'setattr', 'str', 'type', 'xrange']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001661>>>
1662\end{verbatim}\ecode
1663
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001664
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001665\chapter{Output Formatting}
1666
1667So far we've encountered two ways of writing values: {\em expression
1668statements} and the {\tt print} statement. (A third way is using the
1669{\tt write} method of file objects; the standard output file can be
1670referenced as {\tt sys.stdout}. See the Library Reference for more
1671information on this.)
1672
1673Often you'll want more control over the formatting of your output than
1674simply printing space-separated values. The key to nice formatting in
1675Python is to do all the string handling yourself; using string slicing
1676and concatenation operations you can create any lay-out you can imagine.
1677The standard module {\tt string} contains some useful operations for
1678padding strings to a given column width; these will be discussed shortly.
Guido van Rossum6938f061994-08-01 12:22:53 +00001679Finally, the \code{\%} operator (modulo) with a string left argument
1680interprets this string as a C sprintf format string to be applied to the
1681right argument, and returns the string resulting from this formatting
1682operation.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001683
1684One question remains, of course: how do you convert values to strings?
1685Luckily, Python has a way to convert any value to a string: just write
1686the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001687
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001688\bcode\begin{verbatim}
1689>>> x = 10 * 3.14
1690>>> y = 200*200
1691>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1692>>> print s
1693The value of x is 31.4, and y is 40000...
1694>>> # Reverse quotes work on other types besides numbers:
Guido van Rossum6938f061994-08-01 12:22:53 +00001695... p = [x, y]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001696>>> ps = `p`
1697>>> ps
1698'[31.4, 40000]'
1699>>> # Converting a string adds string quotes and backslashes:
Guido van Rossum6938f061994-08-01 12:22:53 +00001700... hello = 'hello, world\n'
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001701>>> hellos = `hello`
1702>>> print hellos
1703'hello, world\012'
1704>>> # The argument of reverse quotes may be a tuple:
Guido van Rossum6938f061994-08-01 12:22:53 +00001705... `x, y, ('foo', 'bar')`
1706"(31.4, 40000, ('foo', 'bar'))"
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001707>>>
1708\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001709%
Guido van Rossum6938f061994-08-01 12:22:53 +00001710Here are two ways to write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001711
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001712\bcode\begin{verbatim}
1713>>> import string
1714>>> for x in range(1, 11):
1715... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1716... # Note trailing comma on previous line
1717... print string.rjust(`x*x*x`, 4)
1718...
1719 1 1 1
1720 2 4 8
1721 3 9 27
1722 4 16 64
1723 5 25 125
1724 6 36 216
1725 7 49 343
1726 8 64 512
1727 9 81 729
172810 100 1000
Guido van Rossum6938f061994-08-01 12:22:53 +00001729>>> for x in range(1,11):
1730... print '%2d %3d %4d' % (x, x*x, x*x*x)
1731...
1732 1 1 1
1733 2 4 8
1734 3 9 27
1735 4 16 64
1736 5 25 125
1737 6 36 216
1738 7 49 343
1739 8 64 512
1740 9 81 729
174110 100 1000
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001742>>>
1743\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001744%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001745(Note that one space between each column was added by the way {\tt print}
1746works: it always adds spaces between its arguments.)
1747
1748This example demonstrates the function {\tt string.rjust()}, which
1749right-justifies a string in a field of a given width by padding it with
1750spaces on the left. There are similar functions {\tt string.ljust()}
1751and {\tt string.center()}. These functions do not write anything, they
1752just return a new string. If the input string is too long, they don't
1753truncate it, but return it unchanged; this will mess up your column
1754lay-out but that's usually better than the alternative, which would be
1755lying about a value. (If you really want truncation you can always add
1756a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1757
1758There is another function, {\tt string.zfill}, which pads a numeric
1759string on the left with zeros. It understands about plus and minus
Guido van Rossum6938f061994-08-01 12:22:53 +00001760signs:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001761
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001762\bcode\begin{verbatim}
1763>>> string.zfill('12', 5)
1764'00012'
1765>>> string.zfill('-3.14', 7)
1766'-003.14'
1767>>> string.zfill('3.14159265359', 5)
1768'3.14159265359'
1769>>>
1770\end{verbatim}\ecode
1771
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001772
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001773\chapter{Errors and Exceptions}
1774
1775Until now error messages haven't been more than mentioned, but if you
1776have tried out the examples you have probably seen some. There are
1777(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1778and {\em exceptions}.
1779
1780\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001781
1782Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001783kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001784
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001785\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001786>>> while 1 print 'Hello world'
Guido van Rossum6938f061994-08-01 12:22:53 +00001787 File "<stdin>", line 1
1788 while 1 print 'Hello world'
1789 ^
1790SyntaxError: invalid syntax
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001791>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001792\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001793%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001794The parser repeats the offending line and displays a little `arrow'
1795pointing at the earliest point in the line where the error was detected.
1796The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001797{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001798the arrow: in the example, the error is detected at the keyword
1799{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001800File name and line number are printed so you know where to look in case
1801the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001802
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001803\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001804
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001805Even if a statement or expression is syntactically correct, it may
1806cause an error when an attempt is made to execute it.
1807Errors detected during execution are called {\em exceptions} and are
1808not unconditionally fatal: you will soon learn how to handle them in
1809Python programs. Most exceptions are not handled by programs,
1810however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001811
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001812\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001813>>> 10 * (1/0)
Guido van Rossum3cbc16d1993-12-17 12:13:53 +00001814Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001815 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001816ZeroDivisionError: integer division or modulo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001817>>> 4 + foo*3
Guido van Rossum6938f061994-08-01 12:22:53 +00001818Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001819 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001820NameError: foo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001821>>> '2' + 2
Guido van Rossum6938f061994-08-01 12:22:53 +00001822Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001823 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001824TypeError: illegal argument type for built-in operation
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001825>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001826\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001827%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001828The last line of the error message indicates what happened.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001829Exceptions come in different types, and the type is printed as part of
1830the message: the types in the example are
Guido van Rossumb2c65561993-05-12 08:53:36 +00001831{\tt ZeroDivisionError},
1832{\tt NameError}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001833and
Guido van Rossumb2c65561993-05-12 08:53:36 +00001834{\tt TypeError}.
1835The string printed as the exception type is the name of the built-in
1836name for the exception that occurred. This is true for all built-in
1837exceptions, but need not be true for user-defined exceptions (although
1838it is a useful convention).
1839Standard exception names are built-in identifiers (not reserved
1840keywords).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001841
Guido van Rossumb2c65561993-05-12 08:53:36 +00001842The rest of the line is a detail whose interpretation depends on the
1843exception type; its meaning is dependent on the exception type.
1844
1845The preceding part of the error message shows the context where the
1846exception happened, in the form of a stack backtrace.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001847In general it contains a stack backtrace listing source lines; however,
1848it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001849
Guido van Rossumb2c65561993-05-12 08:53:36 +00001850The Python library reference manual lists the built-in exceptions and
1851their meanings.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001852
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001853\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001854
1855It is possible to write programs that handle selected exceptions.
1856Look at the following example, which prints a table of inverses of
1857some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001858
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001859\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001860>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001861>>> for x in numbers:
1862... print x,
1863... try:
1864... print 1.0 / x
Guido van Rossumb2c65561993-05-12 08:53:36 +00001865... except ZeroDivisionError:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001866... print '*** has no inverse ***'
1867...
18680.3333 3.00030003
18692.5 0.4
18700 *** has no inverse ***
187110 0.1
1872>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001873\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001874%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001875The {\tt try} statement works as follows.
1876\begin{itemize}
1877\item
1878First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001879{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001880(the statement(s) between the {\tt try} and {\tt except} keywords) is
1881executed.
1882\item
1883If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001884{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001885is skipped and execution of the {\tt try} statement is finished.
1886\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001887If an exception occurs during execution of the try clause,
1888the rest of the clause is skipped. Then if
1889its type matches the exception named after the {\tt except} keyword,
1890the rest of the try clause is skipped, the except clause is executed,
1891and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001892\item
1893If an exception occurs which does not match the exception named in the
1894except clause, it is passed on to outer try statements; if no handler is
1895found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001896{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001897and execution stops with a message as shown above.
1898\end{itemize}
1899A {\tt try} statement may have more than one except clause, to specify
1900handlers for different exceptions.
1901At most one handler will be executed.
1902Handlers only handle exceptions that occur in the corresponding try
1903clause, not in other handlers of the same {\tt try} statement.
1904An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001905e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001906
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001907\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001908... except (RuntimeError, TypeError, NameError):
1909... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001910\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001911%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001912The last except clause may omit the exception name(s), to serve as a
1913wildcard.
Guido van Rossumb2c65561993-05-12 08:53:36 +00001914Use this with extreme caution, since it is easy to mask a real
1915programming error in this way!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001916
1917When an exception occurs, it may have an associated value, also known as
1918the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001919{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001920The presence and type of the argument depend on the exception type.
1921For exception types which have an argument, the except clause may
1922specify a variable after the exception name (or list) to receive the
1923argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001924
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001925\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001926>>> try:
1927... foo()
1928... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001929... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001930...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001931name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001932>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001933\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001934%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001935If an exception has an argument, it is printed as the last part
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001936(`detail') of the message for unhandled exceptions.
1937
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001938Exception handlers don't just handle exceptions if they occur
1939immediately in the try clause, but also if they occur inside functions
1940that are called (even indirectly) in the try clause.
1941For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001942
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001943\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001944>>> def this_fails():
1945... x = 1/0
1946...
1947>>> try:
1948... this_fails()
Guido van Rossumb2c65561993-05-12 08:53:36 +00001949... except ZeroDivisionError, detail:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001950... print 'Handling run-time error:', detail
1951...
Guido van Rossumb2c65561993-05-12 08:53:36 +00001952Handling run-time error: integer division or modulo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001953>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001954\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001955
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001956\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001957
1958The {\tt raise} statement allows the programmer to force a specified
1959exception to occur.
1960For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001961
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001962\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001963>>> raise NameError, 'HiThere'
Guido van Rossum6938f061994-08-01 12:22:53 +00001964Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001965 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001966NameError: HiThere
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001967>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001968\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001969%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001970The first argument to {\tt raise} names the exception to be raised.
1971The optional second argument specifies the exception's argument.
1972
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001973\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001974
1975Programs may name their own exceptions by assigning a string to a
1976variable.
1977For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001978
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001979\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001980>>> my_exc = 'my_exc'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001981>>> try:
1982... raise my_exc, 2*2
1983... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001984... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001985...
Guido van Rossum6938f061994-08-01 12:22:53 +00001986My exception occurred, value: 4
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001987>>> raise my_exc, 1
Guido van Rossum6938f061994-08-01 12:22:53 +00001988Traceback (innermost last):
1989 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001990my_exc: 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001991>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001992\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001993%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001994Many standard modules use this to report errors that may occur in
1995functions they define.
1996
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001997\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001998
1999The {\tt try} statement has another optional clause which is intended to
2000define clean-up actions that must be executed under all circumstances.
2001For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002002
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002003\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002004>>> try:
2005... raise KeyboardInterrupt
2006... finally:
2007... print 'Goodbye, world!'
2008...
2009Goodbye, world!
Guido van Rossum6938f061994-08-01 12:22:53 +00002010Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00002011 File "<stdin>", line 2
Guido van Rossumb2c65561993-05-12 08:53:36 +00002012KeyboardInterrupt
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002013>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00002014\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00002015%
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00002016A {\tt finally} clause is executed whether or not an exception has
2017occurred in the {\tt try} clause. When an exception has occurred, it
Guido van Rossum6938f061994-08-01 12:22:53 +00002018is re-raised after the {\tt finally} clause is executed. The
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00002019{\tt finally} clause is also executed ``on the way out'' when the
2020{\tt try} statement is left via a {\tt break} or {\tt return}
2021statement.
2022
2023A {\tt try} statement must either have one or more {\tt except}
2024clauses or one {\tt finally} clause, but not both.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002025
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002026
2027\chapter{Classes}
2028
2029Python's class mechanism adds classes to the language with a minimum
2030of new syntax and semantics. It is a mixture of the class mechanisms
Guido van Rossum16d6e711994-08-08 12:30:22 +00002031found in \Cpp{} and Modula-3. As is true for modules, classes in Python
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002032do not put an absolute barrier between definition and user, but rather
2033rely on the politeness of the user not to ``break into the
2034definition.'' The most important features of classes are retained
2035with full power, however: the class inheritance mechanism allows
2036multiple base classes, a derived class can override any methods of its
2037base class(es), a method can call the method of a base class with the
2038same name. Objects can contain an arbitrary amount of private data.
2039
Guido van Rossum16d6e711994-08-08 12:30:22 +00002040In \Cpp{} terminology, all class members (including the data members) are
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002041{\em public}, and all member functions are {\em virtual}. There are
Guido van Rossum6938f061994-08-01 12:22:53 +00002042no special constructors or destructors. As in Modula-3, there are no
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002043shorthands for referencing the object's members from its methods: the
2044method function is declared with an explicit first argument
2045representing the object, which is provided implicitly by the call. As
2046in Smalltalk, classes themselves are objects, albeit in the wider
2047sense of the word: in Python, all data types are objects. This
Guido van Rossum16d6e711994-08-08 12:30:22 +00002048provides semantics for importing and renaming. But, just like in \Cpp{}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002049or Modula-3, built-in types cannot be used as base classes for
Guido van Rossum16d6e711994-08-08 12:30:22 +00002050extension by the user. Also, like in \Cpp{} but unlike in Modula-3, most
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002051built-in operators with special syntax (arithmetic operators,
Guido van Rossum6938f061994-08-01 12:22:53 +00002052subscripting etc.) can be redefined for class members.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002053
2054
2055\section{A word about terminology}
2056
2057Lacking universally accepted terminology to talk about classes, I'll
Guido van Rossum16d6e711994-08-08 12:30:22 +00002058make occasional use of Smalltalk and \Cpp{} terms. (I'd use Modula-3
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002059terms, since its object-oriented semantics are closer to those of
Guido van Rossum16d6e711994-08-08 12:30:22 +00002060Python than \Cpp{}, but I expect that few readers have heard of it...)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002061
2062I also have to warn you that there's a terminological pitfall for
2063object-oriented readers: the word ``object'' in Python does not
Guido van Rossum16d6e711994-08-08 12:30:22 +00002064necessarily mean a class instance. Like \Cpp{} and Modula-3, and unlike
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002065Smalltalk, not all types in Python are classes: the basic built-in
2066types like integers and lists aren't, and even somewhat more exotic
2067types like files aren't. However, {\em all} Python types share a little
2068bit of common semantics that is best described by using the word
2069object.
2070
2071Objects have individuality, and multiple names (in multiple scopes)
2072can be bound to the same object. This is known as aliasing in other
2073languages. This is usually not appreciated on a first glance at
2074Python, and can be safely ignored when dealing with immutable basic
2075types (numbers, strings, tuples). However, aliasing has an
Guido van Rossum6938f061994-08-01 12:22:53 +00002076(intended!) effect on the semantics of Python code involving mutable
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002077objects such as lists, dictionaries, and most types representing
2078entities outside the program (files, windows, etc.). This is usually
2079used to the benefit of the program, since aliases behave like pointers
2080in some respects. For example, passing an object is cheap since only
2081a pointer is passed by the implementation; and if a function modifies
2082an object passed as an argument, the caller will see the change --- this
2083obviates the need for two different argument passing mechanisms as in
2084Pascal.
2085
2086
2087\section{Python scopes and name spaces}
2088
2089Before introducing classes, I first have to tell you something about
2090Python's scope rules. Class definitions play some neat tricks with
2091name spaces, and you need to know how scopes and name spaces work to
2092fully understand what's going on. Incidentally, knowledge about this
2093subject is useful for any advanced Python programmer.
2094
2095Let's begin with some definitions.
2096
2097A {\em name space} is a mapping from names to objects. Most name
2098spaces are currently implemented as Python dictionaries, but that's
2099normally not noticeable in any way (except for performance), and it
2100may change in the future. Examples of name spaces are: the set of
2101built-in names (functions such as \verb\abs()\, and built-in exception
2102names); the global names in a module; and the local names in a
2103function invocation. In a sense the set of attributes of an object
2104also form a name space. The important things to know about name
2105spaces is that there is absolutely no relation between names in
2106different name spaces; for instance, two different modules may both
2107define a function ``maximize'' without confusion --- users of the
2108modules must prefix it with the module name.
2109
2110By the way, I use the word {\em attribute} for any name following a
2111dot --- for example, in the expression \verb\z.real\, \verb\real\ is
2112an attribute of the object \verb\z\. Strictly speaking, references to
2113names in modules are attribute references: in the expression
2114\verb\modname.funcname\, \verb\modname\ is a module object and
2115\verb\funcname\ is an attribute of it. In this case there happens to
2116be a straightforward mapping between the module's attributes and the
2117global names defined in the module: they share the same name space!%
2118\footnote{
Guido van Rossum6938f061994-08-01 12:22:53 +00002119 Except for one thing. Module objects have a secret read-only
2120 attribute called {\tt __dict__} which returns the dictionary
2121 used to implement the module's name space; the name
2122 {\tt __dict__} is an attribute but not a global name.
2123 Obviously, using this violates the abstraction of name space
2124 implementation, and should be restricted to things like
2125 post-mortem debuggers...
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002126}
2127
2128Attributes may be read-only or writable. In the latter case,
2129assignment to attributes is possible. Module attributes are writable:
2130you can write \verb\modname.the_answer = 42\. Writable attributes may
2131also be deleted with the del statement, e.g.
2132\verb\del modname.the_answer\.
2133
2134Name spaces are created at different moments and have different
2135lifetimes. The name space containing the built-in names is created
2136when the Python interpreter starts up, and is never deleted. The
2137global name space for a module is created when the module definition
2138is read in; normally, module name spaces also last until the
2139interpreter quits. The statements executed by the top-level
2140invocation of the interpreter, either read from a script file or
2141interactively, are considered part of a module called \verb\__main__\,
2142so they have their own global name space. (The built-in names
Guido van Rossum4bd023f1993-10-27 13:49:20 +00002143actually also live in a module; this is called \verb\__builtin__\.)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002144
2145The local name space for a function is created when the function is
2146called, and deleted when the function returns or raises an exception
2147that is not handled within the function. (Actually, forgetting would
2148be a better way to describe what actually happens.) Of course,
2149recursive invocations each have their own local name space.
2150
2151A {\em scope} is a textual region of a Python program where a name space
2152is directly accessible. ``Directly accessible'' here means that an
2153unqualified reference to a name attempts to find the name in the name
2154space.
2155
2156Although scopes are determined statically, they are used dynamically.
2157At any time during execution, exactly three nested scopes are in use
2158(i.e., exactly three name spaces are directly accessible): the
2159innermost scope, which is searched first, contains the local names,
2160the middle scope, searched next, contains the current module's global
2161names, and the outermost scope (searched last) is the name space
2162containing built-in names.
2163
2164Usually, the local scope references the local names of the (textually)
2165current function. Outside functions, the the local scope references
2166the same name space as the global scope: the module's name space.
2167Class definitions place yet another name space in the local scope.
2168
2169It is important to realize that scopes are determined textually: the
2170global scope of a function defined in a module is that module's name
2171space, no matter from where or by what alias the function is called.
2172On the other hand, the actual search for names is done dynamically, at
2173run time --- however, the the language definition is evolving towards
2174static name resolution, at ``compile'' time, so don't rely on dynamic
2175name resolution! (In fact, local variables are already determined
2176statically.)
2177
2178A special quirk of Python is that assignments always go into the
2179innermost scope. Assignments do not copy data --- they just
2180bind names to objects. The same is true for deletions: the statement
2181\verb\del x\ removes the binding of x from the name space referenced by the
2182local scope. In fact, all operations that introduce new names use the
2183local scope: in particular, import statements and function definitions
2184bind the module or function name in the local scope. (The
2185\verb\global\ statement can be used to indicate that particular
2186variables live in the global scope.)
2187
2188
2189\section{A first look at classes}
2190
2191Classes introduce a little bit of new syntax, three new object types,
2192and some new semantics.
2193
2194
2195\subsection{Class definition syntax}
2196
2197The simplest form of class definition looks like this:
2198
2199\begin{verbatim}
2200 class ClassName:
2201 <statement-1>
2202 .
2203 .
2204 .
2205 <statement-N>
2206\end{verbatim}
2207
2208Class definitions, like function definitions (\verb\def\ statements)
2209must be executed before they have any effect. (You could conceivably
2210place a class definition in a branch of an \verb\if\ statement, or
2211inside a function.)
2212
2213In practice, the statements inside a class definition will usually be
2214function definitions, but other statements are allowed, and sometimes
2215useful --- we'll come back to this later. The function definitions
2216inside a class normally have a peculiar form of argument list,
2217dictated by the calling conventions for methods --- again, this is
2218explained later.
2219
2220When a class definition is entered, a new name space is created, and
2221used as the local scope --- thus, all assignments to local variables
2222go into this new name space. In particular, function definitions bind
2223the name of the new function here.
2224
2225When a class definition is left normally (via the end), a {\em class
2226object} is created. This is basically a wrapper around the contents
2227of the name space created by the class definition; we'll learn more
2228about class objects in the next section. The original local scope
2229(the one in effect just before the class definitions was entered) is
2230reinstated, and the class object is bound here to class name given in
2231the class definition header (ClassName in the example).
2232
2233
2234\subsection{Class objects}
2235
2236Class objects support two kinds of operations: attribute references
2237and instantiation.
2238
2239{\em Attribute references} use the standard syntax used for all
2240attribute references in Python: \verb\obj.name\. Valid attribute
2241names are all the names that were in the class's name space when the
2242class object was created. So, if the class definition looked like
2243this:
2244
2245\begin{verbatim}
2246 class MyClass:
2247 i = 12345
2248 def f(x):
2249 return 'hello world'
2250\end{verbatim}
2251
2252then \verb\MyClass.i\ and \verb\MyClass.f\ are valid attribute
2253references, returning an integer and a function object, respectively.
2254Class attributes can also be assigned to, so you can change the
2255value of \verb\MyClass.i\ by assignment.
2256
2257Class {\em instantiation} uses function notation. Just pretend that
2258the class object is a parameterless function that returns a new
2259instance of the class. For example, (assuming the above class):
2260
2261\begin{verbatim}
2262 x = MyClass()
2263\end{verbatim}
2264
2265creates a new {\em instance} of the class and assigns this object to
2266the local variable \verb\x\.
2267
2268
2269\subsection{Instance objects}
2270
2271Now what can we do with instance objects? The only operations
2272understood by instance objects are attribute references. There are
2273two kinds of valid attribute names.
2274
2275The first I'll call {\em data attributes}. These correspond to
Guido van Rossum16d6e711994-08-08 12:30:22 +00002276``instance variables'' in Smalltalk, and to ``data members'' in \Cpp{}.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002277Data attributes need not be declared; like local variables, they
2278spring into existence when they are first assigned to. For example,
2279if \verb\x\ in the instance of \verb\MyClass\ created above, the
2280following piece of code will print the value 16, without leaving a
2281trace:
2282
2283\begin{verbatim}
2284 x.counter = 1
2285 while x.counter < 10:
2286 x.counter = x.counter * 2
2287 print x.counter
2288 del x.counter
2289\end{verbatim}
2290
2291The second kind of attribute references understood by instance objects
2292are {\em methods}. A method is a function that ``belongs to'' an
2293object. (In Python, the term method is not unique to class instances:
2294other object types can have methods as well, e.g., list objects have
2295methods called append, insert, remove, sort, and so on. However,
2296below, we'll use the term method exclusively to mean methods of class
2297instance objects, unless explicitly stated otherwise.)
2298
2299Valid method names of an instance object depend on its class. By
2300definition, all attributes of a class that are (user-defined) function
2301objects define corresponding methods of its instances. So in our
2302example, \verb\x.f\ is a valid method reference, since
2303\verb\MyClass.f\ is a function, but \verb\x.i\ is not, since
2304\verb\MyClass.i\ is not. But \verb\x.f\ is not the
2305same thing as \verb\MyClass.f\ --- it is a {\em method object}, not a
2306function object.
2307
2308
2309\subsection{Method objects}
2310
2311Usually, a method is called immediately, e.g.:
2312
2313\begin{verbatim}
2314 x.f()
2315\end{verbatim}
2316
2317In our example, this will return the string \verb\'hello world'\.
2318However, it is not necessary to call a method right away: \verb\x.f\
2319is a method object, and can be stored away and called at a later
2320moment, for example:
2321
2322\begin{verbatim}
2323 xf = x.f
2324 while 1:
2325 print xf()
2326\end{verbatim}
2327
2328will continue to print \verb\hello world\ until the end of time.
2329
2330What exactly happens when a method is called? You may have noticed
2331that \verb\x.f()\ was called without an argument above, even though
2332the function definition for \verb\f\ specified an argument. What
2333happened to the argument? Surely Python raises an exception when a
2334function that requires an argument is called without any --- even if
2335the argument isn't actually used...
2336
2337Actually, you may have guessed the answer: the special thing about
2338methods is that the object is passed as the first argument of the
2339function. In our example, the call \verb\x.f()\ is exactly equivalent
2340to \verb\MyClass.f(x)\. In general, calling a method with a list of
2341{\em n} arguments is equivalent to calling the corresponding function
2342with an argument list that is created by inserting the method's object
2343before the first argument.
2344
2345If you still don't understand how methods work, a look at the
2346implementation can perhaps clarify matters. When an instance
2347attribute is referenced that isn't a data attribute, its class is
2348searched. If the name denotes a valid class attribute that is a
2349function object, a method object is created by packing (pointers to)
2350the instance object and the function object just found together in an
2351abstract object: this is the method object. When the method object is
2352called with an argument list, it is unpacked again, a new argument
2353list is constructed from the instance object and the original argument
2354list, and the function object is called with this new argument list.
2355
2356
2357\section{Random remarks}
2358
2359
2360[These should perhaps be placed more carefully...]
2361
2362
2363Data attributes override method attributes with the same name; to
2364avoid accidental name conflicts, which may cause hard-to-find bugs in
2365large programs, it is wise to use some kind of convention that
2366minimizes the chance of conflicts, e.g., capitalize method names,
2367prefix data attribute names with a small unique string (perhaps just
Guido van Rossum6938f061994-08-01 12:22:53 +00002368an underscore), or use verbs for methods and nouns for data attributes.
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002369
2370
2371Data attributes may be referenced by methods as well as by ordinary
2372users (``clients'') of an object. In other words, classes are not
2373usable to implement pure abstract data types. In fact, nothing in
2374Python makes it possible to enforce data hiding --- it is all based
2375upon convention. (On the other hand, the Python implementation,
2376written in C, can completely hide implementation details and control
2377access to an object if necessary; this can be used by extensions to
2378Python written in C.)
2379
2380
2381Clients should use data attributes with care --- clients may mess up
2382invariants maintained by the methods by stamping on their data
2383attributes. Note that clients may add data attributes of their own to
2384an instance object without affecting the validity of the methods, as
2385long as name conflicts are avoided --- again, a naming convention can
2386save a lot of headaches here.
2387
2388
2389There is no shorthand for referencing data attributes (or other
2390methods!) from within methods. I find that this actually increases
2391the readability of methods: there is no chance of confusing local
2392variables and instance variables when glancing through a method.
2393
2394
2395Conventionally, the first argument of methods is often called
2396\verb\self\. This is nothing more than a convention: the name
2397\verb\self\ has absolutely no special meaning to Python. (Note,
2398however, that by not following the convention your code may be less
2399readable by other Python programmers, and it is also conceivable that
2400a {\em class browser} program be written which relies upon such a
2401convention.)
2402
2403
2404Any function object that is a class attribute defines a method for
2405instances of that class. It is not necessary that the function
2406definition is textually enclosed in the class definition: assigning a
2407function object to a local variable in the class is also ok. For
2408example:
2409
2410\begin{verbatim}
2411 # Function defined outside the class
2412 def f1(self, x, y):
2413 return min(x, x+y)
2414
2415 class C:
2416 f = f1
2417 def g(self):
2418 return 'hello world'
2419 h = g
2420\end{verbatim}
2421
2422Now \verb\f\, \verb\g\ and \verb\h\ are all attributes of class
2423\verb\C\ that refer to function objects, and consequently they are all
2424methods of instances of \verb\C\ --- \verb\h\ being exactly equivalent
2425to \verb\g\. Note that this practice usually only serves to confuse
2426the reader of a program.
2427
2428
2429Methods may call other methods by using method attributes of the
2430\verb\self\ argument, e.g.:
2431
2432\begin{verbatim}
2433 class Bag:
2434 def empty(self):
2435 self.data = []
2436 def add(self, x):
2437 self.data.append(x)
2438 def addtwice(self, x):
Guido van Rossum084b0b21992-08-14 09:19:56 +00002439 self.add(x)
2440 self.add(x)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002441\end{verbatim}
2442
2443
2444The instantiation operation (``calling'' a class object) creates an
2445empty object. Many classes like to create objects in a known initial
Guido van Rossum6938f061994-08-01 12:22:53 +00002446state. In early versions of Python, there was no special syntax to
2447enforce this (see below), but a convention was widely used:
2448add a method named \verb\init\ to the class,
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002449which initializes the instance (by assigning to some important data
2450attributes) and returns the instance itself. For example, class
2451\verb\Bag\ above could have the following method:
2452
2453\begin{verbatim}
2454 def init(self):
2455 self.empty()
2456 return self
2457\end{verbatim}
2458
2459The client can then create and initialize an instance in one
2460statement, as follows:
2461
2462\begin{verbatim}
2463 x = Bag().init()
2464\end{verbatim}
2465
Guido van Rossum6938f061994-08-01 12:22:53 +00002466In later versions of Python, a special method named \verb\__init__\ may be
2467defined instead:
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002468
Guido van Rossum6938f061994-08-01 12:22:53 +00002469\begin{verbatim}
2470 def __init__(self):
2471 self.empty()
2472\end{verbatim}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002473
Guido van Rossum6938f061994-08-01 12:22:53 +00002474When a class defines an \verb\__init__\ method, class instantiation
2475automatically invokes \verb\__init__\ for the newly-created class
2476instance. So in the \verb\Bag\ example, a new and initialized instance
2477can be obtained by:
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002478
Guido van Rossum6938f061994-08-01 12:22:53 +00002479\begin{verbatim}
2480 x = Bag()
2481\end{verbatim}
2482
2483Of course, the \verb\__init__\ method may have arguments for greater
2484flexibility. In that case, arguments given to the class instantiation
2485operator are passed on to \verb\__init__\. For example,
2486
2487\bcode\begin{verbatim}
2488>>> class Complex:
2489... def __init__(self, realpart, imagpart):
2490... self.r = realpart
2491... self.i = imagpart
2492...
2493>>> x = Complex(3.0,-4.5)
2494>>> x.r, x.i
2495(3.0, -4.5)
2496>>>
2497\end{verbatim}\ecode
2498%
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002499Methods may reference global names in the same way as ordinary
2500functions. The global scope associated with a method is the module
2501containing the class definition. (The class itself is never used as a
2502global scope!) While one rarely encounters a good reason for using
2503global data in a method, there are many legitimate uses of the global
2504scope: for one thing, functions and modules imported into the global
2505scope can be used by methods, as well as functions and classes defined
2506in it. Usually, the class containing the method is itself defined in
2507this global scope, and in the next section we'll find some good
2508reasons why a method would want to reference its own class!
2509
2510
2511\section{Inheritance}
2512
2513Of course, a language feature would not be worthy of the name ``class''
2514without supporting inheritance. The syntax for a derived class
2515definition looks as follows:
2516
2517\begin{verbatim}
2518 class DerivedClassName(BaseClassName):
2519 <statement-1>
2520 .
2521 .
2522 .
2523 <statement-N>
2524\end{verbatim}
2525
2526The name \verb\BaseClassName\ must be defined in a scope containing
2527the derived class definition. Instead of a base class name, an
2528expression is also allowed. This is useful when the base class is
2529defined in another module, e.g.,
2530
2531\begin{verbatim}
2532 class DerivedClassName(modname.BaseClassName):
2533\end{verbatim}
2534
2535Execution of a derived class definition proceeds the same as for a
2536base class. When the class object is constructed, the base class is
2537remembered. This is used for resolving attribute references: if a
2538requested attribute is not found in the class, it is searched in the
2539base class. This rule is applied recursively if the base class itself
2540is derived from some other class.
2541
2542There's nothing special about instantiation of derived classes:
2543\verb\DerivedClassName()\ creates a new instance of the class. Method
2544references are resolved as follows: the corresponding class attribute
2545is searched, descending down the chain of base classes if necessary,
2546and the method reference is valid if this yields a function object.
2547
2548Derived classes may override methods of their base classes. Because
2549methods have no special privileges when calling other methods of the
2550same object, a method of a base class that calls another method
2551defined in the same base class, may in fact end up calling a method of
Guido van Rossum16d6e711994-08-08 12:30:22 +00002552a derived class that overrides it. (For \Cpp{} programmers: all methods
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002553in Python are ``virtual functions''.)
2554
2555An overriding method in a derived class may in fact want to extend
2556rather than simply replace the base class method of the same name.
2557There is a simple way to call the base class method directly: just
2558call \verb\BaseClassName.methodname(self, arguments)\. This is
2559occasionally useful to clients as well. (Note that this only works if
2560the base class is defined or imported directly in the global scope.)
2561
2562
2563\subsection{Multiple inheritance}
2564
Guido van Rossum6938f061994-08-01 12:22:53 +00002565Python supports a limited form of multiple inheritance as well. A
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002566class definition with multiple base classes looks as follows:
2567
2568\begin{verbatim}
2569 class DerivedClassName(Base1, Base2, Base3):
2570 <statement-1>
2571 .
2572 .
2573 .
2574 <statement-N>
2575\end{verbatim}
2576
2577The only rule necessary to explain the semantics is the resolution
2578rule used for class attribute references. This is depth-first,
2579left-to-right. Thus, if an attribute is not found in
2580\verb\DerivedClassName\, it is searched in \verb\Base1\, then
2581(recursively) in the base classes of \verb\Base1\, and only if it is
2582not found there, it is searched in \verb\Base2\, and so on.
2583
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002584(To some people breadth first---searching \verb\Base2\ and
2585\verb\Base3\ before the base classes of \verb\Base1\---looks more
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002586natural. However, this would require you to know whether a particular
2587attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in
2588one of its base classes before you can figure out the consequences of
2589a name conflict with an attribute of \verb\Base2\. The depth-first
2590rule makes no differences between direct and inherited attributes of
2591\verb\Base1\.)
2592
2593It is clear that indiscriminate use of multiple inheritance is a
2594maintenance nightmare, given the reliance in Python on conventions to
2595avoid accidental name conflicts. A well-known problem with multiple
2596inheritance is a class derived from two classes that happen to have a
2597common base class. While it is easy enough to figure out what happens
2598in this case (the instance will have a single copy of ``instance
2599variables'' or data attributes used by the common base class), it is
2600not clear that these semantics are in any way useful.
2601
2602
2603\section{Odds and ends}
2604
2605Sometimes it is useful to have a data type similar to the Pascal
2606``record'' or C ``struct'', bundling together a couple of named data
2607items. An empty class definition will do nicely, e.g.:
2608
2609\begin{verbatim}
2610 class Employee:
2611 pass
2612
2613 john = Employee() # Create an empty employee record
2614
2615 # Fill the fields of the record
2616 john.name = 'John Doe'
2617 john.dept = 'computer lab'
2618 john.salary = 1000
2619\end{verbatim}
2620
2621
2622A piece of Python code that expects a particular abstract data type
2623can often be passed a class that emulates the methods of that data
2624type instead. For instance, if you have a function that formats some
2625data from a file object, you can define a class with methods
2626\verb\read()\ and \verb\readline()\ that gets the data from a string
2627buffer instead, and pass it as an argument. (Unfortunately, this
2628technique has its limitations: a class can't define operations that
2629are accessed by special syntax such as sequence subscripting or
2630arithmetic operators, and assigning such a ``pseudo-file'' to
2631\verb\sys.stdin\ will not cause the interpreter to read further input
2632from it.)
2633
2634
2635Instance method objects have attributes, too: \verb\m.im_self\ is the
2636object of which the method is an instance, and \verb\m.im_func\ is the
2637function object corresponding to the method.
2638
2639
Guido van Rossum6938f061994-08-01 12:22:53 +00002640\chapter{Recent Additions}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002641
Guido van Rossum6938f061994-08-01 12:22:53 +00002642Python is an evolving language. Since this tutorial was last
2643thoroughly revised, several new features have been added to the
2644language. While ideally I should revise the tutorial to incorporate
2645them in the mainline of the text, lack of time currently requires me
Guido van Rossum16d6e711994-08-08 12:30:22 +00002646to follow a more modest approach. In this chapter I will briefly list the
Guido van Rossum6938f061994-08-01 12:22:53 +00002647most important improvements to the language and how you can use them
2648to your benefit.
2649
2650\section{The Last Printed Expression}
2651
2652In interactive mode, the last printed expression is assigned to the
Guido van Rossum16d6e711994-08-08 12:30:22 +00002653variable \code\_. This means that when you are using Python as a
Guido van Rossum6938f061994-08-01 12:22:53 +00002654desk calculator, it is somewhat easier to continue calculations, for
2655example:
2656
2657\begin{verbatim}
2658 >>> tax = 17.5 / 100
2659 >>> price = 3.50
2660 >>> price * tax
2661 0.6125
2662 >>> price + _
2663 4.1125
2664 >>> round(_, 2)
2665 4.11
2666 >>>
2667\end{verbatim}
2668
2669\section{String Literals}
2670
2671\subsection{Double Quotes}
2672
2673Python can now also use double quotes to surround string literals,
2674e.g. \verb\"this doesn't hurt a bit"\.
2675
2676\subsection{Continuation Of String Literals}
2677
2678String literals can span multiple lines by escaping newlines with
2679backslashes, e.g.
2680
2681\begin{verbatim}
2682 hello = "This is a rather long string containing\n\
2683 several lines of text just as you would do in C.\n\
2684 Note that whitespace at the beginning of the line is\
2685 significant.\n"
2686 print hello
2687\end{verbatim}
2688
2689which would print the following:
2690\begin{verbatim}
2691 This is a rather long string containing
2692 several lines of text just as you would do in C.
2693 Note that whitespace at the beginning of the line is significant.
2694\end{verbatim}
2695
2696\subsection{Triple-quoted strings}
2697
2698In some cases, when you need to include really long strings (e.g.
2699containing several paragraphs of informational text), it is annoying
2700that you have to terminate each line with \verb@\n\@, especially if
2701you would like to reformat the text occasionally with a powerful text
2702editor like Emacs. For such situations, ``triple-quoted'' strings can
2703be used, e.g.
2704
2705\begin{verbatim}
2706 hello = """
2707
2708 This string is bounded by triple double quotes (3 times ").
2709 Newlines in the string are retained, though \
2710 it is still possible\nto use all normal escape sequences.
2711
2712 Whitespace at the beginning of a line is
2713 significant. If you need to include three opening quotes
2714 you have to escape at least one of them, e.g. \""".
2715
2716 This string ends in a newline.
2717 """
2718\end{verbatim}
2719
2720Note that there is no semantic difference between strings quoted with
2721single quotes (\verb/'/) or double quotes (\verb\"\).
2722
2723\subsection{String Literal Juxtaposition}
2724
2725One final twist: you can juxtapose multiple string literals. Two or
2726more adjacent string literals (but not arbitrary expressions!)
2727separated only by whitespace will be concatenated (without intervening
2728whitespace) into a single string object at compile time. This makes
2729it possible to continue a long string on the next line without
2730sacrificing indentation or performance, unlike the use of the string
2731concatenation operator \verb\+\ or the continuation of the literal
2732itself on the next line (since leading whitespace is significant
2733inside all types of string literals). Note that this feature, like
2734all string features except triple-quoted strings, is borrowed from
2735Standard C.
2736
2737\section{The Formatting Operator}
2738
2739\subsection{Basic Usage}
2740
2741The chapter on output formatting is really out of date: there is now
2742an almost complete interface to C-style printf formats. This is done
2743by overloading the modulo operator (\verb\%\) for a left operand
2744which is a string, e.g.
2745
2746\begin{verbatim}
2747 >>> import math
2748 >>> print 'The value of PI is approximately %5.3f.' % math.pi
2749 The value of PI is approximately 3.142.
2750 >>>
2751\end{verbatim}
2752
2753If there is more than one format in the string you pass a tuple as
2754right operand, e.g.
2755
2756\begin{verbatim}
2757 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
2758 >>> for name, phone in table.items():
2759 ... print '%-10s ==> %10d' % (name, phone)
2760 ...
2761 Jack ==> 4098
2762 Dcab ==> 8637678
2763 Sjoerd ==> 4127
2764 >>>
2765\end{verbatim}
2766
2767Most formats work exactly as in C and require that you pass the proper
2768type (however, if you don't you get an exception, not a core dump).
2769The \verb\%s\ format is more relaxed: if the corresponding argument is
2770not a string object, it is converted to string using the \verb\str()\
2771built-in function. Using \verb\*\ to pass the width or precision in
2772as a separate (integer) argument is supported. The C formats
2773\verb\%n\ and \verb\%p\ are not supported.
2774
2775\subsection{Referencing Variables By Name}
2776
2777If you have a really long format string that you don't want to split
2778up, it would be nice if you could reference the variables to be
2779formatted by name instead of by position. This can be done by using
2780an extension of C formats using the form \verb\%(name)format\, e.g.
2781
2782\begin{verbatim}
2783 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
2784 >>> print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table
2785 Jack: 4098; Sjoerd: 4127; Dcab: 8637678
2786 >>>
2787\end{verbatim}
2788
2789This is particularly useful in combination with the new built-in
2790\verb\vars()\ function, which returns a dictionary containing all
2791local variables.
2792
2793\section{Optional Function Arguments}
2794
2795It is now possible to define functions with a variable number of
2796arguments. There are two forms, which can be combined.
2797
2798\subsection{Default Argument Values}
2799
2800The most useful form is to specify a default value for one or more
2801arguments. This creates a function that can be called with fewer
2802arguments than it is defined, e.g.
2803
2804\begin{verbatim}
2805 def ask_ok(prompt, retries = 4, complaint = 'Yes or no, please!'):
2806 while 1:
2807 ok = raw_input(prompt)
2808 if ok in ('y', 'ye', 'yes'): return 1
2809 if ok in ('n', 'no', 'nop', 'nope'): return 0
2810 retries = retries - 1
2811 if retries < 0: raise IOError, 'refusenik user'
2812 print complaint
2813\end{verbatim}
2814
2815This function can be called either like this:
2816\verb\ask_ok('Do you really want to quit?')\ or like this:
2817\verb\ask_ok('OK to overwrite the file?', 2)\.
2818
2819The default values are evaluated at the point of function definition
2820in the {\em defining} scope, so that e.g.
2821
2822\begin{verbatim}
2823 i = 5
2824 def f(arg = i): print arg
2825 i = 6
2826 f()
2827\end{verbatim}
2828
2829will print \verb\5\.
2830
2831\subsection{Arbitrary Argument Lists}
2832
2833It is also possible to specify that a function can be called with an
2834arbitrary number of arguments. These arguments will be wrapped up in
2835a tuple. Before the variable number of arguments, zero or more normal
2836arguments may occur, e.g.
2837
2838\begin{verbatim}
2839 def fprintf(file, format, *args):
2840 file.write(format % args)
2841\end{verbatim}
2842
2843This feature may be combined with the previous, e.g.
2844
2845\begin{verbatim}
2846 def but_is_it_useful(required, optional = None, *remains):
2847 print "I don't know"
2848\end{verbatim}
2849
2850\section{Lambda And Functional Programming Tools}
2851
2852\subsection{Lambda Forms}
2853
Guido van Rossum16d6e711994-08-08 12:30:22 +00002854By popular demand, a few features commonly found in functional
Guido van Rossum6938f061994-08-01 12:22:53 +00002855programming languages and Lisp have been added to Python. With the
2856\verb\lambda\ keyword, small anonymous functions can be created.
2857Here's a function that returns the sum of its two arguments:
2858\verb\lambda a, b: a+b\. Lambda forms can be used wherever function
2859objects are required. They are syntactically restricted to a single
2860expression. Semantically, they are just syntactic sugar for a normal
2861function definition. Like nested function definitions, lambda forms
2862cannot reference variables from the containing scope, but this can be
2863overcome through the judicious use of default argument values, e.g.
2864
2865\begin{verbatim}
2866 def make_incrementor(n):
Guido van Rossumc1be9d51994-08-30 12:08:58 +00002867 return lambda x, incr=n: x+incr
Guido van Rossum6938f061994-08-01 12:22:53 +00002868\end{verbatim}
2869
2870\subsection{Map, Reduce and Filter}
2871
2872Three new built-in functions on sequences are good candidate to pass
2873lambda forms.
2874
2875\subsubsection{Map.}
2876
2877\verb\map(function, sequence)\ calls \verb\function(item)\ for each of
2878the sequence's items and returns a list of the return values. For
2879example, to compute some cubes:
2880
2881\begin{verbatim}
2882 >>> map(lambda x: x*x*x, range(1, 11))
2883 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
2884 >>>
2885\end{verbatim}
2886
2887More than one sequence may be passed; the function must then have as
2888many arguments as there are sequences and is called with the
2889corresponding item from each sequence (or \verb\None\ if some sequence
2890is shorter than another). If \verb\None\ is passed for the function,
2891a function returning its argument(s) is substituted.
2892
2893Combining these two special cases, we see that
2894\verb\map(None, list1, list2)\ is a convenient way of turning a pair
2895of lists into a list of pairs. For example:
2896
2897\begin{verbatim}
2898 >>> seq = range(8)
2899 >>> map(None, seq, map(lambda x: x*x, seq))
2900 [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]
2901 >>>
2902\end{verbatim}
2903
2904\subsubsection{Filter.}
2905
2906\verb\filter(function, sequence)\ returns a sequence (of the same
2907type, if possible) consisting of those items from the sequence for
2908which \verb\function(item)\ is true. For example, to compute some
2909primes:
2910
2911\begin{verbatim}
2912 >>> filter(lambda x: x%2 != 0 and x%3 != 0, range(2, 25))
2913 [5, 7, 11, 13, 17, 19, 23]
2914 >>>
2915\end{verbatim}
2916
2917\subsubsection{Reduce.}
2918
2919\verb\reduce(function, sequence)\ returns a single value constructed
2920by calling the (binary) function on the first two items of the
2921sequence, then on the result and the next item, and so on. For
2922example, to compute the sum of the numbers 1 through 10:
2923
2924\begin{verbatim}
2925 >>> reduce(lambda x, y: x+y, range(1, 11))
2926 55
2927 >>>
2928\end{verbatim}
2929
2930If there's only one item in the sequence, its value is returned; if
2931the sequence is empty, an exception is raised.
2932
2933A third argument can be passed to indicate the starting value. In this
2934case the starting value is returned for an empty sequence, and the
2935function is first applied to the starting value and the first sequence
2936item, then to the result and the next item, and so on. For example,
2937
2938\begin{verbatim}
2939 >>> def sum(seq):
2940 ... return reduce(lambda x, y: x+y, seq, 0)
2941 ...
2942 >>> sum(range(1, 11))
2943 55
2944 >>> sum([])
2945 0
2946 >>>
2947\end{verbatim}
2948
2949\section{Continuation Lines Without Backslashes}
2950
2951While the general mechanism for continuation of a source line on the
2952next physical line remains to place a backslash on the end of the
2953line, expressions inside matched parentheses (or square brackets, or
2954curly braces) can now also be continued without using a backslash.
2955This is particularly useful for calls to functions with many
2956arguments, and for initializations of large tables.
2957
2958For example:
2959
2960\begin{verbatim}
2961 month_names = ['Januari', 'Februari', 'Maart',
2962 'April', 'Mei', 'Juni',
2963 'Juli', 'Augustus', 'September',
2964 'Oktober', 'November', 'December']
2965\end{verbatim}
2966
2967and
2968
2969\begin{verbatim}
2970 CopyInternalHyperLinks(self.context.hyperlinks,
2971 copy.context.hyperlinks,
2972 uidremap)
2973\end{verbatim}
2974
2975\section{Regular Expressions}
2976
2977While C's printf-style output formats, transformed into Python, are
2978adequate for most output formatting jobs, C's scanf-style input
2979formats are not very powerful. Instead of scanf-style input, Python
2980offers Emacs-style regular expressions as a powerful input and
2981scanning mechanism. Read the corresponding section in the Library
2982Reference for a full description.
2983
2984\section{Generalized Dictionaries}
2985
2986The keys of dictionaries are no longer restricted to strings -- they
2987can be numbers, tuples, or (certain) class instances. (Lists and
2988dictionaries are not acceptable as dictionary keys, in order to avoid
2989problems when the object used as a key is modified.)
2990
2991Dictionaries have two new methods: \verb\d.values()\ returns a list of
2992the dictionary's values, and \verb\d.items()\ returns a list of the
2993dictionary's (key, value) pairs. Like \verb\d.keys()\, these
2994operations are slow for large dictionaries. Examples:
2995
2996\begin{verbatim}
2997 >>> d = {100: 'honderd', 1000: 'duizend', 10: 'tien'}
2998 >>> d.keys()
2999 [100, 10, 1000]
3000 >>> d.values()
3001 ['honderd', 'tien', 'duizend']
3002 >>> d.items()
3003 [(100, 'honderd'), (10, 'tien'), (1000, 'duizend')]
3004 >>>
3005\end{verbatim}
3006
3007\section{Miscellaneous New Built-in Functions}
3008
3009The function \verb\vars()\ returns a dictionary containing the current
3010local variables. With a module as argument, it returns that module's
3011global variables. The old function \verb\dir(x)\ returns
3012\verb\vars(x).keys()\.
3013
3014The function \verb\round(x)\ returns a floating point number rounded
3015to the nearest integer (but still expressed as a floating point
3016number). E.g. \verb\round(3.4) == 3.0\ and \verb\round(3.5) == 4.0\.
3017With a second argument it rounds to the specified number of digits,
3018e.g. \verb\round(math.pi, 4) == 3.1416\ or even
3019\verb\round(123.4, -2) == 100.0\.
3020
3021The function \verb\hash(x)\ returns a hash value for an object.
3022All object types acceptable as dictionary keys have a hash value (and
3023it is this hash value that the dictionary implementation uses).
3024
3025The function \verb\id(x)\ return a unique identifier for an object.
3026For two objects x and y, \verb\id(x) == id(y)\ if and only if
3027\verb\x is y\. (In fact the object's address is used.)
3028
3029The function \verb\hasattr(x, name)\ returns whether an object has an
3030attribute with the given name (a string value). The function
3031\verb\getattr(x, name)\ returns the object's attribute with the given
3032name. The function \verb\setattr(x, name, value)\ assigns a value to
3033an object's attribute with the given name. These three functions are
3034useful if the attribute names are not known beforehand. Note that
3035\verb\getattr(x, 'foo')\ is equivalent to \verb\x.foo\, and
3036\verb\setattr(x, 'foo', y)\ is equivalent to \verb\x.foo = y\. By
3037definition, \verb\hasattr(x, name)\ returns true if and only if
3038\verb\getattr(x, name)\ returns without raising an exception.
3039
3040\section{Else Clause For Try Statement}
3041
3042The \verb\try...except\ statement now has an optional \verb\else\
3043clause, which must follow all \verb\except\ clauses. It is useful to
3044place code that must be executed if the \verb\try\ clause does not
3045raise an exception. For example:
3046
3047\begin{verbatim}
3048 for arg in sys.argv:
3049 try:
3050 f = open(arg, 'r')
3051 except IOError:
3052 print 'cannot open', arg
3053 else:
3054 print arg, 'has', len(f.readlines()), 'lines'
3055 f.close()
3056\end{verbatim}
Guido van Rossum5e0759d1992-08-07 16:06:24 +00003057
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00003058\end{document}