blob: 6a4767d349404a41a13a933403c19af56adc12d7 [file] [log] [blame]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001% Format this file with latex.
2
Guido van Rossum4410c751991-06-04 20:22:18 +00003%\documentstyle[11pt,myformat]{article}
4\documentstyle[palatino,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
Guido van Rossum4410c751991-06-04 20:22:18 +000027Python is a simple, yet powerful programming language that bridges the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000028gap 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
Guido van Rossum4410c751991-06-04 20:22:18 +000033The Python interpreter is easily extended with new functions and data
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000034types implemented in C.
Guido van Rossum4410c751991-06-04 20:22:18 +000035Python is also suitable as an extension language for highly
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000036customizable C applications such as editors or window managers.
37
Guido van Rossum4410c751991-06-04 20:22:18 +000038Python is available for various operating systems, amongst which
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000039several flavors of \UNIX, Amoeba, and the Apple Macintosh O.S.
40
41This tutorial introduces the reader informally to the basic concepts and
Guido van Rossum4410c751991-06-04 20:22:18 +000042features of the Python language and system.
43It helps to have a Python interpreter handy for hands-on experience,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000044but 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
Guido van Rossum4410c751991-06-04 20:22:18 +000074In such cases, Python may be just the language for you.
75Python is simple to use, but it is a real programming language, offering
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000076much 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.
Guido van Rossum4410c751991-06-04 20:22:18 +000082Because of its more general data types Python is applicable to a
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000083much 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 Rossum4410c751991-06-04 20:22:18 +000087yet most simple things are at least as easy in Python as in those
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000088languages.
89
Guido van Rossum4410c751991-06-04 20:22:18 +000090Python allows you to split up your program in modules that can be reused
91in other Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000092It 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
Guido van Rossum4410c751991-06-04 20:22:18 +000094program in Python.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000095There are also built-in modules that provide things like file I/O,
96system calls, and even a generic interface to window systems (STDWIN).
97
Guido van Rossum4410c751991-06-04 20:22:18 +000098Python is an interpreted language, which saves you considerable time
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000099during 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
Guido van Rossum4410c751991-06-04 20:22:18 +0000106Python allows writing very compact and readable programs.
107Programs written in Python are typically much shorter than equivalent C
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000108programs:
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
Guido van Rossum4410c751991-06-04 20:22:18 +0000114Python 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
Guido van Rossum4410c751991-06-04 20:22:18 +0000118speed, or to link Python programs to libraries that may be only available
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000119in binary form (such as a vendor-specific graphics library).
Guido van Rossum4410c751991-06-04 20:22:18 +0000120Once you are really hooked, you can link the Python interpreter into an
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000121application written in C and use it as an extension or command language.
122
123\subsection{Where From Here}
124
Guido van Rossum4410c751991-06-04 20:22:18 +0000125Now that you are all excited about Python, you'll want to examine it in
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000126some 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.
Guido van Rossum4410c751991-06-04 20:22:18 +0000134The rest of the tutorial introduces various features of the Python
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000135language 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
Guido van Rossum4410c751991-06-04 20:22:18 +0000141The Python interpreter is usually installed as
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000142{\tt /usr/local/python}
143on those machines where it is available; putting
144{\tt /usr/local}
Guido van Rossum4410c751991-06-04 20:22:18 +0000145in your {\UNIX} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000146typing 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}
Guido van Rossum4410c751991-06-04 20:22:18 +0000154are 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
Guido van Rossum4410c751991-06-04 20:22:18 +0000157The interpreter operates somewhat like the {\UNIX} shell: when called with
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000158standard 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 Rossum4410c751991-06-04 20:22:18 +0000170 connected to standard input of the Python interpreter.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000171}
Guido van Rossumdd010801991-06-07 14:31:11 +0000172A third possibility is ``{\tt python -c command [arg] ...}'', which
173executes the statement(s) in {\tt command}, in analogy of the shell's
174{\tt -c} option.
175When available, the script name and additional arguments thereafter are
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000176passed to the script in the variable
177{\tt sys.argv},
178which is a list of strings.
Guido van Rossumdd010801991-06-07 14:31:11 +0000179When {\tt -c command} is used, {\tt sys.argv} is set to {\tt '-c'}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000180
Guido van Rossumdd010801991-06-07 14:31:11 +0000181When commands are read from a tty, the interpreter is said to be in
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000182{\em interactive\ mode}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000183In this mode it prompts for the next command with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000184{\em primary\ prompt},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000185usually three greater-than signs ({\tt >>>}); for continuation lines
186it prompts with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000187{\em secondary\ prompt},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000188by default three dots ({\tt ...}).
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000189Typing an EOF (Control-D) at the primary prompt causes the interpreter
190to exit with a zero exit status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000191
192When an error occurs in interactive mode, the interpreter prints a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000193message and a stack trace and returns to the primary prompt; with input
194from a file, it exits with a nonzero exit status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000195(Exceptions handled by an
196{\tt except}
197clause in a
198{\tt try}
199statement are not errors in this context.)
200Some errors are unconditionally fatal and cause an exit with a nonzero
201exit; this applies to internal inconsistencies and some cases of running
202out of memory.
203All error messages are written to the standard error stream; normal
204output from the executed commands is written to standard output.
205
206Typing an interrupt (normally Control-C or DEL) to the primary or
207secondary prompt cancels the input and returns to the primary prompt.
208Typing an interrupt while a command is being executed raises the
209{\tt KeyboardInterrupt}
210exception, which may be handled by a
211{\tt try}
212statement.
213
214When a module named
215{\tt foo}
216is imported, the interpreter searches for a file named
217{\tt foo.py}
218in a list of directories specified by the environment variable
219{\tt PYTHONPATH}.
Guido van Rossum4410c751991-06-04 20:22:18 +0000220It has the same syntax as the {\UNIX} shell variable
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000221{\tt PATH},
222i.e., a list of colon-separated directory names.
223When
224{\tt PYTHONPATH}
225is not set, an installation-dependent default path is used, usually
226{\tt .:/usr/local/lib/python}.%
227\footnote{
228 Modules are really searched in the list of directories given by
229 the variable {\tt sys.path} which is initialized from
230 {\tt PYTHONPATH} or from the installation-dependent default.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000231 See the section on Standard Modules later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000232}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000233
Guido van Rossum4410c751991-06-04 20:22:18 +0000234As an important speed-up of the start-up time of short programs, if a
235file called {\tt foo.pyc} exists in the directory where {\tt foo.py}
236is found, this is assumed to contain an already-``compiled'' version
237of the module {\tt foo}. The last modification time of {\tt foo.py}
238is recorded in {\tt foo.pyc}, and if these don't match, {\tt foo.pyc}
239is ignored. Whenever {\tt foo.py} is successfully compiled, an
240attempt is made to write the compiled version to {\tt foo.pyc}.
241
242On BSD'ish {\UNIX} systems, Python scripts can be made directly executable,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000243like shell scripts, by putting the line
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000244\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000245#! /usr/local/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000246\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000247(assuming that's the name of the interpreter) at the beginning of the
248script and giving the file an executable mode.
249(The
250{\tt \#!}
251must be the first two characters of the file.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000252
253\subsection{Interactive Input Editing and History Substitution}
254
Guido van Rossum4410c751991-06-04 20:22:18 +0000255Some versions of the Python interpreter support editing of the current
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000256input line and history substitution, similar to facilities found in the
257Korn shell and the GNU Bash shell.
258This is implemented using the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000259{\em GNU\ Readline}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000260library, which supports Emacs-style and vi-style editing.
261This library has its own documentation which I won't duplicate here;
262however, the basics are easily explained.
263
264If supported,%
265\footnote{
266 Perhaps the quickest check to see whether command line editing
Guido van Rossum4410c751991-06-04 20:22:18 +0000267 is supported is typing Control-P to the first Python prompt
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000268 you get. If it beeps, you have command line editing.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000269 If not, you can skip the rest of this section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000270}
271input line editing is active whenever the interpreter prints a primary
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000272or secondary prompt.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000273The current line can be edited using the conventional Emacs control
274characters.
275The most important of these are:
276C-A (Control-A) moves the cursor to the beginning of the line, C-E to
277the end, C-B moves it one position to the left, C-F to the right.
278Backspace erases the character to the left of the cursor, C-D the
279character to its right.
280C-K kills (erases) the rest of the line to the right of the cursor, C-Y
281yanks back the last killed string.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000282C-underscore undoes the last change you made; it can be repeated for
283cumulative effect.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000284
285History substitution works as follows.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000286All non-empty input lines issued are saved in a history buffer,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000287and when a new prompt is given you are positioned on a new line at the
288bottom of this buffer.
289C-P moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000290Any line in the history buffer can be edited; an asterisk appears in
291front of the prompt to mark a line as modified.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000292Pressing the Return key passes the current line to the interpreter.
293C-R starts an incremental reverse search; C-S starts a forward search.
294
295The key bindings and some other parameters of the Readline library can
296be customized by placing commands in an initialization file called
297{\tt \$HOME/.initrc}.
298Key bindings have the form
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000299\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000300key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000301\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000302and options can be set with
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000303\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000304set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000305\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000306Example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000307\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000308# I prefer vi-style editing:
309set editing-mode vi
310# Edit using a single line:
311set horizontal-scroll-mode On
312# Rebind some keys:
313Meta-h: backward-kill-word
314Control-u: universal-argument
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000315\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +0000316Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000317instead of Readline's default filename completion function.
318If you insist, you can override this by putting
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000319\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000320TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000321\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000322in your
323{\tt \$HOME/.inputrc}.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000324(Of course, this makes it hard to type indented continuation lines.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000325
326This facility is an enormous step forward compared to previous versions of
327the interpreter; however, some wishes are left:
328It would be nice if the proper indentation were suggested on
329continuation lines (the parser knows if an indent token is required
330next).
331The completion mechanism might use the interpreter's symbol table.
332A function to check (or even suggest) matching parentheses, quotes
333etc. would also be useful.
334
335\section{An Informal Introduction to Python}
336
337In the following examples, input and output are distinguished by the
338presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat the
339example, you must type everything after the prompt, when the prompt
340appears; everything on lines that do not begin with a prompt is output
341from the interpreter.
342Note that a secondary prompt on a line by itself in an example means you
343must type a blank line; this is used to end a multi-line command.
344
345\subsection{Using Python as a Calculator}
346
Guido van Rossum4410c751991-06-04 20:22:18 +0000347Let's try some simple Python commands.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000348Start the interpreter and wait for the primary prompt,
349{\tt >>>}.
350The interpreter acts as a simple calculator: you can type an expression
351at it and it will write the value.
352Expression syntax is straightforward: the operators
353{\tt +},
354{\tt -},
355{\tt *}
356and
357{\tt /}
358work just as in most other languages (e.g., Pascal or C); parentheses
359can be used for grouping.
360For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000361\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000362>>> # This is a comment
363>>> 2+2
3644
365>>>
366>>> (50-5+5*6+25)/4
36725
368>>> # Division truncates towards zero:
369>>> 7/3
3702
371>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000372\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000373As in C, the equal sign ({\tt =}) is used to assign a value to a variable.
374The value of an assignment is not written:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000375\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000376>>> width = 20
377>>> height = 5*9
378>>> width * height
379900
380>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000381\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000382There is some support for floating point, but you can't mix floating
383point and integral numbers in expression (yet):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000384\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000385>>> 10.0 / 3.3
3863.0303030303
387>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000388\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +0000389Besides numbers, Python can also manipulate strings, enclosed in single
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000390quotes:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000391\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000392>>> 'foo bar'
393'foo bar'
394>>> 'doesn\'t'
395'doesn\'t'
396>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000397\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000398Strings are written inside quotes and with quotes and other funny
399characters escaped by backslashes, to show the precise value.
400(There is also a way to write strings without quotes and escapes.)
401Strings can be concatenated (glued together) with the
402{\tt +}
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000403operator, and repeated with~{\tt *}:
404\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000405>>> word = 'Help' + 'A'
406>>> word
407'HelpA'
408>>> '<' + word*5 + '>'
409'<HelpAHelpAHelpAHelpAHelpA>'
410>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000411\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000412Strings can be subscripted; as in C, the first character of a string has
413subscript 0.
414There is no separate character type; a character is simply a string of
415size one.
416As in Icon, substrings can be specified with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000417{\em slice}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000418notation: two subscripts (indices) separated by a colon.
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000419\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000420>>> word[4]
421'A'
422>>> word[0:2]
423'He'
424>>> word[2:4]
425'lp'
426>>> # Slice indices have useful defaults:
427>>> word[:2] # Take first two characters
428'He'
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000429>>> word[2:] # Drop first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000430'lpA'
431>>> # A useful invariant: s[:i] + s[i:] = s
432>>> word[:3] + word[3:]
433'HelpA'
434>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000435\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000436Degenerate cases are handled gracefully: an index that is too large is
437replaced by the string size, an upper bound smaller than the lower bound
438returns an empty string.
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000439\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000440>>> word[1:100]
441'elpA'
442>>> word[10:]
443''
444>>> word[2:1]
445''
446>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000447\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000448Slice indices (but not simple subscripts) may be negative numbers, to
449start counting from the right.
450For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000451\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000452>>> word[-2:] # Take last two characters
453'pA'
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000454>>> word[:-2] # Drop last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000455'Hel'
456>>> # But -0 does not count from the right!
457>>> word[-0:] # (since -0 equals 0)
458'HelpA'
459>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000460\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000461The best way to remember how slices work is to think of the indices as
462pointing
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000463{\em between}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000464characters, with the left edge of the first character numbered 0.
465Then the right edge of the last character of a string of
466{\tt n}
467characters has index
468{\tt n},
469for example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000470\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000471 +---+---+---+---+---+
472 | H | e | l | p | A |
473 +---+---+---+---+---+
474 0 1 2 3 4 5
475-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000476\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000477The first row of numbers gives the position of the indices 0...5 in the
478string; the second row gives the corresponding negative indices.
479For nonnegative indices, the length of a slice is the difference of the
480indices, if both are within bounds,
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000481e.g.,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000482the length of
483{\tt word[1:3]}
484is 3--1 = 2.
485
486Finally, the built-in function {\tt len()} computes the length of a
487string:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000488\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000489>>> s = 'supercalifragilisticexpialidocious'
490>>> len(s)
49134
492>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000493\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +0000494Python knows a number of
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000495{\em compound}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000496data types, used to group together other values.
497The most versatile is the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000498{\em list},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000499which can be written as a list of comma-separated values between square
500brackets:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000501\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000502>>> a = ['foo', 'bar', 100, 1234]
503>>> a
504['foo', 'bar', 100, 1234]
505>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000506\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000507As for strings, list subscripts start at 0:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000508\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000509>>> a[0]
510'foo'
511>>> a[3]
5121234
513>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000514\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +0000515Lists can be sliced, concatenated and so on, like strings:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000516\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000517>>> a[1:3]
518['bar', 100]
519>>> a[:2] + ['bletch', 2*2]
520['foo', 'bar', 'bletch', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000521>>> 3*a[:3] + ['Boe!']
522['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000523>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000524\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000525Unlike strings, which are
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000526{\em immutable},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000527it is possible to change individual elements of a list:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000528\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000529>>> a
530['foo', 'bar', 100, 1234]
531>>> a[2] = a[2] + 23
532>>> a
533['foo', 'bar', 123, 1234]
534>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000535\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000536Assignment to slices is also possible, and this may even change the size
537of the list:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000538\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000539>>> # Replace some items:
540>>> a[0:2] = [1, 12]
541>>> a
542[1, 12, 123, 1234]
543>>> # Remove some:
544>>> a[0:2] = []
545>>> a
546[123, 1234]
547>>> # Insert some:
548>>> a[1:1] = ['bletch', 'xyzzy']
549>>> a
550[123, 'bletch', 'xyzzy', 1234]
551>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000552\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000553The built-in function {\tt len()} also applies to lists:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000554\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000555>>> len(a)
5564
557>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000558\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000559
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000560\subsection{Tuples and Sequences}
561
562XXX To Be Done.
563
564\subsection{First Steps Towards Programming}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000565
Guido van Rossum4410c751991-06-04 20:22:18 +0000566Of course, we can use Python for more complicated tasks than adding two
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000567and two together.
568For instance, we can write an initial subsequence of the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000569{\em Fibonacci}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000570series as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000571\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000572>>> # Fibonacci series:
573>>> # the sum of two elements defines the next
574>>> a, b = 0, 1
575>>> while b < 100:
576... print b
577... a, b = b, a+b
578...
5791
5801
5812
5823
5835
5848
58513
58621
58734
58855
58989
590>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000591\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000592This example introduces several new features.
593\begin{itemize}
594\item
595The first line contains a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000596{\em multiple\ assignment}:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000597the variables
598{\tt a}
599and
600{\tt b}
601simultaneously get the new values 0 and 1.
602On the last line this is used again, demonstrating that the expressions
603on the right-hand side are all evaluated first before any of the
604assignments take place.
605\item
606The
607{\tt while}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000608loop executes as long as the condition (here: $b < 100$) remains true.
Guido van Rossum4410c751991-06-04 20:22:18 +0000609In Python, as in C, any non-zero integer value is true; zero is false.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000610The condition may also be a string or list value, in fact any sequence;
611anything with a non-zero length is true, empty sequences are false.
612The test used in the example is a simple comparison.
613The standard comparison operators are written as
614{\tt <},
615{\tt >},
616{\tt =},
617{\tt <=},
618{\tt >=}
619and
620{\tt <>}.%
621\footnote{
622 The ambiguity of using {\tt =}
623 for both assignment and equality is resolved by disallowing
624 unparenthesized conditions at the right hand side of assignments.
625}
626\item
627The
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000628{\em body}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000629of the loop is
Guido van Rossum4410c751991-06-04 20:22:18 +0000630{\em indented}: indentation is Python's way of grouping statements.
631Python does not (yet!) provide an intelligent input line editing
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000632facility, so you have to type a tab or space(s) for each indented line.
Guido van Rossum4410c751991-06-04 20:22:18 +0000633In practice you will prepare more complicated input for Python with a
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000634text editor; most text editors have an auto-indent facility.
635When a compound statement is entered interactively, it must be
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000636followed by a blank line to indicate completion (since the parser
637cannot guess when you have typed the last line).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000638\item
639The
640{\tt print}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000641statement writes the value of the expression(s) it is given.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000642It differs from just writing the expression you want to write (as we did
643earlier in the calculator examples) in the way it handles multiple
644expressions and strings.
645Strings are written without quotes and a space is inserted between
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000646items, so you can format things nicely, like this:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000647\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000648>>> i = 256*256
649>>> print 'The value of i is', i
650The value of i is 65536
651>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000652\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000653A trailing comma avoids the newline after the output:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000654\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000655>>> a, b = 0, 1
656>>> while b < 1000:
657... print b,
658... a, b = b, a+b
659...
6601 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
661>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000662\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000663Note that the interpreter inserts a newline before it prints the next
664prompt if the last line was not completed.
665\end{itemize}
666
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000667\subsection{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000668
Guido van Rossum4410c751991-06-04 20:22:18 +0000669Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000670usual control flow statements known from other languages, with some
671twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000672
673\subsubsection{If Statements}
674
675Perhaps the most well-known statement type is the {\tt if} statement.
676For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000677\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000678>>> if x < 0:
679... x = 0
680... print 'Negative changed to zero'
681... elif x = 0:
682... print 'Zero'
683... elif x = 1:
684... print 'Single'
685... else:
686... print 'More'
687...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000688\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000689There can be zero or more {\tt elif} parts, and the {\tt else} part is
690optional.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000691The keyword `{\tt elif}' is short for `{\tt else if}', and is useful to
692avoid excessive indentation.
693An {\tt if...elif...elif...} sequence is a substitute for the
694{\em switch} or {\em case} statements found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000695
696\subsubsection{For Statements}
697
Guido van Rossum4410c751991-06-04 20:22:18 +0000698The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000699used to in C or Pascal.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000700Rather than always iterating over an arithmetic progression of numbers
701(as Pascal), or leaving the user completely free in the iteration test
Guido van Rossum4410c751991-06-04 20:22:18 +0000702and step (as C), Python's {\tt for} statement iterates over the items
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000703of any sequence (e.g., a list or a string).
704For example (no pun intended):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000705\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000706>>> # Measure some strings:
707>>> a = ['cat', 'window', 'defenestrate']
708>>> for x in a:
709... print x, len(x)
710...
711cat 3
712window 6
713defenestrate 12
714>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000715\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000716
717\subsubsection{The {\tt range()} Function}
718
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000719If you do need to iterate over a sequence of numbers, the built-in
720function {\tt range()} comes in handy.
721It generates lists containing arithmetic progressions,
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000722e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000723\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000724>>> range(10)
725[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
726>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000727\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000728The given end point is never part of the generated list;
729{\tt range(10)} generates a list of 10 values,
730exactly the legal indices for items of a sequence of length 10.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000731It is possible to let the range start at another number, or to specify a
732different increment (even negative):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000733\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000734>>> range(5, 10)
735[5, 6, 7, 8, 9]
736>>> range(0, 10, 3)
737[0, 3, 6, 9]
738>>> range(-10, -100, -30)
739[-10, -40, -70]
740>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000741\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000742To iterate over the indices of a sequence, combine {\tt range()}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000743and {\tt len()} as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000744\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000745>>> a = ['Mary', 'had', 'a', 'little', 'boy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000746>>> for i in range(len(a)):
747... print i, a[i]
748...
7490 Mary
7501 had
7512 a
7523 little
Guido van Rossum2292b8e1991-01-23 16:31:24 +00007534 boy
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000754>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000755\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000756
757\subsubsection{Break Statements and Else Clauses on Loops}
758
759The {\tt break} statement breaks out of the smallest enclosing {\tt for}
760or {\tt while} loop.
761Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000762loop terminates through exhaustion of the list (with {\tt for}) or when
763the condition becomes false (with {\tt while}) but not when the loop is
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000764terminated by a {\tt break} statement.
765This is exemplified by the following loop, which searches for a list
766item of value 0:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000767\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000768>>> for n in range(2, 10):
769... for x in range(2, n):
770... if n % x = 0:
771... print n, 'equals', x, '*', n/x
772... break
773... else:
774... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000775...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00007762 is a prime number
7773 is a prime number
7784 equals 2 * 2
7795 is a prime number
7806 equals 2 * 3
7817 is a prime number
7828 equals 2 * 4
7839 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000784>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000785\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000786
787\subsubsection{Pass Statements}
788
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000789The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000790It can be used when a statement is required syntactically but the
791program requires no action.
792For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000793\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000794>>> while 1:
795... pass # Busy-wait for keyboard interrupt
796...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000797\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000798
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000799\subsubsection{Conditions Revisited}
800
801XXX To Be Done.
802
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000803\subsection{Defining Functions}
804
805We can create a function that writes the Fibonacci series to an
806arbitrary boundary:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000807\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000808>>> def fib(n): # write Fibonacci series up to n
809... a, b = 0, 1
810... while b <= n:
811... print b,
812... a, b = b, a+b
813...
814>>> # Now call the function we just defined:
815>>> fib(2000)
8161 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
817>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000818\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000819The keyword
820{\tt def}
821introduces a function
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000822{\em definition}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000823It must be followed by the function name and the parenthesized list of
824formal parameters.
825The statements that form the body of the function starts at the next
826line, indented by a tab stop.
827The
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000828{\em execution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000829of a function introduces a new symbol table used for the local variables
830of the function.
831More precisely, all variable assignments in a function store the value
832in the local symbol table; variable references first look in the local
833symbol table, then in the global symbol table, and then in the table of
834built-in names.
835Thus, the global symbol table is
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000836{\em read-only}
837within a function.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000838The actual parameters (arguments) to a function call are introduced in
839the local symbol table of the called function when it is called;
840thus, arguments are passed using
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000841{\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000842\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000843 Actually, {\em call by object reference} would be a better
844 description, since if a mutable object is passed, the caller
845 will see any changes the callee makes to it (e.g., items
846 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000847}
848When a function calls another function, a new local symbol table is
849created for that call.
850
851A function definition introduces the function name in the global symbol
852table.
853The value has a type that is recognized by the interpreter as a
854user-defined function.
855This value can be assigned to another name which can then also be used
856as a function.
857This serves as a general renaming mechanism:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000858\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000859>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000860<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000861>>> f = fib
862>>> f(100)
8631 1 2 3 5 8 13 21 34 55 89
864>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000865\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000866You might object that
867{\tt fib}
868is not a function but a procedure.
Guido van Rossum4410c751991-06-04 20:22:18 +0000869In Python, as in C, procedures are just functions that don't return a
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000870value.
871In fact, technically speaking, procedures do return a value, albeit a
872rather boring one.
873This value is called {\tt None} (it's a built-in name).
874Writing the value {\tt None} is normally suppressed by the interpreter
875if it would be the only value written.
876You can see it if you really want to:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000877\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000878>>> print fib(0)
879None
880>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000881\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000882It is simple to write a function that returns a list of the numbers of
883the Fibonacci series, instead of printing it:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000884\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000885>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000886... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000887... a, b = 0, 1
888... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000889... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000890... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000891... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000892...
893>>> f100 = fib2(100) # call it
894>>> f100 # write the result
895[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
896>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000897\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +0000898This example, as usual, demonstrates some new Python features:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000899\begin{itemize}
900\item
901The
902{\tt return}
903statement returns with a value from a function.
904{\tt return}
905without an expression argument is used to return from the middle of a
906procedure (falling off the end also returns from a proceduce).
907\item
908The statement
909{\tt ret.append(b)}
910calls a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000911{\em method}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000912of the list object
913{\tt ret}.
914A method is a function that `belongs' to an object and is named
915{\tt obj.methodname},
916where
917{\tt obj}
918is some object (this may be an expression), and
919{\tt methodname}
920is the name of a method that is defined by the object's type.
921Different types define different methods.
922Methods of different types may have the same name without causing
923ambiguity.
924See the section on classes, later, to find out how you can define your
925own object types and methods.
926The method
927{\tt append}
928shown in the example, is defined for list objects; it adds a new element
929at the end of the list.
930In this case it is equivalent to
931{\tt ret = ret + [b]},
932but more efficient.%
933\footnote{
934 There is a subtle semantic difference if the object
935 is referenced from more than one place.
936}
937\end{itemize}
938The list object type has two more methods:
Guido van Rossum7d9f8d71991-01-22 11:45:00 +0000939\begin{description}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000940\item[{\tt insert(i, x)}]
941Inserts an item at a given position.
942The first argument is the index of the element before which to insert,
943so {\tt a.insert(0, x)} inserts at the front of the list, and
944{\tt a.insert(len(a), x)} is equivalent to {\tt a.append(x)}.
945\item[{\tt sort()}]
946Sorts the elements of the list.
Guido van Rossum7d9f8d71991-01-22 11:45:00 +0000947\end{description}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000948For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000949\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000950>>> a = [10, 100, 1, 1000]
951>>> a.insert(2, -1)
952>>> a
953[10, 100, -1, 1, 1000]
954>>> a.sort()
955>>> a
956[-1, 1, 10, 100, 1000]
957>>> # Strings are sorted according to ASCII:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000958>>> b = ['Mary', 'had', 'a', 'little', 'boy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000959>>> b.sort()
960>>> b
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000961['Mary', 'a', 'boy', 'had', 'little']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000962>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000963\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000964
965\subsection{Modules}
966
Guido van Rossum4410c751991-06-04 20:22:18 +0000967If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000968definitions you have made (functions and variables) are lost.
969Therefore, if you want to write a somewhat longer program, you are
970better off using a text editor to prepare the input for the interpreter
971and run it with that file as input instead.
972This is known as creating a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000973{\em script}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000974As your program gets longer, you may want to split it into several files
975for easier maintenance.
976You may also want to use a handy function that you've written in several
977programs without copying its definition into each program.
Guido van Rossum4410c751991-06-04 20:22:18 +0000978To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000979them in a script or in an interactive instance of the interpreter.
980Such a file is called a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000981{\em module};
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000982definitions from a module can be
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000983{\em imported}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000984into other modules or into the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000985{\em main}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000986module (the collection of variables that you have access to in
987a script and in calculator mode).
988
Guido van Rossum4410c751991-06-04 20:22:18 +0000989A module is a file containing Python definitions and statements.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000990The file name is the module name with the suffix
991{\tt .py}
992appended.
993For instance, use your favorite text editor to create a file called
994{\tt fibo.py}
995in the current directory with the following contents:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000996\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000997# Fibonacci numbers module
998
999def fib(n): # write Fibonacci series up to n
1000 a, b = 0, 1
1001 while b <= n:
1002 print b,
1003 a, b = b, a+b
1004
1005def fib2(n): # return Fibonacci series up to n
1006 ret = []
1007 a, b = 0, 1
1008 while b <= n:
1009 ret.append(b)
1010 a, b = b, a+b
1011 return ret
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001012\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +00001013Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001014following command:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001015\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001016>>> import fibo
1017>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001018\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001019This does not enter the names of the functions defined in
1020{\tt fibo}
1021directly in the symbol table; it only enters the module name
1022{\tt fibo}
1023there.
1024Using the module name you can access the functions:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001025\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001026>>> fibo.fib(1000)
10271 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1028>>> fibo.fib2(100)
1029[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1030>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001031\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001032If you intend to use a function often you can assign it to a local name:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001033\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001034>>> fib = fibo.fib
1035>>> fib(500)
10361 1 2 3 5 8 13 21 34 55 89 144 233 377
1037>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001038\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001039
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001040\subsubsection{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001041
1042A module can contain executable statements as well as function
1043definitions.
1044These statements are intended to initialize the module.
1045They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001046{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001047time the module is imported somewhere.%
1048\footnote{
1049 In fact function definitions are also `statements' that are
1050 `executed'; the execution enters the function name in the
1051 module's global symbol table.
1052}
1053
1054Each module has its own private symbol table, which is used as the
1055global symbol table by all functions defined in the module.
1056Thus, the author of a module can use global variables in the module
1057without worrying about accidental clashes with a user's global
1058variables.
1059On the other hand, if you know what you are doing you can touch a
1060module's global variables with the same notation used to refer to its
1061functions,
1062{\tt modname.itemname}.
1063
1064Modules can import other modules.
1065It is customary but not required to place all
1066{\tt import}
1067statements at the beginning of a module (or script, for that matter).
1068The imported module names are placed in the importing module's global
1069symbol table.
1070
1071There is a variant of the
1072{\tt import}
1073statement that imports names from a module directly into the importing
1074module's symbol table.
1075For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001076\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001077>>> from fibo import fib, fib2
1078>>> fib(500)
10791 1 2 3 5 8 13 21 34 55 89 144 233 377
1080>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001081\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001082This does not introduce the module name from which the imports are taken
1083in the local symbol table (so in the example, {\tt fibo} is not
1084defined).
1085
1086There is even a variant to import all names that a module defines:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001087\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001088>>> from fibo import *
1089>>> fib(500)
10901 1 2 3 5 8 13 21 34 55 89 144 233 377
1091>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001092\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001093This imports all names except those beginning with an underscore
1094({\tt \_}).
1095
1096\subsubsection{Standard Modules}
1097
Guido van Rossum4410c751991-06-04 20:22:18 +00001098Python comes with a library of standard modules, described in a separate
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001099document (Python Library and Module Reference).
1100Some modules are built into the interpreter; these provide access to
1101operations that are not part of the core of the language but are
1102nevertheless built in, either for efficiency or to provide access to
1103operating system primitives such as system calls.
1104The set of such modules is a configuration option; e.g., the
1105{\tt amoeba}
1106module is only provided on systems that somehow support Amoeba
1107primitives.
1108One particular module deserves some attention:
1109{\tt sys},
Guido van Rossum4410c751991-06-04 20:22:18 +00001110which is built into every Python interpreter.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001111The variables
1112{\tt sys.ps1}
1113and
1114{\tt sys.ps2}
1115define the strings used as primary and secondary prompts:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001116\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001117>>> import sys
1118>>> sys.ps1
1119'>>> '
1120>>> sys.ps2
1121'... '
1122>>> sys.ps1 = 'C> '
1123C> print 'Yuck!'
1124Yuck!
1125C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001126\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001127These two variables are only defined if the interpreter is in
1128interactive mode.
1129
1130The variable
1131{\tt sys.path}
1132is a list of strings that determine the interpreter's search path for
1133modules.
1134It is initialized to a default path taken from the environment variable
1135{\tt PYTHONPATH},
1136or from a built-in default if
1137{\tt PYTHONPATH}
1138is not set.
1139You can modify it using standard list operations, e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001140\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001141>>> import sys
1142>>> sys.path.append('/ufs/guido/lib/python')
1143>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001144\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001145
1146\subsection{Errors and Exceptions}
1147
1148Until now error messages haven't yet been mentioned, but if you have
1149tried out the examples you have probably seen some.
1150There are (at least) two distinguishable kinds of errors:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001151{\em syntax\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001152and
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001153{\em exceptions}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001154
1155\subsubsection{Syntax Errors}
1156
1157Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001158kind of complaint you get while you are still learning Python:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001159\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001160>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001161Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001162while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001163 ^
1164Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001165>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001166\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001167The parser repeats the offending line and displays a little `arrow'
1168pointing at the earliest point in the line where the error was detected.
1169The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001170{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001171the arrow: in the example, the error is detected at the keyword
1172{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001173File name and line number are printed so you know where to look in case
1174the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001175
1176\subsubsection{Exceptions}
1177
1178Even if a statement or expression is syntactically correct, it may cause
1179an error when an attempt is made to execute it:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001180\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001181>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001182Unhandled exception: run-time error: integer division by zero
1183Stack backtrace (innermost last):
1184 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001185>>> 4 + foo*3
1186Unhandled exception: undefined name: foo
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001187Stack backtrace (innermost last):
1188 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001189>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001190Unhandled exception: type error: illegal argument type for built-in operation
1191Stack backtrace (innermost last):
1192 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001193>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001194\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001195Errors detected during execution are called
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001196{\em exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001197and are not unconditionally fatal: you will soon learn how to handle
Guido van Rossum4410c751991-06-04 20:22:18 +00001198them in Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001199Most exceptions are not handled by programs, however, and result
1200in error messages as shown here.
1201
1202The first line of the error message indicates what happened.
1203Exceptions come in different types, and the type is printed as part of
1204the message: the types in the example are
1205{\tt run-time error},
1206{\tt undefined name}
1207and
1208{\tt type error}.
1209The rest of the line is a detail whose interpretation depends on the
1210exception type.
1211
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001212The rest of the error message shows the context where the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001213exception happened.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001214In general it contains a stack backtrace listing source lines; however,
1215it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001216
1217Here is a summary of the most common exceptions:
1218\begin{itemize}
1219\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001220{\em Run-time\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001221are generally caused by wrong data used by the program; this can be the
1222programmer's fault or caused by bad input.
1223The detail states the cause of the error in more detail.
1224\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001225{\em Undefined\ name}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001226errors are more serious: these are usually caused by misspelled
1227identifiers.%
1228\footnote{
1229 The parser does not check whether names used in a program are at
1230 all defined elsewhere in the program, so such checks are
1231 postponed until run-time. The same holds for type checking.
1232}
1233The detail is the offending identifier.
1234\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001235{\em Type\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001236are also pretty serious: this is another case of using wrong data (or
1237better, using data the wrong way), but here the error can be glanced
1238from the object type(s) alone.
1239The detail shows in what context the error was detected.
1240\end{itemize}
1241
1242\subsubsection{Handling Exceptions}
1243
1244It is possible to write programs that handle selected exceptions.
1245Look at the following example, which prints a table of inverses of
1246some floating point numbers:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001247\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001248>>> numbers = [0.3333, 2.5, 0.0, 10.0]
1249>>> for x in numbers:
1250... print x,
1251... try:
1252... print 1.0 / x
1253... except RuntimeError:
1254... print '*** has no inverse ***'
1255...
12560.3333 3.00030003
12572.5 0.4
12580 *** has no inverse ***
125910 0.1
1260>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001261\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001262The {\tt try} statement works as follows.
1263\begin{itemize}
1264\item
1265First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001266{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001267(the statement(s) between the {\tt try} and {\tt except} keywords) is
1268executed.
1269\item
1270If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001271{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001272is skipped and execution of the {\tt try} statement is finished.
1273\item
1274If an exception occurs during execution of the try clause, and its
1275type matches the exception named after the {\tt except} keyword, the
1276rest of the try clause is skipped, the except clause is executed, and
1277then execution continues after the {\tt try} statement.
1278\item
1279If an exception occurs which does not match the exception named in the
1280except clause, it is passed on to outer try statements; if no handler is
1281found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001282{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001283and execution stops with a message as shown above.
1284\end{itemize}
1285A {\tt try} statement may have more than one except clause, to specify
1286handlers for different exceptions.
1287At most one handler will be executed.
1288Handlers only handle exceptions that occur in the corresponding try
1289clause, not in other handlers of the same {\tt try} statement.
1290An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001291e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001292\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001293... except (RuntimeError, TypeError, NameError):
1294... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001295\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001296The last except clause may omit the exception name(s), to serve as a
1297wildcard.
1298Use this with extreme caution!
1299
1300When an exception occurs, it may have an associated value, also known as
1301the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001302{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001303The presence and type of the argument depend on the exception type.
1304For exception types which have an argument, the except clause may
1305specify a variable after the exception name (or list) to receive the
1306argument's value, as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001307\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001308>>> try:
1309... foo()
1310... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001311... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001312...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001313name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001314>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001315\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001316If an exception has an argument, it is printed as the third part
1317(`detail') of the message for unhandled exceptions.
1318
1319Standard exception names are built-in identifiers (not reserved
1320keywords).
1321These are in fact string objects whose
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001322{\em object\ identity}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001323(not their value!) identifies the exceptions.%
1324\footnote{
1325 There should really be a separate exception type; it is pure
1326 laziness that exceptions are identified by strings, and this may
1327 be fixed in the future.
1328}
1329The string is printed as the second part of the message for unhandled
1330exceptions.
1331Their names and values are:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001332\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001333EOFError 'end-of-file read'
1334KeyboardInterrupt 'keyboard interrupt'
1335MemoryError 'out of memory' *
1336NameError 'undefined name' *
1337RuntimeError 'run-time error' *
1338SystemError 'system error' *
1339TypeError 'type error' *
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001340\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001341The meanings should be clear enough.
1342Those exceptions with a {\tt *} in the third column have an argument.
1343
1344Exception handlers don't just handle exceptions if they occur
1345immediately in the try clause, but also if they occur inside functions
1346that are called (even indirectly) in the try clause.
1347For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001348\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001349>>> def this_fails():
1350... x = 1/0
1351...
1352>>> try:
1353... this_fails()
1354... except RuntimeError, detail:
1355... print 'Handling run-time error:', detail
1356...
Guido van Rossum67fa1601991-04-23 14:14:57 +00001357Handling run-time error: integer division by zero
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001358>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001359\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001360
1361\subsubsection{Raising Exceptions}
1362
1363The {\tt raise} statement allows the programmer to force a specified
1364exception to occur.
1365For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001366\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001367>>> raise NameError, 'Hi There!'
1368Unhandled exception: undefined name: Hi There!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001369Stack backtrace (innermost last):
1370 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001371>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001372\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001373The first argument to {\tt raise} names the exception to be raised.
1374The optional second argument specifies the exception's argument.
1375
1376\subsubsection{User-defined Exceptions}
1377
1378Programs may name their own exceptions by assigning a string to a
1379variable.
1380For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001381\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001382>>> my_exc = 'nobody likes me!'
1383>>> try:
1384... raise my_exc, 2*2
1385... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001386... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001387...
1388My exception occured, value: 4
1389>>> raise my_exc, 1
1390Unhandled exception: nobody likes me!: 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001391Stack backtrace (innermost last):
1392 File "<stdin>", line 7
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001393>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001394\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001395Many standard modules use this to report errors that may occur in
1396functions they define.
1397
1398\subsubsection{Defining Clean-up Actions}
1399
1400The {\tt try} statement has another optional clause which is intended to
1401define clean-up actions that must be executed under all circumstances.
1402For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001403\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001404>>> try:
1405... raise KeyboardInterrupt
1406... finally:
1407... print 'Goodbye, world!'
1408...
1409Goodbye, world!
1410Unhandled exception: keyboard interrupt
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001411Stack backtrace (innermost last):
1412 File "<stdin>", line 2
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001413>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001414\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001415The
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001416{\em finally\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001417must follow the except clauses(s), if any.
1418It is executed whether or not an exception occurred.
1419If the exception is handled, the finally clause is executed after the
1420handler (and even if another exception occurred in the handler).
1421It is also executed when the {\tt try} statement is left via a
1422{\tt break} or {\tt return} statement.
1423
1424\subsection{Classes}
1425
Guido van Rossum4410c751991-06-04 20:22:18 +00001426Classes in Python make it possible to play the game of encapsulation in a
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001427somewhat different way than it is played with modules.
1428Classes are an advanced topic and are probably best skipped on the first
Guido van Rossum4410c751991-06-04 20:22:18 +00001429encounter with Python.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001430
1431\subsubsection{Prologue}
1432
Guido van Rossum4410c751991-06-04 20:22:18 +00001433Python's class mechanism is not particularly elegant, but quite powerful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001434It is a mixture of the class mechanisms found in C++ and Modula-3.
Guido van Rossum4410c751991-06-04 20:22:18 +00001435As is true for modules, classes in Python do not put an absolute barrier
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001436between definition and user, but rather rely on the politeness of the
1437user not to ``break into the definition.''
1438The most important features of classes are retained with full power,
1439however: the class inheritance mechanism allows multiple base classes,
1440a derived class can override any method of its base class(es), a method
1441can call the method of a base class with the same name.
1442Objects can contain an arbitrary amount of private data.
1443
1444In C++ terminology, all class members (including data members) are
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001445{\em public},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001446and all member functions (methods) are
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001447{\em virtual}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001448There are no special constructors or destructors.
1449As in Modula-3, there are no shorthands for referencing the object's
1450members from its methods: the method function is declared with an
1451explicit first argument representing the object, which is provided
1452implicitly by the call.
1453As in Smalltalk, classes themselves are objects, albeit in the wider
Guido van Rossum4410c751991-06-04 20:22:18 +00001454sense of the word: in Python, all data types are objects.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001455This provides semantics for renaming or aliasing.
1456But, just like in C++ or Modula-3, the built-in types cannot be used as
1457base classes for extension by the user.
1458Also, like Modula-3 but unlike C++, the built-in operators with special
1459syntax (arithmetic operators, subscripting etc.) cannot be redefined for
1460class members.%
1461\footnote{
1462 They can be redefined for new object types implemented in C in
1463 extensions to the interpreter, however. It would require only a
1464 naming convention and a relatively small change to the
1465 interpreter to allow operator overloading for classes, so
1466 perhaps someday...
1467}
1468
1469\subsubsection{A Simple Example}
1470
1471Consider the following example, which defines a class {\tt Set}
1472representing a (finite) mathematical set with operations to add and
1473remove elements, a membership test, and a request for the size of the
1474set.
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001475\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001476class Set():
1477 def new(self):
1478 self.elements = []
1479 return self
1480 def add(self, e):
1481 if e not in self.elements:
1482 self.elements.append(e)
1483 def remove(self, e):
1484 if e in self.elements:
1485 for i in range(len(self.elements)):
1486 if self.elements[i] = e:
1487 del self.elements[i]
1488 break
1489 def is_element(self, e):
1490 return e in self.elements
1491 def size(self):
1492 return len(self.elements)
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001493\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001494Note that the class definition looks like a big compound statement,
1495with all the function definitons indented repective to the
1496{\tt class}
1497keyword.
1498
1499Let's assume that this
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001500{\em class\ definition}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001501is the only contents of the module file
1502{\tt SetClass.py}.
Guido van Rossum4410c751991-06-04 20:22:18 +00001503We can then use it in a Python program as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001504\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001505>>> from SetClass import Set
1506>>> a = Set().new() # create a Set object
1507>>> a.add(2)
1508>>> a.add(3)
1509>>> a.add(1)
1510>>> a.add(1)
1511>>> if a.is_element(3): print '3 is in the set'
1512...
15133 is in the set
1514>>> if not a.is_element(4): print '4 is not in the set'
1515...
15164 is not in the set
1517>>> print 'a has', a.size(), 'elements'
1518a has 3 elements
1519>>> a.remove(1)
1520>>> print 'now a has', a.size(), 'elements'
1521>>>
1522now a has 2 elements
1523>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001524\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001525From the example we learn in the first place that the functions defined
1526in the class (e.g.,
1527{\tt add})
1528can be called using the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001529{\em member}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001530notation for the object
1531{\tt a}.
1532The member function is called with one less argument than it is defined:
1533the object is implicitly passed as the first argument.
1534Thus, the call
1535{\tt a.add(2)}
1536is equivalent to
1537{\tt Set.add(a, 2)}.
1538
Guido van Rossum4410c751991-06-04 20:22:18 +00001539XXX This section is not complete yet! Inheritance!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001540
1541\section{XXX P.M.}
1542
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001543\begin{itemize}
1544\item The {\tt del} statement.
1545\item The {\tt dir()} function.
1546\item Tuples.
1547\item Dictionaries.
1548\item Objects and types in general.
1549\item Backquotes.
Guido van Rossum4410c751991-06-04 20:22:18 +00001550\item Output formatting.
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001551\item And/Or/Not.
Guido van Rossum4410c751991-06-04 20:22:18 +00001552\item ``.pyc'' files.
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001553\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001554
1555\end{document}