blob: 24fb66b0c336ade3901f8a574d30eee373ab9a8f [file] [log] [blame]
Guido van Rossumd33024c1992-04-05 15:06:12 +00001\documentstyle[twoside,a4wide,11pt,myformat]{report}
2% ^^^^^^^^^^^^^^^^^^^^
3% If you have trouble finding these style files, any of the pointed-at
4% style options are optional and may be taken out.
5% But "myformat.sty" should be found in the same directory as this file!
6% Also, "myformat" should be last since it corrects a few style params.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00007
8\title{\bf
Guido van Rossum6fc178f1991-08-16 09:13:42 +00009 Python Tutorial
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000010}
Guido van Rossum6fc178f1991-08-16 09:13:42 +000011
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000012\author{
13 Guido van Rossum \\
14 Dept. CST, CWI, Kruislaan 413 \\
15 1098 SJ Amsterdam, The Netherlands \\
16 E-mail: {\tt guido@cwi.nl}
17}
18
19\begin{document}
20
21\pagenumbering{roman}
22
23\maketitle
24
25\begin{abstract}
26
27\noindent
Guido van Rossum4410c751991-06-04 20:22:18 +000028Python is a simple, yet powerful programming language that bridges the
Guido van Rossum6fc178f1991-08-16 09:13:42 +000029gap between C and shell programming, and is thus ideally suited for
30``throw-away programming''
31and rapid prototyping. Its syntax is put
32together from constructs borrowed from a variety of other languages;
33most prominent are influences from ABC, C, Modula-3 and Icon.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000034
Guido van Rossum4410c751991-06-04 20:22:18 +000035The Python interpreter is easily extended with new functions and data
Guido van Rossum6fc178f1991-08-16 09:13:42 +000036types implemented in C. Python is also suitable as an extension
37language for highly customizable C applications such as editors or
38window managers.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000039
Guido van Rossum4410c751991-06-04 20:22:18 +000040Python is available for various operating systems, amongst which
Guido van Rossum6fc178f1991-08-16 09:13:42 +000041several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
42and MS-DOS.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000043
Guido van Rossum6fc178f1991-08-16 09:13:42 +000044This tutorial introduces the reader informally to the basic concepts
45and features of the Python language and system. It helps to have a
46Python interpreter handy for hands-on experience, but as the examples
47are self-contained, the tutorial can be read off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000048
Guido van Rossum481ae681991-11-25 17:28:03 +000049For a description of standard objects and modules, see the {\em Python
50Library Reference} document. The {\em Python Reference Manual} gives
51a more formal definition of the language.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000052
53\end{abstract}
54
55\pagebreak
Guido van Rossumcdc93551992-02-11 15:53:13 +000056{
57\parskip = 0mm
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000058\tableofcontents
Guido van Rossumcdc93551992-02-11 15:53:13 +000059}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000060
61\pagebreak
62
63\pagenumbering{arabic}
64
Guido van Rossum6fc178f1991-08-16 09:13:42 +000065\chapter{Whetting Your Appetite}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000066
Guido van Rossum6fc178f1991-08-16 09:13:42 +000067If you ever wrote a large shell script, you probably know this
68feeling: you'd love to add yet another feature, but it's already so
69slow, and so big, and so complicated; or the feature involves a system
70call or other funcion that is only accessible from C \ldots Usually
71the problem at hand isn't serious enough to warrant rewriting the
72script in C; perhaps because the problem requires variable-length
73strings or other data types (like sorted lists of file names) that are
74easy in the shell but lots of work to implement in C; or perhaps just
75because you're not sufficiently familiar with C.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000076
Guido van Rossum6fc178f1991-08-16 09:13:42 +000077In such cases, Python may be just the language for you. Python is
78simple to use, but it is a real programming language, offering much
79more structure and support for large programs than the shell has. On
80the other hand, it also offers much more error checking than C, and,
81being a {\em very-high-level language}, it has high-level data types
82built in, such as flexible arrays and dictionaries that would cost you
83days to implement efficiently in C. Because of its more general data
84types Python is applicable to a much larger problem domain than {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +000085Awk} or even {\em Perl}, yet many things are at least as easy in
86Python as in those languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000087
Guido van Rossum6fc178f1991-08-16 09:13:42 +000088Python allows you to split up your program in modules that can be
89reused in other Python programs. It comes with a large collection of
Guido van Rossuma8d754e1992-01-07 16:44:35 +000090standard modules that you can use as the basis of your programs --- or
91as examples to start learning to program in Python. There are also
92built-in modules that provide things like file I/O, system calls,
93sockets, and even a generic interface to window systems (STDWIN).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000094
Guido van Rossuma8d754e1992-01-07 16:44:35 +000095Python is an interpreted language, which can save you considerable time
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000096during program development because no compilation and linking is
Guido van Rossum6fc178f1991-08-16 09:13:42 +000097necessary. The interpreter can be used interactively, which makes it
98easy to experiment with features of the language, to write throw-away
99programs, or to test functions during bottom-up program development.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000100It is also a handy desk calculator.
101
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000102Python allows writing very compact and readable programs. Programs
103written in Python are typically much shorter than equivalent C
104programs, for several reasons:
105\begin{itemize}
106\item
107the high-level data types allow you to express complex operations in a
108single statement;
109\item
110statement grouping is done by indentation instead of begin/end
111brackets;
112\item
113no variable or argument declarations are necessary.
114\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000115
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000116Python is {\em extensible}: if you know how to program in C it is easy
117to add a new built-in
118function or
119module to the interpreter, either to
120perform critical operations at maximum speed, or to link Python
121programs to libraries that may only be available in binary form (such
122as a vendor-specific graphics library). Once you are really hooked,
123you can link the Python interpreter into an application written in C
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000124and use it as an extension or command language for that application.
125
126By the way, the language is named after the BBC show ``Monty
127Python's Flying Circus'' and has nothing to do with nasty reptiles...
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000128
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000129\section{Where From Here}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000130
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000131Now that you are all excited about Python, you'll want to examine it
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000132in some more detail. Since the best way to learn a language is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000133using it, you are invited here to do so.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000134
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000135In the next chapter, the mechanics of using the interpreter are
136explained. This is rather mundane information, but essential for
137trying out the examples shown later.
138
Guido van Rossum4410c751991-06-04 20:22:18 +0000139The rest of the tutorial introduces various features of the Python
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000140language and system though examples, beginning with simple
141expressions, statements and data types, through functions and modules,
142and finally touching upon advanced concepts like exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000143
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000144When you're through with the turtorial (or just getting bored), you
145should read the Library Reference, which gives complete (though terse)
146reference material about built-in and standard types, functions and
147modules that can save you a lot of time when writing Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000148
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000149\chapter{Using the Python Interpreter}
150
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000151\section{Invoking the Interpreter}
152
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000153The Python interpreter is usually installed as {\tt /usr/local/python}
154on those machines where it is available; putting {\tt /usr/local} in
155your {\UNIX} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000156typing the command
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000157
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000158\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000159python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000160\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000161%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000162to the shell. Since the choice of the directory where the interpreter
163lives is an installation option, other places are possible; check with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000164your local Python guru or system administrator. (E.g., {\tt
165/usr/local/bin/python} is a popular alternative location.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000166
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000167The interpreter operates somewhat like the {\UNIX} shell: when called
168with standard input connected to a tty device, it reads and executes
169commands interactively; when called with a file name argument or with
170a file as standard input, it reads and executes a {\em script} from
171that file.
172
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000173A third way of starting the interpreter is
174``{\tt python -c command [arg] ...}'', which
175executes the statement(s) in {\tt command}, analogous to the shell's
176{\tt -c} option. Since Python statements often contain spaces or other
177characters that are special to the shell, it is best to quote {\tt
178command} in its entirety with double quotes.
179
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000180Note that there is a difference between ``{\tt python file}'' and
181``{\tt python $<$file}''. In the latter case, input requests from the
Guido van Rossum573805a1992-03-06 10:56:03 +0000182program, such as calls to {\tt input()} and {\tt raw_input()}, are
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000183satisfied from {\em file}. Since this file has already been read
184until the end by the parser before the program starts executing, the
185program will encounter EOF immediately. In the former case (which is
186usually what you want) they are satisfied from whatever file or device
187is connected to standard input of the Python interpreter.
188
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000189\subsection{Argument Passing}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000190
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000191When known to the interpreter, the script name and additional
192arguments thereafter are passed to the script in the variable {\tt
193sys.argv}, which is a list of strings. Its length is at least one;
194when no script and no arguments are given, {\tt sys.argv[0]} is an
195empty string. When the script name is given as {\tt '-'} (meaning
196standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c
197command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options
198found after {\tt -c command} are not consumed by the Python
199interpreter's option processing but left in {\tt sys.argv} for the
200command to handle.
201
202\subsection{Interactive Mode}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000203
Guido van Rossumdd010801991-06-07 14:31:11 +0000204When commands are read from a tty, the interpreter is said to be in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000205{\em interactive\ mode}. In this mode it prompts for the next command
206with the {\em primary\ prompt}, usually three greater-than signs ({\tt
207>>>}); for continuation lines it prompts with the {\em secondary\
208prompt}, by default three dots ({\tt ...}). Typing an EOF (Control-D)
209at the primary prompt causes the interpreter to exit with a zero exit
210status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000211
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000212The interpreter prints a welcome message stating its version number
213and a copyright notice before printing the first prompt, e.g.:
214
215\bcode\begin{verbatim}
216python
217Python 0.9.5 (Jan 2 1992).
218Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
219>>>
220\end{verbatim}\ecode
221
222\section{The Interpreter and its Environment}
223
224\subsection{Error Handling}
225
226When an error occurs, the interpreter prints an error
227message and a stack trace. In interactive mode, it then returns to
228the primary prompt; when input came from a file, it exits with a
229nonzero exit status after printing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000230the stack trace. (Exceptions handled by an {\tt except} clause in a
231{\tt try} statement are not errors in this context.) Some errors are
232unconditionally fatal and cause an exit with a nonzero exit; this
233applies to internal inconsistencies and some cases of running out of
234memory. All error messages are written to the standard error stream;
235normal output from the executed commands is written to standard
236output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000237
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000238Typing the interrupt character (usually Control-C or DEL) to the
239primary or secondary prompt cancels the input and returns to the
240primary prompt.%
241\footnote{
242 A problem with the GNU Readline package may prevent this.
243}
244Typing an interrupt while a command is executing raises the {\tt
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000245KeyboardInterrupt} exception, which may be handled by a {\tt try}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000246statement.
247
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000248\subsection{The Module Search Path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000249
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000250When a module named {\tt foo} is imported, the interpreter searches
251for a file named {\tt foo.py} in the list of directories specified by
252the environment variable {\tt PYTHONPATH}. It has the same syntax as
253the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
254directory names. When {\tt PYTHONPATH} is not set, an
255installation-dependent default path is used, usually {\tt
256.:/usr/local/lib/python}.
257
258Actually, modules are searched in the list of directories given by the
259variable {\tt sys.path} which is initialized from {\tt PYTHONPATH} or
260the installation-dependent default. This allows Python programs that
261know what they're doing to modify or replace the module search path.
262See the section on Standard Modules later.
263
264\subsection{``Compiled'' Python files}
265
266As an important speed-up of the start-up time for short programs that
267use a lot of standard modules, if a file called {\tt foo.pyc} exists
268in the directory where {\tt foo.py} is found, this is assumed to
269contain an already-``compiled'' version of the module {\tt foo}. The
270modification time of the version of {\tt foo.py} used to create {\tt
271foo.pyc} is recorded in {\tt foo.pyc}, and the file is ignored if
272these don't match.
273
274Whenever {\tt foo.py} is successfully compiled, an attempt is made to
275write the compiled version to {\tt foo.pyc}. It is not an error if
276this attempt fails; if for any reason the file is not written
277completely, the resulting {\tt foo.pyc} file will be recognized as
278invalid and thus ignored later.
279
280\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000281
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000282On BSD'ish {\UNIX} systems, Python scripts can be made directly
283executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000284
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000285\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000286#! /usr/local/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000287\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000288%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000289(assuming that's the name of the interpreter) at the beginning of the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000290script and giving the file an executable mode. The {\tt \#!} must be
291the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000292
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000293\section{Interactive Input Editing and History Substitution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000294
Guido van Rossum4410c751991-06-04 20:22:18 +0000295Some versions of the Python interpreter support editing of the current
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000296input line and history substitution, similar to facilities found in
297the Korn shell and the GNU Bash shell. This is implemented using the
298{\em GNU\ Readline} library, which supports Emacs-style and vi-style
299editing. This library has its own documentation which I won't
300duplicate here; however, the basics are easily explained.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000301
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000302Perhaps the quickest check to see whether command line editing is
303supported is typing Control-P to the first Python prompt you get. If
304it beeps, you have command line editing. If nothing appears to
305happen, or if \verb/^P/ is echoed, you can skip the rest of this
306section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000307
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000308\subsection{Line Editing}
309
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000310If supported, input line editing is active whenever the interpreter
311prints a primary or secondary prompt. The current line can be edited
312using the conventional Emacs control characters. The most important
313of these are: C-A (Control-A) moves the cursor to the beginning of the
314line, C-E to the end, C-B moves it one position to the left, C-F to
315the right. Backspace erases the character to the left of the cursor,
316C-D the character to its right. C-K kills (erases) the rest of the
317line to the right of the cursor, C-Y yanks back the last killed
318string. C-underscore undoes the last change you made; it can be
319repeated for cumulative effect.
320
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000321\subsection{History Substitution}
322
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000323History substitution works as follows. All non-empty input lines
324issued are saved in a history buffer, and when a new prompt is given
325you are positioned on a new line at the bottom of this buffer. C-P
326moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000327Any line in the history buffer can be edited; an asterisk appears in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000328front of the prompt to mark a line as modified. Pressing the Return
329key passes the current line to the interpreter. C-R starts an
330incremental reverse search; C-S starts a forward search.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000331
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000332\subsection{Key Bindings}
333
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000334The key bindings and some other parameters of the Readline library can
335be customized by placing commands in an initialization file called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000336{\tt \$HOME/.inputrc}. Key bindings have the form
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000337
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000338\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000339key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000340\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000341%
342or
343
344\bcode\begin{verbatim}
345"string": function-name
346\end{verbatim}\ecode
347%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000348and options can be set with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000349
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000350\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000351set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000352\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000353%
354For example:
355
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000356\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000357# I prefer vi-style editing:
358set editing-mode vi
359# Edit using a single line:
360set horizontal-scroll-mode On
361# Rebind some keys:
362Meta-h: backward-kill-word
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000363"\C-u": universal-argument
364"\C-x\C-r": re-read-init-file
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000365\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000366%
Guido van Rossum4410c751991-06-04 20:22:18 +0000367Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000368instead of Readline's default filename completion function. If you
369insist, you can override this by putting
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000370
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000371\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000372TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000373\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000374%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000375in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000376indented continuation lines...)
377
378\subsection{Commentary}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000379
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000380This facility is an enormous step forward compared to previous
381versions of the interpreter; however, some wishes are left: It would
382be nice if the proper indentation were suggested on continuation lines
383(the parser knows if an indent token is required next). The
384completion mechanism might use the interpreter's symbol table. A
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000385command to check (or even suggest) matching parentheses, quotes etc.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000386would also be useful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000387
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000388\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000389
390In the following examples, input and output are distinguished by the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000391presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
392the example, you must type everything after the prompt, when the
393prompt appears; lines that do not begin with a prompt are output from
394the interpreter.%
395\footnote{
396 I'd prefer to use different fonts to distinguish input
397 from output, but the amount of LaTeX hacking that would require
398 is currently beyond my ability.
399}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000400Note that a secondary prompt on a line by itself in an example means
401you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000402
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000403\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000404
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000405Let's try some simple Python commands. Start the interpreter and wait
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000406for the primary prompt, {\tt >>>}. (It shouldn't take long.)
407
408\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000409
410The interpreter acts as a simple calculator: you can type an
411expression at it and it will write the value. Expression syntax is
412straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000413work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000414can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000415
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000416\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000417>>> # This is a comment
418>>> 2+2
4194
420>>>
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000421>>> (50-5*6)/4
4225
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000423>>> # Division truncates towards zero:
424>>> 7/3
4252
426>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000427\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000428%
429Like in C, the equal sign ({\tt =}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000430variable. The value of an assignment is not written:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000431
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000432\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000433>>> width = 20
434>>> height = 5*9
435>>> width * height
436900
437>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000438\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000439%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000440A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000441
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000442\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000443>>> # Zero x, y and z
444>>> x = y = z = 0
445>>>
446\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000447%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000448There is full support for floating point; operators with mixed type
449operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000450
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000451\bcode\begin{verbatim}
452>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004533.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000454>>> 7.0 / 2
4553.5
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000456>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000457\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000458
459\subsection{Strings}
460
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000461Besides numbers, Python can also manipulate strings, enclosed in
462single quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000463
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000464\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000465>>> 'foo bar'
466'foo bar'
467>>> 'doesn\'t'
468'doesn\'t'
469>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000470\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000471%
472Strings are written the same way as they are typed for input: inside
473quotes and with quotes and other funny characters escaped by
474backslashes, to show the precise value. (The {\tt print} statement,
475described later, can be used to write strings without quotes or
476escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000477
478Strings can be concatenated (glued together) with the {\tt +}
479operator, and repeated with {\tt *}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000480
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000481\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000482>>> word = 'Help' + 'A'
483>>> word
484'HelpA'
485>>> '<' + word*5 + '>'
486'<HelpAHelpAHelpAHelpAHelpA>'
487>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000488\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000489%
490Strings can be subscripted (indexed); like in C, the first character of
491a string has subscript (index) 0.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000492
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000493There is no separate character type; a character is simply a string of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000494size one. Like in Icon, substrings can be specified with the {\em
495slice} notation: two indices separated by a colon.
496
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000497\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000498>>> word[4]
499'A'
500>>> word[0:2]
501'He'
502>>> word[2:4]
503'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000504>>>
505\end{verbatim}\ecode
506%
507Slice indices have useful defaults; an omitted first index defaults to
508zero, an omitted second index defaults to the size of the string being
509sliced.
510
511\bcode\begin{verbatim}
512>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000513'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000514>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000515'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000516>>>
517\end{verbatim}\ecode
518%
519Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
520equals \verb\s\.
521
522\bcode\begin{verbatim}
523>>> word[:2] + word[2:]
524'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000525>>> word[:3] + word[3:]
526'HelpA'
527>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000528\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000529%
530Degenerate slice indices are handled gracefully: an index that is too
531large is replaced by the string size, an upper bound smaller than the
532lower bound returns an empty string.
533
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000534\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000535>>> word[1:100]
536'elpA'
537>>> word[10:]
538''
539>>> word[2:1]
540''
541>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000542\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000543%
544Indices may be negative numbers, to start counting from the right.
545For example:
546
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000547\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000548>>> word[-1] # The last character
549'A'
550>>> word[-2] # The last-but-one character
551'p'
552>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000553'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000554>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000555'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000556>>>
557\end{verbatim}\ecode
558%
559But note that -0 is really the same as 0, so it does not count from
560the right!
561
562\bcode\begin{verbatim}
563>>> word[-0] # (since -0 equals 0)
564'H'
565>>>
566\end{verbatim}\ecode
567%
568Out-of-range negative slice indices are truncated, but don't try this
569for single-element (non-slice) indices:
570
571\bcode\begin{verbatim}
572>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000573'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000574>>> word[-10] # error
575Unhandled exception: IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000576>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000577\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000578%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000579The best way to remember how slices work is to think of the indices as
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000580pointing {\em between} characters, with the left edge of the first
581character numbered 0. Then the right edge of the last character of a
582string of {\tt n} characters has index {\tt n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000583
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000584\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000585 +---+---+---+---+---+
586 | H | e | l | p | A |
587 +---+---+---+---+---+
588 0 1 2 3 4 5
589-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000590\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000591%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000592The first row of numbers gives the position of the indices 0...5 in
593the string; the second row gives the corresponding negative indices.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000594The slice from \verb\i\ to \verb\j\ consists of all characters between
595the edges labeled \verb\i\ and \verb\j\, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000596
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000597For nonnegative indices, the length of a slice is the difference of
598the indices, if both are within bounds, e.g., the length of
599\verb\word[1:3]\ is 2.
600
601The built-in function {\tt len()} returns the length of a string:
602
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000603\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000604>>> s = 'supercalifragilisticexpialidocious'
605>>> len(s)
60634
607>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000608\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000609
610\subsection{Lists}
611
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000612Python knows a number of {\em compound} data types, used to group
613together other values. The most versatile is the {\em list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000614can be written as a list of comma-separated values (items) between
615square brackets. List items need not all have the same type.
616
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000617\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000618>>> a = ['foo', 'bar', 100, 1234]
619>>> a
620['foo', 'bar', 100, 1234]
621>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000622\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000623%
624Like string indices, list indices start at 0, and lists can be sliced,
625concatenated and so on:
626
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000627\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000628>>> a[0]
629'foo'
630>>> a[3]
6311234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000632>>> a[-2]
633100
634>>> a[1:-1]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000635['bar', 100]
636>>> a[:2] + ['bletch', 2*2]
637['foo', 'bar', 'bletch', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000638>>> 3*a[:3] + ['Boe!']
639['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000640>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000641\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000642%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000643Unlike strings, which are {\em immutable}, it is possible to change
644individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000645
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000646\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000647>>> a
648['foo', 'bar', 100, 1234]
649>>> a[2] = a[2] + 23
650>>> a
651['foo', 'bar', 123, 1234]
652>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000653\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000654%
655Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000656of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000657
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000658\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000659>>> # Replace some items:
660>>> a[0:2] = [1, 12]
661>>> a
662[1, 12, 123, 1234]
663>>> # Remove some:
664>>> a[0:2] = []
665>>> a
666[123, 1234]
667>>> # Insert some:
668>>> a[1:1] = ['bletch', 'xyzzy']
669>>> a
670[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000671>>> a[:0] = a # Insert (a copy of) itself at the beginning
672>>> a
673[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000674>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000675\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000676%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000677The built-in function {\tt len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000678
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000679\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000680>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00006818
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000682>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000683\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000684%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000685It is possible to nest lists (create lists containing other lists),
686for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000687
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000688\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000689>>> q = [2, 3]
690>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000691>>> len(p)
6923
693>>> p[1]
694[2, 3]
695>>> p[1][0]
6962
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000697>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000698>>> p
699[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000700>>> q
701[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000702>>>
703\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000704%
705Note that in the last example, {\tt p[1]} and {\tt q} really refer to
706the same object! We'll come back to {\em object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000707
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000708\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000709
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000710Of course, we can use Python for more complicated tasks than adding
711two and two together. For instance, we can write an initial
712subsequence of the {\em Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000713
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000714\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000715>>> # Fibonacci series:
716>>> # the sum of two elements defines the next
717>>> a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000718>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000719... print b
720... a, b = b, a+b
721...
7221
7231
7242
7253
7265
7278
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000728>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000729\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000730%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000731This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000732
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000733\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000734
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000735\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000736The first line contains a {\em multiple assignment}: the variables
737{\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
738last line this is used again, demonstrating that the expressions on
739the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000740assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000741
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000742\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000743The {\tt while} loop executes as long as the condition (here: {\tt b <
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000744100}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000745true; zero is false. The condition may also be a string or list value,
746in fact any sequence; anything with a non-zero length is true, empty
747sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000748comparison. The standard comparison operators are written the same as
749in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000750
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000751\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000752The {\em body} of the loop is {\em indented}: indentation is Python's
753way of grouping statements. Python does not (yet!) provide an
754intelligent input line editing facility, so you have to type a tab or
755space(s) for each indented line. In practice you will prepare more
756complicated input for Python with a text editor; most text editors have
757an auto-indent facility. When a compound statement is entered
758interactively, it must be followed by a blank line to indicate
759completion (since the parser cannot guess when you have typed the last
760line).
761
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000762\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000763The {\tt print} statement writes the value of the expression(s) it is
764given. It differs from just writing the expression you want to write
765(as we did earlier in the calculator examples) in the way it handles
766multiple expressions and strings. Strings are written without quotes,
767and a space is inserted between items, so you can format things nicely,
768like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000769
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000770\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000771>>> i = 256*256
772>>> print 'The value of i is', i
773The value of i is 65536
774>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000775\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000776%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000777A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000778
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000779\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000780>>> a, b = 0, 1
781>>> while b < 1000:
782... print b,
783... a, b = b, a+b
784...
7851 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
786>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000787\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000788%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000789Note that the interpreter inserts a newline before it prints the next
790prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000791
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000792\end{itemize}
793
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000794\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000795
Guido van Rossum4410c751991-06-04 20:22:18 +0000796Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000797usual control flow statements known from other languages, with some
798twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000799
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000800\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000801
802Perhaps the most well-known statement type is the {\tt if} statement.
803For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000804
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000805\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000806>>> if x < 0:
807... x = 0
808... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000809... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000810... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000811... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000812... print 'Single'
813... else:
814... print 'More'
815...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000816\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000817%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000818There can be zero or more {\tt elif} parts, and the {\tt else} part is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000819optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
820useful to avoid excessive indentation. An {\tt if...elif...elif...}
821sequence is a substitute for the {\em switch} or {\em case} statements
822found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000823
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000824\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000825
Guido van Rossum4410c751991-06-04 20:22:18 +0000826The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000827used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000828arithmetic progression of numbers (like in Pascal), or leaving the user
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000829completely free in the iteration test and step (as C), Python's {\tt
830for} statement iterates over the items of any sequence (e.g., a list
831or a string), in the order that they appear in the sequence. For
832example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000833
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000834\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000835>>> # Measure some strings:
836>>> a = ['cat', 'window', 'defenestrate']
837>>> for x in a:
838... print x, len(x)
839...
840cat 3
841window 6
842defenestrate 12
843>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000844\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000845%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000846It is not safe to modify the sequence being iterated over in the loop
847(this can only happen for mutable sequence types, i.e., lists). If
848you need to modify the list you are iterating over, e.g., duplicate
849selected items, you must iterate over a copy. The slice notation
850makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000851
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000852\bcode\begin{verbatim}
853>>> for x in a[:]: # make a slice copy of the entire list
854... if len(x) > 6: a.insert(0, x)
855...
856>>> a
857['defenestrate', 'cat', 'window', 'defenestrate']
858>>>
859\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000860
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000861\section{The {\tt range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000862
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000863If you do need to iterate over a sequence of numbers, the built-in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000864function {\tt range()} comes in handy. It generates lists containing
865arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000866
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000867\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000868>>> range(10)
869[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
870>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000871\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000872%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000873The given end point is never part of the generated list; {\tt range(10)}
874generates a list of 10 values, exactly the legal indices for items of a
875sequence of length 10. It is possible to let the range start at another
876number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000877
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000878\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000879>>> range(5, 10)
880[5, 6, 7, 8, 9]
881>>> range(0, 10, 3)
882[0, 3, 6, 9]
883>>> range(-10, -100, -30)
884[-10, -40, -70]
885>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000886\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000887%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000888To iterate over the indices of a sequence, combine {\tt range()} and
889{\tt len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000890
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000891\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000892>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000893>>> for i in range(len(a)):
894... print i, a[i]
895...
8960 Mary
8971 had
8982 a
8993 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00009004 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000901>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000902\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000903
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000904\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000905
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000906The {\tt break} statement, like in C, breaks out of the smallest
907enclosing {\tt for} or {\tt while} loop.
908
909The {\tt continue} statement, also borrowed from C, continues with the
910next iteration of the loop.
911
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000912Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000913loop terminates through exhaustion of the list (with {\tt for}) or when
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000914the condition becomes false (with {\tt while}), but not when the loop is
915terminated by a {\tt break} statement. This is exemplified by the
916following loop, which searches for a list item of value 0:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000917
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000918\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000919>>> for n in range(2, 10):
920... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000921... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000922... print n, 'equals', x, '*', n/x
923... break
924... else:
925... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000926...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009272 is a prime number
9283 is a prime number
9294 equals 2 * 2
9305 is a prime number
9316 equals 2 * 3
9327 is a prime number
9338 equals 2 * 4
9349 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000935>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000936\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000937
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000938\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000939
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000940The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000941It can be used when a statement is required syntactically but the
942program requires no action.
943For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000944
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000945\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000946>>> while 1:
947... pass # Busy-wait for keyboard interrupt
948...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000949\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000950
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000951\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000952
953We can create a function that writes the Fibonacci series to an
954arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000955
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000956\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000957>>> def fib(n): # write Fibonacci series up to n
958... a, b = 0, 1
959... while b <= n:
960... print b,
961... a, b = b, a+b
962...
963>>> # Now call the function we just defined:
964>>> fib(2000)
9651 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
966>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000967\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000968%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000969The keyword {\tt def} introduces a function {\em definition}. It must
970be followed by the function name and the parenthesized list of formal
971parameters. The statements that form the body of the function starts at
972the next line, indented by a tab stop.
973
974The {\em execution} of a function introduces a new symbol table used
975for the local variables of the function. More precisely, all variable
976assignments in a function store the value in the local symbol table;
977whereas
978 variable references first look in the local symbol table, then
979in the global symbol table, and then in the table of built-in names.
980Thus,
981global variables cannot be directly assigned to from within a
982function, although they may be referenced.
983
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000984The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000985the local symbol table of the called function when it is called; thus,
986arguments are passed using {\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000987\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000988 Actually, {\em call by object reference} would be a better
989 description, since if a mutable object is passed, the caller
990 will see any changes the callee makes to it (e.g., items
991 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000992}
993When a function calls another function, a new local symbol table is
994created for that call.
995
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000996A function definition introduces the function name in the
997current
998symbol table. The value
999of the function name
1000has a type that is recognized by the interpreter as a user-defined
1001function. This value can be assigned to another name which can then
1002also be used as a function. This serves as a general renaming
1003mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001004
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001005\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001006>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001007<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001008>>> f = fib
1009>>> f(100)
10101 1 2 3 5 8 13 21 34 55 89
1011>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001012\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001013%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001014You might object that {\tt fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001015Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001016value. In fact, technically speaking, procedures do return a value,
1017albeit a rather boring one. This value is called {\tt None} (it's a
1018built-in name). Writing the value {\tt None} is normally suppressed by
1019the interpreter if it would be the only value written. You can see it
1020if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001021
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001022\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001023>>> print fib(0)
1024None
1025>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001026\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001027%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001028It is simple to write a function that returns a list of the numbers of
1029the Fibonacci series, instead of printing it:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001030
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001031\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001032>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001033... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001034... a, b = 0, 1
1035... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001036... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001037... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001038... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001039...
1040>>> f100 = fib2(100) # call it
1041>>> f100 # write the result
1042[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1043>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001044\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001045%
Guido van Rossum4410c751991-06-04 20:22:18 +00001046This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001047
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001048\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001049
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001050\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001051The {\tt return} statement returns with a value from a function. {\tt
1052return} without an expression argument is used to return from the middle
1053of a procedure (falling off the end also returns from a proceduce), in
1054which case the {\tt None} value is returned.
1055
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001056\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001057The statement {\tt result.append(b)} calls a {\em method} of the list
1058object {\tt result}. A method is a function that `belongs' to an
1059object and is named {\tt obj.methodname}, where {\tt obj} is some
1060object (this may be an expression), and {\tt methodname} is the name
1061of a method that is defined by the object's type. Different types
1062define different methods. Methods of different types may have the
1063same name without causing ambiguity. (It is possible to define your
1064own object types and methods, using {\em classes}. This is an
1065advanced feature that is not discussed in this tutorial.)
1066The method {\tt append} shown in the example, is defined for
1067list objects; it adds a new element at the end of the list. In this
1068example
1069it is equivalent to {\tt result = result + [b]}, but more efficient.
1070
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001071\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001072
1073\chapter{Odds and Ends}
1074
1075This chapter describes some things you've learned about already in
1076more detail, and adds some new things as well.
1077
1078\section{More on Lists}
1079
1080The list data type has some more methods. Here are all of the methods
1081of lists objects:
1082
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001083\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001084
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001085\item[{\tt insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001086Insert an item at a given position. The first argument is the index of
1087the element before which to insert, so {\tt a.insert(0, x)} inserts at
1088the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
1089{\tt a.append(x)}.
1090
1091\item[{\tt append(x)}]
1092Equivalent to {\tt a.insert(len(a), x)}.
1093
1094\item[{\tt index(x)}]
1095Return the index in the list of the first item whose value is {\tt x}.
1096It is an error if there is no such item.
1097
1098\item[{\tt remove(x)}]
1099Remove the first item from the list whose value is {\tt x}.
1100It is an error if there is no such item.
1101
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001102\item[{\tt sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001103Sort the items of the list, in place.
1104
1105\item[{\tt reverse()}]
1106Reverse the elements of the list, in place.
1107
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001108\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001109
1110An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001111
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001112\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001113>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001114>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001115>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001116>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001117[66.6, 333, -1, 333, 1, 1234.5, 333]
1118>>> a.index(333)
11191
1120>>> a.remove(333)
1121>>> a
1122[66.6, -1, 333, 1, 1234.5, 333]
1123>>> a.reverse()
1124>>> a
1125[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001126>>> a.sort()
1127>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001128[-1, 1, 66.6, 333, 333, 1234.5]
1129>>>
1130\end{verbatim}\ecode
1131
1132\section{The {\tt del} statement}
1133
1134There is a way to remove an item from a list given its index instead
1135of its value: the {\tt del} statement. This can also be used to
1136remove slices from a list (which we did earlier by assignment of an
1137empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001138
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001139\bcode\begin{verbatim}
1140>>> a
1141[-1, 1, 66.6, 333, 333, 1234.5]
1142>>> del a[0]
1143>>> a
1144[1, 66.6, 333, 333, 1234.5]
1145>>> del a[2:4]
1146>>> a
1147[1, 66.6, 1234.5]
1148>>>
1149\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001150%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001151{\tt del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001152
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001153\bcode\begin{verbatim}
1154>>> del a
1155>>>
1156\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001157%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001158Referencing the name {\tt a} hereafter is an error (at least until
1159another value is assigned to it). We'll find other uses for {\tt del}
1160later.
1161
1162\section{Tuples and Sequences}
1163
1164We saw that lists and strings have many common properties, e.g.,
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001165indexinging and slicing operations. They are two examples of {\em
1166sequence} data types. Since Python is an evolving language, other
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001167sequence data types may be added. There is also another standard
1168sequence data type: the {\em tuple}.
1169
1170A tuple consists of a number of values separated by commas, for
1171instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001172
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001173\bcode\begin{verbatim}
1174>>> t = 12345, 54321, 'hello!'
1175>>> t[0]
117612345
1177>>> t
1178(12345, 54321, 'hello!')
1179>>> # Tuples may be nested:
1180>>> u = t, (1, 2, 3, 4, 5)
1181>>> u
1182((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1183>>>
1184\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001185%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001186As you see, on output tuples are alway enclosed in parentheses, so
1187that nested tuples are interpreted correctly; they may be input with
1188or without surrounding parentheses, although often parentheses are
1189necessary anyway (if the tuple is part of a larger expression).
1190
1191Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1192from a database, etc. Tuples, like strings, are immutable: it is not
1193possible to assign to the individual items of a tuple (you can
1194simulate much of the same effect with slicing and concatenation,
1195though).
1196
1197A special problem is the construction of tuples containing 0 or 1
1198items: the syntax has some extra quirks to accomodate these. Empty
1199tuples are constructed by an empty pair of parentheses; a tuple with
1200one item is constructed by following a value with a comma
1201(it is not sufficient to enclose a single value in parentheses).
1202Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001203
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001204\bcode\begin{verbatim}
1205>>> empty = ()
1206>>> singleton = 'hello', # <-- note trailing comma
1207>>> len(empty)
12080
1209>>> len(singleton)
12101
1211>>> singleton
1212('hello',)
1213>>>
1214\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001215%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001216The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
1217tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
1218are packed together in a tuple. The reverse operation is also
1219possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001220
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001221\bcode\begin{verbatim}
1222>>> x, y, z = t
1223>>>
1224\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001225%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001226This is called, appropriately enough, {\em tuple unpacking}. Tuple
1227unpacking requires that the list of variables on the left has the same
1228number of elements as the length of the tuple. Note that multiple
1229assignment is really just a combination of tuple packing and tuple
1230unpacking!
1231
1232Occasionally, the corresponding operation on lists is useful: {\em list
1233unpacking}. This is supported by enclosing the list of variables in
1234square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001235
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001236\bcode\begin{verbatim}
1237>>> a = ['foo', 'bar', 100, 1234]
1238>>> [a1, a2, a3, a4] = a
1239>>>
1240\end{verbatim}\ecode
1241
1242\section{Dictionaries}
1243
1244Another useful data type built into Python is the {\em dictionary}.
1245Dictionaries are sometimes found in other languages as ``associative
1246memories'' or ``associative arrays''. Unlike sequences, which are
1247indexed by a range of numbers, dictionaries are indexed by {\em keys},
1248which are strings. It is best to think of a dictionary as an unordered set of
1249{\em key:value} pairs, with the requirement that the keys are unique
1250(within one dictionary).
1251A pair of braces creates an empty dictionary: \verb/{}/.
1252Placing a comma-separated list of key:value pairs within the
1253braces adds initial key:value pairs to the dictionary; this is also the
1254way dictionaries are written on output.
1255
1256The main operations on a dictionary are storing a value with some key
1257and extracting the value given the key. It is also possible to delete
1258a key:value pair
1259with {\tt del}.
1260If you store using a key that is already in use, the old value
1261associated with that key is forgotten. It is an error to extract a
1262value using a non-existant key.
1263
1264The {\tt keys()} method of a dictionary object returns a list of all the
1265keys used in the dictionary, in random order (if you want it sorted,
1266just apply the {\tt sort()} method to the list of keys). To check
1267whether a single key is in the dictionary, use the \verb/has_key()/
1268method of the dictionary.
1269
1270Here is a small example using a dictionary:
1271
1272\bcode\begin{verbatim}
1273>>> tel = {'jack': 4098, 'sape': 4139}
1274>>> tel['guido'] = 4127
1275>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001276{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001277>>> tel['jack']
12784098
1279>>> del tel['sape']
1280>>> tel['irv'] = 4127
1281>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001282{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001283>>> tel.keys()
1284['guido', 'irv', 'jack']
1285>>> tel.has_key('guido')
12861
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001287>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001288\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001289
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001290\section{More on Conditions}
1291
1292The conditions used in {\tt while} and {\tt if} statements above can
1293contain other operators besides comparisons.
1294
1295The comparison operators {\tt in} and {\tt not in} check whether a value
1296occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
1297is not} compare whether two objects are really the same object; this
1298only matters for mutable objects like lists. All comparison operators
1299have the same priority, which is lower than that of all numerical
1300operators.
1301
1302Comparisons can be chained: e.g., {\tt a < b = c} tests whether {\tt a}
1303is less than {\tt b} and moreover {\tt b} equals {\tt c}.
1304
1305Comparisons may be combined by the Boolean operators {\tt and} and {\tt
1306or}, and the outcome of a comparison (or of any other Boolean
1307expression) may be negated with {\tt not}. These all have lower
1308priorities than comparison operators again; between them, {\tt not} has
1309the highest priority, and {\tt or} the lowest, so that
1310{\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
1311course, parentheses can be used to express the desired composition.
1312
1313The Boolean operators {\tt and} and {\tt or} are so-called {\em
1314shortcut} operators: their arguments are evaluated from left to right,
1315and evaluation stops as soon as the outcome is determined. E.g., if
1316{\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
1317does not evaluate the expression C. In general, the return value of a
1318shortcut operator, when used as a general value and not as a Boolean, is
1319the last evaluated argument.
1320
1321It is possible to assign the result of a comparison or other Boolean
1322expression to a variable, but you must enclose the entire Boolean
1323expression in parentheses. This is necessary because otherwise an
1324assignment like \verb/a = b = c/ would be ambiguous: does it assign the
1325value of {\tt c} to {\tt a} and {\tt b}, or does it compare {\tt b} to
1326{\tt c} and assign the outcome (0 or 1) to {\tt a}? As it is, the first
1327meaning is what you get, and to get the latter you have to write
1328\verb/a = (b = c)/. (In Python, unlike C, assignment cannot occur
1329inside expressions.)
1330
1331\section{Comparing Sequences and Other Types}
1332
1333Sequence objects may be compared to other objects with the same
1334sequence type. The comparison uses {\em lexicographical} ordering:
1335first the first two items are compared, and if they differ this
1336determines the outcome of the comparison; if they are equal, the next
1337two items are compared, and so on, until either sequence is exhausted.
1338If two items to be compared are themselves sequences of the same type,
1339the lexiographical comparison is carried out recursively. If all
1340items of two sequences compare equal, the sequences are considered
1341equal. If one sequence is an initial subsequence of the other, the
1342shorted sequence is the smaller one. Lexicographical ordering for
1343strings uses the ASCII ordering for individual characters. Some
1344examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001345
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001346\bcode\begin{verbatim}
1347(1, 2, 3) < (1, 2, 4)
1348[1, 2, 3] < [1, 2, 4]
1349'ABC' < 'C' < 'Pascal' < 'Python'
1350(1, 2, 3, 4) < (1, 2, 4)
1351(1, 2) < (1, 2, -1)
1352(1, 2, 3) = (1.0, 2.0, 3.0)
1353(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1354\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001355%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001356Note that comparing objects of different types is legal. The outcome
1357is deterministic but arbitrary: the types are ordered by their name.
1358Thus, a list is always smaller than a string, a string is always
1359smaller than a tuple, etc. Mixed numeric types are compared according
1360to their numeric value, so 0 equals 0.0, etc.%
1361\footnote{
1362 The rules for comparing objects of different types should
1363 not be relied upon; they may change in a future version of
1364 the language.
1365}
1366
1367\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001368
Guido van Rossum4410c751991-06-04 20:22:18 +00001369If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001370definitions you have made (functions and variables) are lost.
1371Therefore, if you want to write a somewhat longer program, you are
1372better off using a text editor to prepare the input for the interpreter
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001373and run it with that file as input instead. This is known as creating a
1374{\em script}. As your program gets longer, you may want to split it
1375into several files for easier maintenance. You may also want to use a
1376handy function that you've written in several programs without copying
1377its definition into each program.
1378
Guido van Rossum4410c751991-06-04 20:22:18 +00001379To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001380them in a script or in an interactive instance of the interpreter.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001381Such a file is called a {\em module}; definitions from a module can be
1382{\em imported} into other modules or into the {\em main} module (the
1383collection of variables that you have access to in a script
1384executed at the top level
1385and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001386
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001387A module is a file containing Python definitions and statements. The
1388file name is the module name with the suffix {\tt .py} appended. For
1389instance, use your favorite text editor to create a file called {\tt
1390fibo.py} in the current directory with the following contents:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001391
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001392\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001393# Fibonacci numbers module
1394
1395def fib(n): # write Fibonacci series up to n
1396 a, b = 0, 1
1397 while b <= n:
1398 print b,
1399 a, b = b, a+b
1400
1401def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001402 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001403 a, b = 0, 1
1404 while b <= n:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001405 result.append(b)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001406 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001407 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001408\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001409%
Guido van Rossum4410c751991-06-04 20:22:18 +00001410Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001411following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001412
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001413\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001414>>> import fibo
1415>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001416\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001417%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001418This does not enter the names of the functions defined in
1419{\tt fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001420directly in the current symbol table; it only enters the module name
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001421{\tt fibo}
1422there.
1423Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001424
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001425\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001426>>> fibo.fib(1000)
14271 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1428>>> fibo.fib2(100)
1429[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1430>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001431\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001432%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001433If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001434
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001435\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001436>>> fib = fibo.fib
1437>>> fib(500)
14381 1 2 3 5 8 13 21 34 55 89 144 233 377
1439>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001440\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001441
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001442\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001443
1444A module can contain executable statements as well as function
1445definitions.
1446These statements are intended to initialize the module.
1447They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001448{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001449time the module is imported somewhere.%
1450\footnote{
1451 In fact function definitions are also `statements' that are
1452 `executed'; the execution enters the function name in the
1453 module's global symbol table.
1454}
1455
1456Each module has its own private symbol table, which is used as the
1457global symbol table by all functions defined in the module.
1458Thus, the author of a module can use global variables in the module
1459without worrying about accidental clashes with a user's global
1460variables.
1461On the other hand, if you know what you are doing you can touch a
1462module's global variables with the same notation used to refer to its
1463functions,
1464{\tt modname.itemname}.
1465
1466Modules can import other modules.
1467It is customary but not required to place all
1468{\tt import}
1469statements at the beginning of a module (or script, for that matter).
1470The imported module names are placed in the importing module's global
1471symbol table.
1472
1473There is a variant of the
1474{\tt import}
1475statement that imports names from a module directly into the importing
1476module's symbol table.
1477For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001478
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001479\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001480>>> from fibo import fib, fib2
1481>>> fib(500)
14821 1 2 3 5 8 13 21 34 55 89 144 233 377
1483>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001484\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001485%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001486This does not introduce the module name from which the imports are taken
1487in the local symbol table (so in the example, {\tt fibo} is not
1488defined).
1489
1490There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001491
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001492\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001493>>> from fibo import *
1494>>> fib(500)
14951 1 2 3 5 8 13 21 34 55 89 144 233 377
1496>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001497\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001498%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001499This imports all names except those beginning with an underscore
Guido van Rossum573805a1992-03-06 10:56:03 +00001500({\tt _}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001501
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001502\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001503
Guido van Rossum4410c751991-06-04 20:22:18 +00001504Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001505document (Python Library Reference). Some modules are built into the
1506interpreter; these provide access to operations that are not part of the
1507core of the language but are nevertheless built in, either for
1508efficiency or to provide access to operating system primitives such as
1509system calls. The set of such modules is a configuration option; e.g.,
1510the {\tt amoeba} module is only provided on systems that somehow support
1511Amoeba primitives. One particular module deserves some attention: {\tt
1512sys}, which is built into every Python interpreter. The variables {\tt
1513sys.ps1} and {\tt sys.ps2} define the strings used as primary and
1514secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001515
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001516\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001517>>> import sys
1518>>> sys.ps1
1519'>>> '
1520>>> sys.ps2
1521'... '
1522>>> sys.ps1 = 'C> '
1523C> print 'Yuck!'
1524Yuck!
1525C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001526\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001527%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001528These two variables are only defined if the interpreter is in
1529interactive mode.
1530
1531The variable
1532{\tt sys.path}
1533is a list of strings that determine the interpreter's search path for
1534modules.
1535It is initialized to a default path taken from the environment variable
1536{\tt PYTHONPATH},
1537or from a built-in default if
1538{\tt PYTHONPATH}
1539is not set.
1540You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001541
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001542\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001543>>> import sys
1544>>> sys.path.append('/ufs/guido/lib/python')
1545>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001546\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001547
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001548\section{The {\tt dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001549
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001550The built-in function {\tt dir} is used to find out which names a module
1551defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001552
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001553\bcode\begin{verbatim}
1554>>> import fibo, sys
1555>>> dir(fibo)
1556['fib', 'fib2']
1557>>> dir(sys)
1558['argv', 'exit', 'modules', 'path', 'ps1', 'ps2', 'stderr', 'stdin', 'stdout']
1559>>>
1560\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001561%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001562Without arguments, {\tt dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001563
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001564\bcode\begin{verbatim}
1565>>> a = [1, 2, 3, 4, 5]
1566>>> import fibo, sys
1567>>> fib = fibo.fib
1568>>> dir()
1569['a', 'fib', 'fibo', 'sys']
1570>>>
1571\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001572%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001573Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001574
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001575{\tt dir()} does not list the names of built-in functions and variables.
1576If you want a list of those, they are defined in the standard module
1577{\tt builtin}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001578
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001579\bcode\begin{verbatim}
1580>>> import builtin
1581>>> dir(builtin)
1582['EOFError', 'KeyboardInterrupt', 'MemoryError', 'NameError', 'None', 'Runti
1583meError', 'SystemError', 'TypeError', 'abs', 'chr', 'dir', 'divmod', 'eval',
1584 'exec', 'float', 'input', 'int', 'len', 'long', 'max', 'min', 'open', 'ord'
1585, 'pow', 'range', 'raw_input', 'reload', 'type']
1586>>>
1587\end{verbatim}\ecode
1588
1589\chapter{Output Formatting}
1590
1591So far we've encountered two ways of writing values: {\em expression
1592statements} and the {\tt print} statement. (A third way is using the
1593{\tt write} method of file objects; the standard output file can be
1594referenced as {\tt sys.stdout}. See the Library Reference for more
1595information on this.)
1596
1597Often you'll want more control over the formatting of your output than
1598simply printing space-separated values. The key to nice formatting in
1599Python is to do all the string handling yourself; using string slicing
1600and concatenation operations you can create any lay-out you can imagine.
1601The standard module {\tt string} contains some useful operations for
1602padding strings to a given column width; these will be discussed shortly.
1603
1604One question remains, of course: how do you convert values to strings?
1605Luckily, Python has a way to convert any value to a string: just write
1606the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001607
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001608\bcode\begin{verbatim}
1609>>> x = 10 * 3.14
1610>>> y = 200*200
1611>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1612>>> print s
1613The value of x is 31.4, and y is 40000...
1614>>> # Reverse quotes work on other types besides numbers:
1615>>> p = [x, y]
1616>>> ps = `p`
1617>>> ps
1618'[31.4, 40000]'
1619>>> # Converting a string adds string quotes and backslashes:
1620>>> hello = 'hello, world\n'
1621>>> hellos = `hello`
1622>>> print hellos
1623'hello, world\012'
1624>>> # The argument of reverse quotes may be a tuple:
1625>>> `x, y, ('foo', 'bar')`
1626'(31.4, 40000, (\'foo\', \'bar\'))'
1627>>>
1628\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001629%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001630Here is how you write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001631
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001632\bcode\begin{verbatim}
1633>>> import string
1634>>> for x in range(1, 11):
1635... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1636... # Note trailing comma on previous line
1637... print string.rjust(`x*x*x`, 4)
1638...
1639 1 1 1
1640 2 4 8
1641 3 9 27
1642 4 16 64
1643 5 25 125
1644 6 36 216
1645 7 49 343
1646 8 64 512
1647 9 81 729
164810 100 1000
1649>>>
1650\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001651%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001652(Note that one space between each column was added by the way {\tt print}
1653works: it always adds spaces between its arguments.)
1654
1655This example demonstrates the function {\tt string.rjust()}, which
1656right-justifies a string in a field of a given width by padding it with
1657spaces on the left. There are similar functions {\tt string.ljust()}
1658and {\tt string.center()}. These functions do not write anything, they
1659just return a new string. If the input string is too long, they don't
1660truncate it, but return it unchanged; this will mess up your column
1661lay-out but that's usually better than the alternative, which would be
1662lying about a value. (If you really want truncation you can always add
1663a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1664
1665There is another function, {\tt string.zfill}, which pads a numeric
1666string on the left with zeros. It understands about plus and minus
1667signs:%
1668\footnote{
1669 Better facilities for formatting floating point numbers are
1670 lacking at this moment.
1671}
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001672
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001673\bcode\begin{verbatim}
1674>>> string.zfill('12', 5)
1675'00012'
1676>>> string.zfill('-3.14', 7)
1677'-003.14'
1678>>> string.zfill('3.14159265359', 5)
1679'3.14159265359'
1680>>>
1681\end{verbatim}\ecode
1682
1683\chapter{Errors and Exceptions}
1684
1685Until now error messages haven't been more than mentioned, but if you
1686have tried out the examples you have probably seen some. There are
1687(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1688and {\em exceptions}.
1689
1690\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001691
1692Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001693kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001694
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001695\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001696>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001697Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001698while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001699 ^
1700Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001701>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001702\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001703%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001704The parser repeats the offending line and displays a little `arrow'
1705pointing at the earliest point in the line where the error was detected.
1706The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001707{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001708the arrow: in the example, the error is detected at the keyword
1709{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001710File name and line number are printed so you know where to look in case
1711the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001712
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001713\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001714
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001715Even if a statement or expression is syntactically correct, it may
1716cause an error when an attempt is made to execute it.
1717Errors detected during execution are called {\em exceptions} and are
1718not unconditionally fatal: you will soon learn how to handle them in
1719Python programs. Most exceptions are not handled by programs,
1720however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001721
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001722\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001723>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001724Unhandled exception: run-time error: integer division by zero
1725Stack backtrace (innermost last):
1726 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001727>>> 4 + foo*3
1728Unhandled exception: undefined name: foo
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001729Stack backtrace (innermost last):
1730 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001731>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001732Unhandled exception: type error: illegal argument type for built-in operation
1733Stack backtrace (innermost last):
1734 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001735>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001736\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001737%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001738The first line of the error message indicates what happened.
1739Exceptions come in different types, and the type is printed as part of
1740the message: the types in the example are
1741{\tt run-time error},
1742{\tt undefined name}
1743and
1744{\tt type error}.
1745The rest of the line is a detail whose interpretation depends on the
1746exception type.
1747
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001748The rest of the error message shows the context where the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001749exception happened.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001750In general it contains a stack backtrace listing source lines; however,
1751it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001752
1753Here is a summary of the most common exceptions:
1754\begin{itemize}
1755\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001756{\em Run-time\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001757are generally caused by wrong data used by the program; this can be the
1758programmer's fault or caused by bad input.
1759The detail states the cause of the error in more detail.
1760\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001761{\em Undefined\ name}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001762errors are more serious: these are usually caused by misspelled
1763identifiers.%
1764\footnote{
1765 The parser does not check whether names used in a program are at
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001766 all defined elsewhere in the program; such checks are
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001767 postponed until run-time. The same holds for type checking.
1768}
1769The detail is the offending identifier.
1770\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001771{\em Type\ errors} are also pretty serious: this is another case of
1772using wrong data (or better, using data the wrong way), but here the
1773error can be gleaned from the object type(s) alone. The detail shows
1774in what context the error was detected.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001775\end{itemize}
1776
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001777\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001778
1779It is possible to write programs that handle selected exceptions.
1780Look at the following example, which prints a table of inverses of
1781some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001782
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001783\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001784>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001785>>> for x in numbers:
1786... print x,
1787... try:
1788... print 1.0 / x
1789... except RuntimeError:
1790... print '*** has no inverse ***'
1791...
17920.3333 3.00030003
17932.5 0.4
17940 *** has no inverse ***
179510 0.1
1796>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001797\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001798%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001799The {\tt try} statement works as follows.
1800\begin{itemize}
1801\item
1802First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001803{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001804(the statement(s) between the {\tt try} and {\tt except} keywords) is
1805executed.
1806\item
1807If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001808{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001809is skipped and execution of the {\tt try} statement is finished.
1810\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001811If an exception occurs during execution of the try clause,
1812the rest of the clause is skipped. Then if
1813its type matches the exception named after the {\tt except} keyword,
1814the rest of the try clause is skipped, the except clause is executed,
1815and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001816\item
1817If an exception occurs which does not match the exception named in the
1818except clause, it is passed on to outer try statements; if no handler is
1819found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001820{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001821and execution stops with a message as shown above.
1822\end{itemize}
1823A {\tt try} statement may have more than one except clause, to specify
1824handlers for different exceptions.
1825At most one handler will be executed.
1826Handlers only handle exceptions that occur in the corresponding try
1827clause, not in other handlers of the same {\tt try} statement.
1828An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001829e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001830
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001831\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001832... except (RuntimeError, TypeError, NameError):
1833... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001834\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001835%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001836The last except clause may omit the exception name(s), to serve as a
1837wildcard.
1838Use this with extreme caution!
1839
1840When an exception occurs, it may have an associated value, also known as
1841the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001842{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001843The presence and type of the argument depend on the exception type.
1844For exception types which have an argument, the except clause may
1845specify a variable after the exception name (or list) to receive the
1846argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001847
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001848\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001849>>> try:
1850... foo()
1851... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001852... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001853...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001854name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001855>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001856\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001857%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001858If an exception has an argument, it is printed as the third part
1859(`detail') of the message for unhandled exceptions.
1860
1861Standard exception names are built-in identifiers (not reserved
1862keywords).
1863These are in fact string objects whose
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001864{\em object\ identity}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001865(not their value!) identifies the exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001866The string is printed as the second part of the message for unhandled
1867exceptions.
1868Their names and values are:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001869
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001870\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001871EOFError 'end-of-file read'
1872KeyboardInterrupt 'keyboard interrupt'
1873MemoryError 'out of memory' *
1874NameError 'undefined name' *
1875RuntimeError 'run-time error' *
1876SystemError 'system error' *
1877TypeError 'type error' *
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001878\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001879%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001880The meanings should be clear enough.
1881Those exceptions with a {\tt *} in the third column have an argument.
1882
1883Exception handlers don't just handle exceptions if they occur
1884immediately in the try clause, but also if they occur inside functions
1885that are called (even indirectly) in the try clause.
1886For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001887
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001888\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001889>>> def this_fails():
1890... x = 1/0
1891...
1892>>> try:
1893... this_fails()
1894... except RuntimeError, detail:
1895... print 'Handling run-time error:', detail
1896...
Guido van Rossum67fa1601991-04-23 14:14:57 +00001897Handling run-time error: integer division by zero
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001898>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001899\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001900
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001901\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001902
1903The {\tt raise} statement allows the programmer to force a specified
1904exception to occur.
1905For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001906
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001907\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001908>>> raise NameError, 'Hi There!'
1909Unhandled exception: undefined name: Hi There!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001910Stack backtrace (innermost last):
1911 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001912>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001913\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001914%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001915The first argument to {\tt raise} names the exception to be raised.
1916The optional second argument specifies the exception's argument.
1917
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001918\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001919
1920Programs may name their own exceptions by assigning a string to a
1921variable.
1922For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001923
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001924\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001925>>> my_exc = 'nobody likes me!'
1926>>> try:
1927... raise my_exc, 2*2
1928... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001929... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001930...
1931My exception occured, value: 4
1932>>> raise my_exc, 1
1933Unhandled exception: nobody likes me!: 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001934Stack backtrace (innermost last):
1935 File "<stdin>", line 7
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001936>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001937\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001938%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001939Many standard modules use this to report errors that may occur in
1940functions they define.
1941
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001942\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001943
1944The {\tt try} statement has another optional clause which is intended to
1945define clean-up actions that must be executed under all circumstances.
1946For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001947
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001948\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001949>>> try:
1950... raise KeyboardInterrupt
1951... finally:
1952... print 'Goodbye, world!'
1953...
1954Goodbye, world!
1955Unhandled exception: keyboard interrupt
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001956Stack backtrace (innermost last):
1957 File "<stdin>", line 2
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001958>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001959\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001960%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001961The
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001962{\em finally\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001963must follow the except clauses(s), if any.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001964It is executed whether or not an exception occurred,
1965or whether or not an exception is handled.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001966If the exception is handled, the finally clause is executed after the
1967handler (and even if another exception occurred in the handler).
1968It is also executed when the {\tt try} statement is left via a
1969{\tt break} or {\tt return} statement.
1970
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001971\end{document}