blob: e45b6e7f6f369670d0042ec4d3e743f65cec180a [file] [log] [blame]
Guido van Rossum37953781992-04-06 14:04:04 +00001\documentstyle[twoside,11pt,myformat]{report}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002
3\title{\bf
Guido van Rossum6fc178f1991-08-16 09:13:42 +00004 Python Tutorial
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00005}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00006
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00007\author{
8 Guido van Rossum \\
9 Dept. CST, CWI, Kruislaan 413 \\
10 1098 SJ Amsterdam, The Netherlands \\
11 E-mail: {\tt guido@cwi.nl}
12}
13
14\begin{document}
15
16\pagenumbering{roman}
17
18\maketitle
19
20\begin{abstract}
21
22\noindent
Guido van Rossum4410c751991-06-04 20:22:18 +000023Python is a simple, yet powerful programming language that bridges the
Guido van Rossum6fc178f1991-08-16 09:13:42 +000024gap between C and shell programming, and is thus ideally suited for
25``throw-away programming''
26and rapid prototyping. Its syntax is put
27together from constructs borrowed from a variety of other languages;
28most prominent are influences from ABC, C, Modula-3 and Icon.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000029
Guido van Rossum4410c751991-06-04 20:22:18 +000030The Python interpreter is easily extended with new functions and data
Guido van Rossum6fc178f1991-08-16 09:13:42 +000031types implemented in C. Python is also suitable as an extension
32language for highly customizable C applications such as editors or
33window managers.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000034
Guido van Rossum4410c751991-06-04 20:22:18 +000035Python is available for various operating systems, amongst which
Guido van Rossum6fc178f1991-08-16 09:13:42 +000036several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
37and MS-DOS.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000038
Guido van Rossum6fc178f1991-08-16 09:13:42 +000039This tutorial introduces the reader informally to the basic concepts
40and features of the Python language and system. It helps to have a
41Python interpreter handy for hands-on experience, but as the examples
42are self-contained, the tutorial can be read off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000043
Guido van Rossum481ae681991-11-25 17:28:03 +000044For a description of standard objects and modules, see the {\em Python
45Library Reference} document. The {\em Python Reference Manual} gives
46a more formal definition of the language.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000047
48\end{abstract}
49
50\pagebreak
Guido van Rossumcdc93551992-02-11 15:53:13 +000051{
52\parskip = 0mm
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000053\tableofcontents
Guido van Rossumcdc93551992-02-11 15:53:13 +000054}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000055
56\pagebreak
57
58\pagenumbering{arabic}
59
Guido van Rossum5e0759d1992-08-07 16:06:24 +000060
Guido van Rossum6fc178f1991-08-16 09:13:42 +000061\chapter{Whetting Your Appetite}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000062
Guido van Rossum6fc178f1991-08-16 09:13:42 +000063If you ever wrote a large shell script, you probably know this
64feeling: you'd love to add yet another feature, but it's already so
65slow, and so big, and so complicated; or the feature involves a system
66call or other funcion that is only accessible from C \ldots Usually
67the problem at hand isn't serious enough to warrant rewriting the
68script in C; perhaps because the problem requires variable-length
69strings or other data types (like sorted lists of file names) that are
70easy in the shell but lots of work to implement in C; or perhaps just
71because you're not sufficiently familiar with C.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000072
Guido van Rossum6fc178f1991-08-16 09:13:42 +000073In such cases, Python may be just the language for you. Python is
74simple to use, but it is a real programming language, offering much
75more structure and support for large programs than the shell has. On
76the other hand, it also offers much more error checking than C, and,
77being a {\em very-high-level language}, it has high-level data types
78built in, such as flexible arrays and dictionaries that would cost you
79days to implement efficiently in C. Because of its more general data
80types Python is applicable to a much larger problem domain than {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +000081Awk} or even {\em Perl}, yet many things are at least as easy in
82Python as in those languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000083
Guido van Rossum6fc178f1991-08-16 09:13:42 +000084Python allows you to split up your program in modules that can be
85reused in other Python programs. It comes with a large collection of
Guido van Rossuma8d754e1992-01-07 16:44:35 +000086standard modules that you can use as the basis of your programs --- or
87as examples to start learning to program in Python. There are also
88built-in modules that provide things like file I/O, system calls,
89sockets, and even a generic interface to window systems (STDWIN).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000090
Guido van Rossuma8d754e1992-01-07 16:44:35 +000091Python is an interpreted language, which can save you considerable time
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000092during program development because no compilation and linking is
Guido van Rossum6fc178f1991-08-16 09:13:42 +000093necessary. The interpreter can be used interactively, which makes it
94easy to experiment with features of the language, to write throw-away
95programs, or to test functions during bottom-up program development.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000096It is also a handy desk calculator.
97
Guido van Rossum6fc178f1991-08-16 09:13:42 +000098Python allows writing very compact and readable programs. Programs
99written in Python are typically much shorter than equivalent C
100programs, for several reasons:
101\begin{itemize}
102\item
103the high-level data types allow you to express complex operations in a
104single statement;
105\item
106statement grouping is done by indentation instead of begin/end
107brackets;
108\item
109no variable or argument declarations are necessary.
110\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000111
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000112Python is {\em extensible}: if you know how to program in C it is easy
113to add a new built-in
114function or
115module to the interpreter, either to
116perform critical operations at maximum speed, or to link Python
117programs to libraries that may only be available in binary form (such
118as a vendor-specific graphics library). Once you are really hooked,
119you can link the Python interpreter into an application written in C
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000120and use it as an extension or command language for that application.
121
122By the way, the language is named after the BBC show ``Monty
123Python's Flying Circus'' and has nothing to do with nasty reptiles...
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000124
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000125\section{Where From Here}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000126
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000127Now that you are all excited about Python, you'll want to examine it
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000128in some more detail. Since the best way to learn a language is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000129using it, you are invited here to do so.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000130
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000131In the next chapter, the mechanics of using the interpreter are
132explained. This is rather mundane information, but essential for
133trying out the examples shown later.
134
Guido van Rossum4410c751991-06-04 20:22:18 +0000135The rest of the tutorial introduces various features of the Python
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000136language and system though examples, beginning with simple
137expressions, statements and data types, through functions and modules,
138and finally touching upon advanced concepts like exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000139
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000140When you're through with the turtorial (or just getting bored), you
141should read the Library Reference, which gives complete (though terse)
142reference material about built-in and standard types, functions and
143modules that can save you a lot of time when writing Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000144
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000145
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000146\chapter{Using the Python Interpreter}
147
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000148\section{Invoking the Interpreter}
149
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000150The Python interpreter is usually installed as {\tt /usr/local/python}
151on those machines where it is available; putting {\tt /usr/local} in
152your {\UNIX} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000153typing the command
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000154
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000155\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000156python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000157\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000158%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000159to the shell. Since the choice of the directory where the interpreter
160lives is an installation option, other places are possible; check with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000161your local Python guru or system administrator. (E.g., {\tt
162/usr/local/bin/python} is a popular alternative location.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000163
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000164The interpreter operates somewhat like the {\UNIX} shell: when called
165with standard input connected to a tty device, it reads and executes
166commands interactively; when called with a file name argument or with
167a file as standard input, it reads and executes a {\em script} from
168that file.
169
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000170A third way of starting the interpreter is
171``{\tt python -c command [arg] ...}'', which
172executes the statement(s) in {\tt command}, analogous to the shell's
173{\tt -c} option. Since Python statements often contain spaces or other
174characters that are special to the shell, it is best to quote {\tt
175command} in its entirety with double quotes.
176
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000177Note that there is a difference between ``{\tt python file}'' and
178``{\tt python $<$file}''. In the latter case, input requests from the
Guido van Rossum573805a1992-03-06 10:56:03 +0000179program, such as calls to {\tt input()} and {\tt raw_input()}, are
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000180satisfied from {\em file}. Since this file has already been read
181until the end by the parser before the program starts executing, the
182program will encounter EOF immediately. In the former case (which is
183usually what you want) they are satisfied from whatever file or device
184is connected to standard input of the Python interpreter.
185
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000186\subsection{Argument Passing}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000187
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000188When known to the interpreter, the script name and additional
189arguments thereafter are passed to the script in the variable {\tt
190sys.argv}, which is a list of strings. Its length is at least one;
191when no script and no arguments are given, {\tt sys.argv[0]} is an
192empty string. When the script name is given as {\tt '-'} (meaning
193standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c
194command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options
195found after {\tt -c command} are not consumed by the Python
196interpreter's option processing but left in {\tt sys.argv} for the
197command to handle.
198
199\subsection{Interactive Mode}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000200
Guido van Rossumdd010801991-06-07 14:31:11 +0000201When commands are read from a tty, the interpreter is said to be in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000202{\em interactive\ mode}. In this mode it prompts for the next command
203with the {\em primary\ prompt}, usually three greater-than signs ({\tt
204>>>}); for continuation lines it prompts with the {\em secondary\
205prompt}, by default three dots ({\tt ...}). Typing an EOF (Control-D)
206at the primary prompt causes the interpreter to exit with a zero exit
207status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000208
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000209The interpreter prints a welcome message stating its version number
210and a copyright notice before printing the first prompt, e.g.:
211
212\bcode\begin{verbatim}
213python
214Python 0.9.5 (Jan 2 1992).
215Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
216>>>
217\end{verbatim}\ecode
218
219\section{The Interpreter and its Environment}
220
221\subsection{Error Handling}
222
223When an error occurs, the interpreter prints an error
224message and a stack trace. In interactive mode, it then returns to
225the primary prompt; when input came from a file, it exits with a
226nonzero exit status after printing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000227the stack trace. (Exceptions handled by an {\tt except} clause in a
228{\tt try} statement are not errors in this context.) Some errors are
229unconditionally fatal and cause an exit with a nonzero exit; this
230applies to internal inconsistencies and some cases of running out of
231memory. All error messages are written to the standard error stream;
232normal output from the executed commands is written to standard
233output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000235Typing the interrupt character (usually Control-C or DEL) to the
236primary or secondary prompt cancels the input and returns to the
237primary prompt.%
238\footnote{
239 A problem with the GNU Readline package may prevent this.
240}
241Typing an interrupt while a command is executing raises the {\tt
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000242KeyboardInterrupt} exception, which may be handled by a {\tt try}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000243statement.
244
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000245\subsection{The Module Search Path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000246
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000247When a module named {\tt foo} is imported, the interpreter searches
248for a file named {\tt foo.py} in the list of directories specified by
249the environment variable {\tt PYTHONPATH}. It has the same syntax as
250the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
251directory names. When {\tt PYTHONPATH} is not set, an
252installation-dependent default path is used, usually {\tt
253.:/usr/local/lib/python}.
254
255Actually, modules are searched in the list of directories given by the
256variable {\tt sys.path} which is initialized from {\tt PYTHONPATH} or
257the installation-dependent default. This allows Python programs that
258know what they're doing to modify or replace the module search path.
259See the section on Standard Modules later.
260
261\subsection{``Compiled'' Python files}
262
263As an important speed-up of the start-up time for short programs that
264use a lot of standard modules, if a file called {\tt foo.pyc} exists
265in the directory where {\tt foo.py} is found, this is assumed to
266contain an already-``compiled'' version of the module {\tt foo}. The
267modification time of the version of {\tt foo.py} used to create {\tt
268foo.pyc} is recorded in {\tt foo.pyc}, and the file is ignored if
269these don't match.
270
271Whenever {\tt foo.py} is successfully compiled, an attempt is made to
272write the compiled version to {\tt foo.pyc}. It is not an error if
273this attempt fails; if for any reason the file is not written
274completely, the resulting {\tt foo.pyc} file will be recognized as
275invalid and thus ignored later.
276
277\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000278
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000279On BSD'ish {\UNIX} systems, Python scripts can be made directly
280executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000281
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000282\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000283#! /usr/local/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000284\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000285%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000286(assuming that's the name of the interpreter) at the beginning of the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000287script and giving the file an executable mode. The {\tt \#!} must be
288the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000289
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000290\section{Interactive Input Editing and History Substitution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000291
Guido van Rossum4410c751991-06-04 20:22:18 +0000292Some versions of the Python interpreter support editing of the current
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000293input line and history substitution, similar to facilities found in
294the Korn shell and the GNU Bash shell. This is implemented using the
295{\em GNU\ Readline} library, which supports Emacs-style and vi-style
296editing. This library has its own documentation which I won't
297duplicate here; however, the basics are easily explained.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000298
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000299Perhaps the quickest check to see whether command line editing is
300supported is typing Control-P to the first Python prompt you get. If
301it beeps, you have command line editing. If nothing appears to
302happen, or if \verb/^P/ is echoed, you can skip the rest of this
303section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000304
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000305\subsection{Line Editing}
306
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000307If supported, input line editing is active whenever the interpreter
308prints a primary or secondary prompt. The current line can be edited
309using the conventional Emacs control characters. The most important
310of these are: C-A (Control-A) moves the cursor to the beginning of the
311line, C-E to the end, C-B moves it one position to the left, C-F to
312the right. Backspace erases the character to the left of the cursor,
313C-D the character to its right. C-K kills (erases) the rest of the
314line to the right of the cursor, C-Y yanks back the last killed
315string. C-underscore undoes the last change you made; it can be
316repeated for cumulative effect.
317
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000318\subsection{History Substitution}
319
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000320History substitution works as follows. All non-empty input lines
321issued are saved in a history buffer, and when a new prompt is given
322you are positioned on a new line at the bottom of this buffer. C-P
323moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000324Any line in the history buffer can be edited; an asterisk appears in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000325front of the prompt to mark a line as modified. Pressing the Return
326key passes the current line to the interpreter. C-R starts an
327incremental reverse search; C-S starts a forward search.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000328
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000329\subsection{Key Bindings}
330
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000331The key bindings and some other parameters of the Readline library can
332be customized by placing commands in an initialization file called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000333{\tt \$HOME/.inputrc}. Key bindings have the form
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000334
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000335\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000336key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000337\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000338%
339or
340
341\bcode\begin{verbatim}
342"string": function-name
343\end{verbatim}\ecode
344%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000345and options can be set with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000346
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000347\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000348set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000349\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000350%
351For example:
352
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000353\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000354# I prefer vi-style editing:
355set editing-mode vi
356# Edit using a single line:
357set horizontal-scroll-mode On
358# Rebind some keys:
359Meta-h: backward-kill-word
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000360"\C-u": universal-argument
361"\C-x\C-r": re-read-init-file
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000362\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000363%
Guido van Rossum4410c751991-06-04 20:22:18 +0000364Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000365instead of Readline's default filename completion function. If you
366insist, you can override this by putting
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000367
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000368\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000369TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000370\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000371%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000372in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000373indented continuation lines...)
374
375\subsection{Commentary}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000376
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000377This facility is an enormous step forward compared to previous
378versions of the interpreter; however, some wishes are left: It would
379be nice if the proper indentation were suggested on continuation lines
380(the parser knows if an indent token is required next). The
381completion mechanism might use the interpreter's symbol table. A
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000382command to check (or even suggest) matching parentheses, quotes etc.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000383would also be useful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000384
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000385
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000386\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000387
388In the following examples, input and output are distinguished by the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000389presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
390the example, you must type everything after the prompt, when the
391prompt appears; lines that do not begin with a prompt are output from
392the interpreter.%
393\footnote{
394 I'd prefer to use different fonts to distinguish input
395 from output, but the amount of LaTeX hacking that would require
396 is currently beyond my ability.
397}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000398Note that a secondary prompt on a line by itself in an example means
399you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000400
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000401\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000402
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000403Let's try some simple Python commands. Start the interpreter and wait
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000404for the primary prompt, {\tt >>>}. (It shouldn't take long.)
405
406\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000407
408The interpreter acts as a simple calculator: you can type an
409expression at it and it will write the value. Expression syntax is
410straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000411work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000412can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000413
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000414\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000415>>> # This is a comment
416>>> 2+2
4174
418>>>
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000419>>> (50-5*6)/4
4205
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000421>>> # Division truncates towards zero:
422>>> 7/3
4232
424>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000425\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000426%
427Like in C, the equal sign ({\tt =}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000428variable. The value of an assignment is not written:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000429
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000430\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000431>>> width = 20
432>>> height = 5*9
433>>> width * height
434900
435>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000436\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000437%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000438A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000439
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000440\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000441>>> # Zero x, y and z
442>>> x = y = z = 0
443>>>
444\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000445%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000446There is full support for floating point; operators with mixed type
447operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000448
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000449\bcode\begin{verbatim}
450>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004513.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000452>>> 7.0 / 2
4533.5
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000454>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000455\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000456
457\subsection{Strings}
458
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000459Besides numbers, Python can also manipulate strings, enclosed in
460single quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000461
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000462\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000463>>> 'foo bar'
464'foo bar'
465>>> 'doesn\'t'
466'doesn\'t'
467>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000468\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000469%
470Strings are written the same way as they are typed for input: inside
471quotes and with quotes and other funny characters escaped by
472backslashes, to show the precise value. (The {\tt print} statement,
473described later, can be used to write strings without quotes or
474escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000475
476Strings can be concatenated (glued together) with the {\tt +}
477operator, and repeated with {\tt *}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000478
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000479\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000480>>> word = 'Help' + 'A'
481>>> word
482'HelpA'
483>>> '<' + word*5 + '>'
484'<HelpAHelpAHelpAHelpAHelpA>'
485>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000486\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000487%
488Strings can be subscripted (indexed); like in C, the first character of
489a string has subscript (index) 0.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000490
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000491There is no separate character type; a character is simply a string of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000492size one. Like in Icon, substrings can be specified with the {\em
493slice} notation: two indices separated by a colon.
494
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000495\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000496>>> word[4]
497'A'
498>>> word[0:2]
499'He'
500>>> word[2:4]
501'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000502>>>
503\end{verbatim}\ecode
504%
505Slice indices have useful defaults; an omitted first index defaults to
506zero, an omitted second index defaults to the size of the string being
507sliced.
508
509\bcode\begin{verbatim}
510>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000511'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000512>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000513'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000514>>>
515\end{verbatim}\ecode
516%
517Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
518equals \verb\s\.
519
520\bcode\begin{verbatim}
521>>> word[:2] + word[2:]
522'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000523>>> word[:3] + word[3:]
524'HelpA'
525>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000526\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000527%
528Degenerate slice indices are handled gracefully: an index that is too
529large is replaced by the string size, an upper bound smaller than the
530lower bound returns an empty string.
531
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000532\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000533>>> word[1:100]
534'elpA'
535>>> word[10:]
536''
537>>> word[2:1]
538''
539>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000540\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000541%
542Indices may be negative numbers, to start counting from the right.
543For example:
544
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000545\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000546>>> word[-1] # The last character
547'A'
548>>> word[-2] # The last-but-one character
549'p'
550>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000551'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000552>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000553'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000554>>>
555\end{verbatim}\ecode
556%
557But note that -0 is really the same as 0, so it does not count from
558the right!
559
560\bcode\begin{verbatim}
561>>> word[-0] # (since -0 equals 0)
562'H'
563>>>
564\end{verbatim}\ecode
565%
566Out-of-range negative slice indices are truncated, but don't try this
567for single-element (non-slice) indices:
568
569\bcode\begin{verbatim}
570>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000571'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000572>>> word[-10] # error
573Unhandled exception: IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000574>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000575\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000576%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000577The best way to remember how slices work is to think of the indices as
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000578pointing {\em between} characters, with the left edge of the first
579character numbered 0. Then the right edge of the last character of a
580string of {\tt n} characters has index {\tt n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000581
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000582\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000583 +---+---+---+---+---+
584 | H | e | l | p | A |
585 +---+---+---+---+---+
586 0 1 2 3 4 5
587-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000588\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000589%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000590The first row of numbers gives the position of the indices 0...5 in
591the string; the second row gives the corresponding negative indices.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000592The slice from \verb\i\ to \verb\j\ consists of all characters between
593the edges labeled \verb\i\ and \verb\j\, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000594
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000595For nonnegative indices, the length of a slice is the difference of
596the indices, if both are within bounds, e.g., the length of
597\verb\word[1:3]\ is 2.
598
599The built-in function {\tt len()} returns the length of a string:
600
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000601\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000602>>> s = 'supercalifragilisticexpialidocious'
603>>> len(s)
60434
605>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000606\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000607
608\subsection{Lists}
609
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000610Python knows a number of {\em compound} data types, used to group
611together other values. The most versatile is the {\em list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000612can be written as a list of comma-separated values (items) between
613square brackets. List items need not all have the same type.
614
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000615\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000616>>> a = ['foo', 'bar', 100, 1234]
617>>> a
618['foo', 'bar', 100, 1234]
619>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000620\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000621%
622Like string indices, list indices start at 0, and lists can be sliced,
623concatenated and so on:
624
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000625\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000626>>> a[0]
627'foo'
628>>> a[3]
6291234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000630>>> a[-2]
631100
632>>> a[1:-1]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000633['bar', 100]
634>>> a[:2] + ['bletch', 2*2]
635['foo', 'bar', 'bletch', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000636>>> 3*a[:3] + ['Boe!']
637['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000638>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000639\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000640%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000641Unlike strings, which are {\em immutable}, it is possible to change
642individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000643
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000644\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000645>>> a
646['foo', 'bar', 100, 1234]
647>>> a[2] = a[2] + 23
648>>> a
649['foo', 'bar', 123, 1234]
650>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000651\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000652%
653Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000654of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000655
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000656\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000657>>> # Replace some items:
658>>> a[0:2] = [1, 12]
659>>> a
660[1, 12, 123, 1234]
661>>> # Remove some:
662>>> a[0:2] = []
663>>> a
664[123, 1234]
665>>> # Insert some:
666>>> a[1:1] = ['bletch', 'xyzzy']
667>>> a
668[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000669>>> a[:0] = a # Insert (a copy of) itself at the beginning
670>>> a
671[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000672>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000673\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000674%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000675The built-in function {\tt len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000676
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000677\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000678>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00006798
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000680>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000681\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000682%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000683It is possible to nest lists (create lists containing other lists),
684for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000685
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000686\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000687>>> q = [2, 3]
688>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000689>>> len(p)
6903
691>>> p[1]
692[2, 3]
693>>> p[1][0]
6942
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000695>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000696>>> p
697[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000698>>> q
699[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000700>>>
701\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000702%
703Note that in the last example, {\tt p[1]} and {\tt q} really refer to
704the same object! We'll come back to {\em object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000705
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000706\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000707
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000708Of course, we can use Python for more complicated tasks than adding
709two and two together. For instance, we can write an initial
710subsequence of the {\em Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000711
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000712\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000713>>> # Fibonacci series:
714>>> # the sum of two elements defines the next
715>>> a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000716>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000717... print b
718... a, b = b, a+b
719...
7201
7211
7222
7233
7245
7258
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000726>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000727\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000728%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000729This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000730
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000731\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000732
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000733\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000734The first line contains a {\em multiple assignment}: the variables
735{\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
736last line this is used again, demonstrating that the expressions on
737the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000738assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000739
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000740\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000741The {\tt while} loop executes as long as the condition (here: {\tt b <
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000742100}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000743true; zero is false. The condition may also be a string or list value,
744in fact any sequence; anything with a non-zero length is true, empty
745sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000746comparison. The standard comparison operators are written the same as
747in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000748
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000749\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000750The {\em body} of the loop is {\em indented}: indentation is Python's
751way of grouping statements. Python does not (yet!) provide an
752intelligent input line editing facility, so you have to type a tab or
753space(s) for each indented line. In practice you will prepare more
754complicated input for Python with a text editor; most text editors have
755an auto-indent facility. When a compound statement is entered
756interactively, it must be followed by a blank line to indicate
757completion (since the parser cannot guess when you have typed the last
758line).
759
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000760\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000761The {\tt print} statement writes the value of the expression(s) it is
762given. It differs from just writing the expression you want to write
763(as we did earlier in the calculator examples) in the way it handles
764multiple expressions and strings. Strings are written without quotes,
765and a space is inserted between items, so you can format things nicely,
766like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000767
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000768\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000769>>> i = 256*256
770>>> print 'The value of i is', i
771The value of i is 65536
772>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000773\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000774%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000775A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000776
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000777\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000778>>> a, b = 0, 1
779>>> while b < 1000:
780... print b,
781... a, b = b, a+b
782...
7831 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
784>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000785\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000786%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000787Note that the interpreter inserts a newline before it prints the next
788prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000789
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000790\end{itemize}
791
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000792
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000793\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000794
Guido van Rossum4410c751991-06-04 20:22:18 +0000795Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000796usual control flow statements known from other languages, with some
797twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000798
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000799\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000800
801Perhaps the most well-known statement type is the {\tt if} statement.
802For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000803
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000804\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000805>>> if x < 0:
806... x = 0
807... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000808... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000809... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000810... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000811... print 'Single'
812... else:
813... print 'More'
814...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000815\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000816%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000817There can be zero or more {\tt elif} parts, and the {\tt else} part is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000818optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
819useful to avoid excessive indentation. An {\tt if...elif...elif...}
820sequence is a substitute for the {\em switch} or {\em case} statements
821found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000822
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000823\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000824
Guido van Rossum4410c751991-06-04 20:22:18 +0000825The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000826used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000827arithmetic progression of numbers (like in Pascal), or leaving the user
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000828completely free in the iteration test and step (as C), Python's {\tt
829for} statement iterates over the items of any sequence (e.g., a list
830or a string), in the order that they appear in the sequence. For
831example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000832
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000833\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000834>>> # Measure some strings:
835>>> a = ['cat', 'window', 'defenestrate']
836>>> for x in a:
837... print x, len(x)
838...
839cat 3
840window 6
841defenestrate 12
842>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000843\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000844%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000845It is not safe to modify the sequence being iterated over in the loop
846(this can only happen for mutable sequence types, i.e., lists). If
847you need to modify the list you are iterating over, e.g., duplicate
848selected items, you must iterate over a copy. The slice notation
849makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000850
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000851\bcode\begin{verbatim}
852>>> for x in a[:]: # make a slice copy of the entire list
853... if len(x) > 6: a.insert(0, x)
854...
855>>> a
856['defenestrate', 'cat', 'window', 'defenestrate']
857>>>
858\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000859
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000860\section{The {\tt range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000861
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000862If you do need to iterate over a sequence of numbers, the built-in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000863function {\tt range()} comes in handy. It generates lists containing
864arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000865
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000866\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000867>>> range(10)
868[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
869>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000870\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000871%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000872The given end point is never part of the generated list; {\tt range(10)}
873generates a list of 10 values, exactly the legal indices for items of a
874sequence of length 10. It is possible to let the range start at another
875number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000876
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000877\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000878>>> range(5, 10)
879[5, 6, 7, 8, 9]
880>>> range(0, 10, 3)
881[0, 3, 6, 9]
882>>> range(-10, -100, -30)
883[-10, -40, -70]
884>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000885\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000886%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000887To iterate over the indices of a sequence, combine {\tt range()} and
888{\tt len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000889
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000890\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000891>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000892>>> for i in range(len(a)):
893... print i, a[i]
894...
8950 Mary
8961 had
8972 a
8983 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00008994 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000900>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000901\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000902
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000903\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000904
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000905The {\tt break} statement, like in C, breaks out of the smallest
906enclosing {\tt for} or {\tt while} loop.
907
908The {\tt continue} statement, also borrowed from C, continues with the
909next iteration of the loop.
910
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000911Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000912loop terminates through exhaustion of the list (with {\tt for}) or when
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000913the condition becomes false (with {\tt while}), but not when the loop is
914terminated by a {\tt break} statement. This is exemplified by the
915following loop, which searches for a list item of value 0:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000916
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000917\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000918>>> for n in range(2, 10):
919... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000920... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000921... print n, 'equals', x, '*', n/x
922... break
923... else:
924... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000925...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009262 is a prime number
9273 is a prime number
9284 equals 2 * 2
9295 is a prime number
9306 equals 2 * 3
9317 is a prime number
9328 equals 2 * 4
9339 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000934>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000935\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000936
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000937\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000938
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000939The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000940It can be used when a statement is required syntactically but the
941program requires no action.
942For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000943
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000944\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000945>>> while 1:
946... pass # Busy-wait for keyboard interrupt
947...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000948\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000949
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000950\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000951
952We can create a function that writes the Fibonacci series to an
953arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000954
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000955\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000956>>> def fib(n): # write Fibonacci series up to n
957... a, b = 0, 1
958... while b <= n:
959... print b,
960... a, b = b, a+b
961...
962>>> # Now call the function we just defined:
963>>> fib(2000)
9641 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
965>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000966\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000967%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000968The keyword {\tt def} introduces a function {\em definition}. It must
969be followed by the function name and the parenthesized list of formal
970parameters. The statements that form the body of the function starts at
971the next line, indented by a tab stop.
972
973The {\em execution} of a function introduces a new symbol table used
974for the local variables of the function. More precisely, all variable
975assignments in a function store the value in the local symbol table;
976whereas
977 variable references first look in the local symbol table, then
978in the global symbol table, and then in the table of built-in names.
979Thus,
980global variables cannot be directly assigned to from within a
981function, although they may be referenced.
982
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000983The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000984the local symbol table of the called function when it is called; thus,
985arguments are passed using {\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000986\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000987 Actually, {\em call by object reference} would be a better
988 description, since if a mutable object is passed, the caller
989 will see any changes the callee makes to it (e.g., items
990 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000991}
992When a function calls another function, a new local symbol table is
993created for that call.
994
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000995A function definition introduces the function name in the
996current
997symbol table. The value
998of the function name
999has a type that is recognized by the interpreter as a user-defined
1000function. This value can be assigned to another name which can then
1001also be used as a function. This serves as a general renaming
1002mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001003
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001004\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001005>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001006<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001007>>> f = fib
1008>>> f(100)
10091 1 2 3 5 8 13 21 34 55 89
1010>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001011\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001012%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001013You might object that {\tt fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001014Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001015value. In fact, technically speaking, procedures do return a value,
1016albeit a rather boring one. This value is called {\tt None} (it's a
1017built-in name). Writing the value {\tt None} is normally suppressed by
1018the interpreter if it would be the only value written. You can see it
1019if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001020
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001021\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001022>>> print fib(0)
1023None
1024>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001025\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001026%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001027It is simple to write a function that returns a list of the numbers of
1028the Fibonacci series, instead of printing it:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001029
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001030\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001031>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001032... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001033... a, b = 0, 1
1034... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001035... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001036... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001037... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001038...
1039>>> f100 = fib2(100) # call it
1040>>> f100 # write the result
1041[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1042>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001043\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001044%
Guido van Rossum4410c751991-06-04 20:22:18 +00001045This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001046
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001047\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001048
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001049\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001050The {\tt return} statement returns with a value from a function. {\tt
1051return} without an expression argument is used to return from the middle
1052of a procedure (falling off the end also returns from a proceduce), in
1053which case the {\tt None} value is returned.
1054
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001055\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001056The statement {\tt result.append(b)} calls a {\em method} of the list
1057object {\tt result}. A method is a function that `belongs' to an
1058object and is named {\tt obj.methodname}, where {\tt obj} is some
1059object (this may be an expression), and {\tt methodname} is the name
1060of a method that is defined by the object's type. Different types
1061define different methods. Methods of different types may have the
1062same name without causing ambiguity. (It is possible to define your
1063own object types and methods, using {\em classes}. This is an
1064advanced feature that is not discussed in this tutorial.)
1065The method {\tt append} shown in the example, is defined for
1066list objects; it adds a new element at the end of the list. In this
1067example
1068it is equivalent to {\tt result = result + [b]}, but more efficient.
1069
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001070\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001071
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001072
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001073\chapter{Odds and Ends}
1074
1075This chapter describes some things you've learned about already in
1076more detail, and adds some new things as well.
1077
1078\section{More on Lists}
1079
1080The list data type has some more methods. Here are all of the methods
1081of lists objects:
1082
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001083\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001084
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001085\item[{\tt insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001086Insert an item at a given position. The first argument is the index of
1087the element before which to insert, so {\tt a.insert(0, x)} inserts at
1088the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
1089{\tt a.append(x)}.
1090
1091\item[{\tt append(x)}]
1092Equivalent to {\tt a.insert(len(a), x)}.
1093
1094\item[{\tt index(x)}]
1095Return the index in the list of the first item whose value is {\tt x}.
1096It is an error if there is no such item.
1097
1098\item[{\tt remove(x)}]
1099Remove the first item from the list whose value is {\tt x}.
1100It is an error if there is no such item.
1101
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001102\item[{\tt sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001103Sort the items of the list, in place.
1104
1105\item[{\tt reverse()}]
1106Reverse the elements of the list, in place.
1107
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001108\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001109
1110An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001111
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001112\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001113>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001114>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001115>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001116>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001117[66.6, 333, -1, 333, 1, 1234.5, 333]
1118>>> a.index(333)
11191
1120>>> a.remove(333)
1121>>> a
1122[66.6, -1, 333, 1, 1234.5, 333]
1123>>> a.reverse()
1124>>> a
1125[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001126>>> a.sort()
1127>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001128[-1, 1, 66.6, 333, 333, 1234.5]
1129>>>
1130\end{verbatim}\ecode
1131
1132\section{The {\tt del} statement}
1133
1134There is a way to remove an item from a list given its index instead
1135of its value: the {\tt del} statement. This can also be used to
1136remove slices from a list (which we did earlier by assignment of an
1137empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001138
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001139\bcode\begin{verbatim}
1140>>> a
1141[-1, 1, 66.6, 333, 333, 1234.5]
1142>>> del a[0]
1143>>> a
1144[1, 66.6, 333, 333, 1234.5]
1145>>> del a[2:4]
1146>>> a
1147[1, 66.6, 1234.5]
1148>>>
1149\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001150%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001151{\tt del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001152
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001153\bcode\begin{verbatim}
1154>>> del a
1155>>>
1156\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001157%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001158Referencing the name {\tt a} hereafter is an error (at least until
1159another value is assigned to it). We'll find other uses for {\tt del}
1160later.
1161
1162\section{Tuples and Sequences}
1163
1164We saw that lists and strings have many common properties, e.g.,
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001165indexinging and slicing operations. They are two examples of {\em
1166sequence} data types. Since Python is an evolving language, other
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001167sequence data types may be added. There is also another standard
1168sequence data type: the {\em tuple}.
1169
1170A tuple consists of a number of values separated by commas, for
1171instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001172
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001173\bcode\begin{verbatim}
1174>>> t = 12345, 54321, 'hello!'
1175>>> t[0]
117612345
1177>>> t
1178(12345, 54321, 'hello!')
1179>>> # Tuples may be nested:
1180>>> u = t, (1, 2, 3, 4, 5)
1181>>> u
1182((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1183>>>
1184\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001185%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001186As you see, on output tuples are alway enclosed in parentheses, so
1187that nested tuples are interpreted correctly; they may be input with
1188or without surrounding parentheses, although often parentheses are
1189necessary anyway (if the tuple is part of a larger expression).
1190
1191Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1192from a database, etc. Tuples, like strings, are immutable: it is not
1193possible to assign to the individual items of a tuple (you can
1194simulate much of the same effect with slicing and concatenation,
1195though).
1196
1197A special problem is the construction of tuples containing 0 or 1
1198items: the syntax has some extra quirks to accomodate these. Empty
1199tuples are constructed by an empty pair of parentheses; a tuple with
1200one item is constructed by following a value with a comma
1201(it is not sufficient to enclose a single value in parentheses).
1202Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001203
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001204\bcode\begin{verbatim}
1205>>> empty = ()
1206>>> singleton = 'hello', # <-- note trailing comma
1207>>> len(empty)
12080
1209>>> len(singleton)
12101
1211>>> singleton
1212('hello',)
1213>>>
1214\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001215%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001216The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
1217tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
1218are packed together in a tuple. The reverse operation is also
1219possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001220
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001221\bcode\begin{verbatim}
1222>>> x, y, z = t
1223>>>
1224\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001225%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001226This is called, appropriately enough, {\em tuple unpacking}. Tuple
1227unpacking requires that the list of variables on the left has the same
1228number of elements as the length of the tuple. Note that multiple
1229assignment is really just a combination of tuple packing and tuple
1230unpacking!
1231
1232Occasionally, the corresponding operation on lists is useful: {\em list
1233unpacking}. This is supported by enclosing the list of variables in
1234square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001235
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001236\bcode\begin{verbatim}
1237>>> a = ['foo', 'bar', 100, 1234]
1238>>> [a1, a2, a3, a4] = a
1239>>>
1240\end{verbatim}\ecode
1241
1242\section{Dictionaries}
1243
1244Another useful data type built into Python is the {\em dictionary}.
1245Dictionaries are sometimes found in other languages as ``associative
1246memories'' or ``associative arrays''. Unlike sequences, which are
1247indexed by a range of numbers, dictionaries are indexed by {\em keys},
1248which are strings. It is best to think of a dictionary as an unordered set of
1249{\em key:value} pairs, with the requirement that the keys are unique
1250(within one dictionary).
1251A pair of braces creates an empty dictionary: \verb/{}/.
1252Placing a comma-separated list of key:value pairs within the
1253braces adds initial key:value pairs to the dictionary; this is also the
1254way dictionaries are written on output.
1255
1256The main operations on a dictionary are storing a value with some key
1257and extracting the value given the key. It is also possible to delete
1258a key:value pair
1259with {\tt del}.
1260If you store using a key that is already in use, the old value
1261associated with that key is forgotten. It is an error to extract a
1262value using a non-existant key.
1263
1264The {\tt keys()} method of a dictionary object returns a list of all the
1265keys used in the dictionary, in random order (if you want it sorted,
1266just apply the {\tt sort()} method to the list of keys). To check
1267whether a single key is in the dictionary, use the \verb/has_key()/
1268method of the dictionary.
1269
1270Here is a small example using a dictionary:
1271
1272\bcode\begin{verbatim}
1273>>> tel = {'jack': 4098, 'sape': 4139}
1274>>> tel['guido'] = 4127
1275>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001276{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001277>>> tel['jack']
12784098
1279>>> del tel['sape']
1280>>> tel['irv'] = 4127
1281>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001282{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001283>>> tel.keys()
1284['guido', 'irv', 'jack']
1285>>> tel.has_key('guido')
12861
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001287>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001288\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001289
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001290\section{More on Conditions}
1291
1292The conditions used in {\tt while} and {\tt if} statements above can
1293contain other operators besides comparisons.
1294
1295The comparison operators {\tt in} and {\tt not in} check whether a value
1296occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
1297is not} compare whether two objects are really the same object; this
1298only matters for mutable objects like lists. All comparison operators
1299have the same priority, which is lower than that of all numerical
1300operators.
1301
1302Comparisons can be chained: e.g., {\tt a < b = c} tests whether {\tt a}
1303is less than {\tt b} and moreover {\tt b} equals {\tt c}.
1304
1305Comparisons may be combined by the Boolean operators {\tt and} and {\tt
1306or}, and the outcome of a comparison (or of any other Boolean
1307expression) may be negated with {\tt not}. These all have lower
1308priorities than comparison operators again; between them, {\tt not} has
1309the highest priority, and {\tt or} the lowest, so that
1310{\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
1311course, parentheses can be used to express the desired composition.
1312
1313The Boolean operators {\tt and} and {\tt or} are so-called {\em
1314shortcut} operators: their arguments are evaluated from left to right,
1315and evaluation stops as soon as the outcome is determined. E.g., if
1316{\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
1317does not evaluate the expression C. In general, the return value of a
1318shortcut operator, when used as a general value and not as a Boolean, is
1319the last evaluated argument.
1320
1321It is possible to assign the result of a comparison or other Boolean
1322expression to a variable, but you must enclose the entire Boolean
1323expression in parentheses. This is necessary because otherwise an
1324assignment like \verb/a = b = c/ would be ambiguous: does it assign the
1325value of {\tt c} to {\tt a} and {\tt b}, or does it compare {\tt b} to
1326{\tt c} and assign the outcome (0 or 1) to {\tt a}? As it is, the first
1327meaning is what you get, and to get the latter you have to write
1328\verb/a = (b = c)/. (In Python, unlike C, assignment cannot occur
1329inside expressions.)
1330
1331\section{Comparing Sequences and Other Types}
1332
1333Sequence objects may be compared to other objects with the same
1334sequence type. The comparison uses {\em lexicographical} ordering:
1335first the first two items are compared, and if they differ this
1336determines the outcome of the comparison; if they are equal, the next
1337two items are compared, and so on, until either sequence is exhausted.
1338If two items to be compared are themselves sequences of the same type,
1339the lexiographical comparison is carried out recursively. If all
1340items of two sequences compare equal, the sequences are considered
1341equal. If one sequence is an initial subsequence of the other, the
1342shorted sequence is the smaller one. Lexicographical ordering for
1343strings uses the ASCII ordering for individual characters. Some
1344examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001345
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001346\bcode\begin{verbatim}
1347(1, 2, 3) < (1, 2, 4)
1348[1, 2, 3] < [1, 2, 4]
1349'ABC' < 'C' < 'Pascal' < 'Python'
1350(1, 2, 3, 4) < (1, 2, 4)
1351(1, 2) < (1, 2, -1)
1352(1, 2, 3) = (1.0, 2.0, 3.0)
1353(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1354\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001355%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001356Note that comparing objects of different types is legal. The outcome
1357is deterministic but arbitrary: the types are ordered by their name.
1358Thus, a list is always smaller than a string, a string is always
1359smaller than a tuple, etc. Mixed numeric types are compared according
1360to their numeric value, so 0 equals 0.0, etc.%
1361\footnote{
1362 The rules for comparing objects of different types should
1363 not be relied upon; they may change in a future version of
1364 the language.
1365}
1366
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001367
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001368\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001369
Guido van Rossum4410c751991-06-04 20:22:18 +00001370If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001371definitions you have made (functions and variables) are lost.
1372Therefore, if you want to write a somewhat longer program, you are
1373better off using a text editor to prepare the input for the interpreter
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001374and run it with that file as input instead. This is known as creating a
1375{\em script}. As your program gets longer, you may want to split it
1376into several files for easier maintenance. You may also want to use a
1377handy function that you've written in several programs without copying
1378its definition into each program.
1379
Guido van Rossum4410c751991-06-04 20:22:18 +00001380To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001381them in a script or in an interactive instance of the interpreter.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001382Such a file is called a {\em module}; definitions from a module can be
1383{\em imported} into other modules or into the {\em main} module (the
1384collection of variables that you have access to in a script
1385executed at the top level
1386and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001387
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001388A module is a file containing Python definitions and statements. The
1389file name is the module name with the suffix {\tt .py} appended. For
1390instance, use your favorite text editor to create a file called {\tt
1391fibo.py} in the current directory with the following contents:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001392
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001393\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001394# Fibonacci numbers module
1395
1396def fib(n): # write Fibonacci series up to n
1397 a, b = 0, 1
1398 while b <= n:
1399 print b,
1400 a, b = b, a+b
1401
1402def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001403 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001404 a, b = 0, 1
1405 while b <= n:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001406 result.append(b)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001407 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001408 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001409\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001410%
Guido van Rossum4410c751991-06-04 20:22:18 +00001411Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001412following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001413
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001414\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001415>>> import fibo
1416>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001417\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001418%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001419This does not enter the names of the functions defined in
1420{\tt fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001421directly in the current symbol table; it only enters the module name
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001422{\tt fibo}
1423there.
1424Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001425
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001426\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001427>>> fibo.fib(1000)
14281 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1429>>> fibo.fib2(100)
1430[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1431>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001432\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001433%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001434If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001435
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001436\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001437>>> fib = fibo.fib
1438>>> fib(500)
14391 1 2 3 5 8 13 21 34 55 89 144 233 377
1440>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001441\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001442
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001443\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001444
1445A module can contain executable statements as well as function
1446definitions.
1447These statements are intended to initialize the module.
1448They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001449{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001450time the module is imported somewhere.%
1451\footnote{
1452 In fact function definitions are also `statements' that are
1453 `executed'; the execution enters the function name in the
1454 module's global symbol table.
1455}
1456
1457Each module has its own private symbol table, which is used as the
1458global symbol table by all functions defined in the module.
1459Thus, the author of a module can use global variables in the module
1460without worrying about accidental clashes with a user's global
1461variables.
1462On the other hand, if you know what you are doing you can touch a
1463module's global variables with the same notation used to refer to its
1464functions,
1465{\tt modname.itemname}.
1466
1467Modules can import other modules.
1468It is customary but not required to place all
1469{\tt import}
1470statements at the beginning of a module (or script, for that matter).
1471The imported module names are placed in the importing module's global
1472symbol table.
1473
1474There is a variant of the
1475{\tt import}
1476statement that imports names from a module directly into the importing
1477module's symbol table.
1478For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001479
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001480\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001481>>> from fibo import fib, fib2
1482>>> fib(500)
14831 1 2 3 5 8 13 21 34 55 89 144 233 377
1484>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001485\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001486%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001487This does not introduce the module name from which the imports are taken
1488in the local symbol table (so in the example, {\tt fibo} is not
1489defined).
1490
1491There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001492
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001493\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001494>>> from fibo import *
1495>>> fib(500)
14961 1 2 3 5 8 13 21 34 55 89 144 233 377
1497>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001498\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001499%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001500This imports all names except those beginning with an underscore
Guido van Rossum573805a1992-03-06 10:56:03 +00001501({\tt _}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001502
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001503\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001504
Guido van Rossum4410c751991-06-04 20:22:18 +00001505Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001506document (Python Library Reference). Some modules are built into the
1507interpreter; these provide access to operations that are not part of the
1508core of the language but are nevertheless built in, either for
1509efficiency or to provide access to operating system primitives such as
1510system calls. The set of such modules is a configuration option; e.g.,
1511the {\tt amoeba} module is only provided on systems that somehow support
1512Amoeba primitives. One particular module deserves some attention: {\tt
1513sys}, which is built into every Python interpreter. The variables {\tt
1514sys.ps1} and {\tt sys.ps2} define the strings used as primary and
1515secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001516
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001517\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001518>>> import sys
1519>>> sys.ps1
1520'>>> '
1521>>> sys.ps2
1522'... '
1523>>> sys.ps1 = 'C> '
1524C> print 'Yuck!'
1525Yuck!
1526C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001527\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001528%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001529These two variables are only defined if the interpreter is in
1530interactive mode.
1531
1532The variable
1533{\tt sys.path}
1534is a list of strings that determine the interpreter's search path for
1535modules.
1536It is initialized to a default path taken from the environment variable
1537{\tt PYTHONPATH},
1538or from a built-in default if
1539{\tt PYTHONPATH}
1540is not set.
1541You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001542
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001543\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001544>>> import sys
1545>>> sys.path.append('/ufs/guido/lib/python')
1546>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001547\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001548
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001549\section{The {\tt dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001550
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001551The built-in function {\tt dir} is used to find out which names a module
1552defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001553
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001554\bcode\begin{verbatim}
1555>>> import fibo, sys
1556>>> dir(fibo)
1557['fib', 'fib2']
1558>>> dir(sys)
1559['argv', 'exit', 'modules', 'path', 'ps1', 'ps2', 'stderr', 'stdin', 'stdout']
1560>>>
1561\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001562%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001563Without arguments, {\tt dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001564
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001565\bcode\begin{verbatim}
1566>>> a = [1, 2, 3, 4, 5]
1567>>> import fibo, sys
1568>>> fib = fibo.fib
1569>>> dir()
1570['a', 'fib', 'fibo', 'sys']
1571>>>
1572\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001573%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001574Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001575
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001576{\tt dir()} does not list the names of built-in functions and variables.
1577If you want a list of those, they are defined in the standard module
1578{\tt builtin}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001579
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001580\bcode\begin{verbatim}
1581>>> import builtin
1582>>> dir(builtin)
1583['EOFError', 'KeyboardInterrupt', 'MemoryError', 'NameError', 'None', 'Runti
1584meError', 'SystemError', 'TypeError', 'abs', 'chr', 'dir', 'divmod', 'eval',
1585 'exec', 'float', 'input', 'int', 'len', 'long', 'max', 'min', 'open', 'ord'
1586, 'pow', 'range', 'raw_input', 'reload', 'type']
1587>>>
1588\end{verbatim}\ecode
1589
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001590
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001591\chapter{Output Formatting}
1592
1593So far we've encountered two ways of writing values: {\em expression
1594statements} and the {\tt print} statement. (A third way is using the
1595{\tt write} method of file objects; the standard output file can be
1596referenced as {\tt sys.stdout}. See the Library Reference for more
1597information on this.)
1598
1599Often you'll want more control over the formatting of your output than
1600simply printing space-separated values. The key to nice formatting in
1601Python is to do all the string handling yourself; using string slicing
1602and concatenation operations you can create any lay-out you can imagine.
1603The standard module {\tt string} contains some useful operations for
1604padding strings to a given column width; these will be discussed shortly.
1605
1606One question remains, of course: how do you convert values to strings?
1607Luckily, Python has a way to convert any value to a string: just write
1608the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001609
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001610\bcode\begin{verbatim}
1611>>> x = 10 * 3.14
1612>>> y = 200*200
1613>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1614>>> print s
1615The value of x is 31.4, and y is 40000...
1616>>> # Reverse quotes work on other types besides numbers:
1617>>> p = [x, y]
1618>>> ps = `p`
1619>>> ps
1620'[31.4, 40000]'
1621>>> # Converting a string adds string quotes and backslashes:
1622>>> hello = 'hello, world\n'
1623>>> hellos = `hello`
1624>>> print hellos
1625'hello, world\012'
1626>>> # The argument of reverse quotes may be a tuple:
1627>>> `x, y, ('foo', 'bar')`
1628'(31.4, 40000, (\'foo\', \'bar\'))'
1629>>>
1630\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001631%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001632Here is how you write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001633
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001634\bcode\begin{verbatim}
1635>>> import string
1636>>> for x in range(1, 11):
1637... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1638... # Note trailing comma on previous line
1639... print string.rjust(`x*x*x`, 4)
1640...
1641 1 1 1
1642 2 4 8
1643 3 9 27
1644 4 16 64
1645 5 25 125
1646 6 36 216
1647 7 49 343
1648 8 64 512
1649 9 81 729
165010 100 1000
1651>>>
1652\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001653%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001654(Note that one space between each column was added by the way {\tt print}
1655works: it always adds spaces between its arguments.)
1656
1657This example demonstrates the function {\tt string.rjust()}, which
1658right-justifies a string in a field of a given width by padding it with
1659spaces on the left. There are similar functions {\tt string.ljust()}
1660and {\tt string.center()}. These functions do not write anything, they
1661just return a new string. If the input string is too long, they don't
1662truncate it, but return it unchanged; this will mess up your column
1663lay-out but that's usually better than the alternative, which would be
1664lying about a value. (If you really want truncation you can always add
1665a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1666
1667There is another function, {\tt string.zfill}, which pads a numeric
1668string on the left with zeros. It understands about plus and minus
1669signs:%
1670\footnote{
1671 Better facilities for formatting floating point numbers are
1672 lacking at this moment.
1673}
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001674
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001675\bcode\begin{verbatim}
1676>>> string.zfill('12', 5)
1677'00012'
1678>>> string.zfill('-3.14', 7)
1679'-003.14'
1680>>> string.zfill('3.14159265359', 5)
1681'3.14159265359'
1682>>>
1683\end{verbatim}\ecode
1684
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001685
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001686\chapter{Errors and Exceptions}
1687
1688Until now error messages haven't been more than mentioned, but if you
1689have tried out the examples you have probably seen some. There are
1690(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1691and {\em exceptions}.
1692
1693\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001694
1695Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001696kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001697
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001698\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001699>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001700Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001701while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001702 ^
1703Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001704>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001705\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001706%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001707The parser repeats the offending line and displays a little `arrow'
1708pointing at the earliest point in the line where the error was detected.
1709The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001710{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001711the arrow: in the example, the error is detected at the keyword
1712{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001713File name and line number are printed so you know where to look in case
1714the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001715
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001716\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001717
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001718Even if a statement or expression is syntactically correct, it may
1719cause an error when an attempt is made to execute it.
1720Errors detected during execution are called {\em exceptions} and are
1721not unconditionally fatal: you will soon learn how to handle them in
1722Python programs. Most exceptions are not handled by programs,
1723however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001724
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001725\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001726>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001727Unhandled exception: run-time error: integer division by zero
1728Stack backtrace (innermost last):
1729 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001730>>> 4 + foo*3
1731Unhandled exception: undefined name: foo
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001732Stack backtrace (innermost last):
1733 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001734>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001735Unhandled exception: type error: illegal argument type for built-in operation
1736Stack backtrace (innermost last):
1737 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001738>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001739\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001740%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001741The first line of the error message indicates what happened.
1742Exceptions come in different types, and the type is printed as part of
1743the message: the types in the example are
1744{\tt run-time error},
1745{\tt undefined name}
1746and
1747{\tt type error}.
1748The rest of the line is a detail whose interpretation depends on the
1749exception type.
1750
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001751The rest of the error message shows the context where the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001752exception happened.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001753In general it contains a stack backtrace listing source lines; however,
1754it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001755
1756Here is a summary of the most common exceptions:
1757\begin{itemize}
1758\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001759{\em Run-time\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001760are generally caused by wrong data used by the program; this can be the
1761programmer's fault or caused by bad input.
1762The detail states the cause of the error in more detail.
1763\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001764{\em Undefined\ name}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001765errors are more serious: these are usually caused by misspelled
1766identifiers.%
1767\footnote{
1768 The parser does not check whether names used in a program are at
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001769 all defined elsewhere in the program; such checks are
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001770 postponed until run-time. The same holds for type checking.
1771}
1772The detail is the offending identifier.
1773\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001774{\em Type\ errors} are also pretty serious: this is another case of
1775using wrong data (or better, using data the wrong way), but here the
1776error can be gleaned from the object type(s) alone. The detail shows
1777in what context the error was detected.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001778\end{itemize}
1779
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001780\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001781
1782It is possible to write programs that handle selected exceptions.
1783Look at the following example, which prints a table of inverses of
1784some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001785
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001786\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001787>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001788>>> for x in numbers:
1789... print x,
1790... try:
1791... print 1.0 / x
1792... except RuntimeError:
1793... print '*** has no inverse ***'
1794...
17950.3333 3.00030003
17962.5 0.4
17970 *** has no inverse ***
179810 0.1
1799>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001800\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001801%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001802The {\tt try} statement works as follows.
1803\begin{itemize}
1804\item
1805First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001806{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001807(the statement(s) between the {\tt try} and {\tt except} keywords) is
1808executed.
1809\item
1810If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001811{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001812is skipped and execution of the {\tt try} statement is finished.
1813\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001814If an exception occurs during execution of the try clause,
1815the rest of the clause is skipped. Then if
1816its type matches the exception named after the {\tt except} keyword,
1817the rest of the try clause is skipped, the except clause is executed,
1818and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001819\item
1820If an exception occurs which does not match the exception named in the
1821except clause, it is passed on to outer try statements; if no handler is
1822found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001823{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001824and execution stops with a message as shown above.
1825\end{itemize}
1826A {\tt try} statement may have more than one except clause, to specify
1827handlers for different exceptions.
1828At most one handler will be executed.
1829Handlers only handle exceptions that occur in the corresponding try
1830clause, not in other handlers of the same {\tt try} statement.
1831An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001832e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001833
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001834\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001835... except (RuntimeError, TypeError, NameError):
1836... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001837\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001838%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001839The last except clause may omit the exception name(s), to serve as a
1840wildcard.
1841Use this with extreme caution!
1842
1843When an exception occurs, it may have an associated value, also known as
1844the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001845{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001846The presence and type of the argument depend on the exception type.
1847For exception types which have an argument, the except clause may
1848specify a variable after the exception name (or list) to receive the
1849argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001850
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001851\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001852>>> try:
1853... foo()
1854... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001855... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001856...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001857name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001858>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001859\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001860%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001861If an exception has an argument, it is printed as the third part
1862(`detail') of the message for unhandled exceptions.
1863
1864Standard exception names are built-in identifiers (not reserved
1865keywords).
1866These are in fact string objects whose
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001867{\em object\ identity}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001868(not their value!) identifies the exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001869The string is printed as the second part of the message for unhandled
1870exceptions.
1871Their names and values are:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001872
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001873\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001874EOFError 'end-of-file read'
1875KeyboardInterrupt 'keyboard interrupt'
1876MemoryError 'out of memory' *
1877NameError 'undefined name' *
1878RuntimeError 'run-time error' *
1879SystemError 'system error' *
1880TypeError 'type error' *
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001881\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001882%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001883The meanings should be clear enough.
1884Those exceptions with a {\tt *} in the third column have an argument.
1885
1886Exception handlers don't just handle exceptions if they occur
1887immediately in the try clause, but also if they occur inside functions
1888that are called (even indirectly) in the try clause.
1889For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001890
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001891\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001892>>> def this_fails():
1893... x = 1/0
1894...
1895>>> try:
1896... this_fails()
1897... except RuntimeError, detail:
1898... print 'Handling run-time error:', detail
1899...
Guido van Rossum67fa1601991-04-23 14:14:57 +00001900Handling run-time error: integer division by zero
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001901>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001902\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001903
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001904\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001905
1906The {\tt raise} statement allows the programmer to force a specified
1907exception to occur.
1908For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001909
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001910\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001911>>> raise NameError, 'Hi There!'
1912Unhandled exception: undefined name: Hi There!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001913Stack backtrace (innermost last):
1914 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001915>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001916\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001917%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001918The first argument to {\tt raise} names the exception to be raised.
1919The optional second argument specifies the exception's argument.
1920
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001921\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001922
1923Programs may name their own exceptions by assigning a string to a
1924variable.
1925For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001926
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001927\bcode\begin{verbatim}
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001928>>> my_exc = 'Nobody likes me'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001929>>> try:
1930... raise my_exc, 2*2
1931... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001932... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001933...
1934My exception occured, value: 4
1935>>> raise my_exc, 1
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001936Nobody likes me: 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001937Stack backtrace (innermost last):
1938 File "<stdin>", line 7
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001939>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001940\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001941%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001942Many standard modules use this to report errors that may occur in
1943functions they define.
1944
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001945\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001946
1947The {\tt try} statement has another optional clause which is intended to
1948define clean-up actions that must be executed under all circumstances.
1949For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001950
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001951\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001952>>> try:
1953... raise KeyboardInterrupt
1954... finally:
1955... print 'Goodbye, world!'
1956...
1957Goodbye, world!
1958Unhandled exception: keyboard interrupt
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001959Stack backtrace (innermost last):
1960 File "<stdin>", line 2
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001961>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001962\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001963%
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001964A {\tt finally} clause is executed whether or not an exception has
1965occurred in the {\tt try} clause. When an exception has occurred, it
1966is re-raised after the {\tt finally} clauses is executed. The
1967{\tt finally} clause is also executed ``on the way out'' when the
1968{\tt try} statement is left via a {\tt break} or {\tt return}
1969statement.
1970
1971A {\tt try} statement must either have one or more {\tt except}
1972clauses or one {\tt finally} clause, but not both.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001973
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001974
1975\chapter{Classes}
1976
1977Python's class mechanism adds classes to the language with a minimum
1978of new syntax and semantics. It is a mixture of the class mechanisms
1979found in C++ and Modula-3. As is true for modules, classes in Python
1980do not put an absolute barrier between definition and user, but rather
1981rely on the politeness of the user not to ``break into the
1982definition.'' The most important features of classes are retained
1983with full power, however: the class inheritance mechanism allows
1984multiple base classes, a derived class can override any methods of its
1985base class(es), a method can call the method of a base class with the
1986same name. Objects can contain an arbitrary amount of private data.
1987
1988In C++ terminology, all class members (including the data members) are
1989{\em public}, and all member functions are {\em virtual}. There are
1990no special constructors or desctructors. As in Modula-3, there are no
1991shorthands for referencing the object's members from its methods: the
1992method function is declared with an explicit first argument
1993representing the object, which is provided implicitly by the call. As
1994in Smalltalk, classes themselves are objects, albeit in the wider
1995sense of the word: in Python, all data types are objects. This
1996provides semantics for importing and renaming. But, just like in C++
1997or Modula-3, built-in types cannot be used as base classes for
1998extension by the user. Also, like in Modula-3 but unlike in C++, the
1999built-in operators with special syntax (arithmetic operators,
2000subscriptong etc.) cannot be redefined for class members.
2001
2002
2003\section{A word about terminology}
2004
2005Lacking universally accepted terminology to talk about classes, I'll
2006make occasional use of Smalltalk and C++ terms. (I'd use Modula-3
2007terms, since its object-oriented semantics are closer to those of
2008Python than C++, but I expect that few readers have heard of it...)
2009
2010I also have to warn you that there's a terminological pitfall for
2011object-oriented readers: the word ``object'' in Python does not
2012necessarily mean a class instance. Like C++ and Modula-3, and unlike
2013Smalltalk, not all types in Python are classes: the basic built-in
2014types like integers and lists aren't, and even somewhat more exotic
2015types like files aren't. However, {\em all} Python types share a little
2016bit of common semantics that is best described by using the word
2017object.
2018
2019Objects have individuality, and multiple names (in multiple scopes)
2020can be bound to the same object. This is known as aliasing in other
2021languages. This is usually not appreciated on a first glance at
2022Python, and can be safely ignored when dealing with immutable basic
2023types (numbers, strings, tuples). However, aliasing has an
2024(intended!) effect on the semantics of Python code involving mutable
2025objects such as lists, dictionaries, and most types representing
2026entities outside the program (files, windows, etc.). This is usually
2027used to the benefit of the program, since aliases behave like pointers
2028in some respects. For example, passing an object is cheap since only
2029a pointer is passed by the implementation; and if a function modifies
2030an object passed as an argument, the caller will see the change --- this
2031obviates the need for two different argument passing mechanisms as in
2032Pascal.
2033
2034
2035\section{Python scopes and name spaces}
2036
2037Before introducing classes, I first have to tell you something about
2038Python's scope rules. Class definitions play some neat tricks with
2039name spaces, and you need to know how scopes and name spaces work to
2040fully understand what's going on. Incidentally, knowledge about this
2041subject is useful for any advanced Python programmer.
2042
2043Let's begin with some definitions.
2044
2045A {\em name space} is a mapping from names to objects. Most name
2046spaces are currently implemented as Python dictionaries, but that's
2047normally not noticeable in any way (except for performance), and it
2048may change in the future. Examples of name spaces are: the set of
2049built-in names (functions such as \verb\abs()\, and built-in exception
2050names); the global names in a module; and the local names in a
2051function invocation. In a sense the set of attributes of an object
2052also form a name space. The important things to know about name
2053spaces is that there is absolutely no relation between names in
2054different name spaces; for instance, two different modules may both
2055define a function ``maximize'' without confusion --- users of the
2056modules must prefix it with the module name.
2057
2058By the way, I use the word {\em attribute} for any name following a
2059dot --- for example, in the expression \verb\z.real\, \verb\real\ is
2060an attribute of the object \verb\z\. Strictly speaking, references to
2061names in modules are attribute references: in the expression
2062\verb\modname.funcname\, \verb\modname\ is a module object and
2063\verb\funcname\ is an attribute of it. In this case there happens to
2064be a straightforward mapping between the module's attributes and the
2065global names defined in the module: they share the same name space!%
2066\footnote{
2067 Except for one thing. Module objects have a secret read-only
2068 attribute called {\tt __dict__} which returns the dictionary
2069 used to implement the module's name space; the name
2070 {\tt __dict__} is an attribute but not a global name.
2071 Obviously, using this violates the abstraction of name space
2072 implementation, and should be restricted to things like
2073 post-mortem debuggers...
2074}
2075
2076Attributes may be read-only or writable. In the latter case,
2077assignment to attributes is possible. Module attributes are writable:
2078you can write \verb\modname.the_answer = 42\. Writable attributes may
2079also be deleted with the del statement, e.g.
2080\verb\del modname.the_answer\.
2081
2082Name spaces are created at different moments and have different
2083lifetimes. The name space containing the built-in names is created
2084when the Python interpreter starts up, and is never deleted. The
2085global name space for a module is created when the module definition
2086is read in; normally, module name spaces also last until the
2087interpreter quits. The statements executed by the top-level
2088invocation of the interpreter, either read from a script file or
2089interactively, are considered part of a module called \verb\__main__\,
2090so they have their own global name space. (The built-in names
2091actually also live in a module; this is called \verb\builtin\,
2092although it should really have been called \verb\__builtin__\.)
2093
2094The local name space for a function is created when the function is
2095called, and deleted when the function returns or raises an exception
2096that is not handled within the function. (Actually, forgetting would
2097be a better way to describe what actually happens.) Of course,
2098recursive invocations each have their own local name space.
2099
2100A {\em scope} is a textual region of a Python program where a name space
2101is directly accessible. ``Directly accessible'' here means that an
2102unqualified reference to a name attempts to find the name in the name
2103space.
2104
2105Although scopes are determined statically, they are used dynamically.
2106At any time during execution, exactly three nested scopes are in use
2107(i.e., exactly three name spaces are directly accessible): the
2108innermost scope, which is searched first, contains the local names,
2109the middle scope, searched next, contains the current module's global
2110names, and the outermost scope (searched last) is the name space
2111containing built-in names.
2112
2113Usually, the local scope references the local names of the (textually)
2114current function. Outside functions, the the local scope references
2115the same name space as the global scope: the module's name space.
2116Class definitions place yet another name space in the local scope.
2117
2118It is important to realize that scopes are determined textually: the
2119global scope of a function defined in a module is that module's name
2120space, no matter from where or by what alias the function is called.
2121On the other hand, the actual search for names is done dynamically, at
2122run time --- however, the the language definition is evolving towards
2123static name resolution, at ``compile'' time, so don't rely on dynamic
2124name resolution! (In fact, local variables are already determined
2125statically.)
2126
2127A special quirk of Python is that assignments always go into the
2128innermost scope. Assignments do not copy data --- they just
2129bind names to objects. The same is true for deletions: the statement
2130\verb\del x\ removes the binding of x from the name space referenced by the
2131local scope. In fact, all operations that introduce new names use the
2132local scope: in particular, import statements and function definitions
2133bind the module or function name in the local scope. (The
2134\verb\global\ statement can be used to indicate that particular
2135variables live in the global scope.)
2136
2137
2138\section{A first look at classes}
2139
2140Classes introduce a little bit of new syntax, three new object types,
2141and some new semantics.
2142
2143
2144\subsection{Class definition syntax}
2145
2146The simplest form of class definition looks like this:
2147
2148\begin{verbatim}
2149 class ClassName:
2150 <statement-1>
2151 .
2152 .
2153 .
2154 <statement-N>
2155\end{verbatim}
2156
2157Class definitions, like function definitions (\verb\def\ statements)
2158must be executed before they have any effect. (You could conceivably
2159place a class definition in a branch of an \verb\if\ statement, or
2160inside a function.)
2161
2162In practice, the statements inside a class definition will usually be
2163function definitions, but other statements are allowed, and sometimes
2164useful --- we'll come back to this later. The function definitions
2165inside a class normally have a peculiar form of argument list,
2166dictated by the calling conventions for methods --- again, this is
2167explained later.
2168
2169When a class definition is entered, a new name space is created, and
2170used as the local scope --- thus, all assignments to local variables
2171go into this new name space. In particular, function definitions bind
2172the name of the new function here.
2173
2174When a class definition is left normally (via the end), a {\em class
2175object} is created. This is basically a wrapper around the contents
2176of the name space created by the class definition; we'll learn more
2177about class objects in the next section. The original local scope
2178(the one in effect just before the class definitions was entered) is
2179reinstated, and the class object is bound here to class name given in
2180the class definition header (ClassName in the example).
2181
2182
2183\subsection{Class objects}
2184
2185Class objects support two kinds of operations: attribute references
2186and instantiation.
2187
2188{\em Attribute references} use the standard syntax used for all
2189attribute references in Python: \verb\obj.name\. Valid attribute
2190names are all the names that were in the class's name space when the
2191class object was created. So, if the class definition looked like
2192this:
2193
2194\begin{verbatim}
2195 class MyClass:
2196 i = 12345
2197 def f(x):
2198 return 'hello world'
2199\end{verbatim}
2200
2201then \verb\MyClass.i\ and \verb\MyClass.f\ are valid attribute
2202references, returning an integer and a function object, respectively.
2203Class attributes can also be assigned to, so you can change the
2204value of \verb\MyClass.i\ by assignment.
2205
2206Class {\em instantiation} uses function notation. Just pretend that
2207the class object is a parameterless function that returns a new
2208instance of the class. For example, (assuming the above class):
2209
2210\begin{verbatim}
2211 x = MyClass()
2212\end{verbatim}
2213
2214creates a new {\em instance} of the class and assigns this object to
2215the local variable \verb\x\.
2216
2217
2218\subsection{Instance objects}
2219
2220Now what can we do with instance objects? The only operations
2221understood by instance objects are attribute references. There are
2222two kinds of valid attribute names.
2223
2224The first I'll call {\em data attributes}. These correspond to
2225``instance variables'' in Smalltalk, and to ``data members'' in C++.
2226Data attributes need not be declared; like local variables, they
2227spring into existence when they are first assigned to. For example,
2228if \verb\x\ in the instance of \verb\MyClass\ created above, the
2229following piece of code will print the value 16, without leaving a
2230trace:
2231
2232\begin{verbatim}
2233 x.counter = 1
2234 while x.counter < 10:
2235 x.counter = x.counter * 2
2236 print x.counter
2237 del x.counter
2238\end{verbatim}
2239
2240The second kind of attribute references understood by instance objects
2241are {\em methods}. A method is a function that ``belongs to'' an
2242object. (In Python, the term method is not unique to class instances:
2243other object types can have methods as well, e.g., list objects have
2244methods called append, insert, remove, sort, and so on. However,
2245below, we'll use the term method exclusively to mean methods of class
2246instance objects, unless explicitly stated otherwise.)
2247
2248Valid method names of an instance object depend on its class. By
2249definition, all attributes of a class that are (user-defined) function
2250objects define corresponding methods of its instances. So in our
2251example, \verb\x.f\ is a valid method reference, since
2252\verb\MyClass.f\ is a function, but \verb\x.i\ is not, since
2253\verb\MyClass.i\ is not. But \verb\x.f\ is not the
2254same thing as \verb\MyClass.f\ --- it is a {\em method object}, not a
2255function object.
2256
2257
2258\subsection{Method objects}
2259
2260Usually, a method is called immediately, e.g.:
2261
2262\begin{verbatim}
2263 x.f()
2264\end{verbatim}
2265
2266In our example, this will return the string \verb\'hello world'\.
2267However, it is not necessary to call a method right away: \verb\x.f\
2268is a method object, and can be stored away and called at a later
2269moment, for example:
2270
2271\begin{verbatim}
2272 xf = x.f
2273 while 1:
2274 print xf()
2275\end{verbatim}
2276
2277will continue to print \verb\hello world\ until the end of time.
2278
2279What exactly happens when a method is called? You may have noticed
2280that \verb\x.f()\ was called without an argument above, even though
2281the function definition for \verb\f\ specified an argument. What
2282happened to the argument? Surely Python raises an exception when a
2283function that requires an argument is called without any --- even if
2284the argument isn't actually used...
2285
2286Actually, you may have guessed the answer: the special thing about
2287methods is that the object is passed as the first argument of the
2288function. In our example, the call \verb\x.f()\ is exactly equivalent
2289to \verb\MyClass.f(x)\. In general, calling a method with a list of
2290{\em n} arguments is equivalent to calling the corresponding function
2291with an argument list that is created by inserting the method's object
2292before the first argument.
2293
2294If you still don't understand how methods work, a look at the
2295implementation can perhaps clarify matters. When an instance
2296attribute is referenced that isn't a data attribute, its class is
2297searched. If the name denotes a valid class attribute that is a
2298function object, a method object is created by packing (pointers to)
2299the instance object and the function object just found together in an
2300abstract object: this is the method object. When the method object is
2301called with an argument list, it is unpacked again, a new argument
2302list is constructed from the instance object and the original argument
2303list, and the function object is called with this new argument list.
2304
2305
2306\section{Random remarks}
2307
2308
2309[These should perhaps be placed more carefully...]
2310
2311
2312Data attributes override method attributes with the same name; to
2313avoid accidental name conflicts, which may cause hard-to-find bugs in
2314large programs, it is wise to use some kind of convention that
2315minimizes the chance of conflicts, e.g., capitalize method names,
2316prefix data attribute names with a small unique string (perhaps just
2317an undescore), or use verbs for methods and nouns for data attributes.
2318
2319
2320Data attributes may be referenced by methods as well as by ordinary
2321users (``clients'') of an object. In other words, classes are not
2322usable to implement pure abstract data types. In fact, nothing in
2323Python makes it possible to enforce data hiding --- it is all based
2324upon convention. (On the other hand, the Python implementation,
2325written in C, can completely hide implementation details and control
2326access to an object if necessary; this can be used by extensions to
2327Python written in C.)
2328
2329
2330Clients should use data attributes with care --- clients may mess up
2331invariants maintained by the methods by stamping on their data
2332attributes. Note that clients may add data attributes of their own to
2333an instance object without affecting the validity of the methods, as
2334long as name conflicts are avoided --- again, a naming convention can
2335save a lot of headaches here.
2336
2337
2338There is no shorthand for referencing data attributes (or other
2339methods!) from within methods. I find that this actually increases
2340the readability of methods: there is no chance of confusing local
2341variables and instance variables when glancing through a method.
2342
2343
2344Conventionally, the first argument of methods is often called
2345\verb\self\. This is nothing more than a convention: the name
2346\verb\self\ has absolutely no special meaning to Python. (Note,
2347however, that by not following the convention your code may be less
2348readable by other Python programmers, and it is also conceivable that
2349a {\em class browser} program be written which relies upon such a
2350convention.)
2351
2352
2353Any function object that is a class attribute defines a method for
2354instances of that class. It is not necessary that the function
2355definition is textually enclosed in the class definition: assigning a
2356function object to a local variable in the class is also ok. For
2357example:
2358
2359\begin{verbatim}
2360 # Function defined outside the class
2361 def f1(self, x, y):
2362 return min(x, x+y)
2363
2364 class C:
2365 f = f1
2366 def g(self):
2367 return 'hello world'
2368 h = g
2369\end{verbatim}
2370
2371Now \verb\f\, \verb\g\ and \verb\h\ are all attributes of class
2372\verb\C\ that refer to function objects, and consequently they are all
2373methods of instances of \verb\C\ --- \verb\h\ being exactly equivalent
2374to \verb\g\. Note that this practice usually only serves to confuse
2375the reader of a program.
2376
2377
2378Methods may call other methods by using method attributes of the
2379\verb\self\ argument, e.g.:
2380
2381\begin{verbatim}
2382 class Bag:
2383 def empty(self):
2384 self.data = []
2385 def add(self, x):
2386 self.data.append(x)
2387 def addtwice(self, x):
2388 self.add(x) self.add(x)
2389\end{verbatim}
2390
2391
2392The instantiation operation (``calling'' a class object) creates an
2393empty object. Many classes like to create objects in a known initial
2394state. There is no special syntax to enforce this, but a convention
2395works almost as well: add a method named \verb\init\ to the class,
2396which initializes the instance (by assigning to some important data
2397attributes) and returns the instance itself. For example, class
2398\verb\Bag\ above could have the following method:
2399
2400\begin{verbatim}
2401 def init(self):
2402 self.empty()
2403 return self
2404\end{verbatim}
2405
2406The client can then create and initialize an instance in one
2407statement, as follows:
2408
2409\begin{verbatim}
2410 x = Bag().init()
2411\end{verbatim}
2412
2413Of course, the \verb\init\ method may have arguments for greater
2414flexibility.
2415
2416Warning: a common mistake is to forget the \verb\return self\ at the
2417end of an init method!
2418
2419
2420Methods may reference global names in the same way as ordinary
2421functions. The global scope associated with a method is the module
2422containing the class definition. (The class itself is never used as a
2423global scope!) While one rarely encounters a good reason for using
2424global data in a method, there are many legitimate uses of the global
2425scope: for one thing, functions and modules imported into the global
2426scope can be used by methods, as well as functions and classes defined
2427in it. Usually, the class containing the method is itself defined in
2428this global scope, and in the next section we'll find some good
2429reasons why a method would want to reference its own class!
2430
2431
2432\section{Inheritance}
2433
2434Of course, a language feature would not be worthy of the name ``class''
2435without supporting inheritance. The syntax for a derived class
2436definition looks as follows:
2437
2438\begin{verbatim}
2439 class DerivedClassName(BaseClassName):
2440 <statement-1>
2441 .
2442 .
2443 .
2444 <statement-N>
2445\end{verbatim}
2446
2447The name \verb\BaseClassName\ must be defined in a scope containing
2448the derived class definition. Instead of a base class name, an
2449expression is also allowed. This is useful when the base class is
2450defined in another module, e.g.,
2451
2452\begin{verbatim}
2453 class DerivedClassName(modname.BaseClassName):
2454\end{verbatim}
2455
2456Execution of a derived class definition proceeds the same as for a
2457base class. When the class object is constructed, the base class is
2458remembered. This is used for resolving attribute references: if a
2459requested attribute is not found in the class, it is searched in the
2460base class. This rule is applied recursively if the base class itself
2461is derived from some other class.
2462
2463There's nothing special about instantiation of derived classes:
2464\verb\DerivedClassName()\ creates a new instance of the class. Method
2465references are resolved as follows: the corresponding class attribute
2466is searched, descending down the chain of base classes if necessary,
2467and the method reference is valid if this yields a function object.
2468
2469Derived classes may override methods of their base classes. Because
2470methods have no special privileges when calling other methods of the
2471same object, a method of a base class that calls another method
2472defined in the same base class, may in fact end up calling a method of
2473a derived class that overrides it. (For C++ programmers: all methods
2474in Python are ``virtual functions''.)
2475
2476An overriding method in a derived class may in fact want to extend
2477rather than simply replace the base class method of the same name.
2478There is a simple way to call the base class method directly: just
2479call \verb\BaseClassName.methodname(self, arguments)\. This is
2480occasionally useful to clients as well. (Note that this only works if
2481the base class is defined or imported directly in the global scope.)
2482
2483
2484\subsection{Multiple inheritance}
2485
2486Poython supports a limited form of multiple inheritance as well. A
2487class definition with multiple base classes looks as follows:
2488
2489\begin{verbatim}
2490 class DerivedClassName(Base1, Base2, Base3):
2491 <statement-1>
2492 .
2493 .
2494 .
2495 <statement-N>
2496\end{verbatim}
2497
2498The only rule necessary to explain the semantics is the resolution
2499rule used for class attribute references. This is depth-first,
2500left-to-right. Thus, if an attribute is not found in
2501\verb\DerivedClassName\, it is searched in \verb\Base1\, then
2502(recursively) in the base classes of \verb\Base1\, and only if it is
2503not found there, it is searched in \verb\Base2\, and so on.
2504
2505(To some people breadth first --- searching \verb\Base2\ and
2506\verb\Base3\ before the base classes of \verb\Base1\ --- looks more
2507natural. However, this would require you to know whether a particular
2508attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in
2509one of its base classes before you can figure out the consequences of
2510a name conflict with an attribute of \verb\Base2\. The depth-first
2511rule makes no differences between direct and inherited attributes of
2512\verb\Base1\.)
2513
2514It is clear that indiscriminate use of multiple inheritance is a
2515maintenance nightmare, given the reliance in Python on conventions to
2516avoid accidental name conflicts. A well-known problem with multiple
2517inheritance is a class derived from two classes that happen to have a
2518common base class. While it is easy enough to figure out what happens
2519in this case (the instance will have a single copy of ``instance
2520variables'' or data attributes used by the common base class), it is
2521not clear that these semantics are in any way useful.
2522
2523
2524\section{Odds and ends}
2525
2526Sometimes it is useful to have a data type similar to the Pascal
2527``record'' or C ``struct'', bundling together a couple of named data
2528items. An empty class definition will do nicely, e.g.:
2529
2530\begin{verbatim}
2531 class Employee:
2532 pass
2533
2534 john = Employee() # Create an empty employee record
2535
2536 # Fill the fields of the record
2537 john.name = 'John Doe'
2538 john.dept = 'computer lab'
2539 john.salary = 1000
2540\end{verbatim}
2541
2542
2543A piece of Python code that expects a particular abstract data type
2544can often be passed a class that emulates the methods of that data
2545type instead. For instance, if you have a function that formats some
2546data from a file object, you can define a class with methods
2547\verb\read()\ and \verb\readline()\ that gets the data from a string
2548buffer instead, and pass it as an argument. (Unfortunately, this
2549technique has its limitations: a class can't define operations that
2550are accessed by special syntax such as sequence subscripting or
2551arithmetic operators, and assigning such a ``pseudo-file'' to
2552\verb\sys.stdin\ will not cause the interpreter to read further input
2553from it.)
2554
2555
2556Instance method objects have attributes, too: \verb\m.im_self\ is the
2557object of which the method is an instance, and \verb\m.im_func\ is the
2558function object corresponding to the method.
2559
2560
2561XXX Mention bw compat hacks.
2562
2563
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002564\end{document}