blob: ee3e47d982418fcb88fb0a1f17d674616e348ff7 [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}
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}.
Guido van Rossum4410c751991-06-04 20:22:18 +0000216It has the same syntax as the {\UNIX} shell variable
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000217{\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
Guido van Rossum4410c751991-06-04 20:22:18 +0000230As an important speed-up of the start-up time of short programs, if a
231file called {\tt foo.pyc} exists in the directory where {\tt foo.py}
232is found, this is assumed to contain an already-``compiled'' version
233of the module {\tt foo}. The last modification time of {\tt foo.py}
234is recorded in {\tt foo.pyc}, and if these don't match, {\tt foo.pyc}
235is ignored. Whenever {\tt foo.py} is successfully compiled, an
236attempt is made to write the compiled version to {\tt foo.pyc}.
237
238On BSD'ish {\UNIX} systems, Python scripts can be made directly executable,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000239like shell scripts, by putting the line
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000240\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000241#! /usr/local/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000242\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000243(assuming that's the name of the interpreter) at the beginning of the
244script and giving the file an executable mode.
245(The
246{\tt \#!}
247must be the first two characters of the file.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000248
249\subsection{Interactive Input Editing and History Substitution}
250
Guido van Rossum4410c751991-06-04 20:22:18 +0000251Some versions of the Python interpreter support editing of the current
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000252input line and history substitution, similar to facilities found in the
253Korn shell and the GNU Bash shell.
254This is implemented using the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000255{\em GNU\ Readline}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000256library, which supports Emacs-style and vi-style editing.
257This library has its own documentation which I won't duplicate here;
258however, the basics are easily explained.
259
260If supported,%
261\footnote{
262 Perhaps the quickest check to see whether command line editing
Guido van Rossum4410c751991-06-04 20:22:18 +0000263 is supported is typing Control-P to the first Python prompt
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000264 you get. If it beeps, you have command line editing.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000265 If not, you can skip the rest of this section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000266}
267input line editing is active whenever the interpreter prints a primary
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000268or secondary prompt.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000269The current line can be edited using the conventional Emacs control
270characters.
271The most important of these are:
272C-A (Control-A) moves the cursor to the beginning of the line, C-E to
273the end, C-B moves it one position to the left, C-F to the right.
274Backspace erases the character to the left of the cursor, C-D the
275character to its right.
276C-K kills (erases) the rest of the line to the right of the cursor, C-Y
277yanks back the last killed string.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000278C-underscore undoes the last change you made; it can be repeated for
279cumulative effect.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000280
281History substitution works as follows.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000282All non-empty input lines issued are saved in a history buffer,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000283and when a new prompt is given you are positioned on a new line at the
284bottom of this buffer.
285C-P moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000286Any line in the history buffer can be edited; an asterisk appears in
287front of the prompt to mark a line as modified.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000288Pressing the Return key passes the current line to the interpreter.
289C-R starts an incremental reverse search; C-S starts a forward search.
290
291The key bindings and some other parameters of the Readline library can
292be customized by placing commands in an initialization file called
293{\tt \$HOME/.initrc}.
294Key bindings have the form
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000295\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000296key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000297\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000298and options can be set with
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000299\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000300set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000301\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000302Example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000303\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000304# I prefer vi-style editing:
305set editing-mode vi
306# Edit using a single line:
307set horizontal-scroll-mode On
308# Rebind some keys:
309Meta-h: backward-kill-word
310Control-u: universal-argument
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000311\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +0000312Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000313instead of Readline's default filename completion function.
314If you insist, you can override this by putting
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000315\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000316TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000317\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000318in your
319{\tt \$HOME/.inputrc}.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000320(Of course, this makes it hard to type indented continuation lines.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000321
322This facility is an enormous step forward compared to previous versions of
323the interpreter; however, some wishes are left:
324It would be nice if the proper indentation were suggested on
325continuation lines (the parser knows if an indent token is required
326next).
327The completion mechanism might use the interpreter's symbol table.
328A function to check (or even suggest) matching parentheses, quotes
329etc. would also be useful.
330
331\section{An Informal Introduction to Python}
332
333In the following examples, input and output are distinguished by the
334presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat the
335example, you must type everything after the prompt, when the prompt
336appears; everything on lines that do not begin with a prompt is output
337from the interpreter.
338Note that a secondary prompt on a line by itself in an example means you
339must type a blank line; this is used to end a multi-line command.
340
341\subsection{Using Python as a Calculator}
342
Guido van Rossum4410c751991-06-04 20:22:18 +0000343Let's try some simple Python commands.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000344Start the interpreter and wait for the primary prompt,
345{\tt >>>}.
346The interpreter acts as a simple calculator: you can type an expression
347at it and it will write the value.
348Expression syntax is straightforward: the operators
349{\tt +},
350{\tt -},
351{\tt *}
352and
353{\tt /}
354work just as in most other languages (e.g., Pascal or C); parentheses
355can be used for grouping.
356For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000357\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000358>>> # This is a comment
359>>> 2+2
3604
361>>>
362>>> (50-5+5*6+25)/4
36325
364>>> # Division truncates towards zero:
365>>> 7/3
3662
367>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000368\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000369As in C, the equal sign ({\tt =}) is used to assign a value to a variable.
370The value of an assignment is not written:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000371\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000372>>> width = 20
373>>> height = 5*9
374>>> width * height
375900
376>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000377\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000378There is some support for floating point, but you can't mix floating
379point and integral numbers in expression (yet):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000380\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000381>>> 10.0 / 3.3
3823.0303030303
383>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000384\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +0000385Besides numbers, Python can also manipulate strings, enclosed in single
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000386quotes:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000387\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000388>>> 'foo bar'
389'foo bar'
390>>> 'doesn\'t'
391'doesn\'t'
392>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000393\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000394Strings are written inside quotes and with quotes and other funny
395characters escaped by backslashes, to show the precise value.
396(There is also a way to write strings without quotes and escapes.)
397Strings can be concatenated (glued together) with the
398{\tt +}
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000399operator, and repeated with~{\tt *}:
400\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000401>>> word = 'Help' + 'A'
402>>> word
403'HelpA'
404>>> '<' + word*5 + '>'
405'<HelpAHelpAHelpAHelpAHelpA>'
406>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000407\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000408Strings can be subscripted; as in C, the first character of a string has
409subscript 0.
410There is no separate character type; a character is simply a string of
411size one.
412As in Icon, substrings can be specified with the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000413{\em slice}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000414notation: two subscripts (indices) separated by a colon.
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000415\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000416>>> word[4]
417'A'
418>>> word[0:2]
419'He'
420>>> word[2:4]
421'lp'
422>>> # Slice indices have useful defaults:
423>>> word[:2] # Take first two characters
424'He'
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000425>>> word[2:] # Drop first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000426'lpA'
427>>> # A useful invariant: s[:i] + s[i:] = s
428>>> word[:3] + word[3:]
429'HelpA'
430>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000431\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000432Degenerate cases are handled gracefully: an index that is too large is
433replaced by the string size, an upper bound smaller than the lower bound
434returns an empty string.
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000435\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000436>>> word[1:100]
437'elpA'
438>>> word[10:]
439''
440>>> word[2:1]
441''
442>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000443\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000444Slice indices (but not simple subscripts) may be negative numbers, to
445start counting from the right.
446For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000447\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000448>>> word[-2:] # Take last two characters
449'pA'
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000450>>> word[:-2] # Drop last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000451'Hel'
452>>> # But -0 does not count from the right!
453>>> word[-0:] # (since -0 equals 0)
454'HelpA'
455>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000456\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000457The best way to remember how slices work is to think of the indices as
458pointing
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000459{\em between}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000460characters, with the left edge of the first character numbered 0.
461Then the right edge of the last character of a string of
462{\tt n}
463characters has index
464{\tt n},
465for example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000466\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000467 +---+---+---+---+---+
468 | H | e | l | p | A |
469 +---+---+---+---+---+
470 0 1 2 3 4 5
471-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000472\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000473The first row of numbers gives the position of the indices 0...5 in the
474string; the second row gives the corresponding negative indices.
475For nonnegative indices, the length of a slice is the difference of the
476indices, if both are within bounds,
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000477e.g.,
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000478the length of
479{\tt word[1:3]}
480is 3--1 = 2.
481
482Finally, the built-in function {\tt len()} computes the length of a
483string:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000484\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000485>>> s = 'supercalifragilisticexpialidocious'
486>>> len(s)
48734
488>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000489\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +0000490Python knows a number of
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000491{\em compound}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000492data types, used to group together other values.
493The most versatile is the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000494{\em list},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000495which can be written as a list of comma-separated values between square
496brackets:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000497\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000498>>> a = ['foo', 'bar', 100, 1234]
499>>> a
500['foo', 'bar', 100, 1234]
501>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000502\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000503As for strings, list subscripts start at 0:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000504\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000505>>> a[0]
506'foo'
507>>> a[3]
5081234
509>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000510\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +0000511Lists can be sliced, concatenated and so on, like strings:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000512\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000513>>> a[1:3]
514['bar', 100]
515>>> a[:2] + ['bletch', 2*2]
516['foo', 'bar', 'bletch', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000517>>> 3*a[:3] + ['Boe!']
518['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000519>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000520\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000521Unlike strings, which are
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000522{\em immutable},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000523it is possible to change individual elements of a list:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000524\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000525>>> a
526['foo', 'bar', 100, 1234]
527>>> a[2] = a[2] + 23
528>>> a
529['foo', 'bar', 123, 1234]
530>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000531\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000532Assignment to slices is also possible, and this may even change the size
533of the list:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000534\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000535>>> # Replace some items:
536>>> a[0:2] = [1, 12]
537>>> a
538[1, 12, 123, 1234]
539>>> # Remove some:
540>>> a[0:2] = []
541>>> a
542[123, 1234]
543>>> # Insert some:
544>>> a[1:1] = ['bletch', 'xyzzy']
545>>> a
546[123, 'bletch', 'xyzzy', 1234]
547>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000548\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000549The built-in function {\tt len()} also applies to lists:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000550\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000551>>> len(a)
5524
553>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000554\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000555
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000556\subsection{Tuples and Sequences}
557
558XXX To Be Done.
559
560\subsection{First Steps Towards Programming}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000561
Guido van Rossum4410c751991-06-04 20:22:18 +0000562Of course, we can use Python for more complicated tasks than adding two
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000563and two together.
564For instance, we can write an initial subsequence of the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000565{\em Fibonacci}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000566series as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000567\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000568>>> # Fibonacci series:
569>>> # the sum of two elements defines the next
570>>> a, b = 0, 1
571>>> while b < 100:
572... print b
573... a, b = b, a+b
574...
5751
5761
5772
5783
5795
5808
58113
58221
58334
58455
58589
586>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000587\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000588This example introduces several new features.
589\begin{itemize}
590\item
591The first line contains a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000592{\em multiple\ assignment}:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000593the variables
594{\tt a}
595and
596{\tt b}
597simultaneously get the new values 0 and 1.
598On the last line this is used again, demonstrating that the expressions
599on the right-hand side are all evaluated first before any of the
600assignments take place.
601\item
602The
603{\tt while}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000604loop executes as long as the condition (here: $b < 100$) remains true.
Guido van Rossum4410c751991-06-04 20:22:18 +0000605In Python, as in C, any non-zero integer value is true; zero is false.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000606The condition may also be a string or list value, in fact any sequence;
607anything with a non-zero length is true, empty sequences are false.
608The test used in the example is a simple comparison.
609The standard comparison operators are written as
610{\tt <},
611{\tt >},
612{\tt =},
613{\tt <=},
614{\tt >=}
615and
616{\tt <>}.%
617\footnote{
618 The ambiguity of using {\tt =}
619 for both assignment and equality is resolved by disallowing
620 unparenthesized conditions at the right hand side of assignments.
621}
622\item
623The
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000624{\em body}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000625of the loop is
Guido van Rossum4410c751991-06-04 20:22:18 +0000626{\em indented}: indentation is Python's way of grouping statements.
627Python does not (yet!) provide an intelligent input line editing
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000628facility, so you have to type a tab or space(s) for each indented line.
Guido van Rossum4410c751991-06-04 20:22:18 +0000629In practice you will prepare more complicated input for Python with a
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000630text editor; most text editors have an auto-indent facility.
631When a compound statement is entered interactively, it must be
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000632followed by a blank line to indicate completion (since the parser
633cannot guess when you have typed the last line).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000634\item
635The
636{\tt print}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000637statement writes the value of the expression(s) it is given.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000638It differs from just writing the expression you want to write (as we did
639earlier in the calculator examples) in the way it handles multiple
640expressions and strings.
641Strings are written without quotes and a space is inserted between
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000642items, so you can format things nicely, like this:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000643\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000644>>> i = 256*256
645>>> print 'The value of i is', i
646The value of i is 65536
647>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000648\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000649A trailing comma avoids the newline after the output:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000650\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000651>>> a, b = 0, 1
652>>> while b < 1000:
653... print b,
654... a, b = b, a+b
655...
6561 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
657>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000658\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000659Note that the interpreter inserts a newline before it prints the next
660prompt if the last line was not completed.
661\end{itemize}
662
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000663\subsection{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000664
Guido van Rossum4410c751991-06-04 20:22:18 +0000665Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000666usual control flow statements known from other languages, with some
667twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000668
669\subsubsection{If Statements}
670
671Perhaps the most well-known statement type is the {\tt if} statement.
672For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000673\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000674>>> if x < 0:
675... x = 0
676... print 'Negative changed to zero'
677... elif x = 0:
678... print 'Zero'
679... elif x = 1:
680... print 'Single'
681... else:
682... print 'More'
683...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000684\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000685There can be zero or more {\tt elif} parts, and the {\tt else} part is
686optional.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000687The keyword `{\tt elif}' is short for `{\tt else if}', and is useful to
688avoid excessive indentation.
689An {\tt if...elif...elif...} sequence is a substitute for the
690{\em switch} or {\em case} statements found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000691
692\subsubsection{For Statements}
693
Guido van Rossum4410c751991-06-04 20:22:18 +0000694The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000695used to in C or Pascal.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000696Rather than always iterating over an arithmetic progression of numbers
697(as Pascal), or leaving the user completely free in the iteration test
Guido van Rossum4410c751991-06-04 20:22:18 +0000698and step (as C), Python's {\tt for} statement iterates over the items
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000699of any sequence (e.g., a list or a string).
700For example (no pun intended):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000701\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000702>>> # Measure some strings:
703>>> a = ['cat', 'window', 'defenestrate']
704>>> for x in a:
705... print x, len(x)
706...
707cat 3
708window 6
709defenestrate 12
710>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000711\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000712
713\subsubsection{The {\tt range()} Function}
714
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000715If you do need to iterate over a sequence of numbers, the built-in
716function {\tt range()} comes in handy.
717It generates lists containing arithmetic progressions,
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000718e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000719\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000720>>> range(10)
721[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
722>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000723\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000724The given end point is never part of the generated list;
725{\tt range(10)} generates a list of 10 values,
726exactly the legal indices for items of a sequence of length 10.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000727It is possible to let the range start at another number, or to specify a
728different increment (even negative):
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000729\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000730>>> range(5, 10)
731[5, 6, 7, 8, 9]
732>>> range(0, 10, 3)
733[0, 3, 6, 9]
734>>> range(-10, -100, -30)
735[-10, -40, -70]
736>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000737\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000738To iterate over the indices of a sequence, combine {\tt range()}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000739and {\tt len()} as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000740\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000741>>> a = ['Mary', 'had', 'a', 'little', 'boy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000742>>> for i in range(len(a)):
743... print i, a[i]
744...
7450 Mary
7461 had
7472 a
7483 little
Guido van Rossum2292b8e1991-01-23 16:31:24 +00007494 boy
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000750>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000751\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000752
753\subsubsection{Break Statements and Else Clauses on Loops}
754
755The {\tt break} statement breaks out of the smallest enclosing {\tt for}
756or {\tt while} loop.
757Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000758loop terminates through exhaustion of the list (with {\tt for}) or when
759the condition becomes false (with {\tt while}) but not when the loop is
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000760terminated by a {\tt break} statement.
761This is exemplified by the following loop, which searches for a list
762item of value 0:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000763\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000764>>> for n in range(2, 10):
765... for x in range(2, n):
766... if n % x = 0:
767... print n, 'equals', x, '*', n/x
768... break
769... else:
770... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000771...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00007722 is a prime number
7733 is a prime number
7744 equals 2 * 2
7755 is a prime number
7766 equals 2 * 3
7777 is a prime number
7788 equals 2 * 4
7799 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000780>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000781\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000782
783\subsubsection{Pass Statements}
784
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000785The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000786It can be used when a statement is required syntactically but the
787program requires no action.
788For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000789\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000790>>> while 1:
791... pass # Busy-wait for keyboard interrupt
792...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000793\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000794
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000795\subsubsection{Conditions Revisited}
796
797XXX To Be Done.
798
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000799\subsection{Defining Functions}
800
801We can create a function that writes the Fibonacci series to an
802arbitrary boundary:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000803\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000804>>> def fib(n): # write Fibonacci series up to n
805... a, b = 0, 1
806... while b <= n:
807... print b,
808... a, b = b, a+b
809...
810>>> # Now call the function we just defined:
811>>> fib(2000)
8121 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
813>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000814\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000815The keyword
816{\tt def}
817introduces a function
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000818{\em definition}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000819It must be followed by the function name and the parenthesized list of
820formal parameters.
821The statements that form the body of the function starts at the next
822line, indented by a tab stop.
823The
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000824{\em execution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000825of a function introduces a new symbol table used for the local variables
826of the function.
827More precisely, all variable assignments in a function store the value
828in the local symbol table; variable references first look in the local
829symbol table, then in the global symbol table, and then in the table of
830built-in names.
831Thus, the global symbol table is
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000832{\em read-only}
833within a function.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000834The actual parameters (arguments) to a function call are introduced in
835the local symbol table of the called function when it is called;
836thus, arguments are passed using
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000837{\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000838\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000839 Actually, {\em call by object reference} would be a better
840 description, since if a mutable object is passed, the caller
841 will see any changes the callee makes to it (e.g., items
842 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000843}
844When a function calls another function, a new local symbol table is
845created for that call.
846
847A function definition introduces the function name in the global symbol
848table.
849The value has a type that is recognized by the interpreter as a
850user-defined function.
851This value can be assigned to another name which can then also be used
852as a function.
853This serves as a general renaming mechanism:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000854\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000855>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000856<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000857>>> f = fib
858>>> f(100)
8591 1 2 3 5 8 13 21 34 55 89
860>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000861\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000862You might object that
863{\tt fib}
864is not a function but a procedure.
Guido van Rossum4410c751991-06-04 20:22:18 +0000865In Python, as in C, procedures are just functions that don't return a
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000866value.
867In fact, technically speaking, procedures do return a value, albeit a
868rather boring one.
869This value is called {\tt None} (it's a built-in name).
870Writing the value {\tt None} is normally suppressed by the interpreter
871if it would be the only value written.
872You can see it if you really want to:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000873\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000874>>> print fib(0)
875None
876>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000877\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000878It is simple to write a function that returns a list of the numbers of
879the Fibonacci series, instead of printing it:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000880\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000881>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000882... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000883... a, b = 0, 1
884... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000885... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000886... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000887... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000888...
889>>> f100 = fib2(100) # call it
890>>> f100 # write the result
891[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
892>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000893\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +0000894This example, as usual, demonstrates some new Python features:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000895\begin{itemize}
896\item
897The
898{\tt return}
899statement returns with a value from a function.
900{\tt return}
901without an expression argument is used to return from the middle of a
902procedure (falling off the end also returns from a proceduce).
903\item
904The statement
905{\tt ret.append(b)}
906calls a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000907{\em method}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000908of the list object
909{\tt ret}.
910A method is a function that `belongs' to an object and is named
911{\tt obj.methodname},
912where
913{\tt obj}
914is some object (this may be an expression), and
915{\tt methodname}
916is the name of a method that is defined by the object's type.
917Different types define different methods.
918Methods of different types may have the same name without causing
919ambiguity.
920See the section on classes, later, to find out how you can define your
921own object types and methods.
922The method
923{\tt append}
924shown in the example, is defined for list objects; it adds a new element
925at the end of the list.
926In this case it is equivalent to
927{\tt ret = ret + [b]},
928but more efficient.%
929\footnote{
930 There is a subtle semantic difference if the object
931 is referenced from more than one place.
932}
933\end{itemize}
934The list object type has two more methods:
Guido van Rossum7d9f8d71991-01-22 11:45:00 +0000935\begin{description}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000936\item[{\tt insert(i, x)}]
937Inserts an item at a given position.
938The first argument is the index of the element before which to insert,
939so {\tt a.insert(0, x)} inserts at the front of the list, and
940{\tt a.insert(len(a), x)} is equivalent to {\tt a.append(x)}.
941\item[{\tt sort()}]
942Sorts the elements of the list.
Guido van Rossum7d9f8d71991-01-22 11:45:00 +0000943\end{description}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000944For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000945\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000946>>> a = [10, 100, 1, 1000]
947>>> a.insert(2, -1)
948>>> a
949[10, 100, -1, 1, 1000]
950>>> a.sort()
951>>> a
952[-1, 1, 10, 100, 1000]
953>>> # Strings are sorted according to ASCII:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000954>>> b = ['Mary', 'had', 'a', 'little', 'boy']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000955>>> b.sort()
956>>> b
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000957['Mary', 'a', 'boy', 'had', 'little']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000958>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000959\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000960
961\subsection{Modules}
962
Guido van Rossum4410c751991-06-04 20:22:18 +0000963If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000964definitions you have made (functions and variables) are lost.
965Therefore, if you want to write a somewhat longer program, you are
966better off using a text editor to prepare the input for the interpreter
967and run it with that file as input instead.
968This is known as creating a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000969{\em script}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000970As your program gets longer, you may want to split it into several files
971for easier maintenance.
972You may also want to use a handy function that you've written in several
973programs without copying its definition into each program.
Guido van Rossum4410c751991-06-04 20:22:18 +0000974To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000975them in a script or in an interactive instance of the interpreter.
976Such a file is called a
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000977{\em module};
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000978definitions from a module can be
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000979{\em imported}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000980into other modules or into the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000981{\em main}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000982module (the collection of variables that you have access to in
983a script and in calculator mode).
984
Guido van Rossum4410c751991-06-04 20:22:18 +0000985A module is a file containing Python definitions and statements.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000986The file name is the module name with the suffix
987{\tt .py}
988appended.
989For instance, use your favorite text editor to create a file called
990{\tt fibo.py}
991in the current directory with the following contents:
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000992\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000993# Fibonacci numbers module
994
995def fib(n): # write Fibonacci series up to n
996 a, b = 0, 1
997 while b <= n:
998 print b,
999 a, b = b, a+b
1000
1001def fib2(n): # return Fibonacci series up to n
1002 ret = []
1003 a, b = 0, 1
1004 while b <= n:
1005 ret.append(b)
1006 a, b = b, a+b
1007 return ret
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001008\end{verbatim}\ecode
Guido van Rossum4410c751991-06-04 20:22:18 +00001009Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001010following command:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001011\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001012>>> import fibo
1013>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001014\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001015This does not enter the names of the functions defined in
1016{\tt fibo}
1017directly in the symbol table; it only enters the module name
1018{\tt fibo}
1019there.
1020Using the module name you can access the functions:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001021\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001022>>> fibo.fib(1000)
10231 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1024>>> fibo.fib2(100)
1025[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1026>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001027\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001028If you intend to use a function often you can assign it to a local name:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001029\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001030>>> fib = fibo.fib
1031>>> fib(500)
10321 1 2 3 5 8 13 21 34 55 89 144 233 377
1033>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001034\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001035
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001036\subsubsection{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001037
1038A module can contain executable statements as well as function
1039definitions.
1040These statements are intended to initialize the module.
1041They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001042{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001043time the module is imported somewhere.%
1044\footnote{
1045 In fact function definitions are also `statements' that are
1046 `executed'; the execution enters the function name in the
1047 module's global symbol table.
1048}
1049
1050Each module has its own private symbol table, which is used as the
1051global symbol table by all functions defined in the module.
1052Thus, the author of a module can use global variables in the module
1053without worrying about accidental clashes with a user's global
1054variables.
1055On the other hand, if you know what you are doing you can touch a
1056module's global variables with the same notation used to refer to its
1057functions,
1058{\tt modname.itemname}.
1059
1060Modules can import other modules.
1061It is customary but not required to place all
1062{\tt import}
1063statements at the beginning of a module (or script, for that matter).
1064The imported module names are placed in the importing module's global
1065symbol table.
1066
1067There is a variant of the
1068{\tt import}
1069statement that imports names from a module directly into the importing
1070module's symbol table.
1071For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001072\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001073>>> from fibo import fib, fib2
1074>>> fib(500)
10751 1 2 3 5 8 13 21 34 55 89 144 233 377
1076>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001077\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001078This does not introduce the module name from which the imports are taken
1079in the local symbol table (so in the example, {\tt fibo} is not
1080defined).
1081
1082There is even a variant to import all names that a module defines:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001083\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001084>>> from fibo import *
1085>>> fib(500)
10861 1 2 3 5 8 13 21 34 55 89 144 233 377
1087>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001088\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001089This imports all names except those beginning with an underscore
1090({\tt \_}).
1091
1092\subsubsection{Standard Modules}
1093
Guido van Rossum4410c751991-06-04 20:22:18 +00001094Python comes with a library of standard modules, described in a separate
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001095document (Python Library and Module Reference).
1096Some modules are built into the interpreter; these provide access to
1097operations that are not part of the core of the language but are
1098nevertheless built in, either for efficiency or to provide access to
1099operating system primitives such as system calls.
1100The set of such modules is a configuration option; e.g., the
1101{\tt amoeba}
1102module is only provided on systems that somehow support Amoeba
1103primitives.
1104One particular module deserves some attention:
1105{\tt sys},
Guido van Rossum4410c751991-06-04 20:22:18 +00001106which is built into every Python interpreter.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001107The variables
1108{\tt sys.ps1}
1109and
1110{\tt sys.ps2}
1111define the strings used as primary and secondary prompts:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001112\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001113>>> import sys
1114>>> sys.ps1
1115'>>> '
1116>>> sys.ps2
1117'... '
1118>>> sys.ps1 = 'C> '
1119C> print 'Yuck!'
1120Yuck!
1121C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001122\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001123These two variables are only defined if the interpreter is in
1124interactive mode.
1125
1126The variable
1127{\tt sys.path}
1128is a list of strings that determine the interpreter's search path for
1129modules.
1130It is initialized to a default path taken from the environment variable
1131{\tt PYTHONPATH},
1132or from a built-in default if
1133{\tt PYTHONPATH}
1134is not set.
1135You can modify it using standard list operations, e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001136\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001137>>> import sys
1138>>> sys.path.append('/ufs/guido/lib/python')
1139>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001140\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001141
1142\subsection{Errors and Exceptions}
1143
1144Until now error messages haven't yet been mentioned, but if you have
1145tried out the examples you have probably seen some.
1146There are (at least) two distinguishable kinds of errors:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001147{\em syntax\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001148and
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001149{\em exceptions}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001150
1151\subsubsection{Syntax Errors}
1152
1153Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001154kind of complaint you get while you are still learning Python:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001155\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001156>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001157Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001158while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001159 ^
1160Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001161>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001162\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001163The parser repeats the offending line and displays a little `arrow'
1164pointing at the earliest point in the line where the error was detected.
1165The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001166{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001167the arrow: in the example, the error is detected at the keyword
1168{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001169File name and line number are printed so you know where to look in case
1170the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001171
1172\subsubsection{Exceptions}
1173
1174Even if a statement or expression is syntactically correct, it may cause
1175an error when an attempt is made to execute it:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001176\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001177>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001178Unhandled exception: run-time error: integer division by zero
1179Stack backtrace (innermost last):
1180 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001181>>> 4 + foo*3
1182Unhandled exception: undefined name: foo
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001183Stack backtrace (innermost last):
1184 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001185>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001186Unhandled exception: type error: illegal argument type for built-in operation
1187Stack backtrace (innermost last):
1188 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001189>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001190\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001191Errors detected during execution are called
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001192{\em exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001193and are not unconditionally fatal: you will soon learn how to handle
Guido van Rossum4410c751991-06-04 20:22:18 +00001194them in Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001195Most exceptions are not handled by programs, however, and result
1196in error messages as shown here.
1197
1198The first line of the error message indicates what happened.
1199Exceptions come in different types, and the type is printed as part of
1200the message: the types in the example are
1201{\tt run-time error},
1202{\tt undefined name}
1203and
1204{\tt type error}.
1205The rest of the line is a detail whose interpretation depends on the
1206exception type.
1207
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001208The rest of the error message shows the context where the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001209exception happened.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001210In general it contains a stack backtrace listing source lines; however,
1211it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001212
1213Here is a summary of the most common exceptions:
1214\begin{itemize}
1215\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001216{\em Run-time\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001217are generally caused by wrong data used by the program; this can be the
1218programmer's fault or caused by bad input.
1219The detail states the cause of the error in more detail.
1220\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001221{\em Undefined\ name}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001222errors are more serious: these are usually caused by misspelled
1223identifiers.%
1224\footnote{
1225 The parser does not check whether names used in a program are at
1226 all defined elsewhere in the program, so such checks are
1227 postponed until run-time. The same holds for type checking.
1228}
1229The detail is the offending identifier.
1230\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001231{\em Type\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001232are also pretty serious: this is another case of using wrong data (or
1233better, using data the wrong way), but here the error can be glanced
1234from the object type(s) alone.
1235The detail shows in what context the error was detected.
1236\end{itemize}
1237
1238\subsubsection{Handling Exceptions}
1239
1240It is possible to write programs that handle selected exceptions.
1241Look at the following example, which prints a table of inverses of
1242some floating point numbers:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001243\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001244>>> numbers = [0.3333, 2.5, 0.0, 10.0]
1245>>> for x in numbers:
1246... print x,
1247... try:
1248... print 1.0 / x
1249... except RuntimeError:
1250... print '*** has no inverse ***'
1251...
12520.3333 3.00030003
12532.5 0.4
12540 *** has no inverse ***
125510 0.1
1256>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001257\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001258The {\tt try} statement works as follows.
1259\begin{itemize}
1260\item
1261First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001262{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001263(the statement(s) between the {\tt try} and {\tt except} keywords) is
1264executed.
1265\item
1266If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001267{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001268is skipped and execution of the {\tt try} statement is finished.
1269\item
1270If an exception occurs during execution of the try clause, and its
1271type matches the exception named after the {\tt except} keyword, the
1272rest of the try clause is skipped, the except clause is executed, and
1273then execution continues after the {\tt try} statement.
1274\item
1275If an exception occurs which does not match the exception named in the
1276except clause, it is passed on to outer try statements; if no handler is
1277found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001278{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001279and execution stops with a message as shown above.
1280\end{itemize}
1281A {\tt try} statement may have more than one except clause, to specify
1282handlers for different exceptions.
1283At most one handler will be executed.
1284Handlers only handle exceptions that occur in the corresponding try
1285clause, not in other handlers of the same {\tt try} statement.
1286An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001287e.g.:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001288\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001289... except (RuntimeError, TypeError, NameError):
1290... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001291\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001292The last except clause may omit the exception name(s), to serve as a
1293wildcard.
1294Use this with extreme caution!
1295
1296When an exception occurs, it may have an associated value, also known as
1297the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001298{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001299The presence and type of the argument depend on the exception type.
1300For exception types which have an argument, the except clause may
1301specify a variable after the exception name (or list) to receive the
1302argument's value, as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001303\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001304>>> try:
1305... foo()
1306... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001307... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001308...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001309name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001310>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001311\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001312If an exception has an argument, it is printed as the third part
1313(`detail') of the message for unhandled exceptions.
1314
1315Standard exception names are built-in identifiers (not reserved
1316keywords).
1317These are in fact string objects whose
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001318{\em object\ identity}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001319(not their value!) identifies the exceptions.%
1320\footnote{
1321 There should really be a separate exception type; it is pure
1322 laziness that exceptions are identified by strings, and this may
1323 be fixed in the future.
1324}
1325The string is printed as the second part of the message for unhandled
1326exceptions.
1327Their names and values are:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001328\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001329EOFError 'end-of-file read'
1330KeyboardInterrupt 'keyboard interrupt'
1331MemoryError 'out of memory' *
1332NameError 'undefined name' *
1333RuntimeError 'run-time error' *
1334SystemError 'system error' *
1335TypeError 'type error' *
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001336\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001337The meanings should be clear enough.
1338Those exceptions with a {\tt *} in the third column have an argument.
1339
1340Exception handlers don't just handle exceptions if they occur
1341immediately in the try clause, but also if they occur inside functions
1342that are called (even indirectly) in the try clause.
1343For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001344\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001345>>> def this_fails():
1346... x = 1/0
1347...
1348>>> try:
1349... this_fails()
1350... except RuntimeError, detail:
1351... print 'Handling run-time error:', detail
1352...
Guido van Rossum67fa1601991-04-23 14:14:57 +00001353Handling run-time error: integer division by zero
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001354>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001355\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001356
1357\subsubsection{Raising Exceptions}
1358
1359The {\tt raise} statement allows the programmer to force a specified
1360exception to occur.
1361For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001362\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001363>>> raise NameError, 'Hi There!'
1364Unhandled exception: undefined name: Hi There!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001365Stack backtrace (innermost last):
1366 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001367>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001368\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001369The first argument to {\tt raise} names the exception to be raised.
1370The optional second argument specifies the exception's argument.
1371
1372\subsubsection{User-defined Exceptions}
1373
1374Programs may name their own exceptions by assigning a string to a
1375variable.
1376For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001377\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001378>>> my_exc = 'nobody likes me!'
1379>>> try:
1380... raise my_exc, 2*2
1381... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001382... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001383...
1384My exception occured, value: 4
1385>>> raise my_exc, 1
1386Unhandled exception: nobody likes me!: 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001387Stack backtrace (innermost last):
1388 File "<stdin>", line 7
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001389>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001390\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001391Many standard modules use this to report errors that may occur in
1392functions they define.
1393
1394\subsubsection{Defining Clean-up Actions}
1395
1396The {\tt try} statement has another optional clause which is intended to
1397define clean-up actions that must be executed under all circumstances.
1398For example:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001399\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001400>>> try:
1401... raise KeyboardInterrupt
1402... finally:
1403... print 'Goodbye, world!'
1404...
1405Goodbye, world!
1406Unhandled exception: keyboard interrupt
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001407Stack backtrace (innermost last):
1408 File "<stdin>", line 2
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001409>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001410\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001411The
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001412{\em finally\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001413must follow the except clauses(s), if any.
1414It is executed whether or not an exception occurred.
1415If the exception is handled, the finally clause is executed after the
1416handler (and even if another exception occurred in the handler).
1417It is also executed when the {\tt try} statement is left via a
1418{\tt break} or {\tt return} statement.
1419
1420\subsection{Classes}
1421
Guido van Rossum4410c751991-06-04 20:22:18 +00001422Classes in Python make it possible to play the game of encapsulation in a
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001423somewhat different way than it is played with modules.
1424Classes are an advanced topic and are probably best skipped on the first
Guido van Rossum4410c751991-06-04 20:22:18 +00001425encounter with Python.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001426
1427\subsubsection{Prologue}
1428
Guido van Rossum4410c751991-06-04 20:22:18 +00001429Python's class mechanism is not particularly elegant, but quite powerful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001430It is a mixture of the class mechanisms found in C++ and Modula-3.
Guido van Rossum4410c751991-06-04 20:22:18 +00001431As is true for modules, classes in Python do not put an absolute barrier
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001432between definition and user, but rather rely on the politeness of the
1433user not to ``break into the definition.''
1434The most important features of classes are retained with full power,
1435however: the class inheritance mechanism allows multiple base classes,
1436a derived class can override any method of its base class(es), a method
1437can call the method of a base class with the same name.
1438Objects can contain an arbitrary amount of private data.
1439
1440In C++ terminology, all class members (including data members) are
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001441{\em public},
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001442and all member functions (methods) are
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001443{\em virtual}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001444There are no special constructors or destructors.
1445As in Modula-3, there are no shorthands for referencing the object's
1446members from its methods: the method function is declared with an
1447explicit first argument representing the object, which is provided
1448implicitly by the call.
1449As in Smalltalk, classes themselves are objects, albeit in the wider
Guido van Rossum4410c751991-06-04 20:22:18 +00001450sense of the word: in Python, all data types are objects.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001451This provides semantics for renaming or aliasing.
1452But, just like in C++ or Modula-3, the built-in types cannot be used as
1453base classes for extension by the user.
1454Also, like Modula-3 but unlike C++, the built-in operators with special
1455syntax (arithmetic operators, subscripting etc.) cannot be redefined for
1456class members.%
1457\footnote{
1458 They can be redefined for new object types implemented in C in
1459 extensions to the interpreter, however. It would require only a
1460 naming convention and a relatively small change to the
1461 interpreter to allow operator overloading for classes, so
1462 perhaps someday...
1463}
1464
1465\subsubsection{A Simple Example}
1466
1467Consider the following example, which defines a class {\tt Set}
1468representing a (finite) mathematical set with operations to add and
1469remove elements, a membership test, and a request for the size of the
1470set.
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001471\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001472class Set():
1473 def new(self):
1474 self.elements = []
1475 return self
1476 def add(self, e):
1477 if e not in self.elements:
1478 self.elements.append(e)
1479 def remove(self, e):
1480 if e in self.elements:
1481 for i in range(len(self.elements)):
1482 if self.elements[i] = e:
1483 del self.elements[i]
1484 break
1485 def is_element(self, e):
1486 return e in self.elements
1487 def size(self):
1488 return len(self.elements)
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001489\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001490Note that the class definition looks like a big compound statement,
1491with all the function definitons indented repective to the
1492{\tt class}
1493keyword.
1494
1495Let's assume that this
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001496{\em class\ definition}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001497is the only contents of the module file
1498{\tt SetClass.py}.
Guido van Rossum4410c751991-06-04 20:22:18 +00001499We can then use it in a Python program as follows:
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001500\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001501>>> from SetClass import Set
1502>>> a = Set().new() # create a Set object
1503>>> a.add(2)
1504>>> a.add(3)
1505>>> a.add(1)
1506>>> a.add(1)
1507>>> if a.is_element(3): print '3 is in the set'
1508...
15093 is in the set
1510>>> if not a.is_element(4): print '4 is not in the set'
1511...
15124 is not in the set
1513>>> print 'a has', a.size(), 'elements'
1514a has 3 elements
1515>>> a.remove(1)
1516>>> print 'now a has', a.size(), 'elements'
1517>>>
1518now a has 2 elements
1519>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001520\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001521From the example we learn in the first place that the functions defined
1522in the class (e.g.,
1523{\tt add})
1524can be called using the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001525{\em member}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001526notation for the object
1527{\tt a}.
1528The member function is called with one less argument than it is defined:
1529the object is implicitly passed as the first argument.
1530Thus, the call
1531{\tt a.add(2)}
1532is equivalent to
1533{\tt Set.add(a, 2)}.
1534
Guido van Rossum4410c751991-06-04 20:22:18 +00001535XXX This section is not complete yet! Inheritance!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001536
1537\section{XXX P.M.}
1538
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001539\begin{itemize}
1540\item The {\tt del} statement.
1541\item The {\tt dir()} function.
1542\item Tuples.
1543\item Dictionaries.
1544\item Objects and types in general.
1545\item Backquotes.
Guido van Rossum4410c751991-06-04 20:22:18 +00001546\item Output formatting.
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001547\item And/Or/Not.
Guido van Rossum4410c751991-06-04 20:22:18 +00001548\item ``.pyc'' files.
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001549\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001550
1551\end{document}