blob: 173ce08fc3ff091c2f2cd7b89a101f52b8bd538b [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)
Guido van Rossum67fa1601991-04-23 14:14:57 +000050gives a more formal definition of 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
Guido van Rossum67fa1601991-04-23 14:14:57 +0000155administrator.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000156
157The interpreter operates somewhat like the \UNIX\ shell: when called with
158standard input connected to a tty device, it reads and executes commands
159interactively; when called with a file name argument or with a file as
160standard input, it reads and executes a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000161{\em script}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000162from that file.%
163\footnote{
164 There is a difference between ``{\tt python file}'' and
165 ``{\tt python $<$file}''. In the latter case {\tt input()} and
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000166 {\tt raw\_input()} are satisfied from {\em file}, which has
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000167 already been read until the end by the parser, so they will read
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000168 EOF immediately. In the former case (which is usually what
169 you want) they are satisfied from whatever file or device is
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000170 connected to standard input of the \Python\ interpreter.
171}
172If available, the script name and additional arguments thereafter are
173passed to the script in the variable
174{\tt sys.argv},
175which is a list of strings.
176
177When standard input is a tty, the interpreter is said to be in
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000178{\em interactive\ mode}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000179In this mode it prompts for the next command with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000180{\em primary\ prompt},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000181usually three greater-than signs ({\tt >>>}); for continuation lines
182it prompts with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000183{\em secondary\ prompt},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000184by default three dots ({\tt ...}).
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000185Typing an EOF (Control-D) at the primary prompt causes the interpreter
186to exit with a zero exit status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000187
188When an error occurs in interactive mode, the interpreter prints a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000189message and a stack trace and returns to the primary prompt; with input
190from a file, it exits with a nonzero exit status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000191(Exceptions handled by an
192{\tt except}
193clause in a
194{\tt try}
195statement are not errors in this context.)
196Some errors are unconditionally fatal and cause an exit with a nonzero
197exit; this applies to internal inconsistencies and some cases of running
198out of memory.
199All error messages are written to the standard error stream; normal
200output from the executed commands is written to standard output.
201
202Typing an interrupt (normally Control-C or DEL) to the primary or
203secondary prompt cancels the input and returns to the primary prompt.
204Typing an interrupt while a command is being executed raises the
205{\tt KeyboardInterrupt}
206exception, which may be handled by a
207{\tt try}
208statement.
209
210When a module named
211{\tt foo}
212is imported, the interpreter searches for a file named
213{\tt foo.py}
214in a list of directories specified by the environment variable
215{\tt PYTHONPATH}.
216It has the same syntax as the \UNIX\ shell variable
217{\tt PATH},
218i.e., a list of colon-separated directory names.
219When
220{\tt PYTHONPATH}
221is not set, an installation-dependent default path is used, usually
222{\tt .:/usr/local/lib/python}.%
223\footnote{
224 Modules are really searched in the list of directories given by
225 the variable {\tt sys.path} which is initialized from
226 {\tt PYTHONPATH} or from the installation-dependent default.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000227 See the section on Standard Modules later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000228}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000229
230On BSD'ish \UNIX\ systems, \Python\ scripts can be made directly executable,
231like shell scripts, by putting the line
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000232\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000233#! /usr/local/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000234\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000235(assuming that's the name of the interpreter) at the beginning of the
236script and giving the file an executable mode.
237(The
238{\tt \#!}
239must be the first two characters of the file.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000240
241\subsection{Interactive Input Editing and History Substitution}
242
243Some versions of the \Python\ interpreter support editing of the current
244input line and history substitution, similar to facilities found in the
245Korn shell and the GNU Bash shell.
246This is implemented using the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000247{\em GNU\ Readline}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000248library, which supports Emacs-style and vi-style editing.
249This library has its own documentation which I won't duplicate here;
250however, the basics are easily explained.
251
252If supported,%
253\footnote{
254 Perhaps the quickest check to see whether command line editing
255 is supported is typing Control-P to the first \Python\ prompt
256 you get. If it beeps, you have command line editing.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000257 If not, you can skip the rest of this section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000258}
259input line editing is active whenever the interpreter prints a primary
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000260or secondary prompt.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000261The current line can be edited using the conventional Emacs control
262characters.
263The most important of these are:
264C-A (Control-A) moves the cursor to the beginning of the line, C-E to
265the end, C-B moves it one position to the left, C-F to the right.
266Backspace erases the character to the left of the cursor, C-D the
267character to its right.
268C-K kills (erases) the rest of the line to the right of the cursor, C-Y
269yanks back the last killed string.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000270C-underscore undoes the last change you made; it can be repeated for
271cumulative effect.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000272
273History substitution works as follows.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000274All non-empty input lines issued are saved in a history buffer,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000275and when a new prompt is given you are positioned on a new line at the
276bottom of this buffer.
277C-P moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000278Any line in the history buffer can be edited; an asterisk appears in
279front of the prompt to mark a line as modified.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000280Pressing the Return key passes the current line to the interpreter.
281C-R starts an incremental reverse search; C-S starts a forward search.
282
283The key bindings and some other parameters of the Readline library can
284be customized by placing commands in an initialization file called
285{\tt \$HOME/.initrc}.
286Key bindings have the form
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000287\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000288key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000289\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000290and options can be set with
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000291\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000292set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000293\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000294Example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000295\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000296# I prefer vi-style editing:
297set editing-mode vi
298# Edit using a single line:
299set horizontal-scroll-mode On
300# Rebind some keys:
301Meta-h: backward-kill-word
302Control-u: universal-argument
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000303\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000304Note that the default binding for TAB in \Python\ is to insert a TAB
305instead of Readline's default filename completion function.
306If you insist, you can override this by putting
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000307\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000308TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000309\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000310in your
311{\tt \$HOME/.inputrc}.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000312(Of course, this makes it hard to type indented continuation lines.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000313
314This facility is an enormous step forward compared to previous versions of
315the interpreter; however, some wishes are left:
316It would be nice if the proper indentation were suggested on
317continuation lines (the parser knows if an indent token is required
318next).
319The completion mechanism might use the interpreter's symbol table.
320A function to check (or even suggest) matching parentheses, quotes
321etc. would also be useful.
322
323\section{An Informal Introduction to Python}
324
325In the following examples, input and output are distinguished by the
326presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat the
327example, you must type everything after the prompt, when the prompt
328appears; everything on lines that do not begin with a prompt is output
329from the interpreter.
330Note that a secondary prompt on a line by itself in an example means you
331must type a blank line; this is used to end a multi-line command.
332
333\subsection{Using Python as a Calculator}
334
335Let's try some simple \Python\ commands.
336Start the interpreter and wait for the primary prompt,
337{\tt >>>}.
338The interpreter acts as a simple calculator: you can type an expression
339at it and it will write the value.
340Expression syntax is straightforward: the operators
341{\tt +},
342{\tt -},
343{\tt *}
344and
345{\tt /}
346work just as in most other languages (e.g., Pascal or C); parentheses
347can be used for grouping.
348For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000349\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000350>>> # This is a comment
351>>> 2+2
3524
353>>>
354>>> (50-5+5*6+25)/4
35525
356>>> # Division truncates towards zero:
357>>> 7/3
3582
359>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000360\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000361As in C, the equal sign ({\tt =}) is used to assign a value to a variable.
362The value of an assignment is not written:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000363\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000364>>> width = 20
365>>> height = 5*9
366>>> width * height
367900
368>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000369\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000370There is some support for floating point, but you can't mix floating
371point and integral numbers in expression (yet):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000372\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000373>>> 10.0 / 3.3
3743.0303030303
375>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000376\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000377Besides numbers, \Python\ can also manipulate strings, enclosed in single
378quotes:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000379\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000380>>> 'foo bar'
381'foo bar'
382>>> 'doesn\'t'
383'doesn\'t'
384>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000385\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000386Strings are written inside quotes and with quotes and other funny
387characters escaped by backslashes, to show the precise value.
388(There is also a way to write strings without quotes and escapes.)
389Strings can be concatenated (glued together) with the
390{\tt +}
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000391operator, and repeated with~{\tt *}:
392\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000393>>> word = 'Help' + 'A'
394>>> word
395'HelpA'
396>>> '<' + word*5 + '>'
397'<HelpAHelpAHelpAHelpAHelpA>'
398>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000399\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000400Strings can be subscripted; as in C, the first character of a string has
401subscript 0.
402There is no separate character type; a character is simply a string of
403size one.
404As in Icon, substrings can be specified with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000405{\em slice}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000406notation: two subscripts (indices) separated by a colon.
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000407\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000408>>> word[4]
409'A'
410>>> word[0:2]
411'He'
412>>> word[2:4]
413'lp'
414>>> # Slice indices have useful defaults:
415>>> word[:2] # Take first two characters
416'He'
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000417>>> word[2:] # Drop first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000418'lpA'
419>>> # A useful invariant: s[:i] + s[i:] = s
420>>> word[:3] + word[3:]
421'HelpA'
422>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000423\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000424Degenerate cases are handled gracefully: an index that is too large is
425replaced by the string size, an upper bound smaller than the lower bound
426returns an empty string.
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000427\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000428>>> word[1:100]
429'elpA'
430>>> word[10:]
431''
432>>> word[2:1]
433''
434>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000435\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000436Slice indices (but not simple subscripts) may be negative numbers, to
437start counting from the right.
438For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000439\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000440>>> word[-2:] # Take last two characters
441'pA'
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000442>>> word[:-2] # Drop last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000443'Hel'
444>>> # But -0 does not count from the right!
445>>> word[-0:] # (since -0 equals 0)
446'HelpA'
447>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000448\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000449The best way to remember how slices work is to think of the indices as
450pointing
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000451{\em between}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000452characters, with the left edge of the first character numbered 0.
453Then the right edge of the last character of a string of
454{\tt n}
455characters has index
456{\tt n},
457for example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000458\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000459 +---+---+---+---+---+
460 | H | e | l | p | A |
461 +---+---+---+---+---+
462 0 1 2 3 4 5
463-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000464\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000465The first row of numbers gives the position of the indices 0...5 in the
466string; the second row gives the corresponding negative indices.
467For nonnegative indices, the length of a slice is the difference of the
468indices, if both are within bounds,
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000469e.g.,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000470the length of
471{\tt word[1:3]}
472is 3--1 = 2.
473
474Finally, the built-in function {\tt len()} computes the length of a
475string:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000476\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000477>>> s = 'supercalifragilisticexpialidocious'
478>>> len(s)
47934
480>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000481\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000482\Python\ knows a number of
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000483{\em compound}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000484data types, used to group together other values.
485The most versatile is the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000486{\em list},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000487which can be written as a list of comma-separated values between square
488brackets:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000489\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000490>>> a = ['foo', 'bar', 100, 1234]
491>>> a
492['foo', 'bar', 100, 1234]
493>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000494\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000495As for strings, list subscripts start at 0:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000496\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000497>>> a[0]
498'foo'
499>>> a[3]
5001234
501>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000502\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000503Lists can be sliced and concatenated like strings:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000504\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000505>>> a[1:3]
506['bar', 100]
507>>> a[:2] + ['bletch', 2*2]
508['foo', 'bar', 'bletch', 4]
509>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000510\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000511Unlike strings, which are
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000512{\em immutable},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000513it is possible to change individual elements of a list:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000514\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000515>>> a
516['foo', 'bar', 100, 1234]
517>>> a[2] = a[2] + 23
518>>> a
519['foo', 'bar', 123, 1234]
520>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000521\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000522Assignment to slices is also possible, and this may even change the size
523of the list:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000524\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000525>>> # Replace some items:
526>>> a[0:2] = [1, 12]
527>>> a
528[1, 12, 123, 1234]
529>>> # Remove some:
530>>> a[0:2] = []
531>>> a
532[123, 1234]
533>>> # Insert some:
534>>> a[1:1] = ['bletch', 'xyzzy']
535>>> a
536[123, 'bletch', 'xyzzy', 1234]
537>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000538\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000539The built-in function {\tt len()} also applies to lists:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000540\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000541>>> len(a)
5424
543>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000544\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000545
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000546\subsection{Tuples and Sequences}
547
548XXX To Be Done.
549
550\subsection{First Steps Towards Programming}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000551
552Of course, we can use \Python\ for more complicated tasks than adding two
553and two together.
554For instance, we can write an initial subsequence of the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000555{\em Fibonacci}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000556series as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000557\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000558>>> # Fibonacci series:
559>>> # the sum of two elements defines the next
560>>> a, b = 0, 1
561>>> while b < 100:
562... print b
563... a, b = b, a+b
564...
5651
5661
5672
5683
5695
5708
57113
57221
57334
57455
57589
576>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000577\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000578This example introduces several new features.
579\begin{itemize}
580\item
581The first line contains a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000582{\em multiple\ assignment}:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000583the variables
584{\tt a}
585and
586{\tt b}
587simultaneously get the new values 0 and 1.
588On the last line this is used again, demonstrating that the expressions
589on the right-hand side are all evaluated first before any of the
590assignments take place.
591\item
592The
593{\tt while}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000594loop executes as long as the condition (here: $b < 100$) remains true.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000595In \Python, as in C, any non-zero integer value is true; zero is false.
596The condition may also be a string or list value, in fact any sequence;
597anything with a non-zero length is true, empty sequences are false.
598The test used in the example is a simple comparison.
599The standard comparison operators are written as
600{\tt <},
601{\tt >},
602{\tt =},
603{\tt <=},
604{\tt >=}
605and
606{\tt <>}.%
607\footnote{
608 The ambiguity of using {\tt =}
609 for both assignment and equality is resolved by disallowing
610 unparenthesized conditions at the right hand side of assignments.
611}
612\item
613The
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000614{\em body}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000615of the loop is
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000616{\em indented}: indentation is \Python's way of grouping statements.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000617\Python\ does not (yet!) provide an intelligent input line editing
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000618facility, so you have to type a tab or space(s) for each indented line.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000619In practice you will prepare more complicated input for \Python\ with a
620text editor; most text editors have an auto-indent facility.
621When a compound statement is entered interactively, it must be
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000622followed by a blank line to indicate completion (since the parser
623cannot guess when you have typed the last line).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000624\item
625The
626{\tt print}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000627statement writes the value of the expression(s) it is given.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000628It differs from just writing the expression you want to write (as we did
629earlier in the calculator examples) in the way it handles multiple
630expressions and strings.
631Strings are written without quotes and a space is inserted between
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000632items, so you can format things nicely, like this:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000633\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000634>>> i = 256*256
635>>> print 'The value of i is', i
636The value of i is 65536
637>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000638\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000639A trailing comma avoids the newline after the output:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000640\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000641>>> a, b = 0, 1
642>>> while b < 1000:
643... print b,
644... a, b = b, a+b
645...
6461 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
647>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000648\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000649Note that the interpreter inserts a newline before it prints the next
650prompt if the last line was not completed.
651\end{itemize}
652
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000653\subsection{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000654
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000655Besides the {\tt while} statement just introduced, \Python\ knows the
656usual control flow statements known from other languages, with some
657twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000658
659\subsubsection{If Statements}
660
661Perhaps the most well-known statement type is the {\tt if} statement.
662For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000663\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000664>>> if x < 0:
665... x = 0
666... print 'Negative changed to zero'
667... elif x = 0:
668... print 'Zero'
669... elif x = 1:
670... print 'Single'
671... else:
672... print 'More'
673...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000674\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000675There can be zero or more {\tt elif} parts, and the {\tt else} part is
676optional.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000677The keyword `{\tt elif}' is short for `{\tt else if}', and is useful to
678avoid excessive indentation.
679An {\tt if...elif...elif...} sequence is a substitute for the
680{\em switch} or {\em case} statements found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000681
682\subsubsection{For Statements}
683
684The {\tt for} statement in \Python\ differs a bit from what you may be
685used to in C or Pascal.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000686Rather than always iterating over an arithmetic progression of numbers
687(as Pascal), or leaving the user completely free in the iteration test
688and step (as C), \Python's {\tt for} statement iterates over the items
689of any sequence (e.g., a list or a string).
690For example (no pun intended):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000691\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000692>>> # Measure some strings:
693>>> a = ['cat', 'window', 'defenestrate']
694>>> for x in a:
695... print x, len(x)
696...
697cat 3
698window 6
699defenestrate 12
700>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000701\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000702
703\subsubsection{The {\tt range()} Function}
704
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000705If you do need to iterate over a sequence of numbers, the built-in
706function {\tt range()} comes in handy.
707It generates lists containing arithmetic progressions,
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000708e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000709\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000710>>> range(10)
711[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
712>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000713\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000714The given end point is never part of the generated list;
715{\tt range(10)} generates a list of 10 values,
716exactly the legal indices for items of a sequence of length 10.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000717It is possible to let the range start at another number, or to specify a
718different increment (even negative):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000719\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000720>>> range(5, 10)
721[5, 6, 7, 8, 9]
722>>> range(0, 10, 3)
723[0, 3, 6, 9]
724>>> range(-10, -100, -30)
725[-10, -40, -70]
726>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000727\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000728To iterate over the indices of a sequence, combine {\tt range()}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000729and {\tt len()} as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000730\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000731>>> a = ['Mary', 'had', 'a', 'little', 'boy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000732>>> for i in range(len(a)):
733... print i, a[i]
734...
7350 Mary
7361 had
7372 a
7383 little
Guido van Rossum2292b8e1991-01-23 16:31:24 +00007394 boy
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000740>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000741\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000742
743\subsubsection{Break Statements and Else Clauses on Loops}
744
745The {\tt break} statement breaks out of the smallest enclosing {\tt for}
746or {\tt while} loop.
747Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000748loop terminates through exhaustion of the list (with {\tt for}) or when
749the condition becomes false (with {\tt while}) but not when the loop is
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000750terminated by a {\tt break} statement.
751This is exemplified by the following loop, which searches for a list
752item of value 0:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000753\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000754>>> for n in range(2, 10):
755... for x in range(2, n):
756... if n % x = 0:
757... print n, 'equals', x, '*', n/x
758... break
759... else:
760... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000761...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00007622 is a prime number
7633 is a prime number
7644 equals 2 * 2
7655 is a prime number
7666 equals 2 * 3
7677 is a prime number
7688 equals 2 * 4
7699 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000770>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000771\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000772
773\subsubsection{Pass Statements}
774
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000775The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000776It can be used when a statement is required syntactically but the
777program requires no action.
778For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000779\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000780>>> while 1:
781... pass # Busy-wait for keyboard interrupt
782...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000783\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000784
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000785\subsubsection{Conditions Revisited}
786
787XXX To Be Done.
788
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000789\subsection{Defining Functions}
790
791We can create a function that writes the Fibonacci series to an
792arbitrary boundary:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000793\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000794>>> def fib(n): # write Fibonacci series up to n
795... a, b = 0, 1
796... while b <= n:
797... print b,
798... a, b = b, a+b
799...
800>>> # Now call the function we just defined:
801>>> fib(2000)
8021 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
803>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000804\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000805The keyword
806{\tt def}
807introduces a function
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000808{\em definition}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000809It must be followed by the function name and the parenthesized list of
810formal parameters.
811The statements that form the body of the function starts at the next
812line, indented by a tab stop.
813The
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000814{\em execution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000815of a function introduces a new symbol table used for the local variables
816of the function.
817More precisely, all variable assignments in a function store the value
818in the local symbol table; variable references first look in the local
819symbol table, then in the global symbol table, and then in the table of
820built-in names.
821Thus, the global symbol table is
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000822{\em read-only}
823within a function.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000824The actual parameters (arguments) to a function call are introduced in
825the local symbol table of the called function when it is called;
826thus, arguments are passed using
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000827{\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000828\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000829 Actually, {\em call by object reference} would be a better
830 description, since if a mutable object is passed, the caller
831 will see any changes the callee makes to it (e.g., items
832 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000833}
834When a function calls another function, a new local symbol table is
835created for that call.
836
837A function definition introduces the function name in the global symbol
838table.
839The value has a type that is recognized by the interpreter as a
840user-defined function.
841This value can be assigned to another name which can then also be used
842as a function.
843This serves as a general renaming mechanism:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000844\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000845>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000846<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000847>>> f = fib
848>>> f(100)
8491 1 2 3 5 8 13 21 34 55 89
850>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000851\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000852You might object that
853{\tt fib}
854is not a function but a procedure.
855In \Python, as in C, procedures are just functions that don't return a
856value.
857In fact, technically speaking, procedures do return a value, albeit a
858rather boring one.
859This value is called {\tt None} (it's a built-in name).
860Writing the value {\tt None} is normally suppressed by the interpreter
861if it would be the only value written.
862You can see it if you really want to:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000863\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000864>>> print fib(0)
865None
866>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000867\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000868It is simple to write a function that returns a list of the numbers of
869the Fibonacci series, instead of printing it:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000870\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000871>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000872... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000873... a, b = 0, 1
874... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000875... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000876... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000877... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000878...
879>>> f100 = fib2(100) # call it
880>>> f100 # write the result
881[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
882>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000883\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000884This example, as usual, demonstrates some new \Python\ features:
885\begin{itemize}
886\item
887The
888{\tt return}
889statement returns with a value from a function.
890{\tt return}
891without an expression argument is used to return from the middle of a
892procedure (falling off the end also returns from a proceduce).
893\item
894The statement
895{\tt ret.append(b)}
896calls a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000897{\em method}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000898of the list object
899{\tt ret}.
900A method is a function that `belongs' to an object and is named
901{\tt obj.methodname},
902where
903{\tt obj}
904is some object (this may be an expression), and
905{\tt methodname}
906is the name of a method that is defined by the object's type.
907Different types define different methods.
908Methods of different types may have the same name without causing
909ambiguity.
910See the section on classes, later, to find out how you can define your
911own object types and methods.
912The method
913{\tt append}
914shown in the example, is defined for list objects; it adds a new element
915at the end of the list.
916In this case it is equivalent to
917{\tt ret = ret + [b]},
918but more efficient.%
919\footnote{
920 There is a subtle semantic difference if the object
921 is referenced from more than one place.
922}
923\end{itemize}
924The list object type has two more methods:
Guido van Rossum7d9f8d71991-01-22 11:45:00 +0000925\begin{description}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000926\item[{\tt insert(i, x)}]
927Inserts an item at a given position.
928The first argument is the index of the element before which to insert,
929so {\tt a.insert(0, x)} inserts at the front of the list, and
930{\tt a.insert(len(a), x)} is equivalent to {\tt a.append(x)}.
931\item[{\tt sort()}]
932Sorts the elements of the list.
Guido van Rossum7d9f8d71991-01-22 11:45:00 +0000933\end{description}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000934For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000935\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000936>>> a = [10, 100, 1, 1000]
937>>> a.insert(2, -1)
938>>> a
939[10, 100, -1, 1, 1000]
940>>> a.sort()
941>>> a
942[-1, 1, 10, 100, 1000]
943>>> # Strings are sorted according to ASCII:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000944>>> b = ['Mary', 'had', 'a', 'little', 'boy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000945>>> b.sort()
946>>> b
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000947['Mary', 'a', 'boy', 'had', 'little']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000948>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000949\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000950
951\subsection{Modules}
952
953If you quit from the \Python\ interpreter and enter it again, the
954definitions you have made (functions and variables) are lost.
955Therefore, if you want to write a somewhat longer program, you are
956better off using a text editor to prepare the input for the interpreter
957and run it with that file as input instead.
958This is known as creating a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000959{\em script}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000960As your program gets longer, you may want to split it into several files
961for easier maintenance.
962You may also want to use a handy function that you've written in several
963programs without copying its definition into each program.
964To support this, \Python\ has a way to put definitions in a file and use
965them in a script or in an interactive instance of the interpreter.
966Such a file is called a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000967{\em module};
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000968definitions from a module can be
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000969{\em imported}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000970into other modules or into the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000971{\em main}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000972module (the collection of variables that you have access to in
973a script and in calculator mode).
974
975A module is a file containing \Python\ definitions and statements.
976The file name is the module name with the suffix
977{\tt .py}
978appended.
979For instance, use your favorite text editor to create a file called
980{\tt fibo.py}
981in the current directory with the following contents:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000982\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000983# Fibonacci numbers module
984
985def fib(n): # write Fibonacci series up to n
986 a, b = 0, 1
987 while b <= n:
988 print b,
989 a, b = b, a+b
990
991def fib2(n): # return Fibonacci series up to n
992 ret = []
993 a, b = 0, 1
994 while b <= n:
995 ret.append(b)
996 a, b = b, a+b
997 return ret
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000998\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000999Now enter the \Python\ interpreter and import this module with the
1000following command:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001001\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001002>>> import fibo
1003>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001004\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001005This does not enter the names of the functions defined in
1006{\tt fibo}
1007directly in the symbol table; it only enters the module name
1008{\tt fibo}
1009there.
1010Using the module name you can access the functions:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001011\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001012>>> fibo.fib(1000)
10131 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1014>>> fibo.fib2(100)
1015[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1016>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001017\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001018If you intend to use a function often you can assign it to a local name:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001019\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001020>>> fib = fibo.fib
1021>>> fib(500)
10221 1 2 3 5 8 13 21 34 55 89 144 233 377
1023>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001024\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001025
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001026\subsubsection{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001027
1028A module can contain executable statements as well as function
1029definitions.
1030These statements are intended to initialize the module.
1031They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001032{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001033time the module is imported somewhere.%
1034\footnote{
1035 In fact function definitions are also `statements' that are
1036 `executed'; the execution enters the function name in the
1037 module's global symbol table.
1038}
1039
1040Each module has its own private symbol table, which is used as the
1041global symbol table by all functions defined in the module.
1042Thus, the author of a module can use global variables in the module
1043without worrying about accidental clashes with a user's global
1044variables.
1045On the other hand, if you know what you are doing you can touch a
1046module's global variables with the same notation used to refer to its
1047functions,
1048{\tt modname.itemname}.
1049
1050Modules can import other modules.
1051It is customary but not required to place all
1052{\tt import}
1053statements at the beginning of a module (or script, for that matter).
1054The imported module names are placed in the importing module's global
1055symbol table.
1056
1057There is a variant of the
1058{\tt import}
1059statement that imports names from a module directly into the importing
1060module's symbol table.
1061For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001062\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001063>>> from fibo import fib, fib2
1064>>> fib(500)
10651 1 2 3 5 8 13 21 34 55 89 144 233 377
1066>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001067\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001068This does not introduce the module name from which the imports are taken
1069in the local symbol table (so in the example, {\tt fibo} is not
1070defined).
1071
1072There is even a variant to import all names that a module defines:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001073\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001074>>> from fibo import *
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 imports all names except those beginning with an underscore
1080({\tt \_}).
1081
1082\subsubsection{Standard Modules}
1083
1084\Python\ comes with a library of standard modules, described in a separate
1085document (Python Library and Module Reference).
1086Some modules are built into the interpreter; these provide access to
1087operations that are not part of the core of the language but are
1088nevertheless built in, either for efficiency or to provide access to
1089operating system primitives such as system calls.
1090The set of such modules is a configuration option; e.g., the
1091{\tt amoeba}
1092module is only provided on systems that somehow support Amoeba
1093primitives.
1094One particular module deserves some attention:
1095{\tt sys},
1096which is built into every \Python\ interpreter.
1097The variables
1098{\tt sys.ps1}
1099and
1100{\tt sys.ps2}
1101define the strings used as primary and secondary prompts:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001102\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001103>>> import sys
1104>>> sys.ps1
1105'>>> '
1106>>> sys.ps2
1107'... '
1108>>> sys.ps1 = 'C> '
1109C> print 'Yuck!'
1110Yuck!
1111C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001112\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001113These two variables are only defined if the interpreter is in
1114interactive mode.
1115
1116The variable
1117{\tt sys.path}
1118is a list of strings that determine the interpreter's search path for
1119modules.
1120It is initialized to a default path taken from the environment variable
1121{\tt PYTHONPATH},
1122or from a built-in default if
1123{\tt PYTHONPATH}
1124is not set.
1125You can modify it using standard list operations, e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001126\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001127>>> import sys
1128>>> sys.path.append('/ufs/guido/lib/python')
1129>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001130\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001131
1132\subsection{Errors and Exceptions}
1133
1134Until now error messages haven't yet been mentioned, but if you have
1135tried out the examples you have probably seen some.
1136There are (at least) two distinguishable kinds of errors:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001137{\em syntax\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001138and
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001139{\em exceptions}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001140
1141\subsubsection{Syntax Errors}
1142
1143Syntax errors, also known as parsing errors, are perhaps the most common
1144kind of complaint you get while you are still learning \Python:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001145\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001146>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001147Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001148while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001149 ^
1150Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001151>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001152\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001153The parser repeats the offending line and displays a little `arrow'
1154pointing at the earliest point in the line where the error was detected.
1155The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001156{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001157the arrow: in the example, the error is detected at the keyword
1158{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001159File name and line number are printed so you know where to look in case
1160the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001161
1162\subsubsection{Exceptions}
1163
1164Even if a statement or expression is syntactically correct, it may cause
1165an error when an attempt is made to execute it:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001166\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001167>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001168Unhandled exception: run-time error: integer division by zero
1169Stack backtrace (innermost last):
1170 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001171>>> 4 + foo*3
1172Unhandled exception: undefined name: foo
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001173Stack backtrace (innermost last):
1174 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001175>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001176Unhandled exception: type error: illegal argument type for built-in operation
1177Stack backtrace (innermost last):
1178 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001179>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001180\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001181Errors detected during execution are called
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001182{\em exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001183and are not unconditionally fatal: you will soon learn how to handle
1184them in \Python\ programs.
1185Most exceptions are not handled by programs, however, and result
1186in error messages as shown here.
1187
1188The first line of the error message indicates what happened.
1189Exceptions come in different types, and the type is printed as part of
1190the message: the types in the example are
1191{\tt run-time error},
1192{\tt undefined name}
1193and
1194{\tt type error}.
1195The rest of the line is a detail whose interpretation depends on the
1196exception type.
1197
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001198The rest of the error message shows the context where the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001199exception happened.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001200In general it contains a stack backtrace listing source lines; however,
1201it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001202
1203Here is a summary of the most common exceptions:
1204\begin{itemize}
1205\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001206{\em Run-time\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001207are generally caused by wrong data used by the program; this can be the
1208programmer's fault or caused by bad input.
1209The detail states the cause of the error in more detail.
1210\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001211{\em Undefined\ name}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001212errors are more serious: these are usually caused by misspelled
1213identifiers.%
1214\footnote{
1215 The parser does not check whether names used in a program are at
1216 all defined elsewhere in the program, so such checks are
1217 postponed until run-time. The same holds for type checking.
1218}
1219The detail is the offending identifier.
1220\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001221{\em Type\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001222are also pretty serious: this is another case of using wrong data (or
1223better, using data the wrong way), but here the error can be glanced
1224from the object type(s) alone.
1225The detail shows in what context the error was detected.
1226\end{itemize}
1227
1228\subsubsection{Handling Exceptions}
1229
1230It is possible to write programs that handle selected exceptions.
1231Look at the following example, which prints a table of inverses of
1232some floating point numbers:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001233\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001234>>> numbers = [0.3333, 2.5, 0.0, 10.0]
1235>>> for x in numbers:
1236... print x,
1237... try:
1238... print 1.0 / x
1239... except RuntimeError:
1240... print '*** has no inverse ***'
1241...
12420.3333 3.00030003
12432.5 0.4
12440 *** has no inverse ***
124510 0.1
1246>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001247\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001248The {\tt try} statement works as follows.
1249\begin{itemize}
1250\item
1251First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001252{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001253(the statement(s) between the {\tt try} and {\tt except} keywords) is
1254executed.
1255\item
1256If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001257{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001258is skipped and execution of the {\tt try} statement is finished.
1259\item
1260If an exception occurs during execution of the try clause, and its
1261type matches the exception named after the {\tt except} keyword, the
1262rest of the try clause is skipped, the except clause is executed, and
1263then execution continues after the {\tt try} statement.
1264\item
1265If an exception occurs which does not match the exception named in the
1266except clause, it is passed on to outer try statements; if no handler is
1267found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001268{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001269and execution stops with a message as shown above.
1270\end{itemize}
1271A {\tt try} statement may have more than one except clause, to specify
1272handlers for different exceptions.
1273At most one handler will be executed.
1274Handlers only handle exceptions that occur in the corresponding try
1275clause, not in other handlers of the same {\tt try} statement.
1276An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001277e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001278\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001279... except (RuntimeError, TypeError, NameError):
1280... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001281\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001282The last except clause may omit the exception name(s), to serve as a
1283wildcard.
1284Use this with extreme caution!
1285
1286When an exception occurs, it may have an associated value, also known as
1287the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001288{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001289The presence and type of the argument depend on the exception type.
1290For exception types which have an argument, the except clause may
1291specify a variable after the exception name (or list) to receive the
1292argument's value, as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001293\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001294>>> try:
1295... foo()
1296... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001297... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001298...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001299name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001300>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001301\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001302If an exception has an argument, it is printed as the third part
1303(`detail') of the message for unhandled exceptions.
1304
1305Standard exception names are built-in identifiers (not reserved
1306keywords).
1307These are in fact string objects whose
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001308{\em object\ identity}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001309(not their value!) identifies the exceptions.%
1310\footnote{
1311 There should really be a separate exception type; it is pure
1312 laziness that exceptions are identified by strings, and this may
1313 be fixed in the future.
1314}
1315The string is printed as the second part of the message for unhandled
1316exceptions.
1317Their names and values are:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001318\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001319EOFError 'end-of-file read'
1320KeyboardInterrupt 'keyboard interrupt'
1321MemoryError 'out of memory' *
1322NameError 'undefined name' *
1323RuntimeError 'run-time error' *
1324SystemError 'system error' *
1325TypeError 'type error' *
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001326\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001327The meanings should be clear enough.
1328Those exceptions with a {\tt *} in the third column have an argument.
1329
1330Exception handlers don't just handle exceptions if they occur
1331immediately in the try clause, but also if they occur inside functions
1332that are called (even indirectly) in the try clause.
1333For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001334\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001335>>> def this_fails():
1336... x = 1/0
1337...
1338>>> try:
1339... this_fails()
1340... except RuntimeError, detail:
1341... print 'Handling run-time error:', detail
1342...
Guido van Rossum67fa1601991-04-23 14:14:57 +00001343Handling run-time error: integer division by zero
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001344>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001345\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001346
1347\subsubsection{Raising Exceptions}
1348
1349The {\tt raise} statement allows the programmer to force a specified
1350exception to occur.
1351For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001352\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001353>>> raise NameError, 'Hi There!'
1354Unhandled exception: undefined name: Hi There!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001355Stack backtrace (innermost last):
1356 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001357>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001358\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001359The first argument to {\tt raise} names the exception to be raised.
1360The optional second argument specifies the exception's argument.
1361
1362\subsubsection{User-defined Exceptions}
1363
1364Programs may name their own exceptions by assigning a string to a
1365variable.
1366For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001367\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001368>>> my_exc = 'nobody likes me!'
1369>>> try:
1370... raise my_exc, 2*2
1371... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001372... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001373...
1374My exception occured, value: 4
1375>>> raise my_exc, 1
1376Unhandled exception: nobody likes me!: 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001377Stack backtrace (innermost last):
1378 File "<stdin>", line 7
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001379>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001380\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001381Many standard modules use this to report errors that may occur in
1382functions they define.
1383
1384\subsubsection{Defining Clean-up Actions}
1385
1386The {\tt try} statement has another optional clause which is intended to
1387define clean-up actions that must be executed under all circumstances.
1388For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001389\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001390>>> try:
1391... raise KeyboardInterrupt
1392... finally:
1393... print 'Goodbye, world!'
1394...
1395Goodbye, world!
1396Unhandled exception: keyboard interrupt
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001397Stack backtrace (innermost last):
1398 File "<stdin>", line 2
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001399>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001400\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001401The
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001402{\em finally\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001403must follow the except clauses(s), if any.
1404It is executed whether or not an exception occurred.
1405If the exception is handled, the finally clause is executed after the
1406handler (and even if another exception occurred in the handler).
1407It is also executed when the {\tt try} statement is left via a
1408{\tt break} or {\tt return} statement.
1409
1410\subsection{Classes}
1411
1412Classes in \Python\ make it possible to play the game of encapsulation in a
1413somewhat different way than it is played with modules.
1414Classes are an advanced topic and are probably best skipped on the first
1415encounter with \Python.
1416
1417\subsubsection{Prologue}
1418
1419\Python's class mechanism is not particularly elegant, but quite powerful.
1420It is a mixture of the class mechanisms found in C++ and Modula-3.
1421As is true for modules, classes in \Python\ do not put an absolute barrier
1422between definition and user, but rather rely on the politeness of the
1423user not to ``break into the definition.''
1424The most important features of classes are retained with full power,
1425however: the class inheritance mechanism allows multiple base classes,
1426a derived class can override any method of its base class(es), a method
1427can call the method of a base class with the same name.
1428Objects can contain an arbitrary amount of private data.
1429
1430In C++ terminology, all class members (including data members) are
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001431{\em public},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001432and all member functions (methods) are
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001433{\em virtual}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001434There are no special constructors or destructors.
1435As in Modula-3, there are no shorthands for referencing the object's
1436members from its methods: the method function is declared with an
1437explicit first argument representing the object, which is provided
1438implicitly by the call.
1439As in Smalltalk, classes themselves are objects, albeit in the wider
1440sense of the word: in \Python, all data types are objects.
1441This provides semantics for renaming or aliasing.
1442But, just like in C++ or Modula-3, the built-in types cannot be used as
1443base classes for extension by the user.
1444Also, like Modula-3 but unlike C++, the built-in operators with special
1445syntax (arithmetic operators, subscripting etc.) cannot be redefined for
1446class members.%
1447\footnote{
1448 They can be redefined for new object types implemented in C in
1449 extensions to the interpreter, however. It would require only a
1450 naming convention and a relatively small change to the
1451 interpreter to allow operator overloading for classes, so
1452 perhaps someday...
1453}
1454
1455\subsubsection{A Simple Example}
1456
1457Consider the following example, which defines a class {\tt Set}
1458representing a (finite) mathematical set with operations to add and
1459remove elements, a membership test, and a request for the size of the
1460set.
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001461\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001462class Set():
1463 def new(self):
1464 self.elements = []
1465 return self
1466 def add(self, e):
1467 if e not in self.elements:
1468 self.elements.append(e)
1469 def remove(self, e):
1470 if e in self.elements:
1471 for i in range(len(self.elements)):
1472 if self.elements[i] = e:
1473 del self.elements[i]
1474 break
1475 def is_element(self, e):
1476 return e in self.elements
1477 def size(self):
1478 return len(self.elements)
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001479\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001480Note that the class definition looks like a big compound statement,
1481with all the function definitons indented repective to the
1482{\tt class}
1483keyword.
1484
1485Let's assume that this
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001486{\em class\ definition}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001487is the only contents of the module file
1488{\tt SetClass.py}.
1489We can then use it in a \Python\ program as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001490\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001491>>> from SetClass import Set
1492>>> a = Set().new() # create a Set object
1493>>> a.add(2)
1494>>> a.add(3)
1495>>> a.add(1)
1496>>> a.add(1)
1497>>> if a.is_element(3): print '3 is in the set'
1498...
14993 is in the set
1500>>> if not a.is_element(4): print '4 is not in the set'
1501...
15024 is not in the set
1503>>> print 'a has', a.size(), 'elements'
1504a has 3 elements
1505>>> a.remove(1)
1506>>> print 'now a has', a.size(), 'elements'
1507>>>
1508now a has 2 elements
1509>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001510\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001511From the example we learn in the first place that the functions defined
1512in the class (e.g.,
1513{\tt add})
1514can be called using the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001515{\em member}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001516notation for the object
1517{\tt a}.
1518The member function is called with one less argument than it is defined:
1519the object is implicitly passed as the first argument.
1520Thus, the call
1521{\tt a.add(2)}
1522is equivalent to
1523{\tt Set.add(a, 2)}.
1524
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001525XXX This section is not complete yet!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001526
1527\section{XXX P.M.}
1528
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001529\begin{itemize}
1530\item The {\tt del} statement.
1531\item The {\tt dir()} function.
1532\item Tuples.
1533\item Dictionaries.
1534\item Objects and types in general.
1535\item Backquotes.
1536\item And/Or/Not.
1537\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001538
1539\end{document}