blob: a17e79c471cdca6d586b0554fa65943fbffa3868 [file] [log] [blame]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001% Format this file with latex.
2
Guido van Rossum56098fa1991-02-19 12:54:06 +00003%\documentstyle[garamond,11pt,myformat]{article}
Guido van Rossum5ce78f11991-01-25 13:27:18 +00004\documentstyle[11pt,myformat]{article}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00005
6\title{\bf
7 Python Tutorial \\
8 (DRAFT)
9}
10
11\author{
12 Guido van Rossum \\
13 Dept. CST, CWI, Kruislaan 413 \\
14 1098 SJ Amsterdam, The Netherlands \\
15 E-mail: {\tt guido@cwi.nl}
16}
17
18\begin{document}
19
20\pagenumbering{roman}
21
22\maketitle
23
24\begin{abstract}
25
26\noindent
27\Python\ is a simple, yet powerful programming language that bridges the
28gap between C and shell programming, and is thus ideally suited for rapid
29prototyping.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000030Its syntax is put together from constructs borrowed from a variety of other
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000031languages; most prominent are influences from ABC, C, Modula-3 and Icon.
32
33The \Python\ interpreter is easily extended with new functions and data
34types implemented in C.
35\Python\ is also suitable as an extension language for highly
36customizable C applications such as editors or window managers.
37
38\Python\ is available for various operating systems, amongst which
39several flavors of \UNIX, Amoeba, and the Apple Macintosh O.S.
40
41This tutorial introduces the reader informally to the basic concepts and
42features of the \Python\ language and system.
43It helps to have a \Python\ interpreter handy for hands-on experience,
44but as the examples are self-contained, the tutorial can be read
45off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000046
47For a description of standard objects and modules, see the Library
48Reference document.
49The Language Reference document (XXX not yet existing)
50gives a more formal reference to the language.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000051
52\end{abstract}
53
54\pagebreak
55
56\tableofcontents
57
58\pagebreak
59
60\pagenumbering{arabic}
61
62\section{Whetting Your Appetite}
63
64If you ever wrote a large shell script, you probably know this feeling:
65you'd love to add yet another feature, but it's already so slow, and so
66big, and so complicated; or the feature involves a system call or other
Guido van Rossum2292b8e1991-01-23 16:31:24 +000067funcion that is only accessible from C \ldots
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000068Usually the problem at hand isn't serious enough to warrant rewriting
69the script in C; perhaps because the problem requires variable-length
70strings or other data types (like sorted lists of file names) that
71are easy in the shell but lots of work to implement in C; or perhaps
72just because you're not sufficiently familiar with C.
73
74In all such cases, \Python\ is just the language for you.
75\Python\ is simple to use, but it is a real programming language, offering
76much more structure and support for large programs than the shell has.
77On the other hand, it also offers much more error checking than C, and,
78being a
Guido van Rossum2292b8e1991-01-23 16:31:24 +000079{\em very-high-level language},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000080it has high-level data types built in, such as flexible arrays and
81dictionaries that would cost you days to implement efficiently in C.
82Because of its more general data types \Python\ is applicable to a
83much larger problem domain than
Guido van Rossum2292b8e1991-01-23 16:31:24 +000084{\em Awk}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000085or even
Guido van Rossum2292b8e1991-01-23 16:31:24 +000086{\em Perl},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000087yet most simple things are at least as easy in \Python\ as in those
88languages.
89
90\Python\ allows you to split up your program in modules that can be reused
91in other \Python\ programs.
92It comes with a large collection of standard modules that you can use as
93the basis for your programs --- or as examples to start learning to
94program in \Python.
95There are also built-in modules that provide things like file I/O,
96system calls, and even a generic interface to window systems (STDWIN).
97
98\Python\ is an interpreted language, which saves you considerable time
99during program development because no compilation and linking is
100necessary.
101The interpreter can be used interactively, which makes it easy to
102experiment with features of the language, to write throw-away programs,
103or to test functions during bottom-up program development.
104It is also a handy desk calculator.
105
106\Python\ allows writing very compact and readable programs.
107Programs written in \Python\ are typically much shorter than equivalent C
108programs:
109No declarations are necessary (all type checking is
110dynamic); statement grouping is done by indentation instead of begin/end
111brackets; and the high-level data types allow you to express complex
112operations in a single statement.
113
114\Python\ is
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000115{\em extensible}:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000116if you know how to program in C it is easy to add a new built-in module
117to the interpreter, either to perform critical operations at maximum
118speed, or to link \Python\ programs to libraries that may be only available
119in binary form (such as a vendor-specific graphics library).
120Once you are really hooked, you can link the \Python\ interpreter into an
121application written in C and use it as an extension or command language.
122
123\subsection{Where From Here}
124
125Now that you are all excited about \Python, you'll want to examine it in
126some more detail.
127Since the best introduction to a language is using it, you are invited
128here to do so.
129
130In the next section, the mechanics of using the interpreter are
131explained.
132This is rather mundane information, but essential for trying out the
133examples shown later.
134The rest of the tutorial introduces various features of the \Python\
135language and system though examples, beginning with simple expressions,
136statements and data types, through functions and modules, and finally
137touching upon advanced concepts like exceptions and classes.
138
139\section{Using the Python Interpreter}
140
141The \Python\ interpreter is usually installed as
142{\tt /usr/local/python}
143on those machines where it is available; putting
144{\tt /usr/local}
145in your \UNIX\ shell's search path makes it possible to start it by
146typing the command
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000147\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000148python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000149\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000150to the shell.
151Since the choice of the directory where the interpreter lives is an
152installation option, other places instead of
153{\tt /usr/local}
154are possible; check with your local \Python\ guru or system
155administrator.%
156\footnote{
157 At CWI, at the time of writing, the interpreter can be found in
158 the following places:
159 On the Amoeba Ultrix machines, use the standard path,
160 {\tt /usr/local/python}.
161 On the Sun file servers, use
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000162 {\tt /ufs/guido/bin/}{\em arch}{\tt /python},
163 where {\em arch} can be {\tt sgi} or {\tt sun4}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000164 On piring, use {\tt /userfs3/amoeba/bin/python}.
165 (If you can't find a binary advertised here, get in touch with me.)
166}
167
168The interpreter operates somewhat like the \UNIX\ shell: when called with
169standard input connected to a tty device, it reads and executes commands
170interactively; when called with a file name argument or with a file as
171standard input, it reads and executes a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000172{\em script}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000173from that file.%
174\footnote{
175 There is a difference between ``{\tt python file}'' and
176 ``{\tt python $<$file}''. In the latter case {\tt input()} and
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000177 {\tt raw\_input()} are satisfied from {\em file}, which has
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000178 already been read until the end by the parser, so they will read
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000179 EOF immediately. In the former case (which is usually what
180 you want) they are satisfied from whatever file or device is
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000181 connected to standard input of the \Python\ interpreter.
182}
183If available, the script name and additional arguments thereafter are
184passed to the script in the variable
185{\tt sys.argv},
186which is a list of strings.
187
188When standard input is a tty, the interpreter is said to be in
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000189{\em interactive\ mode}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000190In this mode it prompts for the next command with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000191{\em primary\ prompt},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000192usually three greater-than signs ({\tt >>>}); for continuation lines
193it prompts with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000194{\em secondary\ prompt},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000195by default three dots ({\tt ...}).
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000196Typing an EOF (Control-D) at the primary prompt causes the interpreter
197to exit with a zero exit status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000198
199When an error occurs in interactive mode, the interpreter prints a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000200message and a stack trace and returns to the primary prompt; with input
201from a file, it exits with a nonzero exit status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000202(Exceptions handled by an
203{\tt except}
204clause in a
205{\tt try}
206statement are not errors in this context.)
207Some errors are unconditionally fatal and cause an exit with a nonzero
208exit; this applies to internal inconsistencies and some cases of running
209out of memory.
210All error messages are written to the standard error stream; normal
211output from the executed commands is written to standard output.
212
213Typing an interrupt (normally Control-C or DEL) to the primary or
214secondary prompt cancels the input and returns to the primary prompt.
215Typing an interrupt while a command is being executed raises the
216{\tt KeyboardInterrupt}
217exception, which may be handled by a
218{\tt try}
219statement.
220
221When a module named
222{\tt foo}
223is imported, the interpreter searches for a file named
224{\tt foo.py}
225in a list of directories specified by the environment variable
226{\tt PYTHONPATH}.
227It has the same syntax as the \UNIX\ shell variable
228{\tt PATH},
229i.e., a list of colon-separated directory names.
230When
231{\tt PYTHONPATH}
232is not set, an installation-dependent default path is used, usually
233{\tt .:/usr/local/lib/python}.%
234\footnote{
235 Modules are really searched in the list of directories given by
236 the variable {\tt sys.path} which is initialized from
237 {\tt PYTHONPATH} or from the installation-dependent default.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000238 See the section on Standard Modules later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000239}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000240
241On BSD'ish \UNIX\ systems, \Python\ scripts can be made directly executable,
242like shell scripts, by putting the line
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000243\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000244#! /usr/local/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000245\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000246(assuming that's the name of the interpreter) at the beginning of the
247script and giving the file an executable mode.
248(The
249{\tt \#!}
250must be the first two characters of the file.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000251
252\subsection{Interactive Input Editing and History Substitution}
253
254Some versions of the \Python\ interpreter support editing of the current
255input line and history substitution, similar to facilities found in the
256Korn shell and the GNU Bash shell.
257This is implemented using the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000258{\em GNU\ Readline}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000259library, which supports Emacs-style and vi-style editing.
260This library has its own documentation which I won't duplicate here;
261however, the basics are easily explained.
262
263If supported,%
264\footnote{
265 Perhaps the quickest check to see whether command line editing
266 is supported is typing Control-P to the first \Python\ prompt
267 you get. If it beeps, you have command line editing.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000268 If not, you can skip the rest of this section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000269}
270input line editing is active whenever the interpreter prints a primary
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000271or secondary prompt.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000272The current line can be edited using the conventional Emacs control
273characters.
274The most important of these are:
275C-A (Control-A) moves the cursor to the beginning of the line, C-E to
276the end, C-B moves it one position to the left, C-F to the right.
277Backspace erases the character to the left of the cursor, C-D the
278character to its right.
279C-K kills (erases) the rest of the line to the right of the cursor, C-Y
280yanks back the last killed string.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000281C-underscore undoes the last change you made; it can be repeated for
282cumulative effect.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000283
284History substitution works as follows.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000285All non-empty input lines issued are saved in a history buffer,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000286and when a new prompt is given you are positioned on a new line at the
287bottom of this buffer.
288C-P moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000289Any line in the history buffer can be edited; an asterisk appears in
290front of the prompt to mark a line as modified.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000291Pressing the Return key passes the current line to the interpreter.
292C-R starts an incremental reverse search; C-S starts a forward search.
293
294The key bindings and some other parameters of the Readline library can
295be customized by placing commands in an initialization file called
296{\tt \$HOME/.initrc}.
297Key bindings have the form
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000298\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000299key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000300\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000301and options can be set with
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000302\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000303set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000304\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000305Example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000306\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000307# I prefer vi-style editing:
308set editing-mode vi
309# Edit using a single line:
310set horizontal-scroll-mode On
311# Rebind some keys:
312Meta-h: backward-kill-word
313Control-u: universal-argument
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000314\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000315Note that the default binding for TAB in \Python\ is to insert a TAB
316instead of Readline's default filename completion function.
317If you insist, you can override this by putting
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000318\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000319TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000320\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000321in your
322{\tt \$HOME/.inputrc}.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000323(Of course, this makes it hard to type indented continuation lines.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000324
325This facility is an enormous step forward compared to previous versions of
326the interpreter; however, some wishes are left:
327It would be nice if the proper indentation were suggested on
328continuation lines (the parser knows if an indent token is required
329next).
330The completion mechanism might use the interpreter's symbol table.
331A function to check (or even suggest) matching parentheses, quotes
332etc. would also be useful.
333
334\section{An Informal Introduction to Python}
335
336In the following examples, input and output are distinguished by the
337presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat the
338example, you must type everything after the prompt, when the prompt
339appears; everything on lines that do not begin with a prompt is output
340from the interpreter.
341Note that a secondary prompt on a line by itself in an example means you
342must type a blank line; this is used to end a multi-line command.
343
344\subsection{Using Python as a Calculator}
345
346Let's try some simple \Python\ commands.
347Start the interpreter and wait for the primary prompt,
348{\tt >>>}.
349The interpreter acts as a simple calculator: you can type an expression
350at it and it will write the value.
351Expression syntax is straightforward: the operators
352{\tt +},
353{\tt -},
354{\tt *}
355and
356{\tt /}
357work just as in most other languages (e.g., Pascal or C); parentheses
358can be used for grouping.
359For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000360\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000361>>> # This is a comment
362>>> 2+2
3634
364>>>
365>>> (50-5+5*6+25)/4
36625
367>>> # Division truncates towards zero:
368>>> 7/3
3692
370>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000371\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000372As in C, the equal sign ({\tt =}) is used to assign a value to a variable.
373The value of an assignment is not written:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000374\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000375>>> width = 20
376>>> height = 5*9
377>>> width * height
378900
379>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000380\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000381There is some support for floating point, but you can't mix floating
382point and integral numbers in expression (yet):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000383\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000384>>> 10.0 / 3.3
3853.0303030303
386>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000387\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000388Besides numbers, \Python\ can also manipulate strings, enclosed in single
389quotes:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000390\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000391>>> 'foo bar'
392'foo bar'
393>>> 'doesn\'t'
394'doesn\'t'
395>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000396\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000397Strings are written inside quotes and with quotes and other funny
398characters escaped by backslashes, to show the precise value.
399(There is also a way to write strings without quotes and escapes.)
400Strings can be concatenated (glued together) with the
401{\tt +}
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000402operator, and repeated with~{\tt *}:
403\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000404>>> word = 'Help' + 'A'
405>>> word
406'HelpA'
407>>> '<' + word*5 + '>'
408'<HelpAHelpAHelpAHelpAHelpA>'
409>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000410\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000411Strings can be subscripted; as in C, the first character of a string has
412subscript 0.
413There is no separate character type; a character is simply a string of
414size one.
415As in Icon, substrings can be specified with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000416{\em slice}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000417notation: two subscripts (indices) separated by a colon.
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000418\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000419>>> word[4]
420'A'
421>>> word[0:2]
422'He'
423>>> word[2:4]
424'lp'
425>>> # Slice indices have useful defaults:
426>>> word[:2] # Take first two characters
427'He'
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000428>>> word[2:] # Drop first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000429'lpA'
430>>> # A useful invariant: s[:i] + s[i:] = s
431>>> word[:3] + word[3:]
432'HelpA'
433>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000434\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000435Degenerate cases are handled gracefully: an index that is too large is
436replaced by the string size, an upper bound smaller than the lower bound
437returns an empty string.
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000438\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000439>>> word[1:100]
440'elpA'
441>>> word[10:]
442''
443>>> word[2:1]
444''
445>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000446\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000447Slice indices (but not simple subscripts) may be negative numbers, to
448start counting from the right.
449For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000450\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000451>>> word[-2:] # Take last two characters
452'pA'
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000453>>> word[:-2] # Drop last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000454'Hel'
455>>> # But -0 does not count from the right!
456>>> word[-0:] # (since -0 equals 0)
457'HelpA'
458>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000459\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000460The best way to remember how slices work is to think of the indices as
461pointing
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000462{\em between}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000463characters, with the left edge of the first character numbered 0.
464Then the right edge of the last character of a string of
465{\tt n}
466characters has index
467{\tt n},
468for example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000469\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000470 +---+---+---+---+---+
471 | H | e | l | p | A |
472 +---+---+---+---+---+
473 0 1 2 3 4 5
474-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000475\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000476The first row of numbers gives the position of the indices 0...5 in the
477string; the second row gives the corresponding negative indices.
478For nonnegative indices, the length of a slice is the difference of the
479indices, if both are within bounds,
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000480e.g.,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000481the length of
482{\tt word[1:3]}
483is 3--1 = 2.
484
485Finally, the built-in function {\tt len()} computes the length of a
486string:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000487\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000488>>> s = 'supercalifragilisticexpialidocious'
489>>> len(s)
49034
491>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000492\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000493\Python\ knows a number of
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000494{\em compound}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000495data types, used to group together other values.
496The most versatile is the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000497{\em list},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000498which can be written as a list of comma-separated values between square
499brackets:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000500\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000501>>> a = ['foo', 'bar', 100, 1234]
502>>> a
503['foo', 'bar', 100, 1234]
504>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000505\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000506As for strings, list subscripts start at 0:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000507\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000508>>> a[0]
509'foo'
510>>> a[3]
5111234
512>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000513\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000514Lists can be sliced and concatenated like strings:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000515\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000516>>> a[1:3]
517['bar', 100]
518>>> a[:2] + ['bletch', 2*2]
519['foo', 'bar', 'bletch', 4]
520>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000521\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000522Unlike strings, which are
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000523{\em immutable},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000524it is possible to change individual elements of a list:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000525\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000526>>> a
527['foo', 'bar', 100, 1234]
528>>> a[2] = a[2] + 23
529>>> a
530['foo', 'bar', 123, 1234]
531>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000532\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000533Assignment to slices is also possible, and this may even change the size
534of the list:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000535\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000536>>> # Replace some items:
537>>> a[0:2] = [1, 12]
538>>> a
539[1, 12, 123, 1234]
540>>> # Remove some:
541>>> a[0:2] = []
542>>> a
543[123, 1234]
544>>> # Insert some:
545>>> a[1:1] = ['bletch', 'xyzzy']
546>>> a
547[123, 'bletch', 'xyzzy', 1234]
548>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000549\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000550The built-in function {\tt len()} also applies to lists:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000551\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000552>>> len(a)
5534
554>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000555\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000556
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000557\subsection{Tuples and Sequences}
558
559XXX To Be Done.
560
561\subsection{First Steps Towards Programming}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000562
563Of course, we can use \Python\ for more complicated tasks than adding two
564and two together.
565For instance, we can write an initial subsequence of the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000566{\em Fibonacci}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000567series as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000568\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000569>>> # Fibonacci series:
570>>> # the sum of two elements defines the next
571>>> a, b = 0, 1
572>>> while b < 100:
573... print b
574... a, b = b, a+b
575...
5761
5771
5782
5793
5805
5818
58213
58321
58434
58555
58689
587>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000588\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000589This example introduces several new features.
590\begin{itemize}
591\item
592The first line contains a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000593{\em multiple\ assignment}:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000594the variables
595{\tt a}
596and
597{\tt b}
598simultaneously get the new values 0 and 1.
599On the last line this is used again, demonstrating that the expressions
600on the right-hand side are all evaluated first before any of the
601assignments take place.
602\item
603The
604{\tt while}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000605loop executes as long as the condition (here: $b < 100$) remains true.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000606In \Python, as in C, any non-zero integer value is true; zero is false.
607The condition may also be a string or list value, in fact any sequence;
608anything with a non-zero length is true, empty sequences are false.
609The test used in the example is a simple comparison.
610The standard comparison operators are written as
611{\tt <},
612{\tt >},
613{\tt =},
614{\tt <=},
615{\tt >=}
616and
617{\tt <>}.%
618\footnote{
619 The ambiguity of using {\tt =}
620 for both assignment and equality is resolved by disallowing
621 unparenthesized conditions at the right hand side of assignments.
622}
623\item
624The
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000625{\em body}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000626of the loop is
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000627{\em indented}: indentation is \Python's way of grouping statements.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000628\Python\ does not (yet!) provide an intelligent input line editing
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000629facility, so you have to type a tab or space(s) for each indented line.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000630In practice you will prepare more complicated input for \Python\ with a
631text editor; most text editors have an auto-indent facility.
632When a compound statement is entered interactively, it must be
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000633followed by a blank line to indicate completion (since the parser
634cannot guess when you have typed the last line).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000635\item
636The
637{\tt print}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000638statement writes the value of the expression(s) it is given.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000639It differs from just writing the expression you want to write (as we did
640earlier in the calculator examples) in the way it handles multiple
641expressions and strings.
642Strings are written without quotes and a space is inserted between
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000643items, so you can format things nicely, like this:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000644\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000645>>> i = 256*256
646>>> print 'The value of i is', i
647The value of i is 65536
648>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000649\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000650A trailing comma avoids the newline after the output:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000651\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000652>>> a, b = 0, 1
653>>> while b < 1000:
654... print b,
655... a, b = b, a+b
656...
6571 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
658>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000659\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000660Note that the interpreter inserts a newline before it prints the next
661prompt if the last line was not completed.
662\end{itemize}
663
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000664\subsection{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000665
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000666Besides the {\tt while} statement just introduced, \Python\ knows the
667usual control flow statements known from other languages, with some
668twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000669
670\subsubsection{If Statements}
671
672Perhaps the most well-known statement type is the {\tt if} statement.
673For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000674\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000675>>> if x < 0:
676... x = 0
677... print 'Negative changed to zero'
678... elif x = 0:
679... print 'Zero'
680... elif x = 1:
681... print 'Single'
682... else:
683... print 'More'
684...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000685\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000686There can be zero or more {\tt elif} parts, and the {\tt else} part is
687optional.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000688The keyword `{\tt elif}' is short for `{\tt else if}', and is useful to
689avoid excessive indentation.
690An {\tt if...elif...elif...} sequence is a substitute for the
691{\em switch} or {\em case} statements found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000692
693\subsubsection{For Statements}
694
695The {\tt for} statement in \Python\ differs a bit from what you may be
696used to in C or Pascal.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000697Rather than always iterating over an arithmetic progression of numbers
698(as Pascal), or leaving the user completely free in the iteration test
699and step (as C), \Python's {\tt for} statement iterates over the items
700of any sequence (e.g., a list or a string).
701For example (no pun intended):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000702\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000703>>> # Measure some strings:
704>>> a = ['cat', 'window', 'defenestrate']
705>>> for x in a:
706... print x, len(x)
707...
708cat 3
709window 6
710defenestrate 12
711>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000712\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000713
714\subsubsection{The {\tt range()} Function}
715
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000716If you do need to iterate over a sequence of numbers, the built-in
717function {\tt range()} comes in handy.
718It generates lists containing arithmetic progressions,
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000719e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000720\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000721>>> range(10)
722[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
723>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000724\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000725The given end point is never part of the generated list;
726{\tt range(10)} generates a list of 10 values,
727exactly the legal indices for items of a sequence of length 10.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000728It is possible to let the range start at another number, or to specify a
729different increment (even negative):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000730\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000731>>> range(5, 10)
732[5, 6, 7, 8, 9]
733>>> range(0, 10, 3)
734[0, 3, 6, 9]
735>>> range(-10, -100, -30)
736[-10, -40, -70]
737>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000738\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000739To iterate over the indices of a sequence, combine {\tt range()}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000740and {\tt len()} as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000741\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000742>>> a = ['Mary', 'had', 'a', 'little', 'boy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000743>>> for i in range(len(a)):
744... print i, a[i]
745...
7460 Mary
7471 had
7482 a
7493 little
Guido van Rossum2292b8e1991-01-23 16:31:24 +00007504 boy
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000751>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000752\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000753
754\subsubsection{Break Statements and Else Clauses on Loops}
755
756The {\tt break} statement breaks out of the smallest enclosing {\tt for}
757or {\tt while} loop.
758Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000759loop terminates through exhaustion of the list (with {\tt for}) or when
760the condition becomes false (with {\tt while}) but not when the loop is
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000761terminated by a {\tt break} statement.
762This is exemplified by the following loop, which searches for a list
763item of value 0:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000764\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000765>>> for n in range(2, 10):
766... for x in range(2, n):
767... if n % x = 0:
768... print n, 'equals', x, '*', n/x
769... break
770... else:
771... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000772...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00007732 is a prime number
7743 is a prime number
7754 equals 2 * 2
7765 is a prime number
7776 equals 2 * 3
7787 is a prime number
7798 equals 2 * 4
7809 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000781>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000782\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000783
784\subsubsection{Pass Statements}
785
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000786The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000787It can be used when a statement is required syntactically but the
788program requires no action.
789For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000790\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000791>>> while 1:
792... pass # Busy-wait for keyboard interrupt
793...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000794\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000795
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000796\subsubsection{Conditions Revisited}
797
798XXX To Be Done.
799
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000800\subsection{Defining Functions}
801
802We can create a function that writes the Fibonacci series to an
803arbitrary boundary:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000804\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000805>>> def fib(n): # write Fibonacci series up to n
806... a, b = 0, 1
807... while b <= n:
808... print b,
809... a, b = b, a+b
810...
811>>> # Now call the function we just defined:
812>>> fib(2000)
8131 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
814>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000815\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000816The keyword
817{\tt def}
818introduces a function
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000819{\em definition}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000820It must be followed by the function name and the parenthesized list of
821formal parameters.
822The statements that form the body of the function starts at the next
823line, indented by a tab stop.
824The
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000825{\em execution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000826of a function introduces a new symbol table used for the local variables
827of the function.
828More precisely, all variable assignments in a function store the value
829in the local symbol table; variable references first look in the local
830symbol table, then in the global symbol table, and then in the table of
831built-in names.
832Thus, the global symbol table is
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000833{\em read-only}
834within a function.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000835The actual parameters (arguments) to a function call are introduced in
836the local symbol table of the called function when it is called;
837thus, arguments are passed using
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000838{\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000839\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000840 Actually, {\em call by object reference} would be a better
841 description, since if a mutable object is passed, the caller
842 will see any changes the callee makes to it (e.g., items
843 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000844}
845When a function calls another function, a new local symbol table is
846created for that call.
847
848A function definition introduces the function name in the global symbol
849table.
850The value has a type that is recognized by the interpreter as a
851user-defined function.
852This value can be assigned to another name which can then also be used
853as a function.
854This serves as a general renaming mechanism:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000855\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000856>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000857<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000858>>> f = fib
859>>> f(100)
8601 1 2 3 5 8 13 21 34 55 89
861>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000862\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000863You might object that
864{\tt fib}
865is not a function but a procedure.
866In \Python, as in C, procedures are just functions that don't return a
867value.
868In fact, technically speaking, procedures do return a value, albeit a
869rather boring one.
870This value is called {\tt None} (it's a built-in name).
871Writing the value {\tt None} is normally suppressed by the interpreter
872if it would be the only value written.
873You can see it if you really want to:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000874\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000875>>> print fib(0)
876None
877>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000878\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000879It is simple to write a function that returns a list of the numbers of
880the Fibonacci series, instead of printing it:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000881\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000882>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000883... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000884... a, b = 0, 1
885... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000886... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000887... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000888... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000889...
890>>> f100 = fib2(100) # call it
891>>> f100 # write the result
892[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
893>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000894\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000895This example, as usual, demonstrates some new \Python\ features:
896\begin{itemize}
897\item
898The
899{\tt return}
900statement returns with a value from a function.
901{\tt return}
902without an expression argument is used to return from the middle of a
903procedure (falling off the end also returns from a proceduce).
904\item
905The statement
906{\tt ret.append(b)}
907calls a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000908{\em method}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000909of the list object
910{\tt ret}.
911A method is a function that `belongs' to an object and is named
912{\tt obj.methodname},
913where
914{\tt obj}
915is some object (this may be an expression), and
916{\tt methodname}
917is the name of a method that is defined by the object's type.
918Different types define different methods.
919Methods of different types may have the same name without causing
920ambiguity.
921See the section on classes, later, to find out how you can define your
922own object types and methods.
923The method
924{\tt append}
925shown in the example, is defined for list objects; it adds a new element
926at the end of the list.
927In this case it is equivalent to
928{\tt ret = ret + [b]},
929but more efficient.%
930\footnote{
931 There is a subtle semantic difference if the object
932 is referenced from more than one place.
933}
934\end{itemize}
935The list object type has two more methods:
Guido van Rossum7d9f8d71991-01-22 11:45:00 +0000936\begin{description}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000937\item[{\tt insert(i, x)}]
938Inserts an item at a given position.
939The first argument is the index of the element before which to insert,
940so {\tt a.insert(0, x)} inserts at the front of the list, and
941{\tt a.insert(len(a), x)} is equivalent to {\tt a.append(x)}.
942\item[{\tt sort()}]
943Sorts the elements of the list.
Guido van Rossum7d9f8d71991-01-22 11:45:00 +0000944\end{description}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000945For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000946\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000947>>> a = [10, 100, 1, 1000]
948>>> a.insert(2, -1)
949>>> a
950[10, 100, -1, 1, 1000]
951>>> a.sort()
952>>> a
953[-1, 1, 10, 100, 1000]
954>>> # Strings are sorted according to ASCII:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000955>>> b = ['Mary', 'had', 'a', 'little', 'boy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000956>>> b.sort()
957>>> b
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000958['Mary', 'a', 'boy', 'had', 'little']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000959>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000960\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000961
962\subsection{Modules}
963
964If you quit from the \Python\ interpreter and enter it again, the
965definitions you have made (functions and variables) are lost.
966Therefore, if you want to write a somewhat longer program, you are
967better off using a text editor to prepare the input for the interpreter
968and run it with that file as input instead.
969This is known as creating a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000970{\em script}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000971As your program gets longer, you may want to split it into several files
972for easier maintenance.
973You may also want to use a handy function that you've written in several
974programs without copying its definition into each program.
975To support this, \Python\ has a way to put definitions in a file and use
976them in a script or in an interactive instance of the interpreter.
977Such a file is called a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000978{\em module};
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000979definitions from a module can be
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000980{\em imported}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000981into other modules or into the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000982{\em main}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000983module (the collection of variables that you have access to in
984a script and in calculator mode).
985
986A module is a file containing \Python\ definitions and statements.
987The file name is the module name with the suffix
988{\tt .py}
989appended.
990For instance, use your favorite text editor to create a file called
991{\tt fibo.py}
992in the current directory with the following contents:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000993\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000994# Fibonacci numbers module
995
996def fib(n): # write Fibonacci series up to n
997 a, b = 0, 1
998 while b <= n:
999 print b,
1000 a, b = b, a+b
1001
1002def fib2(n): # return Fibonacci series up to n
1003 ret = []
1004 a, b = 0, 1
1005 while b <= n:
1006 ret.append(b)
1007 a, b = b, a+b
1008 return ret
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001009\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001010Now enter the \Python\ interpreter and import this module with the
1011following command:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001012\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001013>>> import fibo
1014>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001015\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001016This does not enter the names of the functions defined in
1017{\tt fibo}
1018directly in the symbol table; it only enters the module name
1019{\tt fibo}
1020there.
1021Using the module name you can access the functions:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001022\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001023>>> fibo.fib(1000)
10241 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1025>>> fibo.fib2(100)
1026[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1027>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001028\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001029If you intend to use a function often you can assign it to a local name:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001030\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001031>>> fib = fibo.fib
1032>>> fib(500)
10331 1 2 3 5 8 13 21 34 55 89 144 233 377
1034>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001035\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001036
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001037\subsubsection{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001038
1039A module can contain executable statements as well as function
1040definitions.
1041These statements are intended to initialize the module.
1042They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001043{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001044time the module is imported somewhere.%
1045\footnote{
1046 In fact function definitions are also `statements' that are
1047 `executed'; the execution enters the function name in the
1048 module's global symbol table.
1049}
1050
1051Each module has its own private symbol table, which is used as the
1052global symbol table by all functions defined in the module.
1053Thus, the author of a module can use global variables in the module
1054without worrying about accidental clashes with a user's global
1055variables.
1056On the other hand, if you know what you are doing you can touch a
1057module's global variables with the same notation used to refer to its
1058functions,
1059{\tt modname.itemname}.
1060
1061Modules can import other modules.
1062It is customary but not required to place all
1063{\tt import}
1064statements at the beginning of a module (or script, for that matter).
1065The imported module names are placed in the importing module's global
1066symbol table.
1067
1068There is a variant of the
1069{\tt import}
1070statement that imports names from a module directly into the importing
1071module's symbol table.
1072For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001073\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001074>>> from fibo import fib, fib2
1075>>> fib(500)
10761 1 2 3 5 8 13 21 34 55 89 144 233 377
1077>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001078\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001079This does not introduce the module name from which the imports are taken
1080in the local symbol table (so in the example, {\tt fibo} is not
1081defined).
1082
1083There is even a variant to import all names that a module defines:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001084\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001085>>> from fibo import *
1086>>> fib(500)
10871 1 2 3 5 8 13 21 34 55 89 144 233 377
1088>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001089\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001090This imports all names except those beginning with an underscore
1091({\tt \_}).
1092
1093\subsubsection{Standard Modules}
1094
1095\Python\ comes with a library of standard modules, described in a separate
1096document (Python Library and Module Reference).
1097Some modules are built into the interpreter; these provide access to
1098operations that are not part of the core of the language but are
1099nevertheless built in, either for efficiency or to provide access to
1100operating system primitives such as system calls.
1101The set of such modules is a configuration option; e.g., the
1102{\tt amoeba}
1103module is only provided on systems that somehow support Amoeba
1104primitives.
1105One particular module deserves some attention:
1106{\tt sys},
1107which is built into every \Python\ interpreter.
1108The variables
1109{\tt sys.ps1}
1110and
1111{\tt sys.ps2}
1112define the strings used as primary and secondary prompts:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001113\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001114>>> import sys
1115>>> sys.ps1
1116'>>> '
1117>>> sys.ps2
1118'... '
1119>>> sys.ps1 = 'C> '
1120C> print 'Yuck!'
1121Yuck!
1122C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001123\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001124These two variables are only defined if the interpreter is in
1125interactive mode.
1126
1127The variable
1128{\tt sys.path}
1129is a list of strings that determine the interpreter's search path for
1130modules.
1131It is initialized to a default path taken from the environment variable
1132{\tt PYTHONPATH},
1133or from a built-in default if
1134{\tt PYTHONPATH}
1135is not set.
1136You can modify it using standard list operations, e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001137\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001138>>> import sys
1139>>> sys.path.append('/ufs/guido/lib/python')
1140>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001141\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001142
1143\subsection{Errors and Exceptions}
1144
1145Until now error messages haven't yet been mentioned, but if you have
1146tried out the examples you have probably seen some.
1147There are (at least) two distinguishable kinds of errors:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001148{\em syntax\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001149and
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001150{\em exceptions}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001151
1152\subsubsection{Syntax Errors}
1153
1154Syntax errors, also known as parsing errors, are perhaps the most common
1155kind of complaint you get while you are still learning \Python:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001156\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001157>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001158Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001159while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001160 ^
1161Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001162>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001163\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001164The parser repeats the offending line and displays a little `arrow'
1165pointing at the earliest point in the line where the error was detected.
1166The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001167{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001168the arrow: in the example, the error is detected at the keyword
1169{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001170File name and line number are printed so you know where to look in case
1171the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001172
1173\subsubsection{Exceptions}
1174
1175Even if a statement or expression is syntactically correct, it may cause
1176an error when an attempt is made to execute it:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001177\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001178>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001179Unhandled exception: run-time error: integer division by zero
1180Stack backtrace (innermost last):
1181 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001182>>> 4 + foo*3
1183Unhandled exception: undefined name: foo
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001184Stack backtrace (innermost last):
1185 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001186>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001187Unhandled exception: type error: illegal argument type for built-in operation
1188Stack backtrace (innermost last):
1189 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001190>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001191\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001192Errors detected during execution are called
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001193{\em exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001194and are not unconditionally fatal: you will soon learn how to handle
1195them in \Python\ programs.
1196Most exceptions are not handled by programs, however, and result
1197in error messages as shown here.
1198
1199The first line of the error message indicates what happened.
1200Exceptions come in different types, and the type is printed as part of
1201the message: the types in the example are
1202{\tt run-time error},
1203{\tt undefined name}
1204and
1205{\tt type error}.
1206The rest of the line is a detail whose interpretation depends on the
1207exception type.
1208
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001209The rest of the error message shows the context where the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001210exception happened.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001211In general it contains a stack backtrace listing source lines; however,
1212it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001213
1214Here is a summary of the most common exceptions:
1215\begin{itemize}
1216\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001217{\em Run-time\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001218are generally caused by wrong data used by the program; this can be the
1219programmer's fault or caused by bad input.
1220The detail states the cause of the error in more detail.
1221\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001222{\em Undefined\ name}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001223errors are more serious: these are usually caused by misspelled
1224identifiers.%
1225\footnote{
1226 The parser does not check whether names used in a program are at
1227 all defined elsewhere in the program, so such checks are
1228 postponed until run-time. The same holds for type checking.
1229}
1230The detail is the offending identifier.
1231\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001232{\em Type\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001233are also pretty serious: this is another case of using wrong data (or
1234better, using data the wrong way), but here the error can be glanced
1235from the object type(s) alone.
1236The detail shows in what context the error was detected.
1237\end{itemize}
1238
1239\subsubsection{Handling Exceptions}
1240
1241It is possible to write programs that handle selected exceptions.
1242Look at the following example, which prints a table of inverses of
1243some floating point numbers:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001244\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001245>>> numbers = [0.3333, 2.5, 0.0, 10.0]
1246>>> for x in numbers:
1247... print x,
1248... try:
1249... print 1.0 / x
1250... except RuntimeError:
1251... print '*** has no inverse ***'
1252...
12530.3333 3.00030003
12542.5 0.4
12550 *** has no inverse ***
125610 0.1
1257>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001258\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001259The {\tt try} statement works as follows.
1260\begin{itemize}
1261\item
1262First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001263{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001264(the statement(s) between the {\tt try} and {\tt except} keywords) is
1265executed.
1266\item
1267If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001268{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001269is skipped and execution of the {\tt try} statement is finished.
1270\item
1271If an exception occurs during execution of the try clause, and its
1272type matches the exception named after the {\tt except} keyword, the
1273rest of the try clause is skipped, the except clause is executed, and
1274then execution continues after the {\tt try} statement.
1275\item
1276If an exception occurs which does not match the exception named in the
1277except clause, it is passed on to outer try statements; if no handler is
1278found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001279{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001280and execution stops with a message as shown above.
1281\end{itemize}
1282A {\tt try} statement may have more than one except clause, to specify
1283handlers for different exceptions.
1284At most one handler will be executed.
1285Handlers only handle exceptions that occur in the corresponding try
1286clause, not in other handlers of the same {\tt try} statement.
1287An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001288e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001289\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001290... except (RuntimeError, TypeError, NameError):
1291... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001292\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001293The last except clause may omit the exception name(s), to serve as a
1294wildcard.
1295Use this with extreme caution!
1296
1297When an exception occurs, it may have an associated value, also known as
1298the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001299{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001300The presence and type of the argument depend on the exception type.
1301For exception types which have an argument, the except clause may
1302specify a variable after the exception name (or list) to receive the
1303argument's value, as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001304\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001305>>> try:
1306... foo()
1307... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001308... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001309...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001310name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001311>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001312\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001313If an exception has an argument, it is printed as the third part
1314(`detail') of the message for unhandled exceptions.
1315
1316Standard exception names are built-in identifiers (not reserved
1317keywords).
1318These are in fact string objects whose
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001319{\em object\ identity}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001320(not their value!) identifies the exceptions.%
1321\footnote{
1322 There should really be a separate exception type; it is pure
1323 laziness that exceptions are identified by strings, and this may
1324 be fixed in the future.
1325}
1326The string is printed as the second part of the message for unhandled
1327exceptions.
1328Their names and values are:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001329\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001330EOFError 'end-of-file read'
1331KeyboardInterrupt 'keyboard interrupt'
1332MemoryError 'out of memory' *
1333NameError 'undefined name' *
1334RuntimeError 'run-time error' *
1335SystemError 'system error' *
1336TypeError 'type error' *
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001337\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001338The meanings should be clear enough.
1339Those exceptions with a {\tt *} in the third column have an argument.
1340
1341Exception handlers don't just handle exceptions if they occur
1342immediately in the try clause, but also if they occur inside functions
1343that are called (even indirectly) in the try clause.
1344For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001345\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001346>>> def this_fails():
1347... x = 1/0
1348...
1349>>> try:
1350... this_fails()
1351... except RuntimeError, detail:
1352... print 'Handling run-time error:', detail
1353...
1354Handling run-time error: domain error or zero division
1355>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001356\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001357
1358\subsubsection{Raising Exceptions}
1359
1360The {\tt raise} statement allows the programmer to force a specified
1361exception to occur.
1362For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001363\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001364>>> raise NameError, 'Hi There!'
1365Unhandled exception: undefined name: Hi There!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001366Stack backtrace (innermost last):
1367 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001368>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001369\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001370The first argument to {\tt raise} names the exception to be raised.
1371The optional second argument specifies the exception's argument.
1372
1373\subsubsection{User-defined Exceptions}
1374
1375Programs may name their own exceptions by assigning a string to a
1376variable.
1377For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001378\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001379>>> my_exc = 'nobody likes me!'
1380>>> try:
1381... raise my_exc, 2*2
1382... except my_exc, val:
1383... print 'My exception occured, value:', val
1384...
1385My exception occured, value: 4
1386>>> raise my_exc, 1
1387Unhandled exception: nobody likes me!: 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001388Stack backtrace (innermost last):
1389 File "<stdin>", line 7
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001390>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001391\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001392Many standard modules use this to report errors that may occur in
1393functions they define.
1394
1395\subsubsection{Defining Clean-up Actions}
1396
1397The {\tt try} statement has another optional clause which is intended to
1398define clean-up actions that must be executed under all circumstances.
1399For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001400\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001401>>> try:
1402... raise KeyboardInterrupt
1403... finally:
1404... print 'Goodbye, world!'
1405...
1406Goodbye, world!
1407Unhandled exception: keyboard interrupt
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001408Stack backtrace (innermost last):
1409 File "<stdin>", line 2
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001410>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001411\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001412The
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001413{\em finally\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001414must follow the except clauses(s), if any.
1415It is executed whether or not an exception occurred.
1416If the exception is handled, the finally clause is executed after the
1417handler (and even if another exception occurred in the handler).
1418It is also executed when the {\tt try} statement is left via a
1419{\tt break} or {\tt return} statement.
1420
1421\subsection{Classes}
1422
1423Classes in \Python\ make it possible to play the game of encapsulation in a
1424somewhat different way than it is played with modules.
1425Classes are an advanced topic and are probably best skipped on the first
1426encounter with \Python.
1427
1428\subsubsection{Prologue}
1429
1430\Python's class mechanism is not particularly elegant, but quite powerful.
1431It is a mixture of the class mechanisms found in C++ and Modula-3.
1432As is true for modules, classes in \Python\ do not put an absolute barrier
1433between definition and user, but rather rely on the politeness of the
1434user not to ``break into the definition.''
1435The most important features of classes are retained with full power,
1436however: the class inheritance mechanism allows multiple base classes,
1437a derived class can override any method of its base class(es), a method
1438can call the method of a base class with the same name.
1439Objects can contain an arbitrary amount of private data.
1440
1441In C++ terminology, all class members (including data members) are
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001442{\em public},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001443and all member functions (methods) are
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001444{\em virtual}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001445There are no special constructors or destructors.
1446As in Modula-3, there are no shorthands for referencing the object's
1447members from its methods: the method function is declared with an
1448explicit first argument representing the object, which is provided
1449implicitly by the call.
1450As in Smalltalk, classes themselves are objects, albeit in the wider
1451sense of the word: in \Python, all data types are objects.
1452This provides semantics for renaming or aliasing.
1453But, just like in C++ or Modula-3, the built-in types cannot be used as
1454base classes for extension by the user.
1455Also, like Modula-3 but unlike C++, the built-in operators with special
1456syntax (arithmetic operators, subscripting etc.) cannot be redefined for
1457class members.%
1458\footnote{
1459 They can be redefined for new object types implemented in C in
1460 extensions to the interpreter, however. It would require only a
1461 naming convention and a relatively small change to the
1462 interpreter to allow operator overloading for classes, so
1463 perhaps someday...
1464}
1465
1466\subsubsection{A Simple Example}
1467
1468Consider the following example, which defines a class {\tt Set}
1469representing a (finite) mathematical set with operations to add and
1470remove elements, a membership test, and a request for the size of the
1471set.
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001472\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001473class Set():
1474 def new(self):
1475 self.elements = []
1476 return self
1477 def add(self, e):
1478 if e not in self.elements:
1479 self.elements.append(e)
1480 def remove(self, e):
1481 if e in self.elements:
1482 for i in range(len(self.elements)):
1483 if self.elements[i] = e:
1484 del self.elements[i]
1485 break
1486 def is_element(self, e):
1487 return e in self.elements
1488 def size(self):
1489 return len(self.elements)
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001490\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001491Note that the class definition looks like a big compound statement,
1492with all the function definitons indented repective to the
1493{\tt class}
1494keyword.
1495
1496Let's assume that this
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001497{\em class\ definition}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001498is the only contents of the module file
1499{\tt SetClass.py}.
1500We can then use it in a \Python\ program as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001501\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001502>>> from SetClass import Set
1503>>> a = Set().new() # create a Set object
1504>>> a.add(2)
1505>>> a.add(3)
1506>>> a.add(1)
1507>>> a.add(1)
1508>>> if a.is_element(3): print '3 is in the set'
1509...
15103 is in the set
1511>>> if not a.is_element(4): print '4 is not in the set'
1512...
15134 is not in the set
1514>>> print 'a has', a.size(), 'elements'
1515a has 3 elements
1516>>> a.remove(1)
1517>>> print 'now a has', a.size(), 'elements'
1518>>>
1519now a has 2 elements
1520>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001521\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001522From the example we learn in the first place that the functions defined
1523in the class (e.g.,
1524{\tt add})
1525can be called using the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001526{\em member}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001527notation for the object
1528{\tt a}.
1529The member function is called with one less argument than it is defined:
1530the object is implicitly passed as the first argument.
1531Thus, the call
1532{\tt a.add(2)}
1533is equivalent to
1534{\tt Set.add(a, 2)}.
1535
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001536XXX This section is not complete yet!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001537
1538\section{XXX P.M.}
1539
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001540\begin{itemize}
1541\item The {\tt del} statement.
1542\item The {\tt dir()} function.
1543\item Tuples.
1544\item Dictionaries.
1545\item Objects and types in general.
1546\item Backquotes.
1547\item And/Or/Not.
1548\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001549
1550\end{document}