blob: cc4c5e17b2d7fc0cd9afb2b0f17ab12a468e2038 [file] [log] [blame]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001% Format this file with latex.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00002
Guido van Rossumcdc93551992-02-11 15:53:13 +00003\documentstyle[times,myformat]{report}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004
5\title{\bf
Guido van Rossum6fc178f1991-08-16 09:13:42 +00006 Python Tutorial
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00007}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00008
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00009\author{
10 Guido van Rossum \\
11 Dept. CST, CWI, Kruislaan 413 \\
12 1098 SJ Amsterdam, The Netherlands \\
13 E-mail: {\tt guido@cwi.nl}
14}
15
16\begin{document}
17
18\pagenumbering{roman}
19
20\maketitle
21
22\begin{abstract}
23
24\noindent
Guido van Rossum4410c751991-06-04 20:22:18 +000025Python is a simple, yet powerful programming language that bridges the
Guido van Rossum6fc178f1991-08-16 09:13:42 +000026gap between C and shell programming, and is thus ideally suited for
27``throw-away programming''
28and rapid prototyping. Its syntax is put
29together from constructs borrowed from a variety of other languages;
30most prominent are influences from ABC, C, Modula-3 and Icon.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000031
Guido van Rossum4410c751991-06-04 20:22:18 +000032The Python interpreter is easily extended with new functions and data
Guido van Rossum6fc178f1991-08-16 09:13:42 +000033types implemented in C. Python is also suitable as an extension
34language for highly customizable C applications such as editors or
35window managers.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000036
Guido van Rossum4410c751991-06-04 20:22:18 +000037Python is available for various operating systems, amongst which
Guido van Rossum6fc178f1991-08-16 09:13:42 +000038several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
39and MS-DOS.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000040
Guido van Rossum6fc178f1991-08-16 09:13:42 +000041This tutorial introduces the reader informally to the basic concepts
42and features of the Python language and system. It helps to have a
43Python interpreter handy for hands-on experience, but as the examples
44are self-contained, the tutorial can be read off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000045
Guido van Rossum481ae681991-11-25 17:28:03 +000046For a description of standard objects and modules, see the {\em Python
47Library Reference} document. The {\em Python Reference Manual} gives
48a more formal definition of the language.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000049
50\end{abstract}
51
52\pagebreak
Guido van Rossumcdc93551992-02-11 15:53:13 +000053{
54\parskip = 0mm
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000055\tableofcontents
Guido van Rossumcdc93551992-02-11 15:53:13 +000056}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000057
58\pagebreak
59
60\pagenumbering{arabic}
61
Guido van Rossum6fc178f1991-08-16 09:13:42 +000062\chapter{Whetting Your Appetite}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000063
Guido van Rossum6fc178f1991-08-16 09:13:42 +000064If you ever wrote a large shell script, you probably know this
65feeling: you'd love to add yet another feature, but it's already so
66slow, and so big, and so complicated; or the feature involves a system
67call or other funcion that is only accessible from C \ldots Usually
68the problem at hand isn't serious enough to warrant rewriting the
69script in C; perhaps because the problem requires variable-length
70strings or other data types (like sorted lists of file names) that are
71easy in the shell but lots of work to implement in C; or perhaps just
72because you're not sufficiently familiar with C.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000073
Guido van Rossum6fc178f1991-08-16 09:13:42 +000074In such cases, Python may be just the language for you. Python is
75simple to use, but it is a real programming language, offering much
76more structure and support for large programs than the shell has. On
77the other hand, it also offers much more error checking than C, and,
78being a {\em very-high-level language}, it has high-level data types
79built in, such as flexible arrays and dictionaries that would cost you
80days to implement efficiently in C. Because of its more general data
81types Python is applicable to a much larger problem domain than {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +000082Awk} or even {\em Perl}, yet many things are at least as easy in
83Python as in those languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000084
Guido van Rossum6fc178f1991-08-16 09:13:42 +000085Python allows you to split up your program in modules that can be
86reused in other Python programs. It comes with a large collection of
Guido van Rossuma8d754e1992-01-07 16:44:35 +000087standard modules that you can use as the basis of your programs --- or
88as examples to start learning to program in Python. There are also
89built-in modules that provide things like file I/O, system calls,
90sockets, and even a generic interface to window systems (STDWIN).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000091
Guido van Rossuma8d754e1992-01-07 16:44:35 +000092Python is an interpreted language, which can save you considerable time
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000093during program development because no compilation and linking is
Guido van Rossum6fc178f1991-08-16 09:13:42 +000094necessary. The interpreter can be used interactively, which makes it
95easy to experiment with features of the language, to write throw-away
96programs, or to test functions during bottom-up program development.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000097It is also a handy desk calculator.
98
Guido van Rossum6fc178f1991-08-16 09:13:42 +000099Python allows writing very compact and readable programs. Programs
100written in Python are typically much shorter than equivalent C
101programs, for several reasons:
102\begin{itemize}
103\item
104the high-level data types allow you to express complex operations in a
105single statement;
106\item
107statement grouping is done by indentation instead of begin/end
108brackets;
109\item
110no variable or argument declarations are necessary.
111\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000112
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000113Python is {\em extensible}: if you know how to program in C it is easy
114to add a new built-in
115function or
116module to the interpreter, either to
117perform critical operations at maximum speed, or to link Python
118programs to libraries that may only be available in binary form (such
119as a vendor-specific graphics library). Once you are really hooked,
120you can link the Python interpreter into an application written in C
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000121and use it as an extension or command language for that application.
122
123By the way, the language is named after the BBC show ``Monty
124Python's Flying Circus'' and has nothing to do with nasty reptiles...
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000125
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000126\section{Where From Here}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000127
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000128Now that you are all excited about Python, you'll want to examine it
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000129in some more detail. Since the best way to learn a language is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000130using it, you are invited here to do so.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000131
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000132In the next chapter, the mechanics of using the interpreter are
133explained. This is rather mundane information, but essential for
134trying out the examples shown later.
135
Guido van Rossum4410c751991-06-04 20:22:18 +0000136The rest of the tutorial introduces various features of the Python
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000137language and system though examples, beginning with simple
138expressions, statements and data types, through functions and modules,
139and finally touching upon advanced concepts like exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000140
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000141When you're through with the turtorial (or just getting bored), you
142should read the Library Reference, which gives complete (though terse)
143reference material about built-in and standard types, functions and
144modules that can save you a lot of time when writing Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000145
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000146\chapter{Using the Python Interpreter}
147
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000148\section{Invoking the Interpreter}
149
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000150The Python interpreter is usually installed as {\tt /usr/local/python}
151on those machines where it is available; putting {\tt /usr/local} in
152your {\UNIX} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000153typing the command
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000154
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000155\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000156python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000157\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000158%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000159to the shell. Since the choice of the directory where the interpreter
160lives is an installation option, other places are possible; check with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000161your local Python guru or system administrator. (E.g., {\tt
162/usr/local/bin/python} is a popular alternative location.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000163
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000164The interpreter operates somewhat like the {\UNIX} shell: when called
165with standard input connected to a tty device, it reads and executes
166commands interactively; when called with a file name argument or with
167a file as standard input, it reads and executes a {\em script} from
168that file.
169
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000170A third way of starting the interpreter is
171``{\tt python -c command [arg] ...}'', which
172executes the statement(s) in {\tt command}, analogous to the shell's
173{\tt -c} option. Since Python statements often contain spaces or other
174characters that are special to the shell, it is best to quote {\tt
175command} in its entirety with double quotes.
176
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000177Note that there is a difference between ``{\tt python file}'' and
178``{\tt python $<$file}''. In the latter case, input requests from the
179program, such as calls to {\tt input()} and {\tt raw\_input()}, are
180satisfied from {\em file}. Since this file has already been read
181until the end by the parser before the program starts executing, the
182program will encounter EOF immediately. In the former case (which is
183usually what you want) they are satisfied from whatever file or device
184is connected to standard input of the Python interpreter.
185
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000186\subsection{Argument Passing}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000187
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000188When known to the interpreter, the script name and additional
189arguments thereafter are passed to the script in the variable {\tt
190sys.argv}, which is a list of strings. Its length is at least one;
191when no script and no arguments are given, {\tt sys.argv[0]} is an
192empty string. When the script name is given as {\tt '-'} (meaning
193standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c
194command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options
195found after {\tt -c command} are not consumed by the Python
196interpreter's option processing but left in {\tt sys.argv} for the
197command to handle.
198
199\subsection{Interactive Mode}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000200
Guido van Rossumdd010801991-06-07 14:31:11 +0000201When commands are read from a tty, the interpreter is said to be in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000202{\em interactive\ mode}. In this mode it prompts for the next command
203with the {\em primary\ prompt}, usually three greater-than signs ({\tt
204>>>}); for continuation lines it prompts with the {\em secondary\
205prompt}, by default three dots ({\tt ...}). Typing an EOF (Control-D)
206at the primary prompt causes the interpreter to exit with a zero exit
207status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000208
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000209The interpreter prints a welcome message stating its version number
210and a copyright notice before printing the first prompt, e.g.:
211
212\bcode\begin{verbatim}
213python
214Python 0.9.5 (Jan 2 1992).
215Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
216>>>
217\end{verbatim}\ecode
218
219\section{The Interpreter and its Environment}
220
221\subsection{Error Handling}
222
223When an error occurs, the interpreter prints an error
224message and a stack trace. In interactive mode, it then returns to
225the primary prompt; when input came from a file, it exits with a
226nonzero exit status after printing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000227the stack trace. (Exceptions handled by an {\tt except} clause in a
228{\tt try} statement are not errors in this context.) Some errors are
229unconditionally fatal and cause an exit with a nonzero exit; this
230applies to internal inconsistencies and some cases of running out of
231memory. All error messages are written to the standard error stream;
232normal output from the executed commands is written to standard
233output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000235Typing the interrupt character (usually Control-C or DEL) to the
236primary or secondary prompt cancels the input and returns to the
237primary prompt.%
238\footnote{
239 A problem with the GNU Readline package may prevent this.
240}
241Typing an interrupt while a command is executing raises the {\tt
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000242KeyboardInterrupt} exception, which may be handled by a {\tt try}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000243statement.
244
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000245\subsection{The Module Search Path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000246
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000247When a module named {\tt foo} is imported, the interpreter searches
248for a file named {\tt foo.py} in the list of directories specified by
249the environment variable {\tt PYTHONPATH}. It has the same syntax as
250the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
251directory names. When {\tt PYTHONPATH} is not set, an
252installation-dependent default path is used, usually {\tt
253.:/usr/local/lib/python}.
254
255Actually, modules are searched in the list of directories given by the
256variable {\tt sys.path} which is initialized from {\tt PYTHONPATH} or
257the installation-dependent default. This allows Python programs that
258know what they're doing to modify or replace the module search path.
259See the section on Standard Modules later.
260
261\subsection{``Compiled'' Python files}
262
263As an important speed-up of the start-up time for short programs that
264use a lot of standard modules, if a file called {\tt foo.pyc} exists
265in the directory where {\tt foo.py} is found, this is assumed to
266contain an already-``compiled'' version of the module {\tt foo}. The
267modification time of the version of {\tt foo.py} used to create {\tt
268foo.pyc} is recorded in {\tt foo.pyc}, and the file is ignored if
269these don't match.
270
271Whenever {\tt foo.py} is successfully compiled, an attempt is made to
272write the compiled version to {\tt foo.pyc}. It is not an error if
273this attempt fails; if for any reason the file is not written
274completely, the resulting {\tt foo.pyc} file will be recognized as
275invalid and thus ignored later.
276
277\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000278
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000279On BSD'ish {\UNIX} systems, Python scripts can be made directly
280executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000281
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000282\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000283#! /usr/local/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000284\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000285%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000286(assuming that's the name of the interpreter) at the beginning of the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000287script and giving the file an executable mode. The {\tt \#!} must be
288the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000289
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000290\section{Interactive Input Editing and History Substitution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000291
Guido van Rossum4410c751991-06-04 20:22:18 +0000292Some versions of the Python interpreter support editing of the current
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000293input line and history substitution, similar to facilities found in
294the Korn shell and the GNU Bash shell. This is implemented using the
295{\em GNU\ Readline} library, which supports Emacs-style and vi-style
296editing. This library has its own documentation which I won't
297duplicate here; however, the basics are easily explained.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000298
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000299Perhaps the quickest check to see whether command line editing is
300supported is typing Control-P to the first Python prompt you get. If
301it beeps, you have command line editing. If nothing appears to
302happen, or if \verb/^P/ is echoed, you can skip the rest of this
303section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000304
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000305\subsection{Line Editing}
306
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000307If supported, input line editing is active whenever the interpreter
308prints a primary or secondary prompt. The current line can be edited
309using the conventional Emacs control characters. The most important
310of these are: C-A (Control-A) moves the cursor to the beginning of the
311line, C-E to the end, C-B moves it one position to the left, C-F to
312the right. Backspace erases the character to the left of the cursor,
313C-D the character to its right. C-K kills (erases) the rest of the
314line to the right of the cursor, C-Y yanks back the last killed
315string. C-underscore undoes the last change you made; it can be
316repeated for cumulative effect.
317
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000318\subsection{History Substitution}
319
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000320History substitution works as follows. All non-empty input lines
321issued are saved in a history buffer, and when a new prompt is given
322you are positioned on a new line at the bottom of this buffer. C-P
323moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000324Any line in the history buffer can be edited; an asterisk appears in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000325front of the prompt to mark a line as modified. Pressing the Return
326key passes the current line to the interpreter. C-R starts an
327incremental reverse search; C-S starts a forward search.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000328
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000329\subsection{Key Bindings}
330
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000331The key bindings and some other parameters of the Readline library can
332be customized by placing commands in an initialization file called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000333{\tt \$HOME/.inputrc}. Key bindings have the form
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000334
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000335\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000336key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000337\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000338%
339or
340
341\bcode\begin{verbatim}
342"string": function-name
343\end{verbatim}\ecode
344%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000345and options can be set with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000346
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000347\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000348set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000349\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000350%
351For example:
352
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000353\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000354# I prefer vi-style editing:
355set editing-mode vi
356# Edit using a single line:
357set horizontal-scroll-mode On
358# Rebind some keys:
359Meta-h: backward-kill-word
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000360"\C-u": universal-argument
361"\C-x\C-r": re-read-init-file
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000362\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000363%
Guido van Rossum4410c751991-06-04 20:22:18 +0000364Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000365instead of Readline's default filename completion function. If you
366insist, you can override this by putting
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000367
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000368\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000369TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000370\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000371%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000372in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000373indented continuation lines...)
374
375\subsection{Commentary}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000376
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000377This facility is an enormous step forward compared to previous
378versions of the interpreter; however, some wishes are left: It would
379be nice if the proper indentation were suggested on continuation lines
380(the parser knows if an indent token is required next). The
381completion mechanism might use the interpreter's symbol table. A
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000382command to check (or even suggest) matching parentheses, quotes etc.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000383would also be useful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000384
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000385\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000386
387In the following examples, input and output are distinguished by the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000388presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
389the example, you must type everything after the prompt, when the
390prompt appears; lines that do not begin with a prompt are output from
391the interpreter.%
392\footnote{
393 I'd prefer to use different fonts to distinguish input
394 from output, but the amount of LaTeX hacking that would require
395 is currently beyond my ability.
396}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000397Note that a secondary prompt on a line by itself in an example means
398you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000399
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000400\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000401
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000402Let's try some simple Python commands. Start the interpreter and wait
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000403for the primary prompt, {\tt >>>}. (It shouldn't take long.)
404
405\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000406
407The interpreter acts as a simple calculator: you can type an
408expression at it and it will write the value. Expression syntax is
409straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000410work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000411can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000412
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000413\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000414>>> # This is a comment
415>>> 2+2
4164
417>>>
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000418>>> (50-5*6)/4
4195
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000420>>> # Division truncates towards zero:
421>>> 7/3
4222
423>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000424\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000425%
426Like in C, the equal sign ({\tt =}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000427variable. The value of an assignment is not written:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000428
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000429\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000430>>> width = 20
431>>> height = 5*9
432>>> width * height
433900
434>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000435\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000436%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000437A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000438
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000439\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000440>>> # Zero x, y and z
441>>> x = y = z = 0
442>>>
443\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000444%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000445There is full support for floating point; operators with mixed type
446operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000447
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000448\bcode\begin{verbatim}
449>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004503.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000451>>> 7.0 / 2
4523.5
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000453>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000454\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000455
456\subsection{Strings}
457
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000458Besides numbers, Python can also manipulate strings, enclosed in
459single quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000460
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000461\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000462>>> 'foo bar'
463'foo bar'
464>>> 'doesn\'t'
465'doesn\'t'
466>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000467\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000468%
469Strings are written the same way as they are typed for input: inside
470quotes and with quotes and other funny characters escaped by
471backslashes, to show the precise value. (The {\tt print} statement,
472described later, can be used to write strings without quotes or
473escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000474
475Strings can be concatenated (glued together) with the {\tt +}
476operator, and repeated with {\tt *}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000477
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000478\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000479>>> word = 'Help' + 'A'
480>>> word
481'HelpA'
482>>> '<' + word*5 + '>'
483'<HelpAHelpAHelpAHelpAHelpA>'
484>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000485\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000486%
487Strings can be subscripted (indexed); like in C, the first character of
488a string has subscript (index) 0.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000489
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000490There is no separate character type; a character is simply a string of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000491size one. Like in Icon, substrings can be specified with the {\em
492slice} notation: two indices separated by a colon.
493
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000494\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000495>>> word[4]
496'A'
497>>> word[0:2]
498'He'
499>>> word[2:4]
500'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000501>>>
502\end{verbatim}\ecode
503%
504Slice indices have useful defaults; an omitted first index defaults to
505zero, an omitted second index defaults to the size of the string being
506sliced.
507
508\bcode\begin{verbatim}
509>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000510'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000511>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000512'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000513>>>
514\end{verbatim}\ecode
515%
516Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
517equals \verb\s\.
518
519\bcode\begin{verbatim}
520>>> word[:2] + word[2:]
521'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000522>>> word[:3] + word[3:]
523'HelpA'
524>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000525\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000526%
527Degenerate slice indices are handled gracefully: an index that is too
528large is replaced by the string size, an upper bound smaller than the
529lower bound returns an empty string.
530
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000531\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000532>>> word[1:100]
533'elpA'
534>>> word[10:]
535''
536>>> word[2:1]
537''
538>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000539\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000540%
541Indices may be negative numbers, to start counting from the right.
542For example:
543
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000544\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000545>>> word[-1] # The last character
546'A'
547>>> word[-2] # The last-but-one character
548'p'
549>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000550'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000551>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000552'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000553>>>
554\end{verbatim}\ecode
555%
556But note that -0 is really the same as 0, so it does not count from
557the right!
558
559\bcode\begin{verbatim}
560>>> word[-0] # (since -0 equals 0)
561'H'
562>>>
563\end{verbatim}\ecode
564%
565Out-of-range negative slice indices are truncated, but don't try this
566for single-element (non-slice) indices:
567
568\bcode\begin{verbatim}
569>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000570'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000571>>> word[-10] # error
572Unhandled exception: IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000573>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000574\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000575%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000576The best way to remember how slices work is to think of the indices as
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000577pointing {\em between} characters, with the left edge of the first
578character numbered 0. Then the right edge of the last character of a
579string of {\tt n} characters has index {\tt n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000580
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000581\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000582 +---+---+---+---+---+
583 | H | e | l | p | A |
584 +---+---+---+---+---+
585 0 1 2 3 4 5
586-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000587\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000588%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000589The first row of numbers gives the position of the indices 0...5 in
590the string; the second row gives the corresponding negative indices.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000591The slice from \verb\i\ to \verb\j\ consists of all characters between
592the edges labeled \verb\i\ and \verb\j\, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000593
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000594For nonnegative indices, the length of a slice is the difference of
595the indices, if both are within bounds, e.g., the length of
596\verb\word[1:3]\ is 2.
597
598The built-in function {\tt len()} returns the length of a string:
599
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000600\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000601>>> s = 'supercalifragilisticexpialidocious'
602>>> len(s)
60334
604>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000605\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000606
607\subsection{Lists}
608
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000609Python knows a number of {\em compound} data types, used to group
610together other values. The most versatile is the {\em list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000611can be written as a list of comma-separated values (items) between
612square brackets. List items need not all have the same type.
613
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000614\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000615>>> a = ['foo', 'bar', 100, 1234]
616>>> a
617['foo', 'bar', 100, 1234]
618>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000619\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000620%
621Like string indices, list indices start at 0, and lists can be sliced,
622concatenated and so on:
623
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000624\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000625>>> a[0]
626'foo'
627>>> a[3]
6281234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000629>>> a[-2]
630100
631>>> a[1:-1]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000632['bar', 100]
633>>> a[:2] + ['bletch', 2*2]
634['foo', 'bar', 'bletch', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000635>>> 3*a[:3] + ['Boe!']
636['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000637>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000638\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000639%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000640Unlike strings, which are {\em immutable}, it is possible to change
641individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000642
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000643\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000644>>> a
645['foo', 'bar', 100, 1234]
646>>> a[2] = a[2] + 23
647>>> a
648['foo', 'bar', 123, 1234]
649>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000650\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000651%
652Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000653of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000654
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000655\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000656>>> # Replace some items:
657>>> a[0:2] = [1, 12]
658>>> a
659[1, 12, 123, 1234]
660>>> # Remove some:
661>>> a[0:2] = []
662>>> a
663[123, 1234]
664>>> # Insert some:
665>>> a[1:1] = ['bletch', 'xyzzy']
666>>> a
667[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000668>>> a[:0] = a # Insert (a copy of) itself at the beginning
669>>> a
670[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000671>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000672\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000673%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000674The built-in function {\tt len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000675
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000676\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000677>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00006788
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000679>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000680\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000681%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000682It is possible to nest lists (create lists containing other lists),
683for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000684
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000685\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000686>>> q = [2, 3]
687>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000688>>> len(p)
6893
690>>> p[1]
691[2, 3]
692>>> p[1][0]
6932
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000694>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000695>>> p
696[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000697>>> q
698[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000699>>>
700\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000701%
702Note that in the last example, {\tt p[1]} and {\tt q} really refer to
703the same object! We'll come back to {\em object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000704
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000705\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000706
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000707Of course, we can use Python for more complicated tasks than adding
708two and two together. For instance, we can write an initial
709subsequence of the {\em Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000710
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000711\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000712>>> # Fibonacci series:
713>>> # the sum of two elements defines the next
714>>> a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000715>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000716... print b
717... a, b = b, a+b
718...
7191
7201
7212
7223
7235
7248
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000725>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000726\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000727%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000728This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000729
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000730\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000731
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000732\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000733The first line contains a {\em multiple assignment}: the variables
734{\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
735last line this is used again, demonstrating that the expressions on
736the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000737assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000738
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000739\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000740The {\tt while} loop executes as long as the condition (here: {\tt b <
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000741100}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000742true; zero is false. The condition may also be a string or list value,
743in fact any sequence; anything with a non-zero length is true, empty
744sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000745comparison. The standard comparison operators are written the same as
746in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000747
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000748\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000749The {\em body} of the loop is {\em indented}: indentation is Python's
750way of grouping statements. Python does not (yet!) provide an
751intelligent input line editing facility, so you have to type a tab or
752space(s) for each indented line. In practice you will prepare more
753complicated input for Python with a text editor; most text editors have
754an auto-indent facility. When a compound statement is entered
755interactively, it must be followed by a blank line to indicate
756completion (since the parser cannot guess when you have typed the last
757line).
758
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000759\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000760The {\tt print} statement writes the value of the expression(s) it is
761given. It differs from just writing the expression you want to write
762(as we did earlier in the calculator examples) in the way it handles
763multiple expressions and strings. Strings are written without quotes,
764and a space is inserted between items, so you can format things nicely,
765like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000766
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000767\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000768>>> i = 256*256
769>>> print 'The value of i is', i
770The value of i is 65536
771>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000772\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000773%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000774A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000775
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000776\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000777>>> a, b = 0, 1
778>>> while b < 1000:
779... print b,
780... a, b = b, a+b
781...
7821 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
783>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000784\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000785%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000786Note that the interpreter inserts a newline before it prints the next
787prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000788
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000789\end{itemize}
790
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000791\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000792
Guido van Rossum4410c751991-06-04 20:22:18 +0000793Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000794usual control flow statements known from other languages, with some
795twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000796
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000797\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000798
799Perhaps the most well-known statement type is the {\tt if} statement.
800For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000801
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000802\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000803>>> if x < 0:
804... x = 0
805... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000806... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000807... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000808... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000809... print 'Single'
810... else:
811... print 'More'
812...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000813\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000814%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000815There can be zero or more {\tt elif} parts, and the {\tt else} part is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000816optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
817useful to avoid excessive indentation. An {\tt if...elif...elif...}
818sequence is a substitute for the {\em switch} or {\em case} statements
819found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000820
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000821\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000822
Guido van Rossum4410c751991-06-04 20:22:18 +0000823The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000824used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000825arithmetic progression of numbers (like in Pascal), or leaving the user
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000826completely free in the iteration test and step (as C), Python's {\tt
827for} statement iterates over the items of any sequence (e.g., a list
828or a string), in the order that they appear in the sequence. For
829example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000830
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000831\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000832>>> # Measure some strings:
833>>> a = ['cat', 'window', 'defenestrate']
834>>> for x in a:
835... print x, len(x)
836...
837cat 3
838window 6
839defenestrate 12
840>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000841\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000842%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000843It is not safe to modify the sequence being iterated over in the loop
844(this can only happen for mutable sequence types, i.e., lists). If
845you need to modify the list you are iterating over, e.g., duplicate
846selected items, you must iterate over a copy. The slice notation
847makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000848
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000849\bcode\begin{verbatim}
850>>> for x in a[:]: # make a slice copy of the entire list
851... if len(x) > 6: a.insert(0, x)
852...
853>>> a
854['defenestrate', 'cat', 'window', 'defenestrate']
855>>>
856\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000857
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000858\section{The {\tt range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000859
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000860If you do need to iterate over a sequence of numbers, the built-in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000861function {\tt range()} comes in handy. It generates lists containing
862arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000863
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000864\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000865>>> range(10)
866[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
867>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000868\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000869%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000870The given end point is never part of the generated list; {\tt range(10)}
871generates a list of 10 values, exactly the legal indices for items of a
872sequence of length 10. It is possible to let the range start at another
873number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000874
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000875\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000876>>> range(5, 10)
877[5, 6, 7, 8, 9]
878>>> range(0, 10, 3)
879[0, 3, 6, 9]
880>>> range(-10, -100, -30)
881[-10, -40, -70]
882>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000883\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000884%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000885To iterate over the indices of a sequence, combine {\tt range()} and
886{\tt len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000887
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000888\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000889>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000890>>> for i in range(len(a)):
891... print i, a[i]
892...
8930 Mary
8941 had
8952 a
8963 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00008974 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000898>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000899\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000900
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000901\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000902
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000903The {\tt break} statement, like in C, breaks out of the smallest
904enclosing {\tt for} or {\tt while} loop.
905
906The {\tt continue} statement, also borrowed from C, continues with the
907next iteration of the loop.
908
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000909Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000910loop terminates through exhaustion of the list (with {\tt for}) or when
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000911the condition becomes false (with {\tt while}), but not when the loop is
912terminated by a {\tt break} statement. This is exemplified by the
913following loop, which searches for a list item of value 0:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000914
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000915\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000916>>> for n in range(2, 10):
917... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000918... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000919... print n, 'equals', x, '*', n/x
920... break
921... else:
922... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000923...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009242 is a prime number
9253 is a prime number
9264 equals 2 * 2
9275 is a prime number
9286 equals 2 * 3
9297 is a prime number
9308 equals 2 * 4
9319 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000932>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000933\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000934
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000935\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000936
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000937The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000938It can be used when a statement is required syntactically but the
939program requires no action.
940For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000941
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000942\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000943>>> while 1:
944... pass # Busy-wait for keyboard interrupt
945...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000946\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000947
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000948\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000949
950We can create a function that writes the Fibonacci series to an
951arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000952
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000953\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000954>>> def fib(n): # write Fibonacci series up to n
955... a, b = 0, 1
956... while b <= n:
957... print b,
958... a, b = b, a+b
959...
960>>> # Now call the function we just defined:
961>>> fib(2000)
9621 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
963>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000964\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000965%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000966The keyword {\tt def} introduces a function {\em definition}. It must
967be followed by the function name and the parenthesized list of formal
968parameters. The statements that form the body of the function starts at
969the next line, indented by a tab stop.
970
971The {\em execution} of a function introduces a new symbol table used
972for the local variables of the function. More precisely, all variable
973assignments in a function store the value in the local symbol table;
974whereas
975 variable references first look in the local symbol table, then
976in the global symbol table, and then in the table of built-in names.
977Thus,
978global variables cannot be directly assigned to from within a
979function, although they may be referenced.
980
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000981The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000982the local symbol table of the called function when it is called; thus,
983arguments are passed using {\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000984\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000985 Actually, {\em call by object reference} would be a better
986 description, since if a mutable object is passed, the caller
987 will see any changes the callee makes to it (e.g., items
988 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000989}
990When a function calls another function, a new local symbol table is
991created for that call.
992
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000993A function definition introduces the function name in the
994current
995symbol table. The value
996of the function name
997has a type that is recognized by the interpreter as a user-defined
998function. This value can be assigned to another name which can then
999also be used as a function. This serves as a general renaming
1000mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001001
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001002\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001003>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001004<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001005>>> f = fib
1006>>> f(100)
10071 1 2 3 5 8 13 21 34 55 89
1008>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001009\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001010%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001011You might object that {\tt fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001012Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001013value. In fact, technically speaking, procedures do return a value,
1014albeit a rather boring one. This value is called {\tt None} (it's a
1015built-in name). Writing the value {\tt None} is normally suppressed by
1016the interpreter if it would be the only value written. You can see it
1017if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001018
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001019\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001020>>> print fib(0)
1021None
1022>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001023\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001024%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001025It is simple to write a function that returns a list of the numbers of
1026the Fibonacci series, instead of printing it:
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>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001030... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001031... a, b = 0, 1
1032... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001033... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001034... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001035... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001036...
1037>>> f100 = fib2(100) # call it
1038>>> f100 # write the result
1039[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1040>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001041\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001042%
Guido van Rossum4410c751991-06-04 20:22:18 +00001043This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001044
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001045\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001046
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001047\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001048The {\tt return} statement returns with a value from a function. {\tt
1049return} without an expression argument is used to return from the middle
1050of a procedure (falling off the end also returns from a proceduce), in
1051which case the {\tt None} value is returned.
1052
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001053\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001054The statement {\tt result.append(b)} calls a {\em method} of the list
1055object {\tt result}. A method is a function that `belongs' to an
1056object and is named {\tt obj.methodname}, where {\tt obj} is some
1057object (this may be an expression), and {\tt methodname} is the name
1058of a method that is defined by the object's type. Different types
1059define different methods. Methods of different types may have the
1060same name without causing ambiguity. (It is possible to define your
1061own object types and methods, using {\em classes}. This is an
1062advanced feature that is not discussed in this tutorial.)
1063The method {\tt append} shown in the example, is defined for
1064list objects; it adds a new element at the end of the list. In this
1065example
1066it is equivalent to {\tt result = result + [b]}, but more efficient.
1067
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001068\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001069
1070\chapter{Odds and Ends}
1071
1072This chapter describes some things you've learned about already in
1073more detail, and adds some new things as well.
1074
1075\section{More on Lists}
1076
1077The list data type has some more methods. Here are all of the methods
1078of lists objects:
1079
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001080\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001081
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001082\item[{\tt insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001083Insert an item at a given position. The first argument is the index of
1084the element before which to insert, so {\tt a.insert(0, x)} inserts at
1085the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
1086{\tt a.append(x)}.
1087
1088\item[{\tt append(x)}]
1089Equivalent to {\tt a.insert(len(a), x)}.
1090
1091\item[{\tt index(x)}]
1092Return the index in the list of the first item whose value is {\tt x}.
1093It is an error if there is no such item.
1094
1095\item[{\tt remove(x)}]
1096Remove the first item from the list whose value is {\tt x}.
1097It is an error if there is no such item.
1098
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001099\item[{\tt sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001100Sort the items of the list, in place.
1101
1102\item[{\tt reverse()}]
1103Reverse the elements of the list, in place.
1104
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001105\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001106
1107An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001108
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001109\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001110>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001111>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001112>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001113>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001114[66.6, 333, -1, 333, 1, 1234.5, 333]
1115>>> a.index(333)
11161
1117>>> a.remove(333)
1118>>> a
1119[66.6, -1, 333, 1, 1234.5, 333]
1120>>> a.reverse()
1121>>> a
1122[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001123>>> a.sort()
1124>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001125[-1, 1, 66.6, 333, 333, 1234.5]
1126>>>
1127\end{verbatim}\ecode
1128
1129\section{The {\tt del} statement}
1130
1131There is a way to remove an item from a list given its index instead
1132of its value: the {\tt del} statement. This can also be used to
1133remove slices from a list (which we did earlier by assignment of an
1134empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001135
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001136\bcode\begin{verbatim}
1137>>> a
1138[-1, 1, 66.6, 333, 333, 1234.5]
1139>>> del a[0]
1140>>> a
1141[1, 66.6, 333, 333, 1234.5]
1142>>> del a[2:4]
1143>>> a
1144[1, 66.6, 1234.5]
1145>>>
1146\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001147%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001148{\tt del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001149
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001150\bcode\begin{verbatim}
1151>>> del a
1152>>>
1153\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001154%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001155Referencing the name {\tt a} hereafter is an error (at least until
1156another value is assigned to it). We'll find other uses for {\tt del}
1157later.
1158
1159\section{Tuples and Sequences}
1160
1161We saw that lists and strings have many common properties, e.g.,
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001162indexinging and slicing operations. They are two examples of {\em
1163sequence} data types. Since Python is an evolving language, other
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001164sequence data types may be added. There is also another standard
1165sequence data type: the {\em tuple}.
1166
1167A tuple consists of a number of values separated by commas, for
1168instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001169
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001170\bcode\begin{verbatim}
1171>>> t = 12345, 54321, 'hello!'
1172>>> t[0]
117312345
1174>>> t
1175(12345, 54321, 'hello!')
1176>>> # Tuples may be nested:
1177>>> u = t, (1, 2, 3, 4, 5)
1178>>> u
1179((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1180>>>
1181\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001182%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001183As you see, on output tuples are alway enclosed in parentheses, so
1184that nested tuples are interpreted correctly; they may be input with
1185or without surrounding parentheses, although often parentheses are
1186necessary anyway (if the tuple is part of a larger expression).
1187
1188Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1189from a database, etc. Tuples, like strings, are immutable: it is not
1190possible to assign to the individual items of a tuple (you can
1191simulate much of the same effect with slicing and concatenation,
1192though).
1193
1194A special problem is the construction of tuples containing 0 or 1
1195items: the syntax has some extra quirks to accomodate these. Empty
1196tuples are constructed by an empty pair of parentheses; a tuple with
1197one item is constructed by following a value with a comma
1198(it is not sufficient to enclose a single value in parentheses).
1199Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001200
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001201\bcode\begin{verbatim}
1202>>> empty = ()
1203>>> singleton = 'hello', # <-- note trailing comma
1204>>> len(empty)
12050
1206>>> len(singleton)
12071
1208>>> singleton
1209('hello',)
1210>>>
1211\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001212%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001213The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
1214tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
1215are packed together in a tuple. The reverse operation is also
1216possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001217
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001218\bcode\begin{verbatim}
1219>>> x, y, z = t
1220>>>
1221\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001222%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001223This is called, appropriately enough, {\em tuple unpacking}. Tuple
1224unpacking requires that the list of variables on the left has the same
1225number of elements as the length of the tuple. Note that multiple
1226assignment is really just a combination of tuple packing and tuple
1227unpacking!
1228
1229Occasionally, the corresponding operation on lists is useful: {\em list
1230unpacking}. This is supported by enclosing the list of variables in
1231square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001232
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001233\bcode\begin{verbatim}
1234>>> a = ['foo', 'bar', 100, 1234]
1235>>> [a1, a2, a3, a4] = a
1236>>>
1237\end{verbatim}\ecode
1238
1239\section{Dictionaries}
1240
1241Another useful data type built into Python is the {\em dictionary}.
1242Dictionaries are sometimes found in other languages as ``associative
1243memories'' or ``associative arrays''. Unlike sequences, which are
1244indexed by a range of numbers, dictionaries are indexed by {\em keys},
1245which are strings. It is best to think of a dictionary as an unordered set of
1246{\em key:value} pairs, with the requirement that the keys are unique
1247(within one dictionary).
1248A pair of braces creates an empty dictionary: \verb/{}/.
1249Placing a comma-separated list of key:value pairs within the
1250braces adds initial key:value pairs to the dictionary; this is also the
1251way dictionaries are written on output.
1252
1253The main operations on a dictionary are storing a value with some key
1254and extracting the value given the key. It is also possible to delete
1255a key:value pair
1256with {\tt del}.
1257If you store using a key that is already in use, the old value
1258associated with that key is forgotten. It is an error to extract a
1259value using a non-existant key.
1260
1261The {\tt keys()} method of a dictionary object returns a list of all the
1262keys used in the dictionary, in random order (if you want it sorted,
1263just apply the {\tt sort()} method to the list of keys). To check
1264whether a single key is in the dictionary, use the \verb/has_key()/
1265method of the dictionary.
1266
1267Here is a small example using a dictionary:
1268
1269\bcode\begin{verbatim}
1270>>> tel = {'jack': 4098, 'sape': 4139}
1271>>> tel['guido'] = 4127
1272>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001273{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001274>>> tel['jack']
12754098
1276>>> del tel['sape']
1277>>> tel['irv'] = 4127
1278>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001279{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001280>>> tel.keys()
1281['guido', 'irv', 'jack']
1282>>> tel.has_key('guido')
12831
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001284>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001285\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001286
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001287\section{More on Conditions}
1288
1289The conditions used in {\tt while} and {\tt if} statements above can
1290contain other operators besides comparisons.
1291
1292The comparison operators {\tt in} and {\tt not in} check whether a value
1293occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
1294is not} compare whether two objects are really the same object; this
1295only matters for mutable objects like lists. All comparison operators
1296have the same priority, which is lower than that of all numerical
1297operators.
1298
1299Comparisons can be chained: e.g., {\tt a < b = c} tests whether {\tt a}
1300is less than {\tt b} and moreover {\tt b} equals {\tt c}.
1301
1302Comparisons may be combined by the Boolean operators {\tt and} and {\tt
1303or}, and the outcome of a comparison (or of any other Boolean
1304expression) may be negated with {\tt not}. These all have lower
1305priorities than comparison operators again; between them, {\tt not} has
1306the highest priority, and {\tt or} the lowest, so that
1307{\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
1308course, parentheses can be used to express the desired composition.
1309
1310The Boolean operators {\tt and} and {\tt or} are so-called {\em
1311shortcut} operators: their arguments are evaluated from left to right,
1312and evaluation stops as soon as the outcome is determined. E.g., if
1313{\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
1314does not evaluate the expression C. In general, the return value of a
1315shortcut operator, when used as a general value and not as a Boolean, is
1316the last evaluated argument.
1317
1318It is possible to assign the result of a comparison or other Boolean
1319expression to a variable, but you must enclose the entire Boolean
1320expression in parentheses. This is necessary because otherwise an
1321assignment like \verb/a = b = c/ would be ambiguous: does it assign the
1322value of {\tt c} to {\tt a} and {\tt b}, or does it compare {\tt b} to
1323{\tt c} and assign the outcome (0 or 1) to {\tt a}? As it is, the first
1324meaning is what you get, and to get the latter you have to write
1325\verb/a = (b = c)/. (In Python, unlike C, assignment cannot occur
1326inside expressions.)
1327
1328\section{Comparing Sequences and Other Types}
1329
1330Sequence objects may be compared to other objects with the same
1331sequence type. The comparison uses {\em lexicographical} ordering:
1332first the first two items are compared, and if they differ this
1333determines the outcome of the comparison; if they are equal, the next
1334two items are compared, and so on, until either sequence is exhausted.
1335If two items to be compared are themselves sequences of the same type,
1336the lexiographical comparison is carried out recursively. If all
1337items of two sequences compare equal, the sequences are considered
1338equal. If one sequence is an initial subsequence of the other, the
1339shorted sequence is the smaller one. Lexicographical ordering for
1340strings uses the ASCII ordering for individual characters. Some
1341examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001342
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001343\bcode\begin{verbatim}
1344(1, 2, 3) < (1, 2, 4)
1345[1, 2, 3] < [1, 2, 4]
1346'ABC' < 'C' < 'Pascal' < 'Python'
1347(1, 2, 3, 4) < (1, 2, 4)
1348(1, 2) < (1, 2, -1)
1349(1, 2, 3) = (1.0, 2.0, 3.0)
1350(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1351\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001352%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001353Note that comparing objects of different types is legal. The outcome
1354is deterministic but arbitrary: the types are ordered by their name.
1355Thus, a list is always smaller than a string, a string is always
1356smaller than a tuple, etc. Mixed numeric types are compared according
1357to their numeric value, so 0 equals 0.0, etc.%
1358\footnote{
1359 The rules for comparing objects of different types should
1360 not be relied upon; they may change in a future version of
1361 the language.
1362}
1363
1364\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001365
Guido van Rossum4410c751991-06-04 20:22:18 +00001366If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001367definitions you have made (functions and variables) are lost.
1368Therefore, if you want to write a somewhat longer program, you are
1369better off using a text editor to prepare the input for the interpreter
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001370and run it with that file as input instead. This is known as creating a
1371{\em script}. As your program gets longer, you may want to split it
1372into several files for easier maintenance. You may also want to use a
1373handy function that you've written in several programs without copying
1374its definition into each program.
1375
Guido van Rossum4410c751991-06-04 20:22:18 +00001376To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001377them in a script or in an interactive instance of the interpreter.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001378Such a file is called a {\em module}; definitions from a module can be
1379{\em imported} into other modules or into the {\em main} module (the
1380collection of variables that you have access to in a script
1381executed at the top level
1382and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001383
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001384A module is a file containing Python definitions and statements. The
1385file name is the module name with the suffix {\tt .py} appended. For
1386instance, use your favorite text editor to create a file called {\tt
1387fibo.py} in the current directory with the following contents:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001388
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001389\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001390# Fibonacci numbers module
1391
1392def fib(n): # write Fibonacci series up to n
1393 a, b = 0, 1
1394 while b <= n:
1395 print b,
1396 a, b = b, a+b
1397
1398def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001399 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001400 a, b = 0, 1
1401 while b <= n:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001402 result.append(b)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001403 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001404 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001405\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001406%
Guido van Rossum4410c751991-06-04 20:22:18 +00001407Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001408following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001409
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001410\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001411>>> import fibo
1412>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001413\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001414%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001415This does not enter the names of the functions defined in
1416{\tt fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001417directly in the current symbol table; it only enters the module name
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001418{\tt fibo}
1419there.
1420Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001421
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001422\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001423>>> fibo.fib(1000)
14241 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1425>>> fibo.fib2(100)
1426[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1427>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001428\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001429%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001430If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001431
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001432\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001433>>> fib = fibo.fib
1434>>> fib(500)
14351 1 2 3 5 8 13 21 34 55 89 144 233 377
1436>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001437\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001438
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001439\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001440
1441A module can contain executable statements as well as function
1442definitions.
1443These statements are intended to initialize the module.
1444They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001445{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001446time the module is imported somewhere.%
1447\footnote{
1448 In fact function definitions are also `statements' that are
1449 `executed'; the execution enters the function name in the
1450 module's global symbol table.
1451}
1452
1453Each module has its own private symbol table, which is used as the
1454global symbol table by all functions defined in the module.
1455Thus, the author of a module can use global variables in the module
1456without worrying about accidental clashes with a user's global
1457variables.
1458On the other hand, if you know what you are doing you can touch a
1459module's global variables with the same notation used to refer to its
1460functions,
1461{\tt modname.itemname}.
1462
1463Modules can import other modules.
1464It is customary but not required to place all
1465{\tt import}
1466statements at the beginning of a module (or script, for that matter).
1467The imported module names are placed in the importing module's global
1468symbol table.
1469
1470There is a variant of the
1471{\tt import}
1472statement that imports names from a module directly into the importing
1473module's symbol table.
1474For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001475
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001476\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001477>>> from fibo import fib, fib2
1478>>> fib(500)
14791 1 2 3 5 8 13 21 34 55 89 144 233 377
1480>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001481\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001482%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001483This does not introduce the module name from which the imports are taken
1484in the local symbol table (so in the example, {\tt fibo} is not
1485defined).
1486
1487There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001488
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001489\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001490>>> from fibo import *
1491>>> fib(500)
14921 1 2 3 5 8 13 21 34 55 89 144 233 377
1493>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001494\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001495%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001496This imports all names except those beginning with an underscore
1497({\tt \_}).
1498
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001499\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001500
Guido van Rossum4410c751991-06-04 20:22:18 +00001501Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001502document (Python Library Reference). Some modules are built into the
1503interpreter; these provide access to operations that are not part of the
1504core of the language but are nevertheless built in, either for
1505efficiency or to provide access to operating system primitives such as
1506system calls. The set of such modules is a configuration option; e.g.,
1507the {\tt amoeba} module is only provided on systems that somehow support
1508Amoeba primitives. One particular module deserves some attention: {\tt
1509sys}, which is built into every Python interpreter. The variables {\tt
1510sys.ps1} and {\tt sys.ps2} define the strings used as primary and
1511secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001512
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001513\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001514>>> import sys
1515>>> sys.ps1
1516'>>> '
1517>>> sys.ps2
1518'... '
1519>>> sys.ps1 = 'C> '
1520C> print 'Yuck!'
1521Yuck!
1522C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001523\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001524%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001525These two variables are only defined if the interpreter is in
1526interactive mode.
1527
1528The variable
1529{\tt sys.path}
1530is a list of strings that determine the interpreter's search path for
1531modules.
1532It is initialized to a default path taken from the environment variable
1533{\tt PYTHONPATH},
1534or from a built-in default if
1535{\tt PYTHONPATH}
1536is not set.
1537You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001538
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001539\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001540>>> import sys
1541>>> sys.path.append('/ufs/guido/lib/python')
1542>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001543\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001544
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001545\section{The {\tt dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001546
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001547The built-in function {\tt dir} is used to find out which names a module
1548defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001549
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001550\bcode\begin{verbatim}
1551>>> import fibo, sys
1552>>> dir(fibo)
1553['fib', 'fib2']
1554>>> dir(sys)
1555['argv', 'exit', 'modules', 'path', 'ps1', 'ps2', 'stderr', 'stdin', 'stdout']
1556>>>
1557\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001558%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001559Without arguments, {\tt dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001560
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001561\bcode\begin{verbatim}
1562>>> a = [1, 2, 3, 4, 5]
1563>>> import fibo, sys
1564>>> fib = fibo.fib
1565>>> dir()
1566['a', 'fib', 'fibo', 'sys']
1567>>>
1568\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001569%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001570Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001571
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001572{\tt dir()} does not list the names of built-in functions and variables.
1573If you want a list of those, they are defined in the standard module
1574{\tt builtin}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001575
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001576\bcode\begin{verbatim}
1577>>> import builtin
1578>>> dir(builtin)
1579['EOFError', 'KeyboardInterrupt', 'MemoryError', 'NameError', 'None', 'Runti
1580meError', 'SystemError', 'TypeError', 'abs', 'chr', 'dir', 'divmod', 'eval',
1581 'exec', 'float', 'input', 'int', 'len', 'long', 'max', 'min', 'open', 'ord'
1582, 'pow', 'range', 'raw_input', 'reload', 'type']
1583>>>
1584\end{verbatim}\ecode
1585
1586\chapter{Output Formatting}
1587
1588So far we've encountered two ways of writing values: {\em expression
1589statements} and the {\tt print} statement. (A third way is using the
1590{\tt write} method of file objects; the standard output file can be
1591referenced as {\tt sys.stdout}. See the Library Reference for more
1592information on this.)
1593
1594Often you'll want more control over the formatting of your output than
1595simply printing space-separated values. The key to nice formatting in
1596Python is to do all the string handling yourself; using string slicing
1597and concatenation operations you can create any lay-out you can imagine.
1598The standard module {\tt string} contains some useful operations for
1599padding strings to a given column width; these will be discussed shortly.
1600
1601One question remains, of course: how do you convert values to strings?
1602Luckily, Python has a way to convert any value to a string: just write
1603the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001604
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001605\bcode\begin{verbatim}
1606>>> x = 10 * 3.14
1607>>> y = 200*200
1608>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1609>>> print s
1610The value of x is 31.4, and y is 40000...
1611>>> # Reverse quotes work on other types besides numbers:
1612>>> p = [x, y]
1613>>> ps = `p`
1614>>> ps
1615'[31.4, 40000]'
1616>>> # Converting a string adds string quotes and backslashes:
1617>>> hello = 'hello, world\n'
1618>>> hellos = `hello`
1619>>> print hellos
1620'hello, world\012'
1621>>> # The argument of reverse quotes may be a tuple:
1622>>> `x, y, ('foo', 'bar')`
1623'(31.4, 40000, (\'foo\', \'bar\'))'
1624>>>
1625\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001626%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001627Here is how you write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001628
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001629\bcode\begin{verbatim}
1630>>> import string
1631>>> for x in range(1, 11):
1632... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1633... # Note trailing comma on previous line
1634... print string.rjust(`x*x*x`, 4)
1635...
1636 1 1 1
1637 2 4 8
1638 3 9 27
1639 4 16 64
1640 5 25 125
1641 6 36 216
1642 7 49 343
1643 8 64 512
1644 9 81 729
164510 100 1000
1646>>>
1647\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001648%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001649(Note that one space between each column was added by the way {\tt print}
1650works: it always adds spaces between its arguments.)
1651
1652This example demonstrates the function {\tt string.rjust()}, which
1653right-justifies a string in a field of a given width by padding it with
1654spaces on the left. There are similar functions {\tt string.ljust()}
1655and {\tt string.center()}. These functions do not write anything, they
1656just return a new string. If the input string is too long, they don't
1657truncate it, but return it unchanged; this will mess up your column
1658lay-out but that's usually better than the alternative, which would be
1659lying about a value. (If you really want truncation you can always add
1660a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1661
1662There is another function, {\tt string.zfill}, which pads a numeric
1663string on the left with zeros. It understands about plus and minus
1664signs:%
1665\footnote{
1666 Better facilities for formatting floating point numbers are
1667 lacking at this moment.
1668}
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001669
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001670\bcode\begin{verbatim}
1671>>> string.zfill('12', 5)
1672'00012'
1673>>> string.zfill('-3.14', 7)
1674'-003.14'
1675>>> string.zfill('3.14159265359', 5)
1676'3.14159265359'
1677>>>
1678\end{verbatim}\ecode
1679
1680\chapter{Errors and Exceptions}
1681
1682Until now error messages haven't been more than mentioned, but if you
1683have tried out the examples you have probably seen some. There are
1684(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1685and {\em exceptions}.
1686
1687\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001688
1689Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001690kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001691
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001692\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001693>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001694Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001695while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001696 ^
1697Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001698>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001699\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001700%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001701The parser repeats the offending line and displays a little `arrow'
1702pointing at the earliest point in the line where the error was detected.
1703The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001704{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001705the arrow: in the example, the error is detected at the keyword
1706{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001707File name and line number are printed so you know where to look in case
1708the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001709
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001710\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001711
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001712Even if a statement or expression is syntactically correct, it may
1713cause an error when an attempt is made to execute it.
1714Errors detected during execution are called {\em exceptions} and are
1715not unconditionally fatal: you will soon learn how to handle them in
1716Python programs. Most exceptions are not handled by programs,
1717however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001718
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001719\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001720>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001721Unhandled exception: run-time error: integer division by zero
1722Stack backtrace (innermost last):
1723 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001724>>> 4 + foo*3
1725Unhandled exception: undefined name: foo
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001726Stack backtrace (innermost last):
1727 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001728>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001729Unhandled exception: type error: illegal argument type for built-in operation
1730Stack backtrace (innermost last):
1731 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001732>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001733\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001734%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001735The first line of the error message indicates what happened.
1736Exceptions come in different types, and the type is printed as part of
1737the message: the types in the example are
1738{\tt run-time error},
1739{\tt undefined name}
1740and
1741{\tt type error}.
1742The rest of the line is a detail whose interpretation depends on the
1743exception type.
1744
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001745The rest of the error message shows the context where the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001746exception happened.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001747In general it contains a stack backtrace listing source lines; however,
1748it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001749
1750Here is a summary of the most common exceptions:
1751\begin{itemize}
1752\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001753{\em Run-time\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001754are generally caused by wrong data used by the program; this can be the
1755programmer's fault or caused by bad input.
1756The detail states the cause of the error in more detail.
1757\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001758{\em Undefined\ name}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001759errors are more serious: these are usually caused by misspelled
1760identifiers.%
1761\footnote{
1762 The parser does not check whether names used in a program are at
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001763 all defined elsewhere in the program; such checks are
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001764 postponed until run-time. The same holds for type checking.
1765}
1766The detail is the offending identifier.
1767\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001768{\em Type\ errors} are also pretty serious: this is another case of
1769using wrong data (or better, using data the wrong way), but here the
1770error can be gleaned from the object type(s) alone. The detail shows
1771in what context the error was detected.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001772\end{itemize}
1773
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001774\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001775
1776It is possible to write programs that handle selected exceptions.
1777Look at the following example, which prints a table of inverses of
1778some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001779
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001780\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001781>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001782>>> for x in numbers:
1783... print x,
1784... try:
1785... print 1.0 / x
1786... except RuntimeError:
1787... print '*** has no inverse ***'
1788...
17890.3333 3.00030003
17902.5 0.4
17910 *** has no inverse ***
179210 0.1
1793>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001794\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001795%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001796The {\tt try} statement works as follows.
1797\begin{itemize}
1798\item
1799First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001800{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001801(the statement(s) between the {\tt try} and {\tt except} keywords) is
1802executed.
1803\item
1804If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001805{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001806is skipped and execution of the {\tt try} statement is finished.
1807\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001808If an exception occurs during execution of the try clause,
1809the rest of the clause is skipped. Then if
1810its type matches the exception named after the {\tt except} keyword,
1811the rest of the try clause is skipped, the except clause is executed,
1812and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001813\item
1814If an exception occurs which does not match the exception named in the
1815except clause, it is passed on to outer try statements; if no handler is
1816found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001817{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001818and execution stops with a message as shown above.
1819\end{itemize}
1820A {\tt try} statement may have more than one except clause, to specify
1821handlers for different exceptions.
1822At most one handler will be executed.
1823Handlers only handle exceptions that occur in the corresponding try
1824clause, not in other handlers of the same {\tt try} statement.
1825An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001826e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001827
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001828\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001829... except (RuntimeError, TypeError, NameError):
1830... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001831\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001832%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001833The last except clause may omit the exception name(s), to serve as a
1834wildcard.
1835Use this with extreme caution!
1836
1837When an exception occurs, it may have an associated value, also known as
1838the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001839{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001840The presence and type of the argument depend on the exception type.
1841For exception types which have an argument, the except clause may
1842specify a variable after the exception name (or list) to receive the
1843argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001844
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001845\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001846>>> try:
1847... foo()
1848... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001849... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001850...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001851name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001852>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001853\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001854%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001855If an exception has an argument, it is printed as the third part
1856(`detail') of the message for unhandled exceptions.
1857
1858Standard exception names are built-in identifiers (not reserved
1859keywords).
1860These are in fact string objects whose
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001861{\em object\ identity}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001862(not their value!) identifies the exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001863The string is printed as the second part of the message for unhandled
1864exceptions.
1865Their names and values are:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001866
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001867\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001868EOFError 'end-of-file read'
1869KeyboardInterrupt 'keyboard interrupt'
1870MemoryError 'out of memory' *
1871NameError 'undefined name' *
1872RuntimeError 'run-time error' *
1873SystemError 'system error' *
1874TypeError 'type error' *
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001875\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001876%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001877The meanings should be clear enough.
1878Those exceptions with a {\tt *} in the third column have an argument.
1879
1880Exception handlers don't just handle exceptions if they occur
1881immediately in the try clause, but also if they occur inside functions
1882that are called (even indirectly) in the try clause.
1883For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001884
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001885\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001886>>> def this_fails():
1887... x = 1/0
1888...
1889>>> try:
1890... this_fails()
1891... except RuntimeError, detail:
1892... print 'Handling run-time error:', detail
1893...
Guido van Rossum67fa1601991-04-23 14:14:57 +00001894Handling run-time error: integer division by zero
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001895>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001896\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001897
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001898\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001899
1900The {\tt raise} statement allows the programmer to force a specified
1901exception to occur.
1902For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001903
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001904\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001905>>> raise NameError, 'Hi There!'
1906Unhandled exception: undefined name: Hi There!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001907Stack backtrace (innermost last):
1908 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001909>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001910\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001911%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001912The first argument to {\tt raise} names the exception to be raised.
1913The optional second argument specifies the exception's argument.
1914
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001915\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001916
1917Programs may name their own exceptions by assigning a string to a
1918variable.
1919For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001920
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001921\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001922>>> my_exc = 'nobody likes me!'
1923>>> try:
1924... raise my_exc, 2*2
1925... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001926... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001927...
1928My exception occured, value: 4
1929>>> raise my_exc, 1
1930Unhandled exception: nobody likes me!: 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001931Stack backtrace (innermost last):
1932 File "<stdin>", line 7
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001933>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001934\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001935%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001936Many standard modules use this to report errors that may occur in
1937functions they define.
1938
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001939\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001940
1941The {\tt try} statement has another optional clause which is intended to
1942define clean-up actions that must be executed under all circumstances.
1943For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001944
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001945\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001946>>> try:
1947... raise KeyboardInterrupt
1948... finally:
1949... print 'Goodbye, world!'
1950...
1951Goodbye, world!
1952Unhandled exception: keyboard interrupt
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001953Stack backtrace (innermost last):
1954 File "<stdin>", line 2
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001955>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001956\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001957%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001958The
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001959{\em finally\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001960must follow the except clauses(s), if any.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001961It is executed whether or not an exception occurred,
1962or whether or not an exception is handled.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001963If the exception is handled, the finally clause is executed after the
1964handler (and even if another exception occurred in the handler).
1965It is also executed when the {\tt try} statement is left via a
1966{\tt break} or {\tt return} statement.
1967
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001968\end{document}