blob: 25358083a673dfd653f05adcb5a3b92d62ebf40f [file] [log] [blame]
Guido van Rossum37953781992-04-06 14:04:04 +00001\documentstyle[twoside,11pt,myformat]{report}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002
3\title{\bf
Guido van Rossum6fc178f1991-08-16 09:13:42 +00004 Python Tutorial
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00005}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00006
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00007\author{
8 Guido van Rossum \\
9 Dept. CST, CWI, Kruislaan 413 \\
10 1098 SJ Amsterdam, The Netherlands \\
11 E-mail: {\tt guido@cwi.nl}
12}
13
14\begin{document}
15
16\pagenumbering{roman}
17
18\maketitle
19
20\begin{abstract}
21
22\noindent
Guido van Rossum4410c751991-06-04 20:22:18 +000023Python is a simple, yet powerful programming language that bridges the
Guido van Rossum6fc178f1991-08-16 09:13:42 +000024gap between C and shell programming, and is thus ideally suited for
25``throw-away programming''
26and rapid prototyping. Its syntax is put
27together from constructs borrowed from a variety of other languages;
28most prominent are influences from ABC, C, Modula-3 and Icon.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000029
Guido van Rossum4410c751991-06-04 20:22:18 +000030The Python interpreter is easily extended with new functions and data
Guido van Rossum6fc178f1991-08-16 09:13:42 +000031types implemented in C. Python is also suitable as an extension
32language for highly customizable C applications such as editors or
33window managers.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000034
Guido van Rossum4410c751991-06-04 20:22:18 +000035Python is available for various operating systems, amongst which
Guido van Rossum6fc178f1991-08-16 09:13:42 +000036several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
37and MS-DOS.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000038
Guido van Rossum6fc178f1991-08-16 09:13:42 +000039This tutorial introduces the reader informally to the basic concepts
40and features of the Python language and system. It helps to have a
41Python interpreter handy for hands-on experience, but as the examples
42are self-contained, the tutorial can be read off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000043
Guido van Rossum481ae681991-11-25 17:28:03 +000044For a description of standard objects and modules, see the {\em Python
45Library Reference} document. The {\em Python Reference Manual} gives
46a more formal definition of the language.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000047
48\end{abstract}
49
50\pagebreak
Guido van Rossumcdc93551992-02-11 15:53:13 +000051{
52\parskip = 0mm
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000053\tableofcontents
Guido van Rossumcdc93551992-02-11 15:53:13 +000054}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000055
56\pagebreak
57
58\pagenumbering{arabic}
59
Guido van Rossum5e0759d1992-08-07 16:06:24 +000060
Guido van Rossum6fc178f1991-08-16 09:13:42 +000061\chapter{Whetting Your Appetite}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000062
Guido van Rossum6fc178f1991-08-16 09:13:42 +000063If you ever wrote a large shell script, you probably know this
64feeling: you'd love to add yet another feature, but it's already so
65slow, and so big, and so complicated; or the feature involves a system
66call or other funcion that is only accessible from C \ldots Usually
67the problem at hand isn't serious enough to warrant rewriting the
68script in C; perhaps because the problem requires variable-length
69strings or other data types (like sorted lists of file names) that are
70easy in the shell but lots of work to implement in C; or perhaps just
71because you're not sufficiently familiar with C.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000072
Guido van Rossum6fc178f1991-08-16 09:13:42 +000073In such cases, Python may be just the language for you. Python is
74simple to use, but it is a real programming language, offering much
75more structure and support for large programs than the shell has. On
76the other hand, it also offers much more error checking than C, and,
77being a {\em very-high-level language}, it has high-level data types
78built in, such as flexible arrays and dictionaries that would cost you
79days to implement efficiently in C. Because of its more general data
80types Python is applicable to a much larger problem domain than {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +000081Awk} or even {\em Perl}, yet many things are at least as easy in
82Python as in those languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000083
Guido van Rossum6fc178f1991-08-16 09:13:42 +000084Python allows you to split up your program in modules that can be
85reused in other Python programs. It comes with a large collection of
Guido van Rossuma8d754e1992-01-07 16:44:35 +000086standard modules that you can use as the basis of your programs --- or
87as examples to start learning to program in Python. There are also
88built-in modules that provide things like file I/O, system calls,
89sockets, and even a generic interface to window systems (STDWIN).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000090
Guido van Rossuma8d754e1992-01-07 16:44:35 +000091Python is an interpreted language, which can save you considerable time
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000092during program development because no compilation and linking is
Guido van Rossum6fc178f1991-08-16 09:13:42 +000093necessary. The interpreter can be used interactively, which makes it
94easy to experiment with features of the language, to write throw-away
95programs, or to test functions during bottom-up program development.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000096It is also a handy desk calculator.
97
Guido van Rossum6fc178f1991-08-16 09:13:42 +000098Python allows writing very compact and readable programs. Programs
99written in Python are typically much shorter than equivalent C
100programs, for several reasons:
101\begin{itemize}
102\item
103the high-level data types allow you to express complex operations in a
104single statement;
105\item
106statement grouping is done by indentation instead of begin/end
107brackets;
108\item
109no variable or argument declarations are necessary.
110\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000111
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000112Python is {\em extensible}: if you know how to program in C it is easy
113to add a new built-in
114function or
115module to the interpreter, either to
116perform critical operations at maximum speed, or to link Python
117programs to libraries that may only be available in binary form (such
118as a vendor-specific graphics library). Once you are really hooked,
119you can link the Python interpreter into an application written in C
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000120and use it as an extension or command language for that application.
121
122By the way, the language is named after the BBC show ``Monty
123Python's Flying Circus'' and has nothing to do with nasty reptiles...
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000124
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000125\section{Where From Here}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000126
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000127Now that you are all excited about Python, you'll want to examine it
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000128in some more detail. Since the best way to learn a language is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000129using it, you are invited here to do so.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000130
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000131In the next chapter, the mechanics of using the interpreter are
132explained. This is rather mundane information, but essential for
133trying out the examples shown later.
134
Guido van Rossum4410c751991-06-04 20:22:18 +0000135The rest of the tutorial introduces various features of the Python
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000136language and system though examples, beginning with simple
137expressions, statements and data types, through functions and modules,
138and finally touching upon advanced concepts like exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000139
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000140When you're through with the turtorial (or just getting bored), you
141should read the Library Reference, which gives complete (though terse)
142reference material about built-in and standard types, functions and
143modules that can save you a lot of time when writing Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000144
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000145
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000146\chapter{Using the Python Interpreter}
147
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000148\section{Invoking the Interpreter}
149
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000150The Python interpreter is usually installed as {\tt /usr/local/bin/python}
151on those machines where it is available; putting {\tt /usr/local/bin} in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000152your {\UNIX} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000153typing the command
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000154
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000155\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000156python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000157\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000158%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000159to the shell. Since the choice of the directory where the interpreter
160lives is an installation option, other places are possible; check with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000161your local Python guru or system administrator. (E.g., {\tt
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000162/usr/local/python} is a popular alternative location.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000163
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000164The interpreter operates somewhat like the {\UNIX} shell: when called
165with standard input connected to a tty device, it reads and executes
166commands interactively; when called with a file name argument or with
167a file as standard input, it reads and executes a {\em script} from
168that file.
169
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000170A third way of starting the interpreter is
171``{\tt python -c command [arg] ...}'', which
172executes the statement(s) in {\tt command}, analogous to the shell's
173{\tt -c} option. Since Python statements often contain spaces or other
174characters that are special to the shell, it is best to quote {\tt
175command} in its entirety with double quotes.
176
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000177Note that there is a difference between ``{\tt python file}'' and
178``{\tt python $<$file}''. In the latter case, input requests from the
Guido van Rossum573805a1992-03-06 10:56:03 +0000179program, such as calls to {\tt input()} and {\tt raw_input()}, are
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000180satisfied from {\em file}. Since this file has already been read
181until the end by the parser before the program starts executing, the
182program will encounter EOF immediately. In the former case (which is
183usually what you want) they are satisfied from whatever file or device
184is connected to standard input of the Python interpreter.
185
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000186\subsection{Argument Passing}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000187
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000188When known to the interpreter, the script name and additional
189arguments thereafter are passed to the script in the variable {\tt
190sys.argv}, which is a list of strings. Its length is at least one;
191when no script and no arguments are given, {\tt sys.argv[0]} is an
192empty string. When the script name is given as {\tt '-'} (meaning
193standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c
194command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options
195found after {\tt -c command} are not consumed by the Python
196interpreter's option processing but left in {\tt sys.argv} for the
197command to handle.
198
199\subsection{Interactive Mode}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000200
Guido van Rossumdd010801991-06-07 14:31:11 +0000201When commands are read from a tty, the interpreter is said to be in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000202{\em interactive\ mode}. In this mode it prompts for the next command
203with the {\em primary\ prompt}, usually three greater-than signs ({\tt
204>>>}); for continuation lines it prompts with the {\em secondary\
205prompt}, by default three dots ({\tt ...}). Typing an EOF (Control-D)
206at the primary prompt causes the interpreter to exit with a zero exit
207status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000208
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000209The interpreter prints a welcome message stating its version number
210and a copyright notice before printing the first prompt, e.g.:
211
212\bcode\begin{verbatim}
213python
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000214Python 0.9.7 (Aug 28 1992).
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000215Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
216>>>
217\end{verbatim}\ecode
218
219\section{The Interpreter and its Environment}
220
221\subsection{Error Handling}
222
223When an error occurs, the interpreter prints an error
224message and a stack trace. In interactive mode, it then returns to
225the primary prompt; when input came from a file, it exits with a
226nonzero exit status after printing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000227the stack trace. (Exceptions handled by an {\tt except} clause in a
228{\tt try} statement are not errors in this context.) Some errors are
229unconditionally fatal and cause an exit with a nonzero exit; this
230applies to internal inconsistencies and some cases of running out of
231memory. All error messages are written to the standard error stream;
232normal output from the executed commands is written to standard
233output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000235Typing the interrupt character (usually Control-C or DEL) to the
236primary or secondary prompt cancels the input and returns to the
237primary prompt.%
238\footnote{
239 A problem with the GNU Readline package may prevent this.
240}
241Typing an interrupt while a command is executing raises the {\tt
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000242KeyboardInterrupt} exception, which may be handled by a {\tt try}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000243statement.
244
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000245\subsection{The Module Search Path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000246
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000247When a module named {\tt foo} is imported, the interpreter searches
248for a file named {\tt foo.py} in the list of directories specified by
249the environment variable {\tt PYTHONPATH}. It has the same syntax as
250the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000251directory names. When {\tt PYTHONPATH} is not set, or when the file
252is not found there, the search continues in an installation-dependent
253default path, usually {\tt .:/usr/local/lib/python}.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000254
255Actually, modules are searched in the list of directories given by the
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000256variable {\tt sys.path} which is initialized from {\tt PYTHONPATH} and
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000257the installation-dependent default. This allows Python programs that
258know what they're doing to modify or replace the module search path.
259See the section on Standard Modules later.
260
261\subsection{``Compiled'' Python files}
262
263As an important speed-up of the start-up time for short programs that
264use a lot of standard modules, if a file called {\tt foo.pyc} exists
265in the directory where {\tt foo.py} is found, this is assumed to
266contain an already-``compiled'' version of the module {\tt foo}. The
267modification time of the version of {\tt foo.py} used to create {\tt
268foo.pyc} is recorded in {\tt foo.pyc}, and the file is ignored if
269these don't match.
270
271Whenever {\tt foo.py} is successfully compiled, an attempt is made to
272write the compiled version to {\tt foo.pyc}. It is not an error if
273this attempt fails; if for any reason the file is not written
274completely, the resulting {\tt foo.pyc} file will be recognized as
275invalid and thus ignored later.
276
277\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000278
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000279On BSD'ish {\UNIX} systems, Python scripts can be made directly
280executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000281
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000282\bcode\begin{verbatim}
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000283#! /usr/local/bin/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000284\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000285%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000286(assuming that's the name of the interpreter) at the beginning of the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000287script and giving the file an executable mode. The {\tt \#!} must be
288the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000289
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000290\subsection{The Interactive Startup File}
291
292When you use Python interactively, it is frequently handy to have some
293standard commands executed every time the interpreter is started. You
294can do this by setting an environment variable named {\tt
295PYTHONSTARTUP} to the name of a file containing your start-up
296commands. This is similar to the {\tt /profile} feature of the UNIX
297shells.
298
299This file is only read in interactive sessions, not when Python reads
300commands from a script, and not when {\tt /dev/tty} is given as the
301explicit source of commands (which otherwise behaves like an
302interactive session). It is executed in the same name space where
303interactive commands are executed, so that objects that it defines or
304imports can be used without qualification in the interactive session.
Guido van Rossum7b3c8a11992-09-08 09:20:13 +0000305You can also change the prompts {\tt sys.ps1} and {\tt sys.ps2} in
306this file.
Guido van Rossum9a4e3fc1992-09-03 21:27:55 +0000307
308If you want to read an additional start-up file from the current
309directory, you can program this in the global start-up file, e.g.
310\verb\execfile('.pythonrc')\. If you want to use the startup file
311in a script, you must write this explicitly in the script, e.g.
312\verb\import os;\ \verb\execfile(os.environ['PYTHONSTARTUP'])\.
313
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000314\section{Interactive Input Editing and History Substitution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000315
Guido van Rossum4410c751991-06-04 20:22:18 +0000316Some versions of the Python interpreter support editing of the current
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000317input line and history substitution, similar to facilities found in
318the Korn shell and the GNU Bash shell. This is implemented using the
319{\em GNU\ Readline} library, which supports Emacs-style and vi-style
320editing. This library has its own documentation which I won't
321duplicate here; however, the basics are easily explained.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000322
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000323Perhaps the quickest check to see whether command line editing is
324supported is typing Control-P to the first Python prompt you get. If
325it beeps, you have command line editing. If nothing appears to
326happen, or if \verb/^P/ is echoed, you can skip the rest of this
327section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000328
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000329\subsection{Line Editing}
330
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000331If supported, input line editing is active whenever the interpreter
332prints a primary or secondary prompt. The current line can be edited
333using the conventional Emacs control characters. The most important
334of these are: C-A (Control-A) moves the cursor to the beginning of the
335line, C-E to the end, C-B moves it one position to the left, C-F to
336the right. Backspace erases the character to the left of the cursor,
337C-D the character to its right. C-K kills (erases) the rest of the
338line to the right of the cursor, C-Y yanks back the last killed
339string. C-underscore undoes the last change you made; it can be
340repeated for cumulative effect.
341
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000342\subsection{History Substitution}
343
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000344History substitution works as follows. All non-empty input lines
345issued are saved in a history buffer, and when a new prompt is given
346you are positioned on a new line at the bottom of this buffer. C-P
347moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000348Any line in the history buffer can be edited; an asterisk appears in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000349front of the prompt to mark a line as modified. Pressing the Return
350key passes the current line to the interpreter. C-R starts an
351incremental reverse search; C-S starts a forward search.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000352
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000353\subsection{Key Bindings}
354
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000355The key bindings and some other parameters of the Readline library can
356be customized by placing commands in an initialization file called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000357{\tt \$HOME/.inputrc}. Key bindings have the form
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000358
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000359\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000360key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000361\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000362%
363or
364
365\bcode\begin{verbatim}
366"string": function-name
367\end{verbatim}\ecode
368%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000369and options can be set with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000370
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000371\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000372set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000373\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000374%
375For example:
376
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000377\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000378# I prefer vi-style editing:
379set editing-mode vi
380# Edit using a single line:
381set horizontal-scroll-mode On
382# Rebind some keys:
383Meta-h: backward-kill-word
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000384"\C-u": universal-argument
385"\C-x\C-r": re-read-init-file
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000386\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000387%
Guido van Rossum4410c751991-06-04 20:22:18 +0000388Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000389instead of Readline's default filename completion function. If you
390insist, you can override this by putting
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000391
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000392\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000393TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000394\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000395%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000396in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000397indented continuation lines...)
398
399\subsection{Commentary}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000400
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000401This facility is an enormous step forward compared to previous
402versions of the interpreter; however, some wishes are left: It would
403be nice if the proper indentation were suggested on continuation lines
404(the parser knows if an indent token is required next). The
405completion mechanism might use the interpreter's symbol table. A
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000406command to check (or even suggest) matching parentheses, quotes etc.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000407would also be useful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000408
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000409
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000410\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000411
412In the following examples, input and output are distinguished by the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000413presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
414the example, you must type everything after the prompt, when the
415prompt appears; lines that do not begin with a prompt are output from
416the interpreter.%
417\footnote{
418 I'd prefer to use different fonts to distinguish input
419 from output, but the amount of LaTeX hacking that would require
420 is currently beyond my ability.
421}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000422Note that a secondary prompt on a line by itself in an example means
423you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000424
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000425\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000426
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000427Let's try some simple Python commands. Start the interpreter and wait
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000428for the primary prompt, {\tt >>>}. (It shouldn't take long.)
429
430\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000431
432The interpreter acts as a simple calculator: you can type an
433expression at it and it will write the value. Expression syntax is
434straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000435work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000436can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000437
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000438\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000439>>> # This is a comment
440>>> 2+2
4414
442>>>
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000443>>> (50-5*6)/4
4445
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000445>>> # Division truncates towards zero:
446>>> 7/3
4472
448>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000449\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000450%
451Like in C, the equal sign ({\tt =}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000452variable. The value of an assignment is not written:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000453
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000454\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000455>>> width = 20
456>>> height = 5*9
457>>> width * height
458900
459>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000460\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000461%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000462A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000463
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000464\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000465>>> # Zero x, y and z
466>>> x = y = z = 0
467>>>
468\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000469%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000470There is full support for floating point; operators with mixed type
471operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000472
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000473\bcode\begin{verbatim}
474>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004753.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000476>>> 7.0 / 2
4773.5
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000478>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000479\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000480
481\subsection{Strings}
482
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000483Besides numbers, Python can also manipulate strings, enclosed in
484single quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000485
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000486\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000487>>> 'foo bar'
488'foo bar'
489>>> 'doesn\'t'
490'doesn\'t'
491>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000492\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000493%
494Strings are written the same way as they are typed for input: inside
495quotes and with quotes and other funny characters escaped by
496backslashes, to show the precise value. (The {\tt print} statement,
497described later, can be used to write strings without quotes or
498escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000499
500Strings can be concatenated (glued together) with the {\tt +}
501operator, and repeated with {\tt *}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000502
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000503\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000504>>> word = 'Help' + 'A'
505>>> word
506'HelpA'
507>>> '<' + word*5 + '>'
508'<HelpAHelpAHelpAHelpAHelpA>'
509>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000510\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000511%
512Strings can be subscripted (indexed); like in C, the first character of
513a string has subscript (index) 0.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000514
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000515There is no separate character type; a character is simply a string of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000516size one. Like in Icon, substrings can be specified with the {\em
517slice} notation: two indices separated by a colon.
518
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000519\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000520>>> word[4]
521'A'
522>>> word[0:2]
523'He'
524>>> word[2:4]
525'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000526>>>
527\end{verbatim}\ecode
528%
529Slice indices have useful defaults; an omitted first index defaults to
530zero, an omitted second index defaults to the size of the string being
531sliced.
532
533\bcode\begin{verbatim}
534>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000535'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000536>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000537'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000538>>>
539\end{verbatim}\ecode
540%
541Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
542equals \verb\s\.
543
544\bcode\begin{verbatim}
545>>> word[:2] + word[2:]
546'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000547>>> word[:3] + word[3:]
548'HelpA'
549>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000550\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000551%
552Degenerate slice indices are handled gracefully: an index that is too
553large is replaced by the string size, an upper bound smaller than the
554lower bound returns an empty string.
555
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000556\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000557>>> word[1:100]
558'elpA'
559>>> word[10:]
560''
561>>> word[2:1]
562''
563>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000564\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000565%
566Indices may be negative numbers, to start counting from the right.
567For example:
568
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000569\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000570>>> word[-1] # The last character
571'A'
572>>> word[-2] # The last-but-one character
573'p'
574>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000575'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000576>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000577'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000578>>>
579\end{verbatim}\ecode
580%
581But note that -0 is really the same as 0, so it does not count from
582the right!
583
584\bcode\begin{verbatim}
585>>> word[-0] # (since -0 equals 0)
586'H'
587>>>
588\end{verbatim}\ecode
589%
590Out-of-range negative slice indices are truncated, but don't try this
591for single-element (non-slice) indices:
592
593\bcode\begin{verbatim}
594>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000595'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000596>>> word[-10] # error
597Unhandled exception: IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000598>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000599\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000600%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000601The best way to remember how slices work is to think of the indices as
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000602pointing {\em between} characters, with the left edge of the first
603character numbered 0. Then the right edge of the last character of a
604string of {\tt n} characters has index {\tt n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000605
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000606\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000607 +---+---+---+---+---+
608 | H | e | l | p | A |
609 +---+---+---+---+---+
610 0 1 2 3 4 5
611-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000612\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000613%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000614The first row of numbers gives the position of the indices 0...5 in
615the string; the second row gives the corresponding negative indices.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000616The slice from \verb\i\ to \verb\j\ consists of all characters between
617the edges labeled \verb\i\ and \verb\j\, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000618
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000619For nonnegative indices, the length of a slice is the difference of
620the indices, if both are within bounds, e.g., the length of
621\verb\word[1:3]\ is 2.
622
623The built-in function {\tt len()} returns the length of a string:
624
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000625\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000626>>> s = 'supercalifragilisticexpialidocious'
627>>> len(s)
62834
629>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000630\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000631
632\subsection{Lists}
633
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000634Python knows a number of {\em compound} data types, used to group
635together other values. The most versatile is the {\em list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000636can be written as a list of comma-separated values (items) between
637square brackets. List items need not all have the same type.
638
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000639\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000640>>> a = ['foo', 'bar', 100, 1234]
641>>> a
642['foo', 'bar', 100, 1234]
643>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000644\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000645%
646Like string indices, list indices start at 0, and lists can be sliced,
647concatenated and so on:
648
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000649\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000650>>> a[0]
651'foo'
652>>> a[3]
6531234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000654>>> a[-2]
655100
656>>> a[1:-1]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000657['bar', 100]
658>>> a[:2] + ['bletch', 2*2]
659['foo', 'bar', 'bletch', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000660>>> 3*a[:3] + ['Boe!']
661['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000662>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000663\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000664%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000665Unlike strings, which are {\em immutable}, it is possible to change
666individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000667
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000668\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000669>>> a
670['foo', 'bar', 100, 1234]
671>>> a[2] = a[2] + 23
672>>> a
673['foo', 'bar', 123, 1234]
674>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000675\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000676%
677Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000678of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000679
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000680\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000681>>> # Replace some items:
682>>> a[0:2] = [1, 12]
683>>> a
684[1, 12, 123, 1234]
685>>> # Remove some:
686>>> a[0:2] = []
687>>> a
688[123, 1234]
689>>> # Insert some:
690>>> a[1:1] = ['bletch', 'xyzzy']
691>>> a
692[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000693>>> a[:0] = a # Insert (a copy of) itself at the beginning
694>>> a
695[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000696>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000697\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000698%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000699The built-in function {\tt len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000700
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000701\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000702>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00007038
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 Rossum6fc178f1991-08-16 09:13:42 +0000707It is possible to nest lists (create lists containing other lists),
708for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000709
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000710\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000711>>> q = [2, 3]
712>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000713>>> len(p)
7143
715>>> p[1]
716[2, 3]
717>>> p[1][0]
7182
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000719>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000720>>> p
721[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000722>>> q
723[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000724>>>
725\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000726%
727Note that in the last example, {\tt p[1]} and {\tt q} really refer to
728the same object! We'll come back to {\em object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000729
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000730\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000731
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000732Of course, we can use Python for more complicated tasks than adding
733two and two together. For instance, we can write an initial
734subsequence of the {\em Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000735
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000736\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000737>>> # Fibonacci series:
738>>> # the sum of two elements defines the next
739>>> a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000740>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000741... print b
742... a, b = b, a+b
743...
7441
7451
7462
7473
7485
7498
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000750>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000751\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000752%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000753This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000754
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000755\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000756
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000757\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000758The first line contains a {\em multiple assignment}: the variables
759{\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
760last line this is used again, demonstrating that the expressions on
761the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000762assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000763
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000764\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000765The {\tt while} loop executes as long as the condition (here: {\tt b <
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000766100}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000767true; zero is false. The condition may also be a string or list value,
768in fact any sequence; anything with a non-zero length is true, empty
769sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000770comparison. The standard comparison operators are written the same as
771in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000772
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000773\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000774The {\em body} of the loop is {\em indented}: indentation is Python's
775way of grouping statements. Python does not (yet!) provide an
776intelligent input line editing facility, so you have to type a tab or
777space(s) for each indented line. In practice you will prepare more
778complicated input for Python with a text editor; most text editors have
779an auto-indent facility. When a compound statement is entered
780interactively, it must be followed by a blank line to indicate
781completion (since the parser cannot guess when you have typed the last
782line).
783
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000784\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000785The {\tt print} statement writes the value of the expression(s) it is
786given. It differs from just writing the expression you want to write
787(as we did earlier in the calculator examples) in the way it handles
788multiple expressions and strings. Strings are written without quotes,
789and a space is inserted between items, so you can format things nicely,
790like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000791
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000792\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000793>>> i = 256*256
794>>> print 'The value of i is', i
795The value of i is 65536
796>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000797\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000798%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000799A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000800
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000801\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000802>>> a, b = 0, 1
803>>> while b < 1000:
804... print b,
805... a, b = b, a+b
806...
8071 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
808>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000809\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000810%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000811Note that the interpreter inserts a newline before it prints the next
812prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000813
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000814\end{itemize}
815
Guido van Rossum5e0759d1992-08-07 16:06:24 +0000816
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000817\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000818
Guido van Rossum4410c751991-06-04 20:22:18 +0000819Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000820usual control flow statements known from other languages, with some
821twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000822
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000823\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000824
825Perhaps the most well-known statement type is the {\tt if} statement.
826For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000827
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000828\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000829>>> if x < 0:
830... x = 0
831... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000832... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000833... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000834... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000835... print 'Single'
836... else:
837... print 'More'
838...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000839\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000840%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000841There can be zero or more {\tt elif} parts, and the {\tt else} part is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000842optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
843useful to avoid excessive indentation. An {\tt if...elif...elif...}
844sequence is a substitute for the {\em switch} or {\em case} statements
845found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000846
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000847\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000848
Guido van Rossum4410c751991-06-04 20:22:18 +0000849The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000850used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000851arithmetic progression of numbers (like in Pascal), or leaving the user
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000852completely free in the iteration test and step (as C), Python's {\tt
853for} statement iterates over the items of any sequence (e.g., a list
854or a string), in the order that they appear in the sequence. For
855example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000856
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000857\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000858>>> # Measure some strings:
859>>> a = ['cat', 'window', 'defenestrate']
860>>> for x in a:
861... print x, len(x)
862...
863cat 3
864window 6
865defenestrate 12
866>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000867\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000868%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000869It is not safe to modify the sequence being iterated over in the loop
870(this can only happen for mutable sequence types, i.e., lists). If
871you need to modify the list you are iterating over, e.g., duplicate
872selected items, you must iterate over a copy. The slice notation
873makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000874
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000875\bcode\begin{verbatim}
876>>> for x in a[:]: # make a slice copy of the entire list
877... if len(x) > 6: a.insert(0, x)
878...
879>>> a
880['defenestrate', 'cat', 'window', 'defenestrate']
881>>>
882\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000883
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000884\section{The {\tt range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000885
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000886If you do need to iterate over a sequence of numbers, the built-in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000887function {\tt range()} comes in handy. It generates lists containing
888arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000889
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000890\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000891>>> range(10)
892[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
893>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000894\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000895%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000896The given end point is never part of the generated list; {\tt range(10)}
897generates a list of 10 values, exactly the legal indices for items of a
898sequence of length 10. It is possible to let the range start at another
899number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000900
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000901\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000902>>> range(5, 10)
903[5, 6, 7, 8, 9]
904>>> range(0, 10, 3)
905[0, 3, 6, 9]
906>>> range(-10, -100, -30)
907[-10, -40, -70]
908>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000909\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000910%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000911To iterate over the indices of a sequence, combine {\tt range()} and
912{\tt len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000913
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000914\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000915>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000916>>> for i in range(len(a)):
917... print i, a[i]
918...
9190 Mary
9201 had
9212 a
9223 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00009234 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000924>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000925\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000926
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000927\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000928
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000929The {\tt break} statement, like in C, breaks out of the smallest
930enclosing {\tt for} or {\tt while} loop.
931
932The {\tt continue} statement, also borrowed from C, continues with the
933next iteration of the loop.
934
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000935Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000936loop terminates through exhaustion of the list (with {\tt for}) or when
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000937the condition becomes false (with {\tt while}), but not when the loop is
938terminated by a {\tt break} statement. This is exemplified by the
939following loop, which searches for a list item of value 0:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000940
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000941\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000942>>> for n in range(2, 10):
943... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000944... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000945... print n, 'equals', x, '*', n/x
946... break
947... else:
948... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000949...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009502 is a prime number
9513 is a prime number
9524 equals 2 * 2
9535 is a prime number
9546 equals 2 * 3
9557 is a prime number
9568 equals 2 * 4
9579 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000958>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000959\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000960
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000961\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000962
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000963The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000964It can be used when a statement is required syntactically but the
965program requires no action.
966For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000967
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000968\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000969>>> while 1:
970... pass # Busy-wait for keyboard interrupt
971...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000972\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000973
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000974\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000975
976We can create a function that writes the Fibonacci series to an
977arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000978
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000979\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000980>>> def fib(n): # write Fibonacci series up to n
981... a, b = 0, 1
982... while b <= n:
983... print b,
984... a, b = b, a+b
985...
986>>> # Now call the function we just defined:
987>>> fib(2000)
9881 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
989>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000990\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000991%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000992The keyword {\tt def} introduces a function {\em definition}. It must
993be followed by the function name and the parenthesized list of formal
994parameters. The statements that form the body of the function starts at
995the next line, indented by a tab stop.
996
997The {\em execution} of a function introduces a new symbol table used
998for the local variables of the function. More precisely, all variable
999assignments in a function store the value in the local symbol table;
1000whereas
1001 variable references first look in the local symbol table, then
1002in the global symbol table, and then in the table of built-in names.
1003Thus,
1004global variables cannot be directly assigned to from within a
1005function, although they may be referenced.
1006
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001007The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001008the local symbol table of the called function when it is called; thus,
1009arguments are passed using {\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001010\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001011 Actually, {\em call by object reference} would be a better
1012 description, since if a mutable object is passed, the caller
1013 will see any changes the callee makes to it (e.g., items
1014 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001015}
1016When a function calls another function, a new local symbol table is
1017created for that call.
1018
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001019A function definition introduces the function name in the
1020current
1021symbol table. The value
1022of the function name
1023has a type that is recognized by the interpreter as a user-defined
1024function. This value can be assigned to another name which can then
1025also be used as a function. This serves as a general renaming
1026mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001027
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001028\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001029>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001030<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001031>>> f = fib
1032>>> f(100)
10331 1 2 3 5 8 13 21 34 55 89
1034>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001035\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001036%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001037You might object that {\tt fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001038Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001039value. In fact, technically speaking, procedures do return a value,
1040albeit a rather boring one. This value is called {\tt None} (it's a
1041built-in name). Writing the value {\tt None} is normally suppressed by
1042the interpreter if it would be the only value written. You can see it
1043if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001044
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001045\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001046>>> print fib(0)
1047None
1048>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001049\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001050%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001051It is simple to write a function that returns a list of the numbers of
1052the Fibonacci series, instead of printing it:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001053
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001054\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001055>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001056... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001057... a, b = 0, 1
1058... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001059... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001060... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001061... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001062...
1063>>> f100 = fib2(100) # call it
1064>>> f100 # write the result
1065[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1066>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001067\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001068%
Guido van Rossum4410c751991-06-04 20:22:18 +00001069This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001070
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001071\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001072
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001073\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001074The {\tt return} statement returns with a value from a function. {\tt
1075return} without an expression argument is used to return from the middle
1076of a procedure (falling off the end also returns from a proceduce), in
1077which case the {\tt None} value is returned.
1078
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001079\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001080The statement {\tt result.append(b)} calls a {\em method} of the list
1081object {\tt result}. A method is a function that `belongs' to an
1082object and is named {\tt obj.methodname}, where {\tt obj} is some
1083object (this may be an expression), and {\tt methodname} is the name
1084of a method that is defined by the object's type. Different types
1085define different methods. Methods of different types may have the
1086same name without causing ambiguity. (It is possible to define your
1087own object types and methods, using {\em classes}. This is an
1088advanced feature that is not discussed in this tutorial.)
1089The method {\tt append} shown in the example, is defined for
1090list objects; it adds a new element at the end of the list. In this
1091example
1092it is equivalent to {\tt result = result + [b]}, but more efficient.
1093
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001094\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001095
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001096
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001097\chapter{Odds and Ends}
1098
1099This chapter describes some things you've learned about already in
1100more detail, and adds some new things as well.
1101
1102\section{More on Lists}
1103
1104The list data type has some more methods. Here are all of the methods
1105of lists objects:
1106
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001107\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001108
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001109\item[{\tt insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001110Insert an item at a given position. The first argument is the index of
1111the element before which to insert, so {\tt a.insert(0, x)} inserts at
1112the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
1113{\tt a.append(x)}.
1114
1115\item[{\tt append(x)}]
1116Equivalent to {\tt a.insert(len(a), x)}.
1117
1118\item[{\tt index(x)}]
1119Return the index in the list of the first item whose value is {\tt x}.
1120It is an error if there is no such item.
1121
1122\item[{\tt remove(x)}]
1123Remove the first item from the list whose value is {\tt x}.
1124It is an error if there is no such item.
1125
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001126\item[{\tt sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001127Sort the items of the list, in place.
1128
1129\item[{\tt reverse()}]
1130Reverse the elements of the list, in place.
1131
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001132\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001133
1134An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001135
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001136\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001137>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001138>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001139>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001140>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001141[66.6, 333, -1, 333, 1, 1234.5, 333]
1142>>> a.index(333)
11431
1144>>> a.remove(333)
1145>>> a
1146[66.6, -1, 333, 1, 1234.5, 333]
1147>>> a.reverse()
1148>>> a
1149[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001150>>> a.sort()
1151>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001152[-1, 1, 66.6, 333, 333, 1234.5]
1153>>>
1154\end{verbatim}\ecode
1155
1156\section{The {\tt del} statement}
1157
1158There is a way to remove an item from a list given its index instead
1159of its value: the {\tt del} statement. This can also be used to
1160remove slices from a list (which we did earlier by assignment of an
1161empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001162
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001163\bcode\begin{verbatim}
1164>>> a
1165[-1, 1, 66.6, 333, 333, 1234.5]
1166>>> del a[0]
1167>>> a
1168[1, 66.6, 333, 333, 1234.5]
1169>>> del a[2:4]
1170>>> a
1171[1, 66.6, 1234.5]
1172>>>
1173\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001174%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001175{\tt del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001176
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001177\bcode\begin{verbatim}
1178>>> del a
1179>>>
1180\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001181%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001182Referencing the name {\tt a} hereafter is an error (at least until
1183another value is assigned to it). We'll find other uses for {\tt del}
1184later.
1185
1186\section{Tuples and Sequences}
1187
1188We saw that lists and strings have many common properties, e.g.,
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001189indexinging and slicing operations. They are two examples of {\em
1190sequence} data types. Since Python is an evolving language, other
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001191sequence data types may be added. There is also another standard
1192sequence data type: the {\em tuple}.
1193
1194A tuple consists of a number of values separated by commas, for
1195instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001196
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001197\bcode\begin{verbatim}
1198>>> t = 12345, 54321, 'hello!'
1199>>> t[0]
120012345
1201>>> t
1202(12345, 54321, 'hello!')
1203>>> # Tuples may be nested:
1204>>> u = t, (1, 2, 3, 4, 5)
1205>>> u
1206((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1207>>>
1208\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001209%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001210As you see, on output tuples are alway enclosed in parentheses, so
1211that nested tuples are interpreted correctly; they may be input with
1212or without surrounding parentheses, although often parentheses are
1213necessary anyway (if the tuple is part of a larger expression).
1214
1215Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1216from a database, etc. Tuples, like strings, are immutable: it is not
1217possible to assign to the individual items of a tuple (you can
1218simulate much of the same effect with slicing and concatenation,
1219though).
1220
1221A special problem is the construction of tuples containing 0 or 1
1222items: the syntax has some extra quirks to accomodate these. Empty
1223tuples are constructed by an empty pair of parentheses; a tuple with
1224one item is constructed by following a value with a comma
1225(it is not sufficient to enclose a single value in parentheses).
1226Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001227
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001228\bcode\begin{verbatim}
1229>>> empty = ()
1230>>> singleton = 'hello', # <-- note trailing comma
1231>>> len(empty)
12320
1233>>> len(singleton)
12341
1235>>> singleton
1236('hello',)
1237>>>
1238\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001239%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001240The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
1241tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
1242are packed together in a tuple. The reverse operation is also
1243possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001244
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001245\bcode\begin{verbatim}
1246>>> x, y, z = t
1247>>>
1248\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001249%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001250This is called, appropriately enough, {\em tuple unpacking}. Tuple
1251unpacking requires that the list of variables on the left has the same
1252number of elements as the length of the tuple. Note that multiple
1253assignment is really just a combination of tuple packing and tuple
1254unpacking!
1255
1256Occasionally, the corresponding operation on lists is useful: {\em list
1257unpacking}. This is supported by enclosing the list of variables in
1258square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001259
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001260\bcode\begin{verbatim}
1261>>> a = ['foo', 'bar', 100, 1234]
1262>>> [a1, a2, a3, a4] = a
1263>>>
1264\end{verbatim}\ecode
1265
1266\section{Dictionaries}
1267
1268Another useful data type built into Python is the {\em dictionary}.
1269Dictionaries are sometimes found in other languages as ``associative
1270memories'' or ``associative arrays''. Unlike sequences, which are
1271indexed by a range of numbers, dictionaries are indexed by {\em keys},
1272which are strings. It is best to think of a dictionary as an unordered set of
1273{\em key:value} pairs, with the requirement that the keys are unique
1274(within one dictionary).
1275A pair of braces creates an empty dictionary: \verb/{}/.
1276Placing a comma-separated list of key:value pairs within the
1277braces adds initial key:value pairs to the dictionary; this is also the
1278way dictionaries are written on output.
1279
1280The main operations on a dictionary are storing a value with some key
1281and extracting the value given the key. It is also possible to delete
1282a key:value pair
1283with {\tt del}.
1284If you store using a key that is already in use, the old value
1285associated with that key is forgotten. It is an error to extract a
1286value using a non-existant key.
1287
1288The {\tt keys()} method of a dictionary object returns a list of all the
1289keys used in the dictionary, in random order (if you want it sorted,
1290just apply the {\tt sort()} method to the list of keys). To check
1291whether a single key is in the dictionary, use the \verb/has_key()/
1292method of the dictionary.
1293
1294Here is a small example using a dictionary:
1295
1296\bcode\begin{verbatim}
1297>>> tel = {'jack': 4098, 'sape': 4139}
1298>>> tel['guido'] = 4127
1299>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001300{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001301>>> tel['jack']
13024098
1303>>> del tel['sape']
1304>>> tel['irv'] = 4127
1305>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001306{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001307>>> tel.keys()
1308['guido', 'irv', 'jack']
1309>>> tel.has_key('guido')
13101
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001311>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001312\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001313
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001314\section{More on Conditions}
1315
1316The conditions used in {\tt while} and {\tt if} statements above can
1317contain other operators besides comparisons.
1318
1319The comparison operators {\tt in} and {\tt not in} check whether a value
1320occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
1321is not} compare whether two objects are really the same object; this
1322only matters for mutable objects like lists. All comparison operators
1323have the same priority, which is lower than that of all numerical
1324operators.
1325
1326Comparisons can be chained: e.g., {\tt a < b = c} tests whether {\tt a}
1327is less than {\tt b} and moreover {\tt b} equals {\tt c}.
1328
1329Comparisons may be combined by the Boolean operators {\tt and} and {\tt
1330or}, and the outcome of a comparison (or of any other Boolean
1331expression) may be negated with {\tt not}. These all have lower
1332priorities than comparison operators again; between them, {\tt not} has
1333the highest priority, and {\tt or} the lowest, so that
1334{\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
1335course, parentheses can be used to express the desired composition.
1336
1337The Boolean operators {\tt and} and {\tt or} are so-called {\em
1338shortcut} operators: their arguments are evaluated from left to right,
1339and evaluation stops as soon as the outcome is determined. E.g., if
1340{\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
1341does not evaluate the expression C. In general, the return value of a
1342shortcut operator, when used as a general value and not as a Boolean, is
1343the last evaluated argument.
1344
1345It is possible to assign the result of a comparison or other Boolean
1346expression to a variable, but you must enclose the entire Boolean
1347expression in parentheses. This is necessary because otherwise an
1348assignment like \verb/a = b = c/ would be ambiguous: does it assign the
1349value of {\tt c} to {\tt a} and {\tt b}, or does it compare {\tt b} to
1350{\tt c} and assign the outcome (0 or 1) to {\tt a}? As it is, the first
1351meaning is what you get, and to get the latter you have to write
1352\verb/a = (b = c)/. (In Python, unlike C, assignment cannot occur
1353inside expressions.)
1354
1355\section{Comparing Sequences and Other Types}
1356
1357Sequence objects may be compared to other objects with the same
1358sequence type. The comparison uses {\em lexicographical} ordering:
1359first the first two items are compared, and if they differ this
1360determines the outcome of the comparison; if they are equal, the next
1361two items are compared, and so on, until either sequence is exhausted.
1362If two items to be compared are themselves sequences of the same type,
1363the lexiographical comparison is carried out recursively. If all
1364items of two sequences compare equal, the sequences are considered
1365equal. If one sequence is an initial subsequence of the other, the
1366shorted sequence is the smaller one. Lexicographical ordering for
1367strings uses the ASCII ordering for individual characters. Some
1368examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001369
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001370\bcode\begin{verbatim}
1371(1, 2, 3) < (1, 2, 4)
1372[1, 2, 3] < [1, 2, 4]
1373'ABC' < 'C' < 'Pascal' < 'Python'
1374(1, 2, 3, 4) < (1, 2, 4)
1375(1, 2) < (1, 2, -1)
1376(1, 2, 3) = (1.0, 2.0, 3.0)
1377(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1378\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001379%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001380Note that comparing objects of different types is legal. The outcome
1381is deterministic but arbitrary: the types are ordered by their name.
1382Thus, a list is always smaller than a string, a string is always
1383smaller than a tuple, etc. Mixed numeric types are compared according
1384to their numeric value, so 0 equals 0.0, etc.%
1385\footnote{
1386 The rules for comparing objects of different types should
1387 not be relied upon; they may change in a future version of
1388 the language.
1389}
1390
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001391
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001392\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001393
Guido van Rossum4410c751991-06-04 20:22:18 +00001394If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001395definitions you have made (functions and variables) are lost.
1396Therefore, if you want to write a somewhat longer program, you are
1397better off using a text editor to prepare the input for the interpreter
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001398and run it with that file as input instead. This is known as creating a
1399{\em script}. As your program gets longer, you may want to split it
1400into several files for easier maintenance. You may also want to use a
1401handy function that you've written in several programs without copying
1402its definition into each program.
1403
Guido van Rossum4410c751991-06-04 20:22:18 +00001404To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001405them in a script or in an interactive instance of the interpreter.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001406Such a file is called a {\em module}; definitions from a module can be
1407{\em imported} into other modules or into the {\em main} module (the
1408collection of variables that you have access to in a script
1409executed at the top level
1410and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001411
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001412A module is a file containing Python definitions and statements. The
1413file name is the module name with the suffix {\tt .py} appended. For
1414instance, use your favorite text editor to create a file called {\tt
1415fibo.py} in the current directory with the following contents:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001416
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001417\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001418# Fibonacci numbers module
1419
1420def fib(n): # write Fibonacci series up to n
1421 a, b = 0, 1
1422 while b <= n:
1423 print b,
1424 a, b = b, a+b
1425
1426def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001427 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001428 a, b = 0, 1
1429 while b <= n:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001430 result.append(b)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001431 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001432 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001433\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001434%
Guido van Rossum4410c751991-06-04 20:22:18 +00001435Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001436following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001437
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001438\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001439>>> import fibo
1440>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001441\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001442%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001443This does not enter the names of the functions defined in
1444{\tt fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001445directly in the current symbol table; it only enters the module name
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001446{\tt fibo}
1447there.
1448Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001449
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001450\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001451>>> fibo.fib(1000)
14521 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1453>>> fibo.fib2(100)
1454[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1455>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001456\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001457%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001458If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001459
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001460\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001461>>> fib = fibo.fib
1462>>> fib(500)
14631 1 2 3 5 8 13 21 34 55 89 144 233 377
1464>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001465\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001466
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001467\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001468
1469A module can contain executable statements as well as function
1470definitions.
1471These statements are intended to initialize the module.
1472They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001473{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001474time the module is imported somewhere.%
1475\footnote{
1476 In fact function definitions are also `statements' that are
1477 `executed'; the execution enters the function name in the
1478 module's global symbol table.
1479}
1480
1481Each module has its own private symbol table, which is used as the
1482global symbol table by all functions defined in the module.
1483Thus, the author of a module can use global variables in the module
1484without worrying about accidental clashes with a user's global
1485variables.
1486On the other hand, if you know what you are doing you can touch a
1487module's global variables with the same notation used to refer to its
1488functions,
1489{\tt modname.itemname}.
1490
1491Modules can import other modules.
1492It is customary but not required to place all
1493{\tt import}
1494statements at the beginning of a module (or script, for that matter).
1495The imported module names are placed in the importing module's global
1496symbol table.
1497
1498There is a variant of the
1499{\tt import}
1500statement that imports names from a module directly into the importing
1501module's symbol table.
1502For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001503
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001504\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001505>>> from fibo import fib, fib2
1506>>> fib(500)
15071 1 2 3 5 8 13 21 34 55 89 144 233 377
1508>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001509\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001510%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001511This does not introduce the module name from which the imports are taken
1512in the local symbol table (so in the example, {\tt fibo} is not
1513defined).
1514
1515There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001516
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001517\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001518>>> from fibo import *
1519>>> fib(500)
15201 1 2 3 5 8 13 21 34 55 89 144 233 377
1521>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001522\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001523%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001524This imports all names except those beginning with an underscore
Guido van Rossum573805a1992-03-06 10:56:03 +00001525({\tt _}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001526
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001527\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001528
Guido van Rossum4410c751991-06-04 20:22:18 +00001529Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001530document (Python Library Reference). Some modules are built into the
1531interpreter; these provide access to operations that are not part of the
1532core of the language but are nevertheless built in, either for
1533efficiency or to provide access to operating system primitives such as
1534system calls. The set of such modules is a configuration option; e.g.,
1535the {\tt amoeba} module is only provided on systems that somehow support
1536Amoeba primitives. One particular module deserves some attention: {\tt
1537sys}, which is built into every Python interpreter. The variables {\tt
1538sys.ps1} and {\tt sys.ps2} define the strings used as primary and
1539secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001540
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001541\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001542>>> import sys
1543>>> sys.ps1
1544'>>> '
1545>>> sys.ps2
1546'... '
1547>>> sys.ps1 = 'C> '
1548C> print 'Yuck!'
1549Yuck!
1550C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001551\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001552%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001553These two variables are only defined if the interpreter is in
1554interactive mode.
1555
1556The variable
1557{\tt sys.path}
1558is a list of strings that determine the interpreter's search path for
1559modules.
1560It is initialized to a default path taken from the environment variable
1561{\tt PYTHONPATH},
1562or from a built-in default if
1563{\tt PYTHONPATH}
1564is not set.
1565You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001566
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001567\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001568>>> import sys
1569>>> sys.path.append('/ufs/guido/lib/python')
1570>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001571\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001572
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001573\section{The {\tt dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001574
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001575The built-in function {\tt dir} is used to find out which names a module
1576defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001577
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001578\bcode\begin{verbatim}
1579>>> import fibo, sys
1580>>> dir(fibo)
1581['fib', 'fib2']
1582>>> dir(sys)
1583['argv', 'exit', 'modules', 'path', 'ps1', 'ps2', 'stderr', 'stdin', 'stdout']
1584>>>
1585\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001586%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001587Without arguments, {\tt dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001588
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001589\bcode\begin{verbatim}
1590>>> a = [1, 2, 3, 4, 5]
1591>>> import fibo, sys
1592>>> fib = fibo.fib
1593>>> dir()
1594['a', 'fib', 'fibo', 'sys']
1595>>>
1596\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001597%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001598Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001599
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001600{\tt dir()} does not list the names of built-in functions and variables.
1601If you want a list of those, they are defined in the standard module
1602{\tt builtin}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001603
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001604\bcode\begin{verbatim}
1605>>> import builtin
1606>>> dir(builtin)
1607['EOFError', 'KeyboardInterrupt', 'MemoryError', 'NameError', 'None', 'Runti
1608meError', 'SystemError', 'TypeError', 'abs', 'chr', 'dir', 'divmod', 'eval',
1609 'exec', 'float', 'input', 'int', 'len', 'long', 'max', 'min', 'open', 'ord'
1610, 'pow', 'range', 'raw_input', 'reload', 'type']
1611>>>
1612\end{verbatim}\ecode
1613
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001614
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001615\chapter{Output Formatting}
1616
1617So far we've encountered two ways of writing values: {\em expression
1618statements} and the {\tt print} statement. (A third way is using the
1619{\tt write} method of file objects; the standard output file can be
1620referenced as {\tt sys.stdout}. See the Library Reference for more
1621information on this.)
1622
1623Often you'll want more control over the formatting of your output than
1624simply printing space-separated values. The key to nice formatting in
1625Python is to do all the string handling yourself; using string slicing
1626and concatenation operations you can create any lay-out you can imagine.
1627The standard module {\tt string} contains some useful operations for
1628padding strings to a given column width; these will be discussed shortly.
1629
1630One question remains, of course: how do you convert values to strings?
1631Luckily, Python has a way to convert any value to a string: just write
1632the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001633
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001634\bcode\begin{verbatim}
1635>>> x = 10 * 3.14
1636>>> y = 200*200
1637>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1638>>> print s
1639The value of x is 31.4, and y is 40000...
1640>>> # Reverse quotes work on other types besides numbers:
1641>>> p = [x, y]
1642>>> ps = `p`
1643>>> ps
1644'[31.4, 40000]'
1645>>> # Converting a string adds string quotes and backslashes:
1646>>> hello = 'hello, world\n'
1647>>> hellos = `hello`
1648>>> print hellos
1649'hello, world\012'
1650>>> # The argument of reverse quotes may be a tuple:
1651>>> `x, y, ('foo', 'bar')`
1652'(31.4, 40000, (\'foo\', \'bar\'))'
1653>>>
1654\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001655%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001656Here is how you write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001657
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001658\bcode\begin{verbatim}
1659>>> import string
1660>>> for x in range(1, 11):
1661... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1662... # Note trailing comma on previous line
1663... print string.rjust(`x*x*x`, 4)
1664...
1665 1 1 1
1666 2 4 8
1667 3 9 27
1668 4 16 64
1669 5 25 125
1670 6 36 216
1671 7 49 343
1672 8 64 512
1673 9 81 729
167410 100 1000
1675>>>
1676\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001677%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001678(Note that one space between each column was added by the way {\tt print}
1679works: it always adds spaces between its arguments.)
1680
1681This example demonstrates the function {\tt string.rjust()}, which
1682right-justifies a string in a field of a given width by padding it with
1683spaces on the left. There are similar functions {\tt string.ljust()}
1684and {\tt string.center()}. These functions do not write anything, they
1685just return a new string. If the input string is too long, they don't
1686truncate it, but return it unchanged; this will mess up your column
1687lay-out but that's usually better than the alternative, which would be
1688lying about a value. (If you really want truncation you can always add
1689a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1690
1691There is another function, {\tt string.zfill}, which pads a numeric
1692string on the left with zeros. It understands about plus and minus
1693signs:%
1694\footnote{
1695 Better facilities for formatting floating point numbers are
1696 lacking at this moment.
1697}
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001698
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001699\bcode\begin{verbatim}
1700>>> string.zfill('12', 5)
1701'00012'
1702>>> string.zfill('-3.14', 7)
1703'-003.14'
1704>>> string.zfill('3.14159265359', 5)
1705'3.14159265359'
1706>>>
1707\end{verbatim}\ecode
1708
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001709
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001710\chapter{Errors and Exceptions}
1711
1712Until now error messages haven't been more than mentioned, but if you
1713have tried out the examples you have probably seen some. There are
1714(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1715and {\em exceptions}.
1716
1717\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001718
1719Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001720kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001721
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001722\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001723>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001724Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001725while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001726 ^
1727Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001728>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001729\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001730%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001731The parser repeats the offending line and displays a little `arrow'
1732pointing at the earliest point in the line where the error was detected.
1733The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001734{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001735the arrow: in the example, the error is detected at the keyword
1736{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001737File name and line number are printed so you know where to look in case
1738the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001739
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001740\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001741
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001742Even if a statement or expression is syntactically correct, it may
1743cause an error when an attempt is made to execute it.
1744Errors detected during execution are called {\em exceptions} and are
1745not unconditionally fatal: you will soon learn how to handle them in
1746Python programs. Most exceptions are not handled by programs,
1747however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001748
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001749\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001750>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001751Unhandled exception: run-time error: integer division by zero
1752Stack backtrace (innermost last):
1753 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001754>>> 4 + foo*3
1755Unhandled exception: undefined name: foo
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001756Stack backtrace (innermost last):
1757 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001758>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001759Unhandled exception: type error: illegal argument type for built-in operation
1760Stack backtrace (innermost last):
1761 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001762>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001763\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001764%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001765The first line of the error message indicates what happened.
1766Exceptions come in different types, and the type is printed as part of
1767the message: the types in the example are
1768{\tt run-time error},
1769{\tt undefined name}
1770and
1771{\tt type error}.
1772The rest of the line is a detail whose interpretation depends on the
1773exception type.
1774
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001775The rest of the error message shows the context where the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001776exception happened.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001777In general it contains a stack backtrace listing source lines; however,
1778it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001779
1780Here is a summary of the most common exceptions:
1781\begin{itemize}
1782\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001783{\em Run-time\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001784are generally caused by wrong data used by the program; this can be the
1785programmer's fault or caused by bad input.
1786The detail states the cause of the error in more detail.
1787\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001788{\em Undefined\ name}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001789errors are more serious: these are usually caused by misspelled
1790identifiers.%
1791\footnote{
1792 The parser does not check whether names used in a program are at
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001793 all defined elsewhere in the program; such checks are
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001794 postponed until run-time. The same holds for type checking.
1795}
1796The detail is the offending identifier.
1797\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001798{\em Type\ errors} are also pretty serious: this is another case of
1799using wrong data (or better, using data the wrong way), but here the
1800error can be gleaned from the object type(s) alone. The detail shows
1801in what context the error was detected.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001802\end{itemize}
1803
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001804\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001805
1806It is possible to write programs that handle selected exceptions.
1807Look at the following example, which prints a table of inverses of
1808some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001809
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001810\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001811>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001812>>> for x in numbers:
1813... print x,
1814... try:
1815... print 1.0 / x
1816... except RuntimeError:
1817... print '*** has no inverse ***'
1818...
18190.3333 3.00030003
18202.5 0.4
18210 *** has no inverse ***
182210 0.1
1823>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001824\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001825%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001826The {\tt try} statement works as follows.
1827\begin{itemize}
1828\item
1829First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001830{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001831(the statement(s) between the {\tt try} and {\tt except} keywords) is
1832executed.
1833\item
1834If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001835{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001836is skipped and execution of the {\tt try} statement is finished.
1837\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001838If an exception occurs during execution of the try clause,
1839the rest of the clause is skipped. Then if
1840its type matches the exception named after the {\tt except} keyword,
1841the rest of the try clause is skipped, the except clause is executed,
1842and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001843\item
1844If an exception occurs which does not match the exception named in the
1845except clause, it is passed on to outer try statements; if no handler is
1846found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001847{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001848and execution stops with a message as shown above.
1849\end{itemize}
1850A {\tt try} statement may have more than one except clause, to specify
1851handlers for different exceptions.
1852At most one handler will be executed.
1853Handlers only handle exceptions that occur in the corresponding try
1854clause, not in other handlers of the same {\tt try} statement.
1855An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001856e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001857
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001858\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001859... except (RuntimeError, TypeError, NameError):
1860... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001861\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001862%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001863The last except clause may omit the exception name(s), to serve as a
1864wildcard.
1865Use this with extreme caution!
1866
1867When an exception occurs, it may have an associated value, also known as
1868the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001869{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001870The presence and type of the argument depend on the exception type.
1871For exception types which have an argument, the except clause may
1872specify a variable after the exception name (or list) to receive the
1873argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001874
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001875\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001876>>> try:
1877... foo()
1878... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001879... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001880...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001881name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001882>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001883\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001884%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001885If an exception has an argument, it is printed as the third part
1886(`detail') of the message for unhandled exceptions.
1887
1888Standard exception names are built-in identifiers (not reserved
1889keywords).
1890These are in fact string objects whose
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001891{\em object\ identity}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001892(not their value!) identifies the exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001893The string is printed as the second part of the message for unhandled
1894exceptions.
1895Their names and values are:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001896
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001897\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001898EOFError 'end-of-file read'
1899KeyboardInterrupt 'keyboard interrupt'
1900MemoryError 'out of memory' *
1901NameError 'undefined name' *
1902RuntimeError 'run-time error' *
1903SystemError 'system error' *
1904TypeError 'type error' *
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001905\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001906%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001907The meanings should be clear enough.
1908Those exceptions with a {\tt *} in the third column have an argument.
1909
1910Exception handlers don't just handle exceptions if they occur
1911immediately in the try clause, but also if they occur inside functions
1912that are called (even indirectly) in the try clause.
1913For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001914
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001915\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001916>>> def this_fails():
1917... x = 1/0
1918...
1919>>> try:
1920... this_fails()
1921... except RuntimeError, detail:
1922... print 'Handling run-time error:', detail
1923...
Guido van Rossum67fa1601991-04-23 14:14:57 +00001924Handling run-time error: integer division by zero
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001925>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001926\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001927
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001928\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001929
1930The {\tt raise} statement allows the programmer to force a specified
1931exception to occur.
1932For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001933
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001934\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001935>>> raise NameError, 'Hi There!'
1936Unhandled exception: undefined name: Hi There!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001937Stack backtrace (innermost last):
1938 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001939>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001940\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001941%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001942The first argument to {\tt raise} names the exception to be raised.
1943The optional second argument specifies the exception's argument.
1944
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001945\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001946
1947Programs may name their own exceptions by assigning a string to a
1948variable.
1949For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001950
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001951\bcode\begin{verbatim}
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001952>>> my_exc = 'Nobody likes me'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001953>>> try:
1954... raise my_exc, 2*2
1955... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001956... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001957...
1958My exception occured, value: 4
1959>>> raise my_exc, 1
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001960Nobody likes me: 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001961Stack backtrace (innermost last):
1962 File "<stdin>", line 7
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001963>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001964\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001965%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001966Many standard modules use this to report errors that may occur in
1967functions they define.
1968
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001969\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001970
1971The {\tt try} statement has another optional clause which is intended to
1972define clean-up actions that must be executed under all circumstances.
1973For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001974
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001975\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001976>>> try:
1977... raise KeyboardInterrupt
1978... finally:
1979... print 'Goodbye, world!'
1980...
1981Goodbye, world!
1982Unhandled exception: keyboard interrupt
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001983Stack backtrace (innermost last):
1984 File "<stdin>", line 2
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001985>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001986\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001987%
Guido van Rossumda8c3fd1992-08-09 13:55:25 +00001988A {\tt finally} clause is executed whether or not an exception has
1989occurred in the {\tt try} clause. When an exception has occurred, it
1990is re-raised after the {\tt finally} clauses is executed. The
1991{\tt finally} clause is also executed ``on the way out'' when the
1992{\tt try} statement is left via a {\tt break} or {\tt return}
1993statement.
1994
1995A {\tt try} statement must either have one or more {\tt except}
1996clauses or one {\tt finally} clause, but not both.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001997
Guido van Rossum5e0759d1992-08-07 16:06:24 +00001998
1999\chapter{Classes}
2000
2001Python's class mechanism adds classes to the language with a minimum
2002of new syntax and semantics. It is a mixture of the class mechanisms
2003found in C++ and Modula-3. As is true for modules, classes in Python
2004do not put an absolute barrier between definition and user, but rather
2005rely on the politeness of the user not to ``break into the
2006definition.'' The most important features of classes are retained
2007with full power, however: the class inheritance mechanism allows
2008multiple base classes, a derived class can override any methods of its
2009base class(es), a method can call the method of a base class with the
2010same name. Objects can contain an arbitrary amount of private data.
2011
2012In C++ terminology, all class members (including the data members) are
2013{\em public}, and all member functions are {\em virtual}. There are
2014no special constructors or desctructors. As in Modula-3, there are no
2015shorthands for referencing the object's members from its methods: the
2016method function is declared with an explicit first argument
2017representing the object, which is provided implicitly by the call. As
2018in Smalltalk, classes themselves are objects, albeit in the wider
2019sense of the word: in Python, all data types are objects. This
2020provides semantics for importing and renaming. But, just like in C++
2021or Modula-3, built-in types cannot be used as base classes for
2022extension by the user. Also, like in Modula-3 but unlike in C++, the
2023built-in operators with special syntax (arithmetic operators,
2024subscriptong etc.) cannot be redefined for class members.
2025
2026
2027\section{A word about terminology}
2028
2029Lacking universally accepted terminology to talk about classes, I'll
2030make occasional use of Smalltalk and C++ terms. (I'd use Modula-3
2031terms, since its object-oriented semantics are closer to those of
2032Python than C++, but I expect that few readers have heard of it...)
2033
2034I also have to warn you that there's a terminological pitfall for
2035object-oriented readers: the word ``object'' in Python does not
2036necessarily mean a class instance. Like C++ and Modula-3, and unlike
2037Smalltalk, not all types in Python are classes: the basic built-in
2038types like integers and lists aren't, and even somewhat more exotic
2039types like files aren't. However, {\em all} Python types share a little
2040bit of common semantics that is best described by using the word
2041object.
2042
2043Objects have individuality, and multiple names (in multiple scopes)
2044can be bound to the same object. This is known as aliasing in other
2045languages. This is usually not appreciated on a first glance at
2046Python, and can be safely ignored when dealing with immutable basic
2047types (numbers, strings, tuples). However, aliasing has an
2048(intended!) effect on the semantics of Python code involving mutable
2049objects such as lists, dictionaries, and most types representing
2050entities outside the program (files, windows, etc.). This is usually
2051used to the benefit of the program, since aliases behave like pointers
2052in some respects. For example, passing an object is cheap since only
2053a pointer is passed by the implementation; and if a function modifies
2054an object passed as an argument, the caller will see the change --- this
2055obviates the need for two different argument passing mechanisms as in
2056Pascal.
2057
2058
2059\section{Python scopes and name spaces}
2060
2061Before introducing classes, I first have to tell you something about
2062Python's scope rules. Class definitions play some neat tricks with
2063name spaces, and you need to know how scopes and name spaces work to
2064fully understand what's going on. Incidentally, knowledge about this
2065subject is useful for any advanced Python programmer.
2066
2067Let's begin with some definitions.
2068
2069A {\em name space} is a mapping from names to objects. Most name
2070spaces are currently implemented as Python dictionaries, but that's
2071normally not noticeable in any way (except for performance), and it
2072may change in the future. Examples of name spaces are: the set of
2073built-in names (functions such as \verb\abs()\, and built-in exception
2074names); the global names in a module; and the local names in a
2075function invocation. In a sense the set of attributes of an object
2076also form a name space. The important things to know about name
2077spaces is that there is absolutely no relation between names in
2078different name spaces; for instance, two different modules may both
2079define a function ``maximize'' without confusion --- users of the
2080modules must prefix it with the module name.
2081
2082By the way, I use the word {\em attribute} for any name following a
2083dot --- for example, in the expression \verb\z.real\, \verb\real\ is
2084an attribute of the object \verb\z\. Strictly speaking, references to
2085names in modules are attribute references: in the expression
2086\verb\modname.funcname\, \verb\modname\ is a module object and
2087\verb\funcname\ is an attribute of it. In this case there happens to
2088be a straightforward mapping between the module's attributes and the
2089global names defined in the module: they share the same name space!%
2090\footnote{
2091 Except for one thing. Module objects have a secret read-only
2092 attribute called {\tt __dict__} which returns the dictionary
2093 used to implement the module's name space; the name
2094 {\tt __dict__} is an attribute but not a global name.
2095 Obviously, using this violates the abstraction of name space
2096 implementation, and should be restricted to things like
2097 post-mortem debuggers...
2098}
2099
2100Attributes may be read-only or writable. In the latter case,
2101assignment to attributes is possible. Module attributes are writable:
2102you can write \verb\modname.the_answer = 42\. Writable attributes may
2103also be deleted with the del statement, e.g.
2104\verb\del modname.the_answer\.
2105
2106Name spaces are created at different moments and have different
2107lifetimes. The name space containing the built-in names is created
2108when the Python interpreter starts up, and is never deleted. The
2109global name space for a module is created when the module definition
2110is read in; normally, module name spaces also last until the
2111interpreter quits. The statements executed by the top-level
2112invocation of the interpreter, either read from a script file or
2113interactively, are considered part of a module called \verb\__main__\,
2114so they have their own global name space. (The built-in names
2115actually also live in a module; this is called \verb\builtin\,
2116although it should really have been called \verb\__builtin__\.)
2117
2118The local name space for a function is created when the function is
2119called, and deleted when the function returns or raises an exception
2120that is not handled within the function. (Actually, forgetting would
2121be a better way to describe what actually happens.) Of course,
2122recursive invocations each have their own local name space.
2123
2124A {\em scope} is a textual region of a Python program where a name space
2125is directly accessible. ``Directly accessible'' here means that an
2126unqualified reference to a name attempts to find the name in the name
2127space.
2128
2129Although scopes are determined statically, they are used dynamically.
2130At any time during execution, exactly three nested scopes are in use
2131(i.e., exactly three name spaces are directly accessible): the
2132innermost scope, which is searched first, contains the local names,
2133the middle scope, searched next, contains the current module's global
2134names, and the outermost scope (searched last) is the name space
2135containing built-in names.
2136
2137Usually, the local scope references the local names of the (textually)
2138current function. Outside functions, the the local scope references
2139the same name space as the global scope: the module's name space.
2140Class definitions place yet another name space in the local scope.
2141
2142It is important to realize that scopes are determined textually: the
2143global scope of a function defined in a module is that module's name
2144space, no matter from where or by what alias the function is called.
2145On the other hand, the actual search for names is done dynamically, at
2146run time --- however, the the language definition is evolving towards
2147static name resolution, at ``compile'' time, so don't rely on dynamic
2148name resolution! (In fact, local variables are already determined
2149statically.)
2150
2151A special quirk of Python is that assignments always go into the
2152innermost scope. Assignments do not copy data --- they just
2153bind names to objects. The same is true for deletions: the statement
2154\verb\del x\ removes the binding of x from the name space referenced by the
2155local scope. In fact, all operations that introduce new names use the
2156local scope: in particular, import statements and function definitions
2157bind the module or function name in the local scope. (The
2158\verb\global\ statement can be used to indicate that particular
2159variables live in the global scope.)
2160
2161
2162\section{A first look at classes}
2163
2164Classes introduce a little bit of new syntax, three new object types,
2165and some new semantics.
2166
2167
2168\subsection{Class definition syntax}
2169
2170The simplest form of class definition looks like this:
2171
2172\begin{verbatim}
2173 class ClassName:
2174 <statement-1>
2175 .
2176 .
2177 .
2178 <statement-N>
2179\end{verbatim}
2180
2181Class definitions, like function definitions (\verb\def\ statements)
2182must be executed before they have any effect. (You could conceivably
2183place a class definition in a branch of an \verb\if\ statement, or
2184inside a function.)
2185
2186In practice, the statements inside a class definition will usually be
2187function definitions, but other statements are allowed, and sometimes
2188useful --- we'll come back to this later. The function definitions
2189inside a class normally have a peculiar form of argument list,
2190dictated by the calling conventions for methods --- again, this is
2191explained later.
2192
2193When a class definition is entered, a new name space is created, and
2194used as the local scope --- thus, all assignments to local variables
2195go into this new name space. In particular, function definitions bind
2196the name of the new function here.
2197
2198When a class definition is left normally (via the end), a {\em class
2199object} is created. This is basically a wrapper around the contents
2200of the name space created by the class definition; we'll learn more
2201about class objects in the next section. The original local scope
2202(the one in effect just before the class definitions was entered) is
2203reinstated, and the class object is bound here to class name given in
2204the class definition header (ClassName in the example).
2205
2206
2207\subsection{Class objects}
2208
2209Class objects support two kinds of operations: attribute references
2210and instantiation.
2211
2212{\em Attribute references} use the standard syntax used for all
2213attribute references in Python: \verb\obj.name\. Valid attribute
2214names are all the names that were in the class's name space when the
2215class object was created. So, if the class definition looked like
2216this:
2217
2218\begin{verbatim}
2219 class MyClass:
2220 i = 12345
2221 def f(x):
2222 return 'hello world'
2223\end{verbatim}
2224
2225then \verb\MyClass.i\ and \verb\MyClass.f\ are valid attribute
2226references, returning an integer and a function object, respectively.
2227Class attributes can also be assigned to, so you can change the
2228value of \verb\MyClass.i\ by assignment.
2229
2230Class {\em instantiation} uses function notation. Just pretend that
2231the class object is a parameterless function that returns a new
2232instance of the class. For example, (assuming the above class):
2233
2234\begin{verbatim}
2235 x = MyClass()
2236\end{verbatim}
2237
2238creates a new {\em instance} of the class and assigns this object to
2239the local variable \verb\x\.
2240
2241
2242\subsection{Instance objects}
2243
2244Now what can we do with instance objects? The only operations
2245understood by instance objects are attribute references. There are
2246two kinds of valid attribute names.
2247
2248The first I'll call {\em data attributes}. These correspond to
2249``instance variables'' in Smalltalk, and to ``data members'' in C++.
2250Data attributes need not be declared; like local variables, they
2251spring into existence when they are first assigned to. For example,
2252if \verb\x\ in the instance of \verb\MyClass\ created above, the
2253following piece of code will print the value 16, without leaving a
2254trace:
2255
2256\begin{verbatim}
2257 x.counter = 1
2258 while x.counter < 10:
2259 x.counter = x.counter * 2
2260 print x.counter
2261 del x.counter
2262\end{verbatim}
2263
2264The second kind of attribute references understood by instance objects
2265are {\em methods}. A method is a function that ``belongs to'' an
2266object. (In Python, the term method is not unique to class instances:
2267other object types can have methods as well, e.g., list objects have
2268methods called append, insert, remove, sort, and so on. However,
2269below, we'll use the term method exclusively to mean methods of class
2270instance objects, unless explicitly stated otherwise.)
2271
2272Valid method names of an instance object depend on its class. By
2273definition, all attributes of a class that are (user-defined) function
2274objects define corresponding methods of its instances. So in our
2275example, \verb\x.f\ is a valid method reference, since
2276\verb\MyClass.f\ is a function, but \verb\x.i\ is not, since
2277\verb\MyClass.i\ is not. But \verb\x.f\ is not the
2278same thing as \verb\MyClass.f\ --- it is a {\em method object}, not a
2279function object.
2280
2281
2282\subsection{Method objects}
2283
2284Usually, a method is called immediately, e.g.:
2285
2286\begin{verbatim}
2287 x.f()
2288\end{verbatim}
2289
2290In our example, this will return the string \verb\'hello world'\.
2291However, it is not necessary to call a method right away: \verb\x.f\
2292is a method object, and can be stored away and called at a later
2293moment, for example:
2294
2295\begin{verbatim}
2296 xf = x.f
2297 while 1:
2298 print xf()
2299\end{verbatim}
2300
2301will continue to print \verb\hello world\ until the end of time.
2302
2303What exactly happens when a method is called? You may have noticed
2304that \verb\x.f()\ was called without an argument above, even though
2305the function definition for \verb\f\ specified an argument. What
2306happened to the argument? Surely Python raises an exception when a
2307function that requires an argument is called without any --- even if
2308the argument isn't actually used...
2309
2310Actually, you may have guessed the answer: the special thing about
2311methods is that the object is passed as the first argument of the
2312function. In our example, the call \verb\x.f()\ is exactly equivalent
2313to \verb\MyClass.f(x)\. In general, calling a method with a list of
2314{\em n} arguments is equivalent to calling the corresponding function
2315with an argument list that is created by inserting the method's object
2316before the first argument.
2317
2318If you still don't understand how methods work, a look at the
2319implementation can perhaps clarify matters. When an instance
2320attribute is referenced that isn't a data attribute, its class is
2321searched. If the name denotes a valid class attribute that is a
2322function object, a method object is created by packing (pointers to)
2323the instance object and the function object just found together in an
2324abstract object: this is the method object. When the method object is
2325called with an argument list, it is unpacked again, a new argument
2326list is constructed from the instance object and the original argument
2327list, and the function object is called with this new argument list.
2328
2329
2330\section{Random remarks}
2331
2332
2333[These should perhaps be placed more carefully...]
2334
2335
2336Data attributes override method attributes with the same name; to
2337avoid accidental name conflicts, which may cause hard-to-find bugs in
2338large programs, it is wise to use some kind of convention that
2339minimizes the chance of conflicts, e.g., capitalize method names,
2340prefix data attribute names with a small unique string (perhaps just
2341an undescore), or use verbs for methods and nouns for data attributes.
2342
2343
2344Data attributes may be referenced by methods as well as by ordinary
2345users (``clients'') of an object. In other words, classes are not
2346usable to implement pure abstract data types. In fact, nothing in
2347Python makes it possible to enforce data hiding --- it is all based
2348upon convention. (On the other hand, the Python implementation,
2349written in C, can completely hide implementation details and control
2350access to an object if necessary; this can be used by extensions to
2351Python written in C.)
2352
2353
2354Clients should use data attributes with care --- clients may mess up
2355invariants maintained by the methods by stamping on their data
2356attributes. Note that clients may add data attributes of their own to
2357an instance object without affecting the validity of the methods, as
2358long as name conflicts are avoided --- again, a naming convention can
2359save a lot of headaches here.
2360
2361
2362There is no shorthand for referencing data attributes (or other
2363methods!) from within methods. I find that this actually increases
2364the readability of methods: there is no chance of confusing local
2365variables and instance variables when glancing through a method.
2366
2367
2368Conventionally, the first argument of methods is often called
2369\verb\self\. This is nothing more than a convention: the name
2370\verb\self\ has absolutely no special meaning to Python. (Note,
2371however, that by not following the convention your code may be less
2372readable by other Python programmers, and it is also conceivable that
2373a {\em class browser} program be written which relies upon such a
2374convention.)
2375
2376
2377Any function object that is a class attribute defines a method for
2378instances of that class. It is not necessary that the function
2379definition is textually enclosed in the class definition: assigning a
2380function object to a local variable in the class is also ok. For
2381example:
2382
2383\begin{verbatim}
2384 # Function defined outside the class
2385 def f1(self, x, y):
2386 return min(x, x+y)
2387
2388 class C:
2389 f = f1
2390 def g(self):
2391 return 'hello world'
2392 h = g
2393\end{verbatim}
2394
2395Now \verb\f\, \verb\g\ and \verb\h\ are all attributes of class
2396\verb\C\ that refer to function objects, and consequently they are all
2397methods of instances of \verb\C\ --- \verb\h\ being exactly equivalent
2398to \verb\g\. Note that this practice usually only serves to confuse
2399the reader of a program.
2400
2401
2402Methods may call other methods by using method attributes of the
2403\verb\self\ argument, e.g.:
2404
2405\begin{verbatim}
2406 class Bag:
2407 def empty(self):
2408 self.data = []
2409 def add(self, x):
2410 self.data.append(x)
2411 def addtwice(self, x):
Guido van Rossum084b0b21992-08-14 09:19:56 +00002412 self.add(x)
2413 self.add(x)
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002414\end{verbatim}
2415
2416
2417The instantiation operation (``calling'' a class object) creates an
2418empty object. Many classes like to create objects in a known initial
2419state. There is no special syntax to enforce this, but a convention
2420works almost as well: add a method named \verb\init\ to the class,
2421which initializes the instance (by assigning to some important data
2422attributes) and returns the instance itself. For example, class
2423\verb\Bag\ above could have the following method:
2424
2425\begin{verbatim}
2426 def init(self):
2427 self.empty()
2428 return self
2429\end{verbatim}
2430
2431The client can then create and initialize an instance in one
2432statement, as follows:
2433
2434\begin{verbatim}
2435 x = Bag().init()
2436\end{verbatim}
2437
2438Of course, the \verb\init\ method may have arguments for greater
2439flexibility.
2440
2441Warning: a common mistake is to forget the \verb\return self\ at the
2442end of an init method!
2443
2444
2445Methods may reference global names in the same way as ordinary
2446functions. The global scope associated with a method is the module
2447containing the class definition. (The class itself is never used as a
2448global scope!) While one rarely encounters a good reason for using
2449global data in a method, there are many legitimate uses of the global
2450scope: for one thing, functions and modules imported into the global
2451scope can be used by methods, as well as functions and classes defined
2452in it. Usually, the class containing the method is itself defined in
2453this global scope, and in the next section we'll find some good
2454reasons why a method would want to reference its own class!
2455
2456
2457\section{Inheritance}
2458
2459Of course, a language feature would not be worthy of the name ``class''
2460without supporting inheritance. The syntax for a derived class
2461definition looks as follows:
2462
2463\begin{verbatim}
2464 class DerivedClassName(BaseClassName):
2465 <statement-1>
2466 .
2467 .
2468 .
2469 <statement-N>
2470\end{verbatim}
2471
2472The name \verb\BaseClassName\ must be defined in a scope containing
2473the derived class definition. Instead of a base class name, an
2474expression is also allowed. This is useful when the base class is
2475defined in another module, e.g.,
2476
2477\begin{verbatim}
2478 class DerivedClassName(modname.BaseClassName):
2479\end{verbatim}
2480
2481Execution of a derived class definition proceeds the same as for a
2482base class. When the class object is constructed, the base class is
2483remembered. This is used for resolving attribute references: if a
2484requested attribute is not found in the class, it is searched in the
2485base class. This rule is applied recursively if the base class itself
2486is derived from some other class.
2487
2488There's nothing special about instantiation of derived classes:
2489\verb\DerivedClassName()\ creates a new instance of the class. Method
2490references are resolved as follows: the corresponding class attribute
2491is searched, descending down the chain of base classes if necessary,
2492and the method reference is valid if this yields a function object.
2493
2494Derived classes may override methods of their base classes. Because
2495methods have no special privileges when calling other methods of the
2496same object, a method of a base class that calls another method
2497defined in the same base class, may in fact end up calling a method of
2498a derived class that overrides it. (For C++ programmers: all methods
2499in Python are ``virtual functions''.)
2500
2501An overriding method in a derived class may in fact want to extend
2502rather than simply replace the base class method of the same name.
2503There is a simple way to call the base class method directly: just
2504call \verb\BaseClassName.methodname(self, arguments)\. This is
2505occasionally useful to clients as well. (Note that this only works if
2506the base class is defined or imported directly in the global scope.)
2507
2508
2509\subsection{Multiple inheritance}
2510
2511Poython supports a limited form of multiple inheritance as well. A
2512class definition with multiple base classes looks as follows:
2513
2514\begin{verbatim}
2515 class DerivedClassName(Base1, Base2, Base3):
2516 <statement-1>
2517 .
2518 .
2519 .
2520 <statement-N>
2521\end{verbatim}
2522
2523The only rule necessary to explain the semantics is the resolution
2524rule used for class attribute references. This is depth-first,
2525left-to-right. Thus, if an attribute is not found in
2526\verb\DerivedClassName\, it is searched in \verb\Base1\, then
2527(recursively) in the base classes of \verb\Base1\, and only if it is
2528not found there, it is searched in \verb\Base2\, and so on.
2529
Guido van Rossum95cd2ef1992-12-08 14:37:55 +00002530(To some people breadth first---searching \verb\Base2\ and
2531\verb\Base3\ before the base classes of \verb\Base1\---looks more
Guido van Rossum5e0759d1992-08-07 16:06:24 +00002532natural. However, this would require you to know whether a particular
2533attribute of \verb\Base1\ is actually defined in \verb\Base1\ or in
2534one of its base classes before you can figure out the consequences of
2535a name conflict with an attribute of \verb\Base2\. The depth-first
2536rule makes no differences between direct and inherited attributes of
2537\verb\Base1\.)
2538
2539It is clear that indiscriminate use of multiple inheritance is a
2540maintenance nightmare, given the reliance in Python on conventions to
2541avoid accidental name conflicts. A well-known problem with multiple
2542inheritance is a class derived from two classes that happen to have a
2543common base class. While it is easy enough to figure out what happens
2544in this case (the instance will have a single copy of ``instance
2545variables'' or data attributes used by the common base class), it is
2546not clear that these semantics are in any way useful.
2547
2548
2549\section{Odds and ends}
2550
2551Sometimes it is useful to have a data type similar to the Pascal
2552``record'' or C ``struct'', bundling together a couple of named data
2553items. An empty class definition will do nicely, e.g.:
2554
2555\begin{verbatim}
2556 class Employee:
2557 pass
2558
2559 john = Employee() # Create an empty employee record
2560
2561 # Fill the fields of the record
2562 john.name = 'John Doe'
2563 john.dept = 'computer lab'
2564 john.salary = 1000
2565\end{verbatim}
2566
2567
2568A piece of Python code that expects a particular abstract data type
2569can often be passed a class that emulates the methods of that data
2570type instead. For instance, if you have a function that formats some
2571data from a file object, you can define a class with methods
2572\verb\read()\ and \verb\readline()\ that gets the data from a string
2573buffer instead, and pass it as an argument. (Unfortunately, this
2574technique has its limitations: a class can't define operations that
2575are accessed by special syntax such as sequence subscripting or
2576arithmetic operators, and assigning such a ``pseudo-file'' to
2577\verb\sys.stdin\ will not cause the interpreter to read further input
2578from it.)
2579
2580
2581Instance method objects have attributes, too: \verb\m.im_self\ is the
2582object of which the method is an instance, and \verb\m.im_func\ is the
2583function object corresponding to the method.
2584
2585
2586XXX Mention bw compat hacks.
2587
2588
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002589\end{document}