blob: 532748876a7646f1c50b0f1edd3151f053f7a919 [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 \\
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00009 Dept. CST, CWI, P.O. Box 94079 \\
10 1090 GB Amsterdam, The Netherlands \\
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000011 E-mail: {\tt guido@cwi.nl}
12}
13
Guido van Rossum83eb9621993-11-23 16:28:45 +000014\date{19 November 1993 \\ Release 0.9.9.++} % XXX update before release!
15
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000016\begin{document}
17
18\pagenumbering{roman}
19
20\maketitle
21
22\begin{abstract}
23
24\noindent
Guido van Rossum4410c751991-06-04 20:22:18 +000025Python is a simple, yet powerful programming language that bridges the
Guido van Rossum6fc178f1991-08-16 09:13:42 +000026gap between C and shell programming, and is thus ideally suited for
27``throw-away programming''
28and rapid prototyping. Its syntax is put
29together from constructs borrowed from a variety of other languages;
30most prominent are influences from ABC, C, Modula-3 and Icon.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000031
Guido van Rossum4410c751991-06-04 20:22:18 +000032The Python interpreter is easily extended with new functions and data
Guido van Rossum6fc178f1991-08-16 09:13:42 +000033types implemented in C. Python is also suitable as an extension
34language for highly customizable C applications such as editors or
35window managers.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000036
Guido van Rossum4410c751991-06-04 20:22:18 +000037Python is available for various operating systems, amongst which
Guido van Rossum6fc178f1991-08-16 09:13:42 +000038several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
39and MS-DOS.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000040
Guido van Rossum6fc178f1991-08-16 09:13:42 +000041This tutorial introduces the reader informally to the basic concepts
42and features of the Python language and system. It helps to have a
43Python interpreter handy for hands-on experience, but as the examples
44are self-contained, the tutorial can be read off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000045
Guido van Rossum481ae681991-11-25 17:28:03 +000046For a description of standard objects and modules, see the {\em Python
47Library Reference} document. The {\em Python Reference Manual} gives
48a more formal definition of the language.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000049
50\end{abstract}
51
52\pagebreak
Guido van Rossumcdc93551992-02-11 15:53:13 +000053{
54\parskip = 0mm
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000055\tableofcontents
Guido van Rossumcdc93551992-02-11 15:53:13 +000056}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000057
58\pagebreak
59
60\pagenumbering{arabic}
61
Guido van Rossum5e0759d1992-08-07 16:06:24 +000062
Guido van Rossum6fc178f1991-08-16 09:13:42 +000063\chapter{Whetting Your Appetite}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000064
Guido van Rossum6fc178f1991-08-16 09:13:42 +000065If you ever wrote a large shell script, you probably know this
66feeling: you'd love to add yet another feature, but it's already so
67slow, and so big, and so complicated; or the feature involves a system
68call or other funcion that is only accessible from C \ldots Usually
69the problem at hand isn't serious enough to warrant rewriting the
70script in C; perhaps because the problem requires variable-length
71strings or other data types (like sorted lists of file names) that are
72easy in the shell but lots of work to implement in C; or perhaps just
73because you're not sufficiently familiar with C.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000074
Guido van Rossum6fc178f1991-08-16 09:13:42 +000075In such cases, Python may be just the language for you. Python is
76simple to use, but it is a real programming language, offering much
77more structure and support for large programs than the shell has. On
78the other hand, it also offers much more error checking than C, and,
79being a {\em very-high-level language}, it has high-level data types
80built in, such as flexible arrays and dictionaries that would cost you
81days to implement efficiently in C. Because of its more general data
82types Python is applicable to a much larger problem domain than {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +000083Awk} or even {\em Perl}, yet many things are at least as easy in
84Python as in those languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000085
Guido van Rossum6fc178f1991-08-16 09:13:42 +000086Python allows you to split up your program in modules that can be
87reused in other Python programs. It comes with a large collection of
Guido van Rossuma8d754e1992-01-07 16:44:35 +000088standard modules that you can use as the basis of your programs --- or
89as examples to start learning to program in Python. There are also
90built-in modules that provide things like file I/O, system calls,
91sockets, and even a generic interface to window systems (STDWIN).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000092
Guido van Rossuma8d754e1992-01-07 16:44:35 +000093Python is an interpreted language, which can save you considerable time
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000094during program development because no compilation and linking is
Guido van Rossum6fc178f1991-08-16 09:13:42 +000095necessary. The interpreter can be used interactively, which makes it
96easy to experiment with features of the language, to write throw-away
97programs, or to test functions during bottom-up program development.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000098It is also a handy desk calculator.
99
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000100Python allows writing very compact and readable programs. Programs
101written in Python are typically much shorter than equivalent C
102programs, for several reasons:
103\begin{itemize}
104\item
105the high-level data types allow you to express complex operations in a
106single statement;
107\item
108statement grouping is done by indentation instead of begin/end
109brackets;
110\item
111no variable or argument declarations are necessary.
112\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000113
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000114Python is {\em extensible}: if you know how to program in C it is easy
115to add a new built-in
116function or
117module to the interpreter, either to
118perform critical operations at maximum speed, or to link Python
119programs to libraries that may only be available in binary form (such
120as a vendor-specific graphics library). Once you are really hooked,
121you can link the Python interpreter into an application written in C
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000122and use it as an extension or command language for that application.
123
124By the way, the language is named after the BBC show ``Monty
125Python's Flying Circus'' and has nothing to do with nasty reptiles...
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000126
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000127\section{Where From Here}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000128
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000129Now that you are all excited about Python, you'll want to examine it
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000130in some more detail. Since the best way to learn a language is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000131using it, you are invited here to do so.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000132
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000133In the next chapter, the mechanics of using the interpreter are
134explained. This is rather mundane information, but essential for
135trying out the examples shown later.
136
Guido van Rossum4410c751991-06-04 20:22:18 +0000137The rest of the tutorial introduces various features of the Python
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000138language and system though examples, beginning with simple
139expressions, statements and data types, through functions and modules,
140and finally touching upon advanced concepts like exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000141
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000142When you're through with the turtorial (or just getting bored), you
143should read the Library Reference, which gives complete (though terse)
144reference material about built-in and standard types, functions and
145modules that can save you a lot of time when writing Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000146
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000147
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000148\chapter{Using the Python Interpreter}
149
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000150\section{Invoking the Interpreter}
151
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000152The Python interpreter is usually installed as {\tt /usr/local/bin/python}
153on those machines where it is available; putting {\tt /usr/local/bin} in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000154your {\UNIX} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000155typing the command
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000156
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000157\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000158python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000159\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000160%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000161to the shell. Since the choice of the directory where the interpreter
162lives is an installation option, other places are possible; check with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000163your local Python guru or system administrator. (E.g., {\tt
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000164/usr/local/python} is a popular alternative location.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000165
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000166The interpreter operates somewhat like the {\UNIX} shell: when called
167with standard input connected to a tty device, it reads and executes
168commands interactively; when called with a file name argument or with
169a file as standard input, it reads and executes a {\em script} from
170that file.
171
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000172A third way of starting the interpreter is
173``{\tt python -c command [arg] ...}'', which
174executes the statement(s) in {\tt command}, analogous to the shell's
175{\tt -c} option. Since Python statements often contain spaces or other
176characters that are special to the shell, it is best to quote {\tt
177command} in its entirety with double quotes.
178
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000179Note that there is a difference between ``{\tt python file}'' and
180``{\tt python $<$file}''. In the latter case, input requests from the
Guido van Rossum573805a1992-03-06 10:56:03 +0000181program, such as calls to {\tt input()} and {\tt raw_input()}, are
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000182satisfied from {\em file}. Since this file has already been read
183until the end by the parser before the program starts executing, the
184program will encounter EOF immediately. In the former case (which is
185usually what you want) they are satisfied from whatever file or device
186is connected to standard input of the Python interpreter.
187
Guido van Rossumb2c65561993-05-12 08:53:36 +0000188When a script file is used, it is sometimes useful to be able to run
189the script and enter interactive mode afterwards. This can be done by
190passing {\tt -i} before the script. (This does not work if the script
191is read from standard input, for the same reason as explained in the
192previous paragraph.)
193
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000194\subsection{Argument Passing}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000195
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000196When known to the interpreter, the script name and additional
197arguments thereafter are passed to the script in the variable {\tt
198sys.argv}, which is a list of strings. Its length is at least one;
199when no script and no arguments are given, {\tt sys.argv[0]} is an
200empty string. When the script name is given as {\tt '-'} (meaning
201standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c
202command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options
203found after {\tt -c command} are not consumed by the Python
204interpreter's option processing but left in {\tt sys.argv} for the
205command to handle.
206
207\subsection{Interactive Mode}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000208
Guido van Rossumdd010801991-06-07 14:31:11 +0000209When commands are read from a tty, the interpreter is said to be in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000210{\em interactive\ mode}. In this mode it prompts for the next command
211with the {\em primary\ prompt}, usually three greater-than signs ({\tt
212>>>}); for continuation lines it prompts with the {\em secondary\
213prompt}, by default three dots ({\tt ...}). Typing an EOF (Control-D)
214at the primary prompt causes the interpreter to exit with a zero exit
215status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000216
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000217The interpreter prints a welcome message stating its version number
218and a copyright notice before printing the first prompt, e.g.:
219
220\bcode\begin{verbatim}
221python
Guido van Rossumb2c65561993-05-12 08:53:36 +0000222Python 0.9.9 (Apr 2 1993).
223Copyright 1990, 1991, 1992, 1993 Stichting Mathematisch Centrum, Amsterdam
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000224>>>
225\end{verbatim}\ecode
226
227\section{The Interpreter and its Environment}
228
229\subsection{Error Handling}
230
231When an error occurs, the interpreter prints an error
232message and a stack trace. In interactive mode, it then returns to
233the primary prompt; when input came from a file, it exits with a
234nonzero exit status after printing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000235the stack trace. (Exceptions handled by an {\tt except} clause in a
236{\tt try} statement are not errors in this context.) Some errors are
237unconditionally fatal and cause an exit with a nonzero exit; this
238applies to internal inconsistencies and some cases of running out of
239memory. All error messages are written to the standard error stream;
240normal output from the executed commands is written to standard
241output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000242
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000243Typing the interrupt character (usually Control-C or DEL) to the
244primary or secondary prompt cancels the input and returns to the
245primary prompt.%
246\footnote{
247 A problem with the GNU Readline package may prevent this.
248}
249Typing an interrupt while a command is executing raises the {\tt
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000250KeyboardInterrupt} exception, which may be handled by a {\tt try}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000251statement.
252
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000253\subsection{The Module Search Path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000254
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000255When a module named {\tt foo} is imported, the interpreter searches
256for a file named {\tt foo.py} in the list of directories specified by
257the environment variable {\tt PYTHONPATH}. It has the same syntax as
258the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000259directory names. When {\tt PYTHONPATH} is not set, or when the file
260is not found there, the search continues in an installation-dependent
261default path, usually {\tt .:/usr/local/lib/python}.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000262
263Actually, modules are searched in the list of directories given by the
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000264variable {\tt sys.path} which is initialized from {\tt PYTHONPATH} and
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000265the installation-dependent default. This allows Python programs that
266know what they're doing to modify or replace the module search path.
267See the section on Standard Modules later.
268
269\subsection{``Compiled'' Python files}
270
271As an important speed-up of the start-up time for short programs that
272use a lot of standard modules, if a file called {\tt foo.pyc} exists
273in the directory where {\tt foo.py} is found, this is assumed to
274contain an already-``compiled'' version of the module {\tt foo}. The
275modification time of the version of {\tt foo.py} used to create {\tt
276foo.pyc} is recorded in {\tt foo.pyc}, and the file is ignored if
277these don't match.
278
279Whenever {\tt foo.py} is successfully compiled, an attempt is made to
280write the compiled version to {\tt foo.pyc}. It is not an error if
281this attempt fails; if for any reason the file is not written
282completely, the resulting {\tt foo.pyc} file will be recognized as
283invalid and thus ignored later.
284
285\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000286
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000287On BSD'ish {\UNIX} systems, Python scripts can be made directly
288executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000289
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000290\bcode\begin{verbatim}
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000291#! /usr/local/bin/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000292\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000293%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000294(assuming that's the name of the interpreter) at the beginning of the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000295script and giving the file an executable mode. The {\tt \#!} must be
296the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000297
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000298\subsection{The Interactive Startup File}
299
300When you use Python interactively, it is frequently handy to have some
301standard commands executed every time the interpreter is started. You
302can do this by setting an environment variable named {\tt
303PYTHONSTARTUP} to the name of a file containing your start-up
304commands. This is similar to the {\tt /profile} feature of the UNIX
305shells.
306
307This file is only read in interactive sessions, not when Python reads
308commands from a script, and not when {\tt /dev/tty} is given as the
309explicit source of commands (which otherwise behaves like an
310interactive session). It is executed in the same name space where
311interactive commands are executed, so that objects that it defines or
312imports can be used without qualification in the interactive session.
Guido van Rossum7b3c8a11992-09-08 09:20:13 +0000313You can also change the prompts {\tt sys.ps1} and {\tt sys.ps2} in
314this file.
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000315
316If you want to read an additional start-up file from the current
317directory, you can program this in the global start-up file, e.g.
318\verb\execfile('.pythonrc')\. If you want to use the startup file
319in a script, you must write this explicitly in the script, e.g.
320\verb\import os;\ \verb\execfile(os.environ['PYTHONSTARTUP'])\.
321
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000322\section{Interactive Input Editing and History Substitution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000323
Guido van Rossum4410c751991-06-04 20:22:18 +0000324Some versions of the Python interpreter support editing of the current
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000325input line and history substitution, similar to facilities found in
326the Korn shell and the GNU Bash shell. This is implemented using the
327{\em GNU\ Readline} library, which supports Emacs-style and vi-style
328editing. This library has its own documentation which I won't
329duplicate here; however, the basics are easily explained.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000330
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000331Perhaps the quickest check to see whether command line editing is
332supported is typing Control-P to the first Python prompt you get. If
333it beeps, you have command line editing. If nothing appears to
334happen, or if \verb/^P/ is echoed, you can skip the rest of this
335section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000336
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000337\subsection{Line Editing}
338
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000339If supported, input line editing is active whenever the interpreter
340prints a primary or secondary prompt. The current line can be edited
341using the conventional Emacs control characters. The most important
342of these are: C-A (Control-A) moves the cursor to the beginning of the
343line, C-E to the end, C-B moves it one position to the left, C-F to
344the right. Backspace erases the character to the left of the cursor,
345C-D the character to its right. C-K kills (erases) the rest of the
346line to the right of the cursor, C-Y yanks back the last killed
347string. C-underscore undoes the last change you made; it can be
348repeated for cumulative effect.
349
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000350\subsection{History Substitution}
351
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000352History substitution works as follows. All non-empty input lines
353issued are saved in a history buffer, and when a new prompt is given
354you are positioned on a new line at the bottom of this buffer. C-P
355moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000356Any line in the history buffer can be edited; an asterisk appears in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000357front of the prompt to mark a line as modified. Pressing the Return
358key passes the current line to the interpreter. C-R starts an
359incremental reverse search; C-S starts a forward search.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000360
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000361\subsection{Key Bindings}
362
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000363The key bindings and some other parameters of the Readline library can
364be customized by placing commands in an initialization file called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000365{\tt \$HOME/.inputrc}. Key bindings have the form
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000366
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000367\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000368key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000369\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000370%
371or
372
373\bcode\begin{verbatim}
374"string": function-name
375\end{verbatim}\ecode
376%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000377and options can be set with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000378
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000379\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000380set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000381\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000382%
383For example:
384
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000385\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000386# I prefer vi-style editing:
387set editing-mode vi
388# Edit using a single line:
389set horizontal-scroll-mode On
390# Rebind some keys:
391Meta-h: backward-kill-word
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000392"\C-u": universal-argument
393"\C-x\C-r": re-read-init-file
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000394\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000395%
Guido van Rossum4410c751991-06-04 20:22:18 +0000396Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000397instead of Readline's default filename completion function. If you
398insist, you can override this by putting
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000399
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000400\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000401TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000402\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000403%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000404in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000405indented continuation lines...)
406
407\subsection{Commentary}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000408
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000409This facility is an enormous step forward compared to previous
410versions of the interpreter; however, some wishes are left: It would
411be nice if the proper indentation were suggested on continuation lines
412(the parser knows if an indent token is required next). The
413completion mechanism might use the interpreter's symbol table. A
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000414command to check (or even suggest) matching parentheses, quotes etc.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000415would also be useful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000416
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000417
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000418\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000419
420In the following examples, input and output are distinguished by the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000421presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
422the example, you must type everything after the prompt, when the
423prompt appears; lines that do not begin with a prompt are output from
424the interpreter.%
425\footnote{
426 I'd prefer to use different fonts to distinguish input
427 from output, but the amount of LaTeX hacking that would require
428 is currently beyond my ability.
429}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000430Note that a secondary prompt on a line by itself in an example means
431you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000432
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000433\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000434
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000435Let's try some simple Python commands. Start the interpreter and wait
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000436for the primary prompt, {\tt >>>}. (It shouldn't take long.)
437
438\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000439
440The interpreter acts as a simple calculator: you can type an
441expression at it and it will write the value. Expression syntax is
442straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000443work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000444can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000445
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000446\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000447>>> # This is a comment
448>>> 2+2
4494
450>>>
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000451>>> (50-5*6)/4
4525
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000453>>> # Division truncates towards zero:
454>>> 7/3
4552
456>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000457\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000458%
459Like in C, the equal sign ({\tt =}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000460variable. The value of an assignment is not written:
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>>> width = 20
464>>> height = 5*9
465>>> width * height
466900
467>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000468\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000469%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000470A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000471
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000472\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000473>>> # Zero x, y and z
474>>> x = y = z = 0
475>>>
476\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000477%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000478There is full support for floating point; operators with mixed type
479operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000480
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000481\bcode\begin{verbatim}
482>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004833.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000484>>> 7.0 / 2
4853.5
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000486>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000487\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000488
489\subsection{Strings}
490
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000491Besides numbers, Python can also manipulate strings, enclosed in
492single quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000493
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000494\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000495>>> 'foo bar'
496'foo bar'
497>>> 'doesn\'t'
498'doesn\'t'
499>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000500\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000501%
502Strings are written the same way as they are typed for input: inside
503quotes and with quotes and other funny characters escaped by
504backslashes, to show the precise value. (The {\tt print} statement,
505described later, can be used to write strings without quotes or
506escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000507
508Strings can be concatenated (glued together) with the {\tt +}
509operator, and repeated with {\tt *}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000510
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000511\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000512>>> word = 'Help' + 'A'
513>>> word
514'HelpA'
515>>> '<' + word*5 + '>'
516'<HelpAHelpAHelpAHelpAHelpA>'
517>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000518\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000519%
520Strings can be subscripted (indexed); like in C, the first character of
521a string has subscript (index) 0.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000522
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000523There is no separate character type; a character is simply a string of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000524size one. Like in Icon, substrings can be specified with the {\em
525slice} notation: two indices separated by a colon.
526
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000527\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000528>>> word[4]
529'A'
530>>> word[0:2]
531'He'
532>>> word[2:4]
533'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000534>>>
535\end{verbatim}\ecode
536%
537Slice indices have useful defaults; an omitted first index defaults to
538zero, an omitted second index defaults to the size of the string being
539sliced.
540
541\bcode\begin{verbatim}
542>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000543'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000544>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000545'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000546>>>
547\end{verbatim}\ecode
548%
549Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
550equals \verb\s\.
551
552\bcode\begin{verbatim}
553>>> word[:2] + word[2:]
554'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000555>>> word[:3] + word[3:]
556'HelpA'
557>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000558\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000559%
560Degenerate slice indices are handled gracefully: an index that is too
561large is replaced by the string size, an upper bound smaller than the
562lower bound returns an empty string.
563
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000564\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000565>>> word[1:100]
566'elpA'
567>>> word[10:]
568''
569>>> word[2:1]
570''
571>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000572\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000573%
574Indices may be negative numbers, to start counting from the right.
575For example:
576
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000577\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000578>>> word[-1] # The last character
579'A'
580>>> word[-2] # The last-but-one character
581'p'
582>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000583'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000584>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000585'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000586>>>
587\end{verbatim}\ecode
588%
589But note that -0 is really the same as 0, so it does not count from
590the right!
591
592\bcode\begin{verbatim}
593>>> word[-0] # (since -0 equals 0)
594'H'
595>>>
596\end{verbatim}\ecode
597%
598Out-of-range negative slice indices are truncated, but don't try this
599for single-element (non-slice) indices:
600
601\bcode\begin{verbatim}
602>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000603'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000604>>> word[-10] # error
605Unhandled exception: IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000606>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000607\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000608%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000609The best way to remember how slices work is to think of the indices as
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000610pointing {\em between} characters, with the left edge of the first
611character numbered 0. Then the right edge of the last character of a
612string of {\tt n} characters has index {\tt n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000613
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000614\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000615 +---+---+---+---+---+
616 | H | e | l | p | A |
617 +---+---+---+---+---+
618 0 1 2 3 4 5
619-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000620\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000621%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000622The first row of numbers gives the position of the indices 0...5 in
623the string; the second row gives the corresponding negative indices.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000624The slice from \verb\i\ to \verb\j\ consists of all characters between
625the edges labeled \verb\i\ and \verb\j\, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000626
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000627For nonnegative indices, the length of a slice is the difference of
628the indices, if both are within bounds, e.g., the length of
629\verb\word[1:3]\ is 2.
630
631The built-in function {\tt len()} returns the length of a string:
632
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000633\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000634>>> s = 'supercalifragilisticexpialidocious'
635>>> len(s)
63634
637>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000638\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000639
640\subsection{Lists}
641
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000642Python knows a number of {\em compound} data types, used to group
643together other values. The most versatile is the {\em list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000644can be written as a list of comma-separated values (items) between
645square brackets. List items need not all have the same type.
646
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000647\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000648>>> a = ['foo', 'bar', 100, 1234]
649>>> a
650['foo', 'bar', 100, 1234]
651>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000652\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000653%
654Like string indices, list indices start at 0, and lists can be sliced,
655concatenated and so on:
656
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000657\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000658>>> a[0]
659'foo'
660>>> a[3]
6611234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000662>>> a[-2]
663100
664>>> a[1:-1]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000665['bar', 100]
666>>> a[:2] + ['bletch', 2*2]
667['foo', 'bar', 'bletch', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000668>>> 3*a[:3] + ['Boe!']
669['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000670>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000671\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000672%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000673Unlike strings, which are {\em immutable}, it is possible to change
674individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000675
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000676\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000677>>> a
678['foo', 'bar', 100, 1234]
679>>> a[2] = a[2] + 23
680>>> a
681['foo', 'bar', 123, 1234]
682>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000683\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000684%
685Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000686of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000687
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000688\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000689>>> # Replace some items:
690>>> a[0:2] = [1, 12]
691>>> a
692[1, 12, 123, 1234]
693>>> # Remove some:
694>>> a[0:2] = []
695>>> a
696[123, 1234]
697>>> # Insert some:
698>>> a[1:1] = ['bletch', 'xyzzy']
699>>> a
700[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000701>>> a[:0] = a # Insert (a copy of) itself at the beginning
702>>> a
703[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000704>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000705\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000706%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000707The built-in function {\tt len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000708
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000709\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000710>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00007118
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000712>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000713\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000714%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000715It is possible to nest lists (create lists containing other lists),
716for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000717
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000718\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000719>>> q = [2, 3]
720>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000721>>> len(p)
7223
723>>> p[1]
724[2, 3]
725>>> p[1][0]
7262
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000727>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000728>>> p
729[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000730>>> q
731[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000732>>>
733\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000734%
735Note that in the last example, {\tt p[1]} and {\tt q} really refer to
736the same object! We'll come back to {\em object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000737
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000738\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000739
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000740Of course, we can use Python for more complicated tasks than adding
741two and two together. For instance, we can write an initial
742subsequence of the {\em Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000743
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000744\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000745>>> # Fibonacci series:
746>>> # the sum of two elements defines the next
747>>> a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000748>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000749... print b
750... a, b = b, a+b
751...
7521
7531
7542
7553
7565
7578
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000758>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000759\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000760%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000761This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000762
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000763\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000764
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000765\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000766The first line contains a {\em multiple assignment}: the variables
767{\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
768last line this is used again, demonstrating that the expressions on
769the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000770assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000771
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000772\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000773The {\tt while} loop executes as long as the condition (here: {\tt b <
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000774100}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000775true; zero is false. The condition may also be a string or list value,
776in fact any sequence; anything with a non-zero length is true, empty
777sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000778comparison. The standard comparison operators are written the same as
779in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000780
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000781\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000782The {\em body} of the loop is {\em indented}: indentation is Python's
783way of grouping statements. Python does not (yet!) provide an
784intelligent input line editing facility, so you have to type a tab or
785space(s) for each indented line. In practice you will prepare more
786complicated input for Python with a text editor; most text editors have
787an auto-indent facility. When a compound statement is entered
788interactively, it must be followed by a blank line to indicate
789completion (since the parser cannot guess when you have typed the last
790line).
791
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000792\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000793The {\tt print} statement writes the value of the expression(s) it is
794given. It differs from just writing the expression you want to write
795(as we did earlier in the calculator examples) in the way it handles
796multiple expressions and strings. Strings are written without quotes,
797and a space is inserted between items, so you can format things nicely,
798like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000799
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000800\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000801>>> i = 256*256
802>>> print 'The value of i is', i
803The value of i is 65536
804>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000805\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000806%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000807A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000808
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000809\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000810>>> a, b = 0, 1
811>>> while b < 1000:
812... print b,
813... a, b = b, a+b
814...
8151 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
816>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000817\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000818%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000819Note that the interpreter inserts a newline before it prints the next
820prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000821
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000822\end{itemize}
823
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000824
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000825\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000826
Guido van Rossum4410c751991-06-04 20:22:18 +0000827Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000828usual control flow statements known from other languages, with some
829twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000830
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000831\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000832
833Perhaps the most well-known statement type is the {\tt if} statement.
834For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000835
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000836\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000837>>> if x < 0:
838... x = 0
839... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000840... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000841... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000842... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000843... print 'Single'
844... else:
845... print 'More'
846...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000847\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000848%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000849There can be zero or more {\tt elif} parts, and the {\tt else} part is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000850optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
851useful to avoid excessive indentation. An {\tt if...elif...elif...}
852sequence is a substitute for the {\em switch} or {\em case} statements
853found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000854
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000855\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000856
Guido van Rossum4410c751991-06-04 20:22:18 +0000857The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000858used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000859arithmetic progression of numbers (like in Pascal), or leaving the user
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000860completely free in the iteration test and step (as C), Python's {\tt
861for} statement iterates over the items of any sequence (e.g., a list
862or a string), in the order that they appear in the sequence. For
863example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000864
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000865\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000866>>> # Measure some strings:
867>>> a = ['cat', 'window', 'defenestrate']
868>>> for x in a:
869... print x, len(x)
870...
871cat 3
872window 6
873defenestrate 12
874>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000875\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000876%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000877It is not safe to modify the sequence being iterated over in the loop
878(this can only happen for mutable sequence types, i.e., lists). If
879you need to modify the list you are iterating over, e.g., duplicate
880selected items, you must iterate over a copy. The slice notation
881makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000882
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000883\bcode\begin{verbatim}
884>>> for x in a[:]: # make a slice copy of the entire list
885... if len(x) > 6: a.insert(0, x)
886...
887>>> a
888['defenestrate', 'cat', 'window', 'defenestrate']
889>>>
890\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000891
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000892\section{The {\tt range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000893
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000894If you do need to iterate over a sequence of numbers, the built-in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000895function {\tt range()} comes in handy. It generates lists containing
896arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000897
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000898\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000899>>> range(10)
900[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
901>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000902\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000903%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000904The given end point is never part of the generated list; {\tt range(10)}
905generates a list of 10 values, exactly the legal indices for items of a
906sequence of length 10. It is possible to let the range start at another
907number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000908
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000909\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000910>>> range(5, 10)
911[5, 6, 7, 8, 9]
912>>> range(0, 10, 3)
913[0, 3, 6, 9]
914>>> range(-10, -100, -30)
915[-10, -40, -70]
916>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000917\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000918%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000919To iterate over the indices of a sequence, combine {\tt range()} and
920{\tt len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000921
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000922\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000923>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000924>>> for i in range(len(a)):
925... print i, a[i]
926...
9270 Mary
9281 had
9292 a
9303 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00009314 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000932>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000933\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000934
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000935\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000936
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000937The {\tt break} statement, like in C, breaks out of the smallest
938enclosing {\tt for} or {\tt while} loop.
939
940The {\tt continue} statement, also borrowed from C, continues with the
941next iteration of the loop.
942
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000943Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000944loop terminates through exhaustion of the list (with {\tt for}) or when
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000945the condition becomes false (with {\tt while}), but not when the loop is
946terminated by a {\tt break} statement. This is exemplified by the
947following loop, which searches for a list item of value 0:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000948
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000949\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000950>>> for n in range(2, 10):
951... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000952... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000953... print n, 'equals', x, '*', n/x
954... break
955... else:
956... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000957...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009582 is a prime number
9593 is a prime number
9604 equals 2 * 2
9615 is a prime number
9626 equals 2 * 3
9637 is a prime number
9648 equals 2 * 4
9659 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000966>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000967\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000968
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000969\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000970
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000971The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000972It can be used when a statement is required syntactically but the
973program requires no action.
974For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000975
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000976\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000977>>> while 1:
978... pass # Busy-wait for keyboard interrupt
979...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000980\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000981
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000982\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000983
984We can create a function that writes the Fibonacci series to an
985arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000986
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000987\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000988>>> def fib(n): # write Fibonacci series up to n
989... a, b = 0, 1
990... while b <= n:
991... print b,
992... a, b = b, a+b
993...
994>>> # Now call the function we just defined:
995>>> fib(2000)
9961 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
997>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000998\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000999%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001000The keyword {\tt def} introduces a function {\em definition}. It must
1001be followed by the function name and the parenthesized list of formal
1002parameters. The statements that form the body of the function starts at
1003the next line, indented by a tab stop.
1004
1005The {\em execution} of a function introduces a new symbol table used
1006for the local variables of the function. More precisely, all variable
1007assignments in a function store the value in the local symbol table;
1008whereas
1009 variable references first look in the local symbol table, then
1010in the global symbol table, and then in the table of built-in names.
1011Thus,
1012global variables cannot be directly assigned to from within a
1013function, although they may be referenced.
1014
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001015The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001016the local symbol table of the called function when it is called; thus,
1017arguments are passed using {\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001018\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001019 Actually, {\em call by object reference} would be a better
1020 description, since if a mutable object is passed, the caller
1021 will see any changes the callee makes to it (e.g., items
1022 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001023}
1024When a function calls another function, a new local symbol table is
1025created for that call.
1026
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001027A function definition introduces the function name in the
1028current
1029symbol table. The value
1030of the function name
1031has a type that is recognized by the interpreter as a user-defined
1032function. This value can be assigned to another name which can then
1033also be used as a function. This serves as a general renaming
1034mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001035
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001036\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001037>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001038<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001039>>> f = fib
1040>>> f(100)
10411 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 Rossum6fc178f1991-08-16 09:13:42 +00001045You might object that {\tt fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001046Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001047value. In fact, technically speaking, procedures do return a value,
1048albeit a rather boring one. This value is called {\tt None} (it's a
1049built-in name). Writing the value {\tt None} is normally suppressed by
1050the interpreter if it would be the only value written. You can see it
1051if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001052
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001053\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001054>>> print fib(0)
1055None
1056>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001057\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001058%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001059It is simple to write a function that returns a list of the numbers of
1060the Fibonacci series, instead of printing it:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001061
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001062\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001063>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001064... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001065... a, b = 0, 1
1066... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001067... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001068... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001069... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001070...
1071>>> f100 = fib2(100) # call it
1072>>> f100 # write the result
1073[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1074>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001075\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001076%
Guido van Rossum4410c751991-06-04 20:22:18 +00001077This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001078
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001079\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001080
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001081\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001082The {\tt return} statement returns with a value from a function. {\tt
1083return} without an expression argument is used to return from the middle
1084of a procedure (falling off the end also returns from a proceduce), in
1085which case the {\tt None} value is returned.
1086
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001087\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001088The statement {\tt result.append(b)} calls a {\em method} of the list
1089object {\tt result}. A method is a function that `belongs' to an
1090object and is named {\tt obj.methodname}, where {\tt obj} is some
1091object (this may be an expression), and {\tt methodname} is the name
1092of a method that is defined by the object's type. Different types
1093define different methods. Methods of different types may have the
1094same name without causing ambiguity. (It is possible to define your
1095own object types and methods, using {\em classes}. This is an
1096advanced feature that is not discussed in this tutorial.)
1097The method {\tt append} shown in the example, is defined for
1098list objects; it adds a new element at the end of the list. In this
1099example
1100it is equivalent to {\tt result = result + [b]}, but more efficient.
1101
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001102\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001103
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001104
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001105\chapter{Odds and Ends}
1106
1107This chapter describes some things you've learned about already in
1108more detail, and adds some new things as well.
1109
1110\section{More on Lists}
1111
1112The list data type has some more methods. Here are all of the methods
1113of lists objects:
1114
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001115\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001116
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001117\item[{\tt insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001118Insert an item at a given position. The first argument is the index of
1119the element before which to insert, so {\tt a.insert(0, x)} inserts at
1120the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
1121{\tt a.append(x)}.
1122
1123\item[{\tt append(x)}]
1124Equivalent to {\tt a.insert(len(a), x)}.
1125
1126\item[{\tt index(x)}]
1127Return the index in the list of the first item whose value is {\tt x}.
1128It is an error if there is no such item.
1129
1130\item[{\tt remove(x)}]
1131Remove the first item from the list whose value is {\tt x}.
1132It is an error if there is no such item.
1133
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001134\item[{\tt sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001135Sort the items of the list, in place.
1136
1137\item[{\tt reverse()}]
1138Reverse the elements of the list, in place.
1139
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001140\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001141
1142An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001143
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001144\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001145>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001146>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001147>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001148>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001149[66.6, 333, -1, 333, 1, 1234.5, 333]
1150>>> a.index(333)
11511
1152>>> a.remove(333)
1153>>> a
1154[66.6, -1, 333, 1, 1234.5, 333]
1155>>> a.reverse()
1156>>> a
1157[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001158>>> a.sort()
1159>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001160[-1, 1, 66.6, 333, 333, 1234.5]
1161>>>
1162\end{verbatim}\ecode
1163
1164\section{The {\tt del} statement}
1165
1166There is a way to remove an item from a list given its index instead
1167of its value: the {\tt del} statement. This can also be used to
1168remove slices from a list (which we did earlier by assignment of an
1169empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001170
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001171\bcode\begin{verbatim}
1172>>> a
1173[-1, 1, 66.6, 333, 333, 1234.5]
1174>>> del a[0]
1175>>> a
1176[1, 66.6, 333, 333, 1234.5]
1177>>> del a[2:4]
1178>>> a
1179[1, 66.6, 1234.5]
1180>>>
1181\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001182%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001183{\tt del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001184
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001185\bcode\begin{verbatim}
1186>>> del a
1187>>>
1188\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001189%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001190Referencing the name {\tt a} hereafter is an error (at least until
1191another value is assigned to it). We'll find other uses for {\tt del}
1192later.
1193
1194\section{Tuples and Sequences}
1195
1196We saw that lists and strings have many common properties, e.g.,
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001197indexinging and slicing operations. They are two examples of {\em
1198sequence} data types. Since Python is an evolving language, other
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001199sequence data types may be added. There is also another standard
1200sequence data type: the {\em tuple}.
1201
1202A tuple consists of a number of values separated by commas, for
1203instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001204
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001205\bcode\begin{verbatim}
1206>>> t = 12345, 54321, 'hello!'
1207>>> t[0]
120812345
1209>>> t
1210(12345, 54321, 'hello!')
1211>>> # Tuples may be nested:
1212>>> u = t, (1, 2, 3, 4, 5)
1213>>> u
1214((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1215>>>
1216\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001217%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001218As you see, on output tuples are alway enclosed in parentheses, so
1219that nested tuples are interpreted correctly; they may be input with
1220or without surrounding parentheses, although often parentheses are
1221necessary anyway (if the tuple is part of a larger expression).
1222
1223Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1224from a database, etc. Tuples, like strings, are immutable: it is not
1225possible to assign to the individual items of a tuple (you can
1226simulate much of the same effect with slicing and concatenation,
1227though).
1228
1229A special problem is the construction of tuples containing 0 or 1
1230items: the syntax has some extra quirks to accomodate these. Empty
1231tuples are constructed by an empty pair of parentheses; a tuple with
1232one item is constructed by following a value with a comma
1233(it is not sufficient to enclose a single value in parentheses).
1234Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001235
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001236\bcode\begin{verbatim}
1237>>> empty = ()
1238>>> singleton = 'hello', # <-- note trailing comma
1239>>> len(empty)
12400
1241>>> len(singleton)
12421
1243>>> singleton
1244('hello',)
1245>>>
1246\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001247%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001248The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
1249tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
1250are packed together in a tuple. The reverse operation is also
1251possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001252
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001253\bcode\begin{verbatim}
1254>>> x, y, z = t
1255>>>
1256\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001257%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001258This is called, appropriately enough, {\em tuple unpacking}. Tuple
1259unpacking requires that the list of variables on the left has the same
1260number of elements as the length of the tuple. Note that multiple
1261assignment is really just a combination of tuple packing and tuple
1262unpacking!
1263
1264Occasionally, the corresponding operation on lists is useful: {\em list
1265unpacking}. This is supported by enclosing the list of variables in
1266square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001267
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001268\bcode\begin{verbatim}
1269>>> a = ['foo', 'bar', 100, 1234]
1270>>> [a1, a2, a3, a4] = a
1271>>>
1272\end{verbatim}\ecode
1273
1274\section{Dictionaries}
1275
1276Another useful data type built into Python is the {\em dictionary}.
1277Dictionaries are sometimes found in other languages as ``associative
1278memories'' or ``associative arrays''. Unlike sequences, which are
1279indexed by a range of numbers, dictionaries are indexed by {\em keys},
1280which are strings. It is best to think of a dictionary as an unordered set of
1281{\em key:value} pairs, with the requirement that the keys are unique
1282(within one dictionary).
1283A pair of braces creates an empty dictionary: \verb/{}/.
1284Placing a comma-separated list of key:value pairs within the
1285braces adds initial key:value pairs to the dictionary; this is also the
1286way dictionaries are written on output.
1287
1288The main operations on a dictionary are storing a value with some key
1289and extracting the value given the key. It is also possible to delete
1290a key:value pair
1291with {\tt del}.
1292If you store using a key that is already in use, the old value
1293associated with that key is forgotten. It is an error to extract a
1294value using a non-existant key.
1295
1296The {\tt keys()} method of a dictionary object returns a list of all the
1297keys used in the dictionary, in random order (if you want it sorted,
1298just apply the {\tt sort()} method to the list of keys). To check
1299whether a single key is in the dictionary, use the \verb/has_key()/
1300method of the dictionary.
1301
1302Here is a small example using a dictionary:
1303
1304\bcode\begin{verbatim}
1305>>> tel = {'jack': 4098, 'sape': 4139}
1306>>> tel['guido'] = 4127
1307>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001308{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001309>>> tel['jack']
13104098
1311>>> del tel['sape']
1312>>> tel['irv'] = 4127
1313>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001314{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001315>>> tel.keys()
1316['guido', 'irv', 'jack']
1317>>> tel.has_key('guido')
13181
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001319>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001320\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001321
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001322\section{More on Conditions}
1323
1324The conditions used in {\tt while} and {\tt if} statements above can
1325contain other operators besides comparisons.
1326
1327The comparison operators {\tt in} and {\tt not in} check whether a value
1328occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
1329is not} compare whether two objects are really the same object; this
1330only matters for mutable objects like lists. All comparison operators
1331have the same priority, which is lower than that of all numerical
1332operators.
1333
1334Comparisons can be chained: e.g., {\tt a < b = c} tests whether {\tt a}
1335is less than {\tt b} and moreover {\tt b} equals {\tt c}.
1336
1337Comparisons may be combined by the Boolean operators {\tt and} and {\tt
1338or}, and the outcome of a comparison (or of any other Boolean
1339expression) may be negated with {\tt not}. These all have lower
1340priorities than comparison operators again; between them, {\tt not} has
1341the highest priority, and {\tt or} the lowest, so that
1342{\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
1343course, parentheses can be used to express the desired composition.
1344
1345The Boolean operators {\tt and} and {\tt or} are so-called {\em
1346shortcut} operators: their arguments are evaluated from left to right,
1347and evaluation stops as soon as the outcome is determined. E.g., if
1348{\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
1349does not evaluate the expression C. In general, the return value of a
1350shortcut operator, when used as a general value and not as a Boolean, is
1351the last evaluated argument.
1352
1353It is possible to assign the result of a comparison or other Boolean
1354expression to a variable, but you must enclose the entire Boolean
1355expression in parentheses. This is necessary because otherwise an
1356assignment like \verb/a = b = c/ would be ambiguous: does it assign the
1357value of {\tt c} to {\tt a} and {\tt b}, or does it compare {\tt b} to
1358{\tt c} and assign the outcome (0 or 1) to {\tt a}? As it is, the first
1359meaning is what you get, and to get the latter you have to write
1360\verb/a = (b = c)/. (In Python, unlike C, assignment cannot occur
1361inside expressions.)
1362
1363\section{Comparing Sequences and Other Types}
1364
1365Sequence objects may be compared to other objects with the same
1366sequence type. The comparison uses {\em lexicographical} ordering:
1367first the first two items are compared, and if they differ this
1368determines the outcome of the comparison; if they are equal, the next
1369two items are compared, and so on, until either sequence is exhausted.
1370If two items to be compared are themselves sequences of the same type,
1371the lexiographical comparison is carried out recursively. If all
1372items of two sequences compare equal, the sequences are considered
1373equal. If one sequence is an initial subsequence of the other, the
1374shorted sequence is the smaller one. Lexicographical ordering for
1375strings uses the ASCII ordering for individual characters. Some
1376examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001377
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001378\bcode\begin{verbatim}
1379(1, 2, 3) < (1, 2, 4)
1380[1, 2, 3] < [1, 2, 4]
1381'ABC' < 'C' < 'Pascal' < 'Python'
1382(1, 2, 3, 4) < (1, 2, 4)
1383(1, 2) < (1, 2, -1)
1384(1, 2, 3) = (1.0, 2.0, 3.0)
1385(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1386\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001387%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001388Note that comparing objects of different types is legal. The outcome
1389is deterministic but arbitrary: the types are ordered by their name.
1390Thus, a list is always smaller than a string, a string is always
1391smaller than a tuple, etc. Mixed numeric types are compared according
1392to their numeric value, so 0 equals 0.0, etc.%
1393\footnote{
1394 The rules for comparing objects of different types should
1395 not be relied upon; they may change in a future version of
1396 the language.
1397}
1398
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001399
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001400\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001401
Guido van Rossum4410c751991-06-04 20:22:18 +00001402If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001403definitions you have made (functions and variables) are lost.
1404Therefore, if you want to write a somewhat longer program, you are
1405better off using a text editor to prepare the input for the interpreter
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001406and run it with that file as input instead. This is known as creating a
1407{\em script}. As your program gets longer, you may want to split it
1408into several files for easier maintenance. You may also want to use a
1409handy function that you've written in several programs without copying
1410its definition into each program.
1411
Guido van Rossum4410c751991-06-04 20:22:18 +00001412To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001413them in a script or in an interactive instance of the interpreter.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001414Such a file is called a {\em module}; definitions from a module can be
1415{\em imported} into other modules or into the {\em main} module (the
1416collection of variables that you have access to in a script
1417executed at the top level
1418and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001419
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001420A module is a file containing Python definitions and statements. The
1421file name is the module name with the suffix {\tt .py} appended. For
1422instance, use your favorite text editor to create a file called {\tt
1423fibo.py} in the current directory with the following contents:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001424
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001425\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001426# Fibonacci numbers module
1427
1428def fib(n): # write Fibonacci series up to n
1429 a, b = 0, 1
1430 while b <= n:
1431 print b,
1432 a, b = b, a+b
1433
1434def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001435 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001436 a, b = 0, 1
1437 while b <= n:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001438 result.append(b)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001439 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001440 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001441\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001442%
Guido van Rossum4410c751991-06-04 20:22:18 +00001443Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001444following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001445
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001446\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001447>>> import fibo
1448>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001449\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001450%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001451This does not enter the names of the functions defined in
1452{\tt fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001453directly in the current symbol table; it only enters the module name
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001454{\tt fibo}
1455there.
1456Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001457
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001458\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001459>>> fibo.fib(1000)
14601 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1461>>> fibo.fib2(100)
1462[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1463>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001464\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001465%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001466If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001467
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001468\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001469>>> fib = fibo.fib
1470>>> fib(500)
14711 1 2 3 5 8 13 21 34 55 89 144 233 377
1472>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001473\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001474
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001475\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001476
1477A module can contain executable statements as well as function
1478definitions.
1479These statements are intended to initialize the module.
1480They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001481{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001482time the module is imported somewhere.%
1483\footnote{
1484 In fact function definitions are also `statements' that are
1485 `executed'; the execution enters the function name in the
1486 module's global symbol table.
1487}
1488
1489Each module has its own private symbol table, which is used as the
1490global symbol table by all functions defined in the module.
1491Thus, the author of a module can use global variables in the module
1492without worrying about accidental clashes with a user's global
1493variables.
1494On the other hand, if you know what you are doing you can touch a
1495module's global variables with the same notation used to refer to its
1496functions,
1497{\tt modname.itemname}.
1498
1499Modules can import other modules.
1500It is customary but not required to place all
1501{\tt import}
1502statements at the beginning of a module (or script, for that matter).
1503The imported module names are placed in the importing module's global
1504symbol table.
1505
1506There is a variant of the
1507{\tt import}
1508statement that imports names from a module directly into the importing
1509module's symbol table.
1510For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001511
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001512\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001513>>> from fibo import fib, fib2
1514>>> fib(500)
15151 1 2 3 5 8 13 21 34 55 89 144 233 377
1516>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001517\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001518%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001519This does not introduce the module name from which the imports are taken
1520in the local symbol table (so in the example, {\tt fibo} is not
1521defined).
1522
1523There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001524
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001525\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001526>>> from fibo import *
1527>>> fib(500)
15281 1 2 3 5 8 13 21 34 55 89 144 233 377
1529>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001530\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001531%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001532This imports all names except those beginning with an underscore
Guido van Rossum573805a1992-03-06 10:56:03 +00001533({\tt _}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001534
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001535\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001536
Guido van Rossum4410c751991-06-04 20:22:18 +00001537Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001538document (Python Library Reference). Some modules are built into the
1539interpreter; these provide access to operations that are not part of the
1540core of the language but are nevertheless built in, either for
1541efficiency or to provide access to operating system primitives such as
1542system calls. The set of such modules is a configuration option; e.g.,
1543the {\tt amoeba} module is only provided on systems that somehow support
1544Amoeba primitives. One particular module deserves some attention: {\tt
1545sys}, which is built into every Python interpreter. The variables {\tt
1546sys.ps1} and {\tt sys.ps2} define the strings used as primary and
1547secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001548
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001549\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001550>>> import sys
1551>>> sys.ps1
1552'>>> '
1553>>> sys.ps2
1554'... '
1555>>> sys.ps1 = 'C> '
1556C> print 'Yuck!'
1557Yuck!
1558C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001559\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001560%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001561These two variables are only defined if the interpreter is in
1562interactive mode.
1563
1564The variable
1565{\tt sys.path}
1566is a list of strings that determine the interpreter's search path for
1567modules.
1568It is initialized to a default path taken from the environment variable
1569{\tt PYTHONPATH},
1570or from a built-in default if
1571{\tt PYTHONPATH}
1572is not set.
1573You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001574
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001575\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001576>>> import sys
1577>>> sys.path.append('/ufs/guido/lib/python')
1578>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001579\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001580
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001581\section{The {\tt dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001582
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001583The built-in function {\tt dir} is used to find out which names a module
1584defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001585
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001586\bcode\begin{verbatim}
1587>>> import fibo, sys
1588>>> dir(fibo)
1589['fib', 'fib2']
1590>>> dir(sys)
1591['argv', 'exit', 'modules', 'path', 'ps1', 'ps2', 'stderr', 'stdin', 'stdout']
1592>>>
1593\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001594%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001595Without arguments, {\tt dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001596
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001597\bcode\begin{verbatim}
1598>>> a = [1, 2, 3, 4, 5]
1599>>> import fibo, sys
1600>>> fib = fibo.fib
1601>>> dir()
1602['a', 'fib', 'fibo', 'sys']
1603>>>
1604\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001605%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001606Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001607
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001608{\tt dir()} does not list the names of built-in functions and variables.
1609If you want a list of those, they are defined in the standard module
Guido van Rossum4bd023f1993-10-27 13:49:20 +00001610{\tt __builtin__}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001611
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001612\bcode\begin{verbatim}
Guido van Rossum4bd023f1993-10-27 13:49:20 +00001613>>> import __builtin__
1614>>> dir(__builtin__)
1615['AccessError', 'AttributeError', 'ConflictError', 'EOFError', 'IOError', 'I
1616mportError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'MemoryError', '
1617NameError', 'None', 'OverflowError', 'RuntimeError', 'SyntaxError', 'SystemE
1618rror', 'SystemExit', 'TypeError', 'ValueError', 'ZeroDivisionError', 'abs',
1619'apply', 'chr', 'cmp', 'coerce', 'compile', 'dir', 'divmod', 'eval', 'execfi
1620le', 'float', 'getattr', 'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'le
1621n', 'long', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'range', 'raw_input',
1622 'reload', 'repr', 'round', 'setattr', 'str', 'type']
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001623>>>
1624\end{verbatim}\ecode
1625
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001626
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001627\chapter{Output Formatting}
1628
1629So far we've encountered two ways of writing values: {\em expression
1630statements} and the {\tt print} statement. (A third way is using the
1631{\tt write} method of file objects; the standard output file can be
1632referenced as {\tt sys.stdout}. See the Library Reference for more
1633information on this.)
1634
1635Often you'll want more control over the formatting of your output than
1636simply printing space-separated values. The key to nice formatting in
1637Python is to do all the string handling yourself; using string slicing
1638and concatenation operations you can create any lay-out you can imagine.
1639The standard module {\tt string} contains some useful operations for
1640padding strings to a given column width; these will be discussed shortly.
1641
1642One question remains, of course: how do you convert values to strings?
1643Luckily, Python has a way to convert any value to a string: just write
1644the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001645
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001646\bcode\begin{verbatim}
1647>>> x = 10 * 3.14
1648>>> y = 200*200
1649>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1650>>> print s
1651The value of x is 31.4, and y is 40000...
1652>>> # Reverse quotes work on other types besides numbers:
1653>>> p = [x, y]
1654>>> ps = `p`
1655>>> ps
1656'[31.4, 40000]'
1657>>> # Converting a string adds string quotes and backslashes:
1658>>> hello = 'hello, world\n'
1659>>> hellos = `hello`
1660>>> print hellos
1661'hello, world\012'
1662>>> # The argument of reverse quotes may be a tuple:
1663>>> `x, y, ('foo', 'bar')`
1664'(31.4, 40000, (\'foo\', \'bar\'))'
1665>>>
1666\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001667%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001668Here is how you write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001669
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001670\bcode\begin{verbatim}
1671>>> import string
1672>>> for x in range(1, 11):
1673... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1674... # Note trailing comma on previous line
1675... print string.rjust(`x*x*x`, 4)
1676...
1677 1 1 1
1678 2 4 8
1679 3 9 27
1680 4 16 64
1681 5 25 125
1682 6 36 216
1683 7 49 343
1684 8 64 512
1685 9 81 729
168610 100 1000
1687>>>
1688\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001689%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001690(Note that one space between each column was added by the way {\tt print}
1691works: it always adds spaces between its arguments.)
1692
1693This example demonstrates the function {\tt string.rjust()}, which
1694right-justifies a string in a field of a given width by padding it with
1695spaces on the left. There are similar functions {\tt string.ljust()}
1696and {\tt string.center()}. These functions do not write anything, they
1697just return a new string. If the input string is too long, they don't
1698truncate it, but return it unchanged; this will mess up your column
1699lay-out but that's usually better than the alternative, which would be
1700lying about a value. (If you really want truncation you can always add
1701a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1702
1703There is another function, {\tt string.zfill}, which pads a numeric
1704string on the left with zeros. It understands about plus and minus
1705signs:%
1706\footnote{
1707 Better facilities for formatting floating point numbers are
1708 lacking at this moment.
1709}
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001710
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001711\bcode\begin{verbatim}
1712>>> string.zfill('12', 5)
1713'00012'
1714>>> string.zfill('-3.14', 7)
1715'-003.14'
1716>>> string.zfill('3.14159265359', 5)
1717'3.14159265359'
1718>>>
1719\end{verbatim}\ecode
1720
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001721
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001722\chapter{Errors and Exceptions}
1723
1724Until now error messages haven't been more than mentioned, but if you
1725have tried out the examples you have probably seen some. There are
1726(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1727and {\em exceptions}.
1728
1729\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001730
1731Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001732kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001733
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001734\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001735>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001736Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001737while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001738 ^
1739Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001740>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001741\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001742%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001743The parser repeats the offending line and displays a little `arrow'
1744pointing at the earliest point in the line where the error was detected.
1745The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001746{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001747the arrow: in the example, the error is detected at the keyword
1748{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001749File name and line number are printed so you know where to look in case
1750the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001751
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001752\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001753
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001754Even if a statement or expression is syntactically correct, it may
1755cause an error when an attempt is made to execute it.
1756Errors detected during execution are called {\em exceptions} and are
1757not unconditionally fatal: you will soon learn how to handle them in
1758Python programs. Most exceptions are not handled by programs,
1759however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001760
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001761\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001762>>> 10 * (1/0)
Guido van Rossum3cbc16d1993-12-17 12:13:53 +00001763Traceback (innermost last):
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001764 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001765ZeroDivisionError: integer division or modulo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001766>>> 4 + foo*3
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001767Stack backtrace (innermost last):
1768 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001769NameError: foo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001770>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001771Stack backtrace (innermost last):
1772 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001773TypeError: illegal argument type for built-in operation
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001774>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001775\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001776%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001777The last line of the error message indicates what happened.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001778Exceptions come in different types, and the type is printed as part of
1779the message: the types in the example are
Guido van Rossumb2c65561993-05-12 08:53:36 +00001780{\tt ZeroDivisionError},
1781{\tt NameError}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001782and
Guido van Rossumb2c65561993-05-12 08:53:36 +00001783{\tt TypeError}.
1784The string printed as the exception type is the name of the built-in
1785name for the exception that occurred. This is true for all built-in
1786exceptions, but need not be true for user-defined exceptions (although
1787it is a useful convention).
1788Standard exception names are built-in identifiers (not reserved
1789keywords).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001790
Guido van Rossumb2c65561993-05-12 08:53:36 +00001791The rest of the line is a detail whose interpretation depends on the
1792exception type; its meaning is dependent on the exception type.
1793
1794The preceding part of the error message shows the context where the
1795exception happened, in the form of a stack backtrace.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001796In general it contains a stack backtrace listing source lines; however,
1797it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001798
Guido van Rossumb2c65561993-05-12 08:53:36 +00001799The Python library reference manual lists the built-in exceptions and
1800their meanings.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001801
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001802\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001803
1804It is possible to write programs that handle selected exceptions.
1805Look at the following example, which prints a table of inverses of
1806some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001807
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001808\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001809>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001810>>> for x in numbers:
1811... print x,
1812... try:
1813... print 1.0 / x
Guido van Rossumb2c65561993-05-12 08:53:36 +00001814... except ZeroDivisionError:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001815... print '*** has no inverse ***'
1816...
18170.3333 3.00030003
18182.5 0.4
18190 *** has no inverse ***
182010 0.1
1821>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001822\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001823%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001824The {\tt try} statement works as follows.
1825\begin{itemize}
1826\item
1827First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001828{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001829(the statement(s) between the {\tt try} and {\tt except} keywords) is
1830executed.
1831\item
1832If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001833{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001834is skipped and execution of the {\tt try} statement is finished.
1835\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001836If an exception occurs during execution of the try clause,
1837the rest of the clause is skipped. Then if
1838its type matches the exception named after the {\tt except} keyword,
1839the rest of the try clause is skipped, the except clause is executed,
1840and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001841\item
1842If an exception occurs which does not match the exception named in the
1843except clause, it is passed on to outer try statements; if no handler is
1844found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001845{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001846and execution stops with a message as shown above.
1847\end{itemize}
1848A {\tt try} statement may have more than one except clause, to specify
1849handlers for different exceptions.
1850At most one handler will be executed.
1851Handlers only handle exceptions that occur in the corresponding try
1852clause, not in other handlers of the same {\tt try} statement.
1853An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001854e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001855
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001856\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001857... except (RuntimeError, TypeError, NameError):
1858... pass
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 +00001861The last except clause may omit the exception name(s), to serve as a
1862wildcard.
Guido van Rossumb2c65561993-05-12 08:53:36 +00001863Use this with extreme caution, since it is easy to mask a real
1864programming error in this way!
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001865
1866When an exception occurs, it may have an associated value, also known as
1867the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001868{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001869The presence and type of the argument depend on the exception type.
1870For exception types which have an argument, the except clause may
1871specify a variable after the exception name (or list) to receive the
1872argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001873
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001874\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001875>>> try:
1876... foo()
1877... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001878... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001879...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001880name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001881>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001882\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001883%
Guido van Rossumb2c65561993-05-12 08:53:36 +00001884If an exception has an argument, it is printed as the last part
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001885(`detail') of the message for unhandled exceptions.
1886
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001887Exception handlers don't just handle exceptions if they occur
1888immediately in the try clause, but also if they occur inside functions
1889that are called (even indirectly) in the try clause.
1890For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001891
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001892\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001893>>> def this_fails():
1894... x = 1/0
1895...
1896>>> try:
1897... this_fails()
Guido van Rossumb2c65561993-05-12 08:53:36 +00001898... except ZeroDivisionError, detail:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001899... print 'Handling run-time error:', detail
1900...
Guido van Rossumb2c65561993-05-12 08:53:36 +00001901Handling run-time error: integer division or modulo
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001902>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001903\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001904
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001905\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001906
1907The {\tt raise} statement allows the programmer to force a specified
1908exception to occur.
1909For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001910
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001911\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001912>>> raise NameError, 'HiThere'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001913Stack backtrace (innermost last):
1914 File "<stdin>", line 1
Guido van Rossumb2c65561993-05-12 08:53:36 +00001915NameError: HiThere
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001916>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001917\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001918%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001919The first argument to {\tt raise} names the exception to be raised.
1920The optional second argument specifies the exception's argument.
1921
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001922\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001923
1924Programs may name their own exceptions by assigning a string to a
1925variable.
1926For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001927
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001928\bcode\begin{verbatim}
Guido van Rossumb2c65561993-05-12 08:53:36 +00001929>>> my_exc = 'my_exc'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001930>>> try:
1931... raise my_exc, 2*2
1932... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001933... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001934...
1935My exception occured, value: 4
1936>>> raise my_exc, 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001937Stack backtrace (innermost last):
1938 File "<stdin>", line 7
Guido van Rossumb2c65561993-05-12 08:53:36 +00001939my_exc: 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001940>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001941\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001942%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001943Many standard modules use this to report errors that may occur in
1944functions they define.
1945
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001946\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001947
1948The {\tt try} statement has another optional clause which is intended to
1949define clean-up actions that must be executed under all circumstances.
1950For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001951
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001952\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001953>>> try:
1954... raise KeyboardInterrupt
1955... finally:
1956... print 'Goodbye, world!'
1957...
1958Goodbye, world!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001959Stack backtrace (innermost last):
1960 File "<stdin>", line 2
Guido van Rossumb2c65561993-05-12 08:53:36 +00001961KeyboardInterrupt
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001962>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001963\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001964%
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001965A {\tt finally} clause is executed whether or not an exception has
1966occurred in the {\tt try} clause. When an exception has occurred, it
1967is re-raised after the {\tt finally} clauses is executed. The
1968{\tt finally} clause is also executed ``on the way out'' when the
1969{\tt try} statement is left via a {\tt break} or {\tt return}
1970statement.
1971
1972A {\tt try} statement must either have one or more {\tt except}
1973clauses or one {\tt finally} clause, but not both.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001974
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001975
1976\chapter{Classes}
1977
1978Python's class mechanism adds classes to the language with a minimum
1979of new syntax and semantics. It is a mixture of the class mechanisms
1980found in C++ and Modula-3. As is true for modules, classes in Python
1981do not put an absolute barrier between definition and user, but rather
1982rely on the politeness of the user not to ``break into the
1983definition.'' The most important features of classes are retained
1984with full power, however: the class inheritance mechanism allows
1985multiple base classes, a derived class can override any methods of its
1986base class(es), a method can call the method of a base class with the
1987same name. Objects can contain an arbitrary amount of private data.
1988
1989In C++ terminology, all class members (including the data members) are
1990{\em public}, and all member functions are {\em virtual}. There are
1991no special constructors or desctructors. As in Modula-3, there are no
1992shorthands for referencing the object's members from its methods: the
1993method function is declared with an explicit first argument
1994representing the object, which is provided implicitly by the call. As
1995in Smalltalk, classes themselves are objects, albeit in the wider
1996sense of the word: in Python, all data types are objects. This
1997provides semantics for importing and renaming. But, just like in C++
1998or Modula-3, built-in types cannot be used as base classes for
1999extension by the user. Also, like in Modula-3 but unlike in C++, the
2000built-in operators with special syntax (arithmetic operators,
2001subscriptong etc.) cannot be redefined for class members.
2002
2003
2004\section{A word about terminology}
2005
2006Lacking universally accepted terminology to talk about classes, I'll
2007make occasional use of Smalltalk and C++ terms. (I'd use Modula-3
2008terms, since its object-oriented semantics are closer to those of
2009Python than C++, but I expect that few readers have heard of it...)
2010
2011I also have to warn you that there's a terminological pitfall for
2012object-oriented readers: the word ``object'' in Python does not
2013necessarily mean a class instance. Like C++ and Modula-3, and unlike
2014Smalltalk, not all types in Python are classes: the basic built-in
2015types like integers and lists aren't, and even somewhat more exotic
2016types like files aren't. However, {\em all} Python types share a little
2017bit of common semantics that is best described by using the word
2018object.
2019
2020Objects have individuality, and multiple names (in multiple scopes)
2021can be bound to the same object. This is known as aliasing in other
2022languages. This is usually not appreciated on a first glance at
2023Python, and can be safely ignored when dealing with immutable basic
2024types (numbers, strings, tuples). However, aliasing has an
2025(intended!) effect on the semantics of Python code involving mutable
2026objects such as lists, dictionaries, and most types representing
2027entities outside the program (files, windows, etc.). This is usually
2028used to the benefit of the program, since aliases behave like pointers
2029in some respects. For example, passing an object is cheap since only
2030a pointer is passed by the implementation; and if a function modifies
2031an object passed as an argument, the caller will see the change --- this
2032obviates the need for two different argument passing mechanisms as in
2033Pascal.
2034
2035
2036\section{Python scopes and name spaces}
2037
2038Before introducing classes, I first have to tell you something about
2039Python's scope rules. Class definitions play some neat tricks with
2040name spaces, and you need to know how scopes and name spaces work to
2041fully understand what's going on. Incidentally, knowledge about this
2042subject is useful for any advanced Python programmer.
2043
2044Let's begin with some definitions.
2045
2046A {\em name space} is a mapping from names to objects. Most name
2047spaces are currently implemented as Python dictionaries, but that's
2048normally not noticeable in any way (except for performance), and it
2049may change in the future. Examples of name spaces are: the set of
2050built-in names (functions such as \verb\abs()\, and built-in exception
2051names); the global names in a module; and the local names in a
2052function invocation. In a sense the set of attributes of an object
2053also form a name space. The important things to know about name
2054spaces is that there is absolutely no relation between names in
2055different name spaces; for instance, two different modules may both
2056define a function ``maximize'' without confusion --- users of the
2057modules must prefix it with the module name.
2058
2059By the way, I use the word {\em attribute} for any name following a
2060dot --- for example, in the expression \verb\z.real\, \verb\real\ is
2061an attribute of the object \verb\z\. Strictly speaking, references to
2062names in modules are attribute references: in the expression
2063\verb\modname.funcname\, \verb\modname\ is a module object and
2064\verb\funcname\ is an attribute of it. In this case there happens to
2065be a straightforward mapping between the module's attributes and the
2066global names defined in the module: they share the same name space!%
2067\footnote{
2068 Except for one thing. Module objects have a secret read-only
2069 attribute called {\tt __dict__} which returns the dictionary
2070 used to implement the module's name space; the name
2071 {\tt __dict__} is an attribute but not a global name.
2072 Obviously, using this violates the abstraction of name space
2073 implementation, and should be restricted to things like
2074 post-mortem debuggers...
2075}
2076
2077Attributes may be read-only or writable. In the latter case,
2078assignment to attributes is possible. Module attributes are writable:
2079you can write \verb\modname.the_answer = 42\. Writable attributes may
2080also be deleted with the del statement, e.g.
2081\verb\del modname.the_answer\.
2082
2083Name spaces are created at different moments and have different
2084lifetimes. The name space containing the built-in names is created
2085when the Python interpreter starts up, and is never deleted. The
2086global name space for a module is created when the module definition
2087is read in; normally, module name spaces also last until the
2088interpreter quits. The statements executed by the top-level
2089invocation of the interpreter, either read from a script file or
2090interactively, are considered part of a module called \verb\__main__\,
2091so they have their own global name space. (The built-in names
Guido van Rossum4bd023f1993-10-27 13:49:20 +00002092actually also live in a module; this is called \verb\__builtin__\.)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002093
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):
Guido van Rossum084b0b21992-08-14 09:19:56 +00002388 self.add(x)
2389 self.add(x)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002390\end{verbatim}
2391
2392
2393The instantiation operation (``calling'' a class object) creates an
2394empty object. Many classes like to create objects in a known initial
2395state. There is no special syntax to enforce this, but a convention
2396works almost as well: add a method named \verb\init\ to the class,
2397which initializes the instance (by assigning to some important data
2398attributes) and returns the instance itself. For example, class
2399\verb\Bag\ above could have the following method:
2400
2401\begin{verbatim}
2402 def init(self):
2403 self.empty()
2404 return self
2405\end{verbatim}
2406
2407The client can then create and initialize an instance in one
2408statement, as follows:
2409
2410\begin{verbatim}
2411 x = Bag().init()
2412\end{verbatim}
2413
2414Of course, the \verb\init\ method may have arguments for greater
2415flexibility.
2416
2417Warning: a common mistake is to forget the \verb\return self\ at the
2418end of an init method!
2419
2420
2421Methods may reference global names in the same way as ordinary
2422functions. The global scope associated with a method is the module
2423containing the class definition. (The class itself is never used as a
2424global scope!) While one rarely encounters a good reason for using
2425global data in a method, there are many legitimate uses of the global
2426scope: for one thing, functions and modules imported into the global
2427scope can be used by methods, as well as functions and classes defined
2428in it. Usually, the class containing the method is itself defined in
2429this global scope, and in the next section we'll find some good
2430reasons why a method would want to reference its own class!
2431
2432
2433\section{Inheritance}
2434
2435Of course, a language feature would not be worthy of the name ``class''
2436without supporting inheritance. The syntax for a derived class
2437definition looks as follows:
2438
2439\begin{verbatim}
2440 class DerivedClassName(BaseClassName):
2441 <statement-1>
2442 .
2443 .
2444 .
2445 <statement-N>
2446\end{verbatim}
2447
2448The name \verb\BaseClassName\ must be defined in a scope containing
2449the derived class definition. Instead of a base class name, an
2450expression is also allowed. This is useful when the base class is
2451defined in another module, e.g.,
2452
2453\begin{verbatim}
2454 class DerivedClassName(modname.BaseClassName):
2455\end{verbatim}
2456
2457Execution of a derived class definition proceeds the same as for a
2458base class. When the class object is constructed, the base class is
2459remembered. This is used for resolving attribute references: if a
2460requested attribute is not found in the class, it is searched in the
2461base class. This rule is applied recursively if the base class itself
2462is derived from some other class.
2463
2464There's nothing special about instantiation of derived classes:
2465\verb\DerivedClassName()\ creates a new instance of the class. Method
2466references are resolved as follows: the corresponding class attribute
2467is searched, descending down the chain of base classes if necessary,
2468and the method reference is valid if this yields a function object.
2469
2470Derived classes may override methods of their base classes. Because
2471methods have no special privileges when calling other methods of the
2472same object, a method of a base class that calls another method
2473defined in the same base class, may in fact end up calling a method of
2474a derived class that overrides it. (For C++ programmers: all methods
2475in Python are ``virtual functions''.)
2476
2477An overriding method in a derived class may in fact want to extend
2478rather than simply replace the base class method of the same name.
2479There is a simple way to call the base class method directly: just
2480call \verb\BaseClassName.methodname(self, arguments)\. This is
2481occasionally useful to clients as well. (Note that this only works if
2482the base class is defined or imported directly in the global scope.)
2483
2484
2485\subsection{Multiple inheritance}
2486
2487Poython supports a limited form of multiple inheritance as well. A
2488class definition with multiple base classes looks as follows:
2489
2490\begin{verbatim}
2491 class DerivedClassName(Base1, Base2, Base3):
2492 <statement-1>
2493 .
2494 .
2495 .
2496 <statement-N>
2497\end{verbatim}
2498
2499The only rule necessary to explain the semantics is the resolution
2500rule used for class attribute references. This is depth-first,
2501left-to-right. Thus, if an attribute is not found in
2502\verb\DerivedClassName\, it is searched in \verb\Base1\, then
2503(recursively) in the base classes of \verb\Base1\, and only if it is
2504not found there, it is searched in \verb\Base2\, and so on.
2505
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002506(To some people breadth first---searching \verb\Base2\ and
2507\verb\Base3\ before the base classes of \verb\Base1\---looks more
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002508natural. However, this would require you to know whether a particular
2509attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in
2510one of its base classes before you can figure out the consequences of
2511a name conflict with an attribute of \verb\Base2\. The depth-first
2512rule makes no differences between direct and inherited attributes of
2513\verb\Base1\.)
2514
2515It is clear that indiscriminate use of multiple inheritance is a
2516maintenance nightmare, given the reliance in Python on conventions to
2517avoid accidental name conflicts. A well-known problem with multiple
2518inheritance is a class derived from two classes that happen to have a
2519common base class. While it is easy enough to figure out what happens
2520in this case (the instance will have a single copy of ``instance
2521variables'' or data attributes used by the common base class), it is
2522not clear that these semantics are in any way useful.
2523
2524
2525\section{Odds and ends}
2526
2527Sometimes it is useful to have a data type similar to the Pascal
2528``record'' or C ``struct'', bundling together a couple of named data
2529items. An empty class definition will do nicely, e.g.:
2530
2531\begin{verbatim}
2532 class Employee:
2533 pass
2534
2535 john = Employee() # Create an empty employee record
2536
2537 # Fill the fields of the record
2538 john.name = 'John Doe'
2539 john.dept = 'computer lab'
2540 john.salary = 1000
2541\end{verbatim}
2542
2543
2544A piece of Python code that expects a particular abstract data type
2545can often be passed a class that emulates the methods of that data
2546type instead. For instance, if you have a function that formats some
2547data from a file object, you can define a class with methods
2548\verb\read()\ and \verb\readline()\ that gets the data from a string
2549buffer instead, and pass it as an argument. (Unfortunately, this
2550technique has its limitations: a class can't define operations that
2551are accessed by special syntax such as sequence subscripting or
2552arithmetic operators, and assigning such a ``pseudo-file'' to
2553\verb\sys.stdin\ will not cause the interpreter to read further input
2554from it.)
2555
2556
2557Instance method objects have attributes, too: \verb\m.im_self\ is the
2558object of which the method is an instance, and \verb\m.im_func\ is the
2559function object corresponding to the method.
2560
2561
2562XXX Mention bw compat hacks.
2563
2564
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002565\end{document}