blob: 83a0d8b9cf2c37535f3abecb0176d25a6ded8cd2 [file] [log] [blame]
Guido van Rossum37953781992-04-06 14:04:04 +00001\documentstyle[twoside,11pt,myformat]{report}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00002
3\title{\bf
Guido van Rossum6fc178f1991-08-16 09:13:42 +00004 Python Tutorial
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00005}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00006
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00007\author{
8 Guido van Rossum \\
9 Dept. CST, CWI, Kruislaan 413 \\
10 1098 SJ Amsterdam, The Netherlands \\
11 E-mail: {\tt guido@cwi.nl}
12}
13
14\begin{document}
15
16\pagenumbering{roman}
17
18\maketitle
19
20\begin{abstract}
21
22\noindent
Guido van Rossum4410c751991-06-04 20:22:18 +000023Python is a simple, yet powerful programming language that bridges the
Guido van Rossum6fc178f1991-08-16 09:13:42 +000024gap between C and shell programming, and is thus ideally suited for
25``throw-away programming''
26and rapid prototyping. Its syntax is put
27together from constructs borrowed from a variety of other languages;
28most prominent are influences from ABC, C, Modula-3 and Icon.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000029
Guido van Rossum4410c751991-06-04 20:22:18 +000030The Python interpreter is easily extended with new functions and data
Guido van Rossum6fc178f1991-08-16 09:13:42 +000031types implemented in C. Python is also suitable as an extension
32language for highly customizable C applications such as editors or
33window managers.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000034
Guido van Rossum4410c751991-06-04 20:22:18 +000035Python is available for various operating systems, amongst which
Guido van Rossum6fc178f1991-08-16 09:13:42 +000036several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
37and MS-DOS.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000038
Guido van Rossum6fc178f1991-08-16 09:13:42 +000039This tutorial introduces the reader informally to the basic concepts
40and features of the Python language and system. It helps to have a
41Python interpreter handy for hands-on experience, but as the examples
42are self-contained, the tutorial can be read off-line as well.
Guido van Rossum2292b8e1991-01-23 16:31:24 +000043
Guido van Rossum481ae681991-11-25 17:28:03 +000044For a description of standard objects and modules, see the {\em Python
45Library Reference} document. The {\em Python Reference Manual} gives
46a more formal definition of the language.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000047
48\end{abstract}
49
50\pagebreak
Guido van Rossumcdc93551992-02-11 15:53:13 +000051{
52\parskip = 0mm
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000053\tableofcontents
Guido van Rossumcdc93551992-02-11 15:53:13 +000054}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000055
56\pagebreak
57
58\pagenumbering{arabic}
59
Guido van Rossum6fc178f1991-08-16 09:13:42 +000060\chapter{Whetting Your Appetite}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000061
Guido van Rossum6fc178f1991-08-16 09:13:42 +000062If you ever wrote a large shell script, you probably know this
63feeling: you'd love to add yet another feature, but it's already so
64slow, and so big, and so complicated; or the feature involves a system
65call or other funcion that is only accessible from C \ldots Usually
66the problem at hand isn't serious enough to warrant rewriting the
67script in C; perhaps because the problem requires variable-length
68strings or other data types (like sorted lists of file names) that are
69easy in the shell but lots of work to implement in C; or perhaps just
70because you're not sufficiently familiar with C.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000071
Guido van Rossum6fc178f1991-08-16 09:13:42 +000072In such cases, Python may be just the language for you. Python is
73simple to use, but it is a real programming language, offering much
74more structure and support for large programs than the shell has. On
75the other hand, it also offers much more error checking than C, and,
76being a {\em very-high-level language}, it has high-level data types
77built in, such as flexible arrays and dictionaries that would cost you
78days to implement efficiently in C. Because of its more general data
79types Python is applicable to a much larger problem domain than {\em
Guido van Rossuma8d754e1992-01-07 16:44:35 +000080Awk} or even {\em Perl}, yet many things are at least as easy in
81Python as in those languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000082
Guido van Rossum6fc178f1991-08-16 09:13:42 +000083Python allows you to split up your program in modules that can be
84reused in other Python programs. It comes with a large collection of
Guido van Rossuma8d754e1992-01-07 16:44:35 +000085standard modules that you can use as the basis of your programs --- or
86as examples to start learning to program in Python. There are also
87built-in modules that provide things like file I/O, system calls,
88sockets, and even a generic interface to window systems (STDWIN).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000089
Guido van Rossuma8d754e1992-01-07 16:44:35 +000090Python is an interpreted language, which can save you considerable time
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000091during program development because no compilation and linking is
Guido van Rossum6fc178f1991-08-16 09:13:42 +000092necessary. The interpreter can be used interactively, which makes it
93easy to experiment with features of the language, to write throw-away
94programs, or to test functions during bottom-up program development.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +000095It is also a handy desk calculator.
96
Guido van Rossum6fc178f1991-08-16 09:13:42 +000097Python allows writing very compact and readable programs. Programs
98written in Python are typically much shorter than equivalent C
99programs, for several reasons:
100\begin{itemize}
101\item
102the high-level data types allow you to express complex operations in a
103single statement;
104\item
105statement grouping is done by indentation instead of begin/end
106brackets;
107\item
108no variable or argument declarations are necessary.
109\end{itemize}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000110
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000111Python is {\em extensible}: if you know how to program in C it is easy
112to add a new built-in
113function or
114module to the interpreter, either to
115perform critical operations at maximum speed, or to link Python
116programs to libraries that may only be available in binary form (such
117as a vendor-specific graphics library). Once you are really hooked,
118you can link the Python interpreter into an application written in C
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000119and use it as an extension or command language for that application.
120
121By the way, the language is named after the BBC show ``Monty
122Python's Flying Circus'' and has nothing to do with nasty reptiles...
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000123
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000124\section{Where From Here}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000125
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000126Now that you are all excited about Python, you'll want to examine it
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000127in some more detail. Since the best way to learn a language is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000128using it, you are invited here to do so.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000129
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000130In the next chapter, the mechanics of using the interpreter are
131explained. This is rather mundane information, but essential for
132trying out the examples shown later.
133
Guido van Rossum4410c751991-06-04 20:22:18 +0000134The rest of the tutorial introduces various features of the Python
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000135language and system though examples, beginning with simple
136expressions, statements and data types, through functions and modules,
137and finally touching upon advanced concepts like exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000138
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000139When you're through with the turtorial (or just getting bored), you
140should read the Library Reference, which gives complete (though terse)
141reference material about built-in and standard types, functions and
142modules that can save you a lot of time when writing Python programs.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000143
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000144\chapter{Using the Python Interpreter}
145
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000146\section{Invoking the Interpreter}
147
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000148The Python interpreter is usually installed as {\tt /usr/local/python}
149on those machines where it is available; putting {\tt /usr/local} in
150your {\UNIX} shell's search path makes it possible to start it by
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000151typing the command
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000152
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000153\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000154python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000155\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000156%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000157to the shell. Since the choice of the directory where the interpreter
158lives is an installation option, other places are possible; check with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000159your local Python guru or system administrator. (E.g., {\tt
160/usr/local/bin/python} is a popular alternative location.)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000161
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000162The interpreter operates somewhat like the {\UNIX} shell: when called
163with standard input connected to a tty device, it reads and executes
164commands interactively; when called with a file name argument or with
165a file as standard input, it reads and executes a {\em script} from
166that file.
167
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000168A third way of starting the interpreter is
169``{\tt python -c command [arg] ...}'', which
170executes the statement(s) in {\tt command}, analogous to the shell's
171{\tt -c} option. Since Python statements often contain spaces or other
172characters that are special to the shell, it is best to quote {\tt
173command} in its entirety with double quotes.
174
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000175Note that there is a difference between ``{\tt python file}'' and
176``{\tt python $<$file}''. In the latter case, input requests from the
Guido van Rossum573805a1992-03-06 10:56:03 +0000177program, such as calls to {\tt input()} and {\tt raw_input()}, are
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000178satisfied from {\em file}. Since this file has already been read
179until the end by the parser before the program starts executing, the
180program will encounter EOF immediately. In the former case (which is
181usually what you want) they are satisfied from whatever file or device
182is connected to standard input of the Python interpreter.
183
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000184\subsection{Argument Passing}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000185
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000186When known to the interpreter, the script name and additional
187arguments thereafter are passed to the script in the variable {\tt
188sys.argv}, which is a list of strings. Its length is at least one;
189when no script and no arguments are given, {\tt sys.argv[0]} is an
190empty string. When the script name is given as {\tt '-'} (meaning
191standard input), {\tt sys.argv[0]} is set to {\tt '-'}. When {\tt -c
192command} is used, {\tt sys.argv[0]} is set to {\tt '-c'}. Options
193found after {\tt -c command} are not consumed by the Python
194interpreter's option processing but left in {\tt sys.argv} for the
195command to handle.
196
197\subsection{Interactive Mode}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000198
Guido van Rossumdd010801991-06-07 14:31:11 +0000199When commands are read from a tty, the interpreter is said to be in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000200{\em interactive\ mode}. In this mode it prompts for the next command
201with the {\em primary\ prompt}, usually three greater-than signs ({\tt
202>>>}); for continuation lines it prompts with the {\em secondary\
203prompt}, by default three dots ({\tt ...}). Typing an EOF (Control-D)
204at the primary prompt causes the interpreter to exit with a zero exit
205status.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000206
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000207The interpreter prints a welcome message stating its version number
208and a copyright notice before printing the first prompt, e.g.:
209
210\bcode\begin{verbatim}
211python
212Python 0.9.5 (Jan 2 1992).
213Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
214>>>
215\end{verbatim}\ecode
216
217\section{The Interpreter and its Environment}
218
219\subsection{Error Handling}
220
221When an error occurs, the interpreter prints an error
222message and a stack trace. In interactive mode, it then returns to
223the primary prompt; when input came from a file, it exits with a
224nonzero exit status after printing
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000225the stack trace. (Exceptions handled by an {\tt except} clause in a
226{\tt try} statement are not errors in this context.) Some errors are
227unconditionally fatal and cause an exit with a nonzero exit; this
228applies to internal inconsistencies and some cases of running out of
229memory. All error messages are written to the standard error stream;
230normal output from the executed commands is written to standard
231output.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000232
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000233Typing the interrupt character (usually Control-C or DEL) to the
234primary or secondary prompt cancels the input and returns to the
235primary prompt.%
236\footnote{
237 A problem with the GNU Readline package may prevent this.
238}
239Typing an interrupt while a command is executing raises the {\tt
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000240KeyboardInterrupt} exception, which may be handled by a {\tt try}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000241statement.
242
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000243\subsection{The Module Search Path}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000244
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000245When a module named {\tt foo} is imported, the interpreter searches
246for a file named {\tt foo.py} in the list of directories specified by
247the environment variable {\tt PYTHONPATH}. It has the same syntax as
248the {\UNIX} shell variable {\tt PATH}, i.e., a list of colon-separated
249directory names. When {\tt PYTHONPATH} is not set, an
250installation-dependent default path is used, usually {\tt
251.:/usr/local/lib/python}.
252
253Actually, modules are searched in the list of directories given by the
254variable {\tt sys.path} which is initialized from {\tt PYTHONPATH} or
255the installation-dependent default. This allows Python programs that
256know what they're doing to modify or replace the module search path.
257See the section on Standard Modules later.
258
259\subsection{``Compiled'' Python files}
260
261As an important speed-up of the start-up time for short programs that
262use a lot of standard modules, if a file called {\tt foo.pyc} exists
263in the directory where {\tt foo.py} is found, this is assumed to
264contain an already-``compiled'' version of the module {\tt foo}. The
265modification time of the version of {\tt foo.py} used to create {\tt
266foo.pyc} is recorded in {\tt foo.pyc}, and the file is ignored if
267these don't match.
268
269Whenever {\tt foo.py} is successfully compiled, an attempt is made to
270write the compiled version to {\tt foo.pyc}. It is not an error if
271this attempt fails; if for any reason the file is not written
272completely, the resulting {\tt foo.pyc} file will be recognized as
273invalid and thus ignored later.
274
275\subsection{Executable Python scripts}
Guido van Rossum4410c751991-06-04 20:22:18 +0000276
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000277On BSD'ish {\UNIX} systems, Python scripts can be made directly
278executable, like shell scripts, by putting the line
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000279
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000280\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000281#! /usr/local/python
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000282\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000283%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000284(assuming that's the name of the interpreter) at the beginning of the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000285script and giving the file an executable mode. The {\tt \#!} must be
286the first two characters of the file.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000287
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000288\section{Interactive Input Editing and History Substitution}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000289
Guido van Rossum4410c751991-06-04 20:22:18 +0000290Some versions of the Python interpreter support editing of the current
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000291input line and history substitution, similar to facilities found in
292the Korn shell and the GNU Bash shell. This is implemented using the
293{\em GNU\ Readline} library, which supports Emacs-style and vi-style
294editing. This library has its own documentation which I won't
295duplicate here; however, the basics are easily explained.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000296
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000297Perhaps the quickest check to see whether command line editing is
298supported is typing Control-P to the first Python prompt you get. If
299it beeps, you have command line editing. If nothing appears to
300happen, or if \verb/^P/ is echoed, you can skip the rest of this
301section.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000302
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000303\subsection{Line Editing}
304
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000305If supported, input line editing is active whenever the interpreter
306prints a primary or secondary prompt. The current line can be edited
307using the conventional Emacs control characters. The most important
308of these are: C-A (Control-A) moves the cursor to the beginning of the
309line, C-E to the end, C-B moves it one position to the left, C-F to
310the right. Backspace erases the character to the left of the cursor,
311C-D the character to its right. C-K kills (erases) the rest of the
312line to the right of the cursor, C-Y yanks back the last killed
313string. C-underscore undoes the last change you made; it can be
314repeated for cumulative effect.
315
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000316\subsection{History Substitution}
317
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000318History substitution works as follows. All non-empty input lines
319issued are saved in a history buffer, and when a new prompt is given
320you are positioned on a new line at the bottom of this buffer. C-P
321moves one line up (back) in the history buffer, C-N moves one down.
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000322Any line in the history buffer can be edited; an asterisk appears in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000323front of the prompt to mark a line as modified. Pressing the Return
324key passes the current line to the interpreter. C-R starts an
325incremental reverse search; C-S starts a forward search.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000326
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000327\subsection{Key Bindings}
328
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000329The key bindings and some other parameters of the Readline library can
330be customized by placing commands in an initialization file called
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000331{\tt \$HOME/.inputrc}. Key bindings have the form
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000332
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000333\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000334key-name: function-name
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000335\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000336%
337or
338
339\bcode\begin{verbatim}
340"string": function-name
341\end{verbatim}\ecode
342%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000343and options can be set with
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000344
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000345\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000346set option-name value
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000347\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000348%
349For example:
350
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000351\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000352# I prefer vi-style editing:
353set editing-mode vi
354# Edit using a single line:
355set horizontal-scroll-mode On
356# Rebind some keys:
357Meta-h: backward-kill-word
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000358"\C-u": universal-argument
359"\C-x\C-r": re-read-init-file
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000360\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000361%
Guido van Rossum4410c751991-06-04 20:22:18 +0000362Note that the default binding for TAB in Python is to insert a TAB
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000363instead of Readline's default filename completion function. If you
364insist, you can override this by putting
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000365
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000366\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000367TAB: complete
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000368\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000369%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000370in your {\tt \$HOME/.inputrc}. (Of course, this makes it hard to type
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000371indented continuation lines...)
372
373\subsection{Commentary}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000374
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000375This facility is an enormous step forward compared to previous
376versions of the interpreter; however, some wishes are left: It would
377be nice if the proper indentation were suggested on continuation lines
378(the parser knows if an indent token is required next). The
379completion mechanism might use the interpreter's symbol table. A
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000380command to check (or even suggest) matching parentheses, quotes etc.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000381would also be useful.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000382
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000383\chapter{An Informal Introduction to Python}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000384
385In the following examples, input and output are distinguished by the
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000386presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat
387the example, you must type everything after the prompt, when the
388prompt appears; lines that do not begin with a prompt are output from
389the interpreter.%
390\footnote{
391 I'd prefer to use different fonts to distinguish input
392 from output, but the amount of LaTeX hacking that would require
393 is currently beyond my ability.
394}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000395Note that a secondary prompt on a line by itself in an example means
396you must type a blank line; this is used to end a multi-line command.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000397
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000398\section{Using Python as a Calculator}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000399
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000400Let's try some simple Python commands. Start the interpreter and wait
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000401for the primary prompt, {\tt >>>}. (It shouldn't take long.)
402
403\subsection{Numbers}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000404
405The interpreter acts as a simple calculator: you can type an
406expression at it and it will write the value. Expression syntax is
407straightforward: the operators {\tt +}, {\tt -}, {\tt *} and {\tt /}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000408work just like in most other languages (e.g., Pascal or C); parentheses
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000409can be used for grouping. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000410
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000411\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000412>>> # This is a comment
413>>> 2+2
4144
415>>>
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000416>>> (50-5*6)/4
4175
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000418>>> # Division truncates towards zero:
419>>> 7/3
4202
421>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000422\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000423%
424Like in C, the equal sign ({\tt =}) is used to assign a value to a
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000425variable. The value of an assignment is not written:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000426
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000427\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000428>>> width = 20
429>>> height = 5*9
430>>> width * height
431900
432>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000433\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000434%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000435A value can be assigned to several variables simultaneously:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000436
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000437\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000438>>> # Zero x, y and z
439>>> x = y = z = 0
440>>>
441\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000442%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000443There is full support for floating point; operators with mixed type
444operands convert the integer operand to floating point:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000445
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000446\bcode\begin{verbatim}
447>>> 4 * 2.5 / 3.3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00004483.0303030303
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000449>>> 7.0 / 2
4503.5
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000451>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000452\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000453
454\subsection{Strings}
455
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000456Besides numbers, Python can also manipulate strings, enclosed in
457single quotes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000458
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000459\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000460>>> 'foo bar'
461'foo bar'
462>>> 'doesn\'t'
463'doesn\'t'
464>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000465\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000466%
467Strings are written the same way as they are typed for input: inside
468quotes and with quotes and other funny characters escaped by
469backslashes, to show the precise value. (The {\tt print} statement,
470described later, can be used to write strings without quotes or
471escapes.)
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000472
473Strings can be concatenated (glued together) with the {\tt +}
474operator, and repeated with {\tt *}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000475
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000476\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000477>>> word = 'Help' + 'A'
478>>> word
479'HelpA'
480>>> '<' + word*5 + '>'
481'<HelpAHelpAHelpAHelpAHelpA>'
482>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000483\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000484%
485Strings can be subscripted (indexed); like in C, the first character of
486a string has subscript (index) 0.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000487
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000488There is no separate character type; a character is simply a string of
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000489size one. Like in Icon, substrings can be specified with the {\em
490slice} notation: two indices separated by a colon.
491
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000492\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000493>>> word[4]
494'A'
495>>> word[0:2]
496'He'
497>>> word[2:4]
498'lp'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000499>>>
500\end{verbatim}\ecode
501%
502Slice indices have useful defaults; an omitted first index defaults to
503zero, an omitted second index defaults to the size of the string being
504sliced.
505
506\bcode\begin{verbatim}
507>>> word[:2] # The first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000508'He'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000509>>> word[2:] # All but the first two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000510'lpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000511>>>
512\end{verbatim}\ecode
513%
514Here's a useful invariant of slice operations: \verb\s[:i] + s[i:]\
515equals \verb\s\.
516
517\bcode\begin{verbatim}
518>>> word[:2] + word[2:]
519'HelpA'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000520>>> word[:3] + word[3:]
521'HelpA'
522>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000523\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000524%
525Degenerate slice indices are handled gracefully: an index that is too
526large is replaced by the string size, an upper bound smaller than the
527lower bound returns an empty string.
528
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000529\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000530>>> word[1:100]
531'elpA'
532>>> word[10:]
533''
534>>> word[2:1]
535''
536>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000537\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000538%
539Indices may be negative numbers, to start counting from the right.
540For example:
541
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000542\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000543>>> word[-1] # The last character
544'A'
545>>> word[-2] # The last-but-one character
546'p'
547>>> word[-2:] # The last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000548'pA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000549>>> word[:-2] # All but the last two characters
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000550'Hel'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000551>>>
552\end{verbatim}\ecode
553%
554But note that -0 is really the same as 0, so it does not count from
555the right!
556
557\bcode\begin{verbatim}
558>>> word[-0] # (since -0 equals 0)
559'H'
560>>>
561\end{verbatim}\ecode
562%
563Out-of-range negative slice indices are truncated, but don't try this
564for single-element (non-slice) indices:
565
566\bcode\begin{verbatim}
567>>> word[-100:]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000568'HelpA'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000569>>> word[-10] # error
570Unhandled exception: IndexError: string index out of range
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000571>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000572\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000573%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000574The best way to remember how slices work is to think of the indices as
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000575pointing {\em between} characters, with the left edge of the first
576character numbered 0. Then the right edge of the last character of a
577string of {\tt n} characters has index {\tt n}, for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000578
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000579\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000580 +---+---+---+---+---+
581 | H | e | l | p | A |
582 +---+---+---+---+---+
583 0 1 2 3 4 5
584-5 -4 -3 -2 -1
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000585\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000586%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000587The first row of numbers gives the position of the indices 0...5 in
588the string; the second row gives the corresponding negative indices.
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000589The slice from \verb\i\ to \verb\j\ consists of all characters between
590the edges labeled \verb\i\ and \verb\j\, respectively.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000591
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000592For nonnegative indices, the length of a slice is the difference of
593the indices, if both are within bounds, e.g., the length of
594\verb\word[1:3]\ is 2.
595
596The built-in function {\tt len()} returns the length of a string:
597
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000598\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000599>>> s = 'supercalifragilisticexpialidocious'
600>>> len(s)
60134
602>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000603\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000604
605\subsection{Lists}
606
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000607Python knows a number of {\em compound} data types, used to group
608together other values. The most versatile is the {\em list}, which
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000609can be written as a list of comma-separated values (items) between
610square brackets. List items need not all have the same type.
611
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000612\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000613>>> a = ['foo', 'bar', 100, 1234]
614>>> a
615['foo', 'bar', 100, 1234]
616>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000617\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000618%
619Like string indices, list indices start at 0, and lists can be sliced,
620concatenated and so on:
621
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000622\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000623>>> a[0]
624'foo'
625>>> a[3]
6261234
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000627>>> a[-2]
628100
629>>> a[1:-1]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000630['bar', 100]
631>>> a[:2] + ['bletch', 2*2]
632['foo', 'bar', 'bletch', 4]
Guido van Rossum4410c751991-06-04 20:22:18 +0000633>>> 3*a[:3] + ['Boe!']
634['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000635>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000636\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000637%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000638Unlike strings, which are {\em immutable}, it is possible to change
639individual elements of a list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000640
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000641\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000642>>> a
643['foo', 'bar', 100, 1234]
644>>> a[2] = a[2] + 23
645>>> a
646['foo', 'bar', 123, 1234]
647>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000648\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000649%
650Assignment to slices is also possible, and this can even change the size
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000651of the list:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000652
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000653\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000654>>> # Replace some items:
655>>> a[0:2] = [1, 12]
656>>> a
657[1, 12, 123, 1234]
658>>> # Remove some:
659>>> a[0:2] = []
660>>> a
661[123, 1234]
662>>> # Insert some:
663>>> a[1:1] = ['bletch', 'xyzzy']
664>>> a
665[123, 'bletch', 'xyzzy', 1234]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000666>>> a[:0] = a # Insert (a copy of) itself at the beginning
667>>> a
668[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000669>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000670\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000671%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000672The built-in function {\tt len()} also applies to lists:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000673
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000674\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000675>>> len(a)
Guido van Rossuma8d754e1992-01-07 16:44:35 +00006768
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000677>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000678\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000679%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000680It is possible to nest lists (create lists containing other lists),
681for example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000682
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000683\bcode\begin{verbatim}
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000684>>> q = [2, 3]
685>>> p = [1, q, 4]
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000686>>> len(p)
6873
688>>> p[1]
689[2, 3]
690>>> p[1][0]
6912
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000692>>> p[1].append('xtra') # See section 5.1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000693>>> p
694[1, [2, 3, 'xtra'], 4]
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000695>>> q
696[2, 3, 'xtra']
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000697>>>
698\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000699%
700Note that in the last example, {\tt p[1]} and {\tt q} really refer to
701the same object! We'll come back to {\em object semantics} later.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000702
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000703\section{First Steps Towards Programming}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000704
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000705Of course, we can use Python for more complicated tasks than adding
706two and two together. For instance, we can write an initial
707subsequence of the {\em Fibonacci} series as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000708
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000709\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000710>>> # Fibonacci series:
711>>> # the sum of two elements defines the next
712>>> a, b = 0, 1
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000713>>> while b < 10:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000714... print b
715... a, b = b, a+b
716...
7171
7181
7192
7203
7215
7228
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000723>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000724\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000725%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000726This example introduces several new features.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000727
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000728\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000729
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000730\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000731The first line contains a {\em multiple assignment}: the variables
732{\tt a} and {\tt b} simultaneously get the new values 0 and 1. On the
733last line this is used again, demonstrating that the expressions on
734the right-hand side are all evaluated first before any of the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000735assignments take place.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000736
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000737\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000738The {\tt while} loop executes as long as the condition (here: {\tt b <
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000739100}) remains true. In Python, like in C, any non-zero integer value is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000740true; zero is false. The condition may also be a string or list value,
741in fact any sequence; anything with a non-zero length is true, empty
742sequences are false. The test used in the example is a simple
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000743comparison. The standard comparison operators are written the same as
744in C: {\tt <}, {\tt >}, {\tt ==}, {\tt <=}, {\tt >=} and {\tt !=}.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000745
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000746\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000747The {\em body} of the loop is {\em indented}: indentation is Python's
748way of grouping statements. Python does not (yet!) provide an
749intelligent input line editing facility, so you have to type a tab or
750space(s) for each indented line. In practice you will prepare more
751complicated input for Python with a text editor; most text editors have
752an auto-indent facility. When a compound statement is entered
753interactively, it must be followed by a blank line to indicate
754completion (since the parser cannot guess when you have typed the last
755line).
756
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000757\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000758The {\tt print} statement writes the value of the expression(s) it is
759given. It differs from just writing the expression you want to write
760(as we did earlier in the calculator examples) in the way it handles
761multiple expressions and strings. Strings are written without quotes,
762and a space is inserted between items, so you can format things nicely,
763like this:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000764
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000765\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000766>>> i = 256*256
767>>> print 'The value of i is', i
768The value of i is 65536
769>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000770\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000771%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000772A trailing comma avoids the newline after the output:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000773
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000774\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000775>>> a, b = 0, 1
776>>> while b < 1000:
777... print b,
778... a, b = b, a+b
779...
7801 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
781>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000782\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000783%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000784Note that the interpreter inserts a newline before it prints the next
785prompt if the last line was not completed.
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000786
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000787\end{itemize}
788
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000789\chapter{More Control Flow Tools}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000790
Guido van Rossum4410c751991-06-04 20:22:18 +0000791Besides the {\tt while} statement just introduced, Python knows the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000792usual control flow statements known from other languages, with some
793twists.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000794
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000795\section{If Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000796
797Perhaps the most well-known statement type is the {\tt if} statement.
798For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000799
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000800\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000801>>> if x < 0:
802... x = 0
803... print 'Negative changed to zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000804... elif x == 0:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000805... print 'Zero'
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000806... elif x == 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000807... print 'Single'
808... else:
809... print 'More'
810...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000811\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000812%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000813There can be zero or more {\tt elif} parts, and the {\tt else} part is
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000814optional. The keyword `{\tt elif}' is short for `{\tt else if}', and is
815useful to avoid excessive indentation. An {\tt if...elif...elif...}
816sequence is a substitute for the {\em switch} or {\em case} statements
817found in other languages.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000818
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000819\section{For Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000820
Guido van Rossum4410c751991-06-04 20:22:18 +0000821The {\tt for} statement in Python differs a bit from what you may be
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000822used to in C or Pascal. Rather than always iterating over an
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000823arithmetic progression of numbers (like in Pascal), or leaving the user
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000824completely free in the iteration test and step (as C), Python's {\tt
825for} statement iterates over the items of any sequence (e.g., a list
826or a string), in the order that they appear in the sequence. For
827example (no pun intended):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000828
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000829\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000830>>> # Measure some strings:
831>>> a = ['cat', 'window', 'defenestrate']
832>>> for x in a:
833... print x, len(x)
834...
835cat 3
836window 6
837defenestrate 12
838>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000839\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000840%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000841It is not safe to modify the sequence being iterated over in the loop
842(this can only happen for mutable sequence types, i.e., lists). If
843you need to modify the list you are iterating over, e.g., duplicate
844selected items, you must iterate over a copy. The slice notation
845makes this particularly convenient:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000846
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000847\bcode\begin{verbatim}
848>>> for x in a[:]: # make a slice copy of the entire list
849... if len(x) > 6: a.insert(0, x)
850...
851>>> a
852['defenestrate', 'cat', 'window', 'defenestrate']
853>>>
854\end{verbatim}\ecode
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000855
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000856\section{The {\tt range()} Function}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000857
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000858If you do need to iterate over a sequence of numbers, the built-in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000859function {\tt range()} comes in handy. It generates lists containing
860arithmetic progressions, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000861
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000862\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000863>>> range(10)
864[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
865>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000866\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000867%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000868The given end point is never part of the generated list; {\tt range(10)}
869generates a list of 10 values, exactly the legal indices for items of a
870sequence of length 10. It is possible to let the range start at another
871number, or to specify a different increment (even negative):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000872
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000873\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000874>>> range(5, 10)
875[5, 6, 7, 8, 9]
876>>> range(0, 10, 3)
877[0, 3, 6, 9]
878>>> range(-10, -100, -30)
879[-10, -40, -70]
880>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000881\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000882%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000883To iterate over the indices of a sequence, combine {\tt range()} and
884{\tt len()} as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000885
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000886\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000887>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000888>>> for i in range(len(a)):
889... print i, a[i]
890...
8910 Mary
8921 had
8932 a
8943 little
Guido van Rossum6fc178f1991-08-16 09:13:42 +00008954 lamb
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000896>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000897\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000898
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000899\section{Break and Continue Statements, and Else Clauses on Loops}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000900
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000901The {\tt break} statement, like in C, breaks out of the smallest
902enclosing {\tt for} or {\tt while} loop.
903
904The {\tt continue} statement, also borrowed from C, continues with the
905next iteration of the loop.
906
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000907Loop statements may have an {\tt else} clause; it is executed when the
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000908loop terminates through exhaustion of the list (with {\tt for}) or when
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000909the condition becomes false (with {\tt while}), but not when the loop is
910terminated by a {\tt break} statement. This is exemplified by the
911following loop, which searches for a list item of value 0:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000912
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000913\bcode\begin{verbatim}
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000914>>> for n in range(2, 10):
915... for x in range(2, n):
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000916... if n % x == 0:
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000917... print n, 'equals', x, '*', n/x
918... break
919... else:
920... print n, 'is a prime number'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000921...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00009222 is a prime number
9233 is a prime number
9244 equals 2 * 2
9255 is a prime number
9266 equals 2 * 3
9277 is a prime number
9288 equals 2 * 4
9299 equals 3 * 3
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000930>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000931\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000932
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000933\section{Pass Statements}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000934
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000935The {\tt pass} statement does nothing.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000936It can be used when a statement is required syntactically but the
937program requires no action.
938For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000939
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000940\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000941>>> while 1:
942... pass # Busy-wait for keyboard interrupt
943...
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000944\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000945
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000946\section{Defining Functions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000947
948We can create a function that writes the Fibonacci series to an
949arbitrary boundary:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000950
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000951\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000952>>> def fib(n): # write Fibonacci series up to n
953... a, b = 0, 1
954... while b <= n:
955... print b,
956... a, b = b, a+b
957...
958>>> # Now call the function we just defined:
959>>> fib(2000)
9601 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
961>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +0000962\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000963%
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000964The keyword {\tt def} introduces a function {\em definition}. It must
965be followed by the function name and the parenthesized list of formal
966parameters. The statements that form the body of the function starts at
967the next line, indented by a tab stop.
968
969The {\em execution} of a function introduces a new symbol table used
970for the local variables of the function. More precisely, all variable
971assignments in a function store the value in the local symbol table;
972whereas
973 variable references first look in the local symbol table, then
974in the global symbol table, and then in the table of built-in names.
975Thus,
976global variables cannot be directly assigned to from within a
977function, although they may be referenced.
978
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000979The actual parameters (arguments) to a function call are introduced in
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000980the local symbol table of the called function when it is called; thus,
981arguments are passed using {\em call\ by\ value}.%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000982\footnote{
Guido van Rossum2292b8e1991-01-23 16:31:24 +0000983 Actually, {\em call by object reference} would be a better
984 description, since if a mutable object is passed, the caller
985 will see any changes the callee makes to it (e.g., items
986 inserted into a list).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +0000987}
988When a function calls another function, a new local symbol table is
989created for that call.
990
Guido van Rossum6fc178f1991-08-16 09:13:42 +0000991A function definition introduces the function name in the
992current
993symbol table. The value
994of the function name
995has a type that is recognized by the interpreter as a user-defined
996function. This value can be assigned to another name which can then
997also be used as a function. This serves as a general renaming
998mechanism:
Guido van Rossuma8d754e1992-01-07 16:44:35 +0000999
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001000\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001001>>> fib
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001002<function object at 10042ed0>
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001003>>> f = fib
1004>>> f(100)
10051 1 2 3 5 8 13 21 34 55 89
1006>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001007\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001008%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001009You might object that {\tt fib} is not a function but a procedure. In
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001010Python, like in C, procedures are just functions that don't return a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001011value. In fact, technically speaking, procedures do return a value,
1012albeit a rather boring one. This value is called {\tt None} (it's a
1013built-in name). Writing the value {\tt None} is normally suppressed by
1014the interpreter if it would be the only value written. You can see it
1015if you really want to:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001016
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001017\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001018>>> print fib(0)
1019None
1020>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001021\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001022%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001023It is simple to write a function that returns a list of the numbers of
1024the Fibonacci series, instead of printing it:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001025
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001026\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001027>>> def fib2(n): # return Fibonacci series up to n
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001028... result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001029... a, b = 0, 1
1030... while b <= n:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001031... result.append(b) # see below
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001032... a, b = b, a+b
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001033... return result
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001034...
1035>>> f100 = fib2(100) # call it
1036>>> f100 # write the result
1037[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1038>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001039\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001040%
Guido van Rossum4410c751991-06-04 20:22:18 +00001041This example, as usual, demonstrates some new Python features:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001042
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001043\begin{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001044
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001045\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001046The {\tt return} statement returns with a value from a function. {\tt
1047return} without an expression argument is used to return from the middle
1048of a procedure (falling off the end also returns from a proceduce), in
1049which case the {\tt None} value is returned.
1050
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001051\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001052The statement {\tt result.append(b)} calls a {\em method} of the list
1053object {\tt result}. A method is a function that `belongs' to an
1054object and is named {\tt obj.methodname}, where {\tt obj} is some
1055object (this may be an expression), and {\tt methodname} is the name
1056of a method that is defined by the object's type. Different types
1057define different methods. Methods of different types may have the
1058same name without causing ambiguity. (It is possible to define your
1059own object types and methods, using {\em classes}. This is an
1060advanced feature that is not discussed in this tutorial.)
1061The method {\tt append} shown in the example, is defined for
1062list objects; it adds a new element at the end of the list. In this
1063example
1064it is equivalent to {\tt result = result + [b]}, but more efficient.
1065
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001066\end{itemize}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001067
1068\chapter{Odds and Ends}
1069
1070This chapter describes some things you've learned about already in
1071more detail, and adds some new things as well.
1072
1073\section{More on Lists}
1074
1075The list data type has some more methods. Here are all of the methods
1076of lists objects:
1077
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001078\begin{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001079
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001080\item[{\tt insert(i, x)}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001081Insert an item at a given position. The first argument is the index of
1082the element before which to insert, so {\tt a.insert(0, x)} inserts at
1083the front of the list, and {\tt a.insert(len(a), x)} is equivalent to
1084{\tt a.append(x)}.
1085
1086\item[{\tt append(x)}]
1087Equivalent to {\tt a.insert(len(a), x)}.
1088
1089\item[{\tt index(x)}]
1090Return the index in the list of the first item whose value is {\tt x}.
1091It is an error if there is no such item.
1092
1093\item[{\tt remove(x)}]
1094Remove the first item from the list whose value is {\tt x}.
1095It is an error if there is no such item.
1096
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001097\item[{\tt sort()}]
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001098Sort the items of the list, in place.
1099
1100\item[{\tt reverse()}]
1101Reverse the elements of the list, in place.
1102
Guido van Rossum7d9f8d71991-01-22 11:45:00 +00001103\end{description}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001104
1105An example that uses all list methods:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001106
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001107\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001108>>> a = [66.6, 333, 333, 1, 1234.5]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001109>>> a.insert(2, -1)
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001110>>> a.append(333)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001111>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001112[66.6, 333, -1, 333, 1, 1234.5, 333]
1113>>> a.index(333)
11141
1115>>> a.remove(333)
1116>>> a
1117[66.6, -1, 333, 1, 1234.5, 333]
1118>>> a.reverse()
1119>>> a
1120[333, 1234.5, 1, 333, -1, 66.6]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001121>>> a.sort()
1122>>> a
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001123[-1, 1, 66.6, 333, 333, 1234.5]
1124>>>
1125\end{verbatim}\ecode
1126
1127\section{The {\tt del} statement}
1128
1129There is a way to remove an item from a list given its index instead
1130of its value: the {\tt del} statement. This can also be used to
1131remove slices from a list (which we did earlier by assignment of an
1132empty list to the slice). For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001133
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001134\bcode\begin{verbatim}
1135>>> a
1136[-1, 1, 66.6, 333, 333, 1234.5]
1137>>> del a[0]
1138>>> a
1139[1, 66.6, 333, 333, 1234.5]
1140>>> del a[2:4]
1141>>> a
1142[1, 66.6, 1234.5]
1143>>>
1144\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001145%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001146{\tt del} can also be used to delete entire variables:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001147
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001148\bcode\begin{verbatim}
1149>>> del a
1150>>>
1151\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001152%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001153Referencing the name {\tt a} hereafter is an error (at least until
1154another value is assigned to it). We'll find other uses for {\tt del}
1155later.
1156
1157\section{Tuples and Sequences}
1158
1159We saw that lists and strings have many common properties, e.g.,
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001160indexinging and slicing operations. They are two examples of {\em
1161sequence} data types. Since Python is an evolving language, other
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001162sequence data types may be added. There is also another standard
1163sequence data type: the {\em tuple}.
1164
1165A tuple consists of a number of values separated by commas, for
1166instance:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001167
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001168\bcode\begin{verbatim}
1169>>> t = 12345, 54321, 'hello!'
1170>>> t[0]
117112345
1172>>> t
1173(12345, 54321, 'hello!')
1174>>> # Tuples may be nested:
1175>>> u = t, (1, 2, 3, 4, 5)
1176>>> u
1177((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
1178>>>
1179\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001180%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001181As you see, on output tuples are alway enclosed in parentheses, so
1182that nested tuples are interpreted correctly; they may be input with
1183or without surrounding parentheses, although often parentheses are
1184necessary anyway (if the tuple is part of a larger expression).
1185
1186Tuples have many uses, e.g., (x, y) coordinate pairs, employee records
1187from a database, etc. Tuples, like strings, are immutable: it is not
1188possible to assign to the individual items of a tuple (you can
1189simulate much of the same effect with slicing and concatenation,
1190though).
1191
1192A special problem is the construction of tuples containing 0 or 1
1193items: the syntax has some extra quirks to accomodate these. Empty
1194tuples are constructed by an empty pair of parentheses; a tuple with
1195one item is constructed by following a value with a comma
1196(it is not sufficient to enclose a single value in parentheses).
1197Ugly, but effective. For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001198
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001199\bcode\begin{verbatim}
1200>>> empty = ()
1201>>> singleton = 'hello', # <-- note trailing comma
1202>>> len(empty)
12030
1204>>> len(singleton)
12051
1206>>> singleton
1207('hello',)
1208>>>
1209\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001210%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001211The statement {\tt t = 12345, 54321, 'hello!'} is an example of {\em
1212tuple packing}: the values {\tt 12345}, {\tt 54321} and {\tt 'hello!'}
1213are packed together in a tuple. The reverse operation is also
1214possible, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001215
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001216\bcode\begin{verbatim}
1217>>> x, y, z = t
1218>>>
1219\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001220%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001221This is called, appropriately enough, {\em tuple unpacking}. Tuple
1222unpacking requires that the list of variables on the left has the same
1223number of elements as the length of the tuple. Note that multiple
1224assignment is really just a combination of tuple packing and tuple
1225unpacking!
1226
1227Occasionally, the corresponding operation on lists is useful: {\em list
1228unpacking}. This is supported by enclosing the list of variables in
1229square brackets:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001230
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001231\bcode\begin{verbatim}
1232>>> a = ['foo', 'bar', 100, 1234]
1233>>> [a1, a2, a3, a4] = a
1234>>>
1235\end{verbatim}\ecode
1236
1237\section{Dictionaries}
1238
1239Another useful data type built into Python is the {\em dictionary}.
1240Dictionaries are sometimes found in other languages as ``associative
1241memories'' or ``associative arrays''. Unlike sequences, which are
1242indexed by a range of numbers, dictionaries are indexed by {\em keys},
1243which are strings. It is best to think of a dictionary as an unordered set of
1244{\em key:value} pairs, with the requirement that the keys are unique
1245(within one dictionary).
1246A pair of braces creates an empty dictionary: \verb/{}/.
1247Placing a comma-separated list of key:value pairs within the
1248braces adds initial key:value pairs to the dictionary; this is also the
1249way dictionaries are written on output.
1250
1251The main operations on a dictionary are storing a value with some key
1252and extracting the value given the key. It is also possible to delete
1253a key:value pair
1254with {\tt del}.
1255If you store using a key that is already in use, the old value
1256associated with that key is forgotten. It is an error to extract a
1257value using a non-existant key.
1258
1259The {\tt keys()} method of a dictionary object returns a list of all the
1260keys used in the dictionary, in random order (if you want it sorted,
1261just apply the {\tt sort()} method to the list of keys). To check
1262whether a single key is in the dictionary, use the \verb/has_key()/
1263method of the dictionary.
1264
1265Here is a small example using a dictionary:
1266
1267\bcode\begin{verbatim}
1268>>> tel = {'jack': 4098, 'sape': 4139}
1269>>> tel['guido'] = 4127
1270>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001271{'sape': 4139, 'guido': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001272>>> tel['jack']
12734098
1274>>> del tel['sape']
1275>>> tel['irv'] = 4127
1276>>> tel
Guido van Rossum8f96f771991-11-12 15:45:03 +00001277{'guido': 4127, 'irv': 4127, 'jack': 4098}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001278>>> tel.keys()
1279['guido', 'irv', 'jack']
1280>>> tel.has_key('guido')
12811
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001282>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001283\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001284
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001285\section{More on Conditions}
1286
1287The conditions used in {\tt while} and {\tt if} statements above can
1288contain other operators besides comparisons.
1289
1290The comparison operators {\tt in} and {\tt not in} check whether a value
1291occurs (does not occur) in a sequence. The operators {\tt is} and {\tt
1292is not} compare whether two objects are really the same object; this
1293only matters for mutable objects like lists. All comparison operators
1294have the same priority, which is lower than that of all numerical
1295operators.
1296
1297Comparisons can be chained: e.g., {\tt a < b = c} tests whether {\tt a}
1298is less than {\tt b} and moreover {\tt b} equals {\tt c}.
1299
1300Comparisons may be combined by the Boolean operators {\tt and} and {\tt
1301or}, and the outcome of a comparison (or of any other Boolean
1302expression) may be negated with {\tt not}. These all have lower
1303priorities than comparison operators again; between them, {\tt not} has
1304the highest priority, and {\tt or} the lowest, so that
1305{\tt A and not B or C} is equivalent to {\tt (A and (not B)) or C}. Of
1306course, parentheses can be used to express the desired composition.
1307
1308The Boolean operators {\tt and} and {\tt or} are so-called {\em
1309shortcut} operators: their arguments are evaluated from left to right,
1310and evaluation stops as soon as the outcome is determined. E.g., if
1311{\tt A} and {\tt C} are true but {\tt B} is false, {\tt A and B and C}
1312does not evaluate the expression C. In general, the return value of a
1313shortcut operator, when used as a general value and not as a Boolean, is
1314the last evaluated argument.
1315
1316It is possible to assign the result of a comparison or other Boolean
1317expression to a variable, but you must enclose the entire Boolean
1318expression in parentheses. This is necessary because otherwise an
1319assignment like \verb/a = b = c/ would be ambiguous: does it assign the
1320value of {\tt c} to {\tt a} and {\tt b}, or does it compare {\tt b} to
1321{\tt c} and assign the outcome (0 or 1) to {\tt a}? As it is, the first
1322meaning is what you get, and to get the latter you have to write
1323\verb/a = (b = c)/. (In Python, unlike C, assignment cannot occur
1324inside expressions.)
1325
1326\section{Comparing Sequences and Other Types}
1327
1328Sequence objects may be compared to other objects with the same
1329sequence type. The comparison uses {\em lexicographical} ordering:
1330first the first two items are compared, and if they differ this
1331determines the outcome of the comparison; if they are equal, the next
1332two items are compared, and so on, until either sequence is exhausted.
1333If two items to be compared are themselves sequences of the same type,
1334the lexiographical comparison is carried out recursively. If all
1335items of two sequences compare equal, the sequences are considered
1336equal. If one sequence is an initial subsequence of the other, the
1337shorted sequence is the smaller one. Lexicographical ordering for
1338strings uses the ASCII ordering for individual characters. Some
1339examples of comparisons between sequences with the same types:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001340
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001341\bcode\begin{verbatim}
1342(1, 2, 3) < (1, 2, 4)
1343[1, 2, 3] < [1, 2, 4]
1344'ABC' < 'C' < 'Pascal' < 'Python'
1345(1, 2, 3, 4) < (1, 2, 4)
1346(1, 2) < (1, 2, -1)
1347(1, 2, 3) = (1.0, 2.0, 3.0)
1348(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
1349\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001350%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001351Note that comparing objects of different types is legal. The outcome
1352is deterministic but arbitrary: the types are ordered by their name.
1353Thus, a list is always smaller than a string, a string is always
1354smaller than a tuple, etc. Mixed numeric types are compared according
1355to their numeric value, so 0 equals 0.0, etc.%
1356\footnote{
1357 The rules for comparing objects of different types should
1358 not be relied upon; they may change in a future version of
1359 the language.
1360}
1361
1362\chapter{Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001363
Guido van Rossum4410c751991-06-04 20:22:18 +00001364If you quit from the Python interpreter and enter it again, the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001365definitions you have made (functions and variables) are lost.
1366Therefore, if you want to write a somewhat longer program, you are
1367better off using a text editor to prepare the input for the interpreter
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001368and run it with that file as input instead. This is known as creating a
1369{\em script}. As your program gets longer, you may want to split it
1370into several files for easier maintenance. You may also want to use a
1371handy function that you've written in several programs without copying
1372its definition into each program.
1373
Guido van Rossum4410c751991-06-04 20:22:18 +00001374To support this, Python has a way to put definitions in a file and use
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001375them in a script or in an interactive instance of the interpreter.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001376Such a file is called a {\em module}; definitions from a module can be
1377{\em imported} into other modules or into the {\em main} module (the
1378collection of variables that you have access to in a script
1379executed at the top level
1380and in calculator mode).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001381
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001382A module is a file containing Python definitions and statements. The
1383file name is the module name with the suffix {\tt .py} appended. For
1384instance, use your favorite text editor to create a file called {\tt
1385fibo.py} in the current directory with the following contents:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001386
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001387\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001388# Fibonacci numbers module
1389
1390def fib(n): # write Fibonacci series up to n
1391 a, b = 0, 1
1392 while b <= n:
1393 print b,
1394 a, b = b, a+b
1395
1396def fib2(n): # return Fibonacci series up to n
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001397 result = []
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001398 a, b = 0, 1
1399 while b <= n:
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001400 result.append(b)
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001401 a, b = b, a+b
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001402 return result
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001403\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001404%
Guido van Rossum4410c751991-06-04 20:22:18 +00001405Now enter the Python interpreter and import this module with the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001406following command:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001407
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001408\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001409>>> import fibo
1410>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001411\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001412%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001413This does not enter the names of the functions defined in
1414{\tt fibo}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001415directly in the current symbol table; it only enters the module name
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001416{\tt fibo}
1417there.
1418Using the module name you can access the functions:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001419
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001420\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001421>>> fibo.fib(1000)
14221 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1423>>> fibo.fib2(100)
1424[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
1425>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001426\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001427%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001428If you intend to use a function often you can assign it to a local name:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001429
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001430\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001431>>> fib = fibo.fib
1432>>> fib(500)
14331 1 2 3 5 8 13 21 34 55 89 144 233 377
1434>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001435\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001436
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001437\section{More on Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001438
1439A module can contain executable statements as well as function
1440definitions.
1441These statements are intended to initialize the module.
1442They are executed only the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001443{\em first}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001444time the module is imported somewhere.%
1445\footnote{
1446 In fact function definitions are also `statements' that are
1447 `executed'; the execution enters the function name in the
1448 module's global symbol table.
1449}
1450
1451Each module has its own private symbol table, which is used as the
1452global symbol table by all functions defined in the module.
1453Thus, the author of a module can use global variables in the module
1454without worrying about accidental clashes with a user's global
1455variables.
1456On the other hand, if you know what you are doing you can touch a
1457module's global variables with the same notation used to refer to its
1458functions,
1459{\tt modname.itemname}.
1460
1461Modules can import other modules.
1462It is customary but not required to place all
1463{\tt import}
1464statements at the beginning of a module (or script, for that matter).
1465The imported module names are placed in the importing module's global
1466symbol table.
1467
1468There is a variant of the
1469{\tt import}
1470statement that imports names from a module directly into the importing
1471module's symbol table.
1472For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001473
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001474\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001475>>> from fibo import fib, fib2
1476>>> fib(500)
14771 1 2 3 5 8 13 21 34 55 89 144 233 377
1478>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001479\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001480%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001481This does not introduce the module name from which the imports are taken
1482in the local symbol table (so in the example, {\tt fibo} is not
1483defined).
1484
1485There is even a variant to import all names that a module defines:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001486
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001487\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001488>>> from fibo import *
1489>>> fib(500)
14901 1 2 3 5 8 13 21 34 55 89 144 233 377
1491>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001492\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001493%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001494This imports all names except those beginning with an underscore
Guido van Rossum573805a1992-03-06 10:56:03 +00001495({\tt _}).
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001496
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001497\section{Standard Modules}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001498
Guido van Rossum4410c751991-06-04 20:22:18 +00001499Python comes with a library of standard modules, described in a separate
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001500document (Python Library Reference). Some modules are built into the
1501interpreter; these provide access to operations that are not part of the
1502core of the language but are nevertheless built in, either for
1503efficiency or to provide access to operating system primitives such as
1504system calls. The set of such modules is a configuration option; e.g.,
1505the {\tt amoeba} module is only provided on systems that somehow support
1506Amoeba primitives. One particular module deserves some attention: {\tt
1507sys}, which is built into every Python interpreter. The variables {\tt
1508sys.ps1} and {\tt sys.ps2} define the strings used as primary and
1509secondary prompts:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001510
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001511\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001512>>> import sys
1513>>> sys.ps1
1514'>>> '
1515>>> sys.ps2
1516'... '
1517>>> sys.ps1 = 'C> '
1518C> print 'Yuck!'
1519Yuck!
1520C>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001521\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001522%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001523These two variables are only defined if the interpreter is in
1524interactive mode.
1525
1526The variable
1527{\tt sys.path}
1528is a list of strings that determine the interpreter's search path for
1529modules.
1530It is initialized to a default path taken from the environment variable
1531{\tt PYTHONPATH},
1532or from a built-in default if
1533{\tt PYTHONPATH}
1534is not set.
1535You can modify it using standard list operations, e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001536
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001537\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001538>>> import sys
1539>>> sys.path.append('/ufs/guido/lib/python')
1540>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001541\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001542
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001543\section{The {\tt dir()} function}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001544
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001545The built-in function {\tt dir} is used to find out which names a module
1546defines. It returns a sorted list of strings:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001547
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001548\bcode\begin{verbatim}
1549>>> import fibo, sys
1550>>> dir(fibo)
1551['fib', 'fib2']
1552>>> dir(sys)
1553['argv', 'exit', 'modules', 'path', 'ps1', 'ps2', 'stderr', 'stdin', 'stdout']
1554>>>
1555\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001556%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001557Without arguments, {\tt dir()} lists the names you have defined currently:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001558
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001559\bcode\begin{verbatim}
1560>>> a = [1, 2, 3, 4, 5]
1561>>> import fibo, sys
1562>>> fib = fibo.fib
1563>>> dir()
1564['a', 'fib', 'fibo', 'sys']
1565>>>
1566\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001567%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001568Note that it lists all types of names: variables, modules, functions, etc.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001569
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001570{\tt dir()} does not list the names of built-in functions and variables.
1571If you want a list of those, they are defined in the standard module
1572{\tt builtin}:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001573
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001574\bcode\begin{verbatim}
1575>>> import builtin
1576>>> dir(builtin)
1577['EOFError', 'KeyboardInterrupt', 'MemoryError', 'NameError', 'None', 'Runti
1578meError', 'SystemError', 'TypeError', 'abs', 'chr', 'dir', 'divmod', 'eval',
1579 'exec', 'float', 'input', 'int', 'len', 'long', 'max', 'min', 'open', 'ord'
1580, 'pow', 'range', 'raw_input', 'reload', 'type']
1581>>>
1582\end{verbatim}\ecode
1583
1584\chapter{Output Formatting}
1585
1586So far we've encountered two ways of writing values: {\em expression
1587statements} and the {\tt print} statement. (A third way is using the
1588{\tt write} method of file objects; the standard output file can be
1589referenced as {\tt sys.stdout}. See the Library Reference for more
1590information on this.)
1591
1592Often you'll want more control over the formatting of your output than
1593simply printing space-separated values. The key to nice formatting in
1594Python is to do all the string handling yourself; using string slicing
1595and concatenation operations you can create any lay-out you can imagine.
1596The standard module {\tt string} contains some useful operations for
1597padding strings to a given column width; these will be discussed shortly.
1598
1599One question remains, of course: how do you convert values to strings?
1600Luckily, Python has a way to convert any value to a string: just write
1601the value between reverse quotes (\verb/``/). Some examples:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001602
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001603\bcode\begin{verbatim}
1604>>> x = 10 * 3.14
1605>>> y = 200*200
1606>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'
1607>>> print s
1608The value of x is 31.4, and y is 40000...
1609>>> # Reverse quotes work on other types besides numbers:
1610>>> p = [x, y]
1611>>> ps = `p`
1612>>> ps
1613'[31.4, 40000]'
1614>>> # Converting a string adds string quotes and backslashes:
1615>>> hello = 'hello, world\n'
1616>>> hellos = `hello`
1617>>> print hellos
1618'hello, world\012'
1619>>> # The argument of reverse quotes may be a tuple:
1620>>> `x, y, ('foo', 'bar')`
1621'(31.4, 40000, (\'foo\', \'bar\'))'
1622>>>
1623\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001624%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001625Here is how you write a table of squares and cubes:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001626
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001627\bcode\begin{verbatim}
1628>>> import string
1629>>> for x in range(1, 11):
1630... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),
1631... # Note trailing comma on previous line
1632... print string.rjust(`x*x*x`, 4)
1633...
1634 1 1 1
1635 2 4 8
1636 3 9 27
1637 4 16 64
1638 5 25 125
1639 6 36 216
1640 7 49 343
1641 8 64 512
1642 9 81 729
164310 100 1000
1644>>>
1645\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001646%
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001647(Note that one space between each column was added by the way {\tt print}
1648works: it always adds spaces between its arguments.)
1649
1650This example demonstrates the function {\tt string.rjust()}, which
1651right-justifies a string in a field of a given width by padding it with
1652spaces on the left. There are similar functions {\tt string.ljust()}
1653and {\tt string.center()}. These functions do not write anything, they
1654just return a new string. If the input string is too long, they don't
1655truncate it, but return it unchanged; this will mess up your column
1656lay-out but that's usually better than the alternative, which would be
1657lying about a value. (If you really want truncation you can always add
1658a slice operation, as in {\tt string.ljust(x,~n)[0:n]}.)
1659
1660There is another function, {\tt string.zfill}, which pads a numeric
1661string on the left with zeros. It understands about plus and minus
1662signs:%
1663\footnote{
1664 Better facilities for formatting floating point numbers are
1665 lacking at this moment.
1666}
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001667
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001668\bcode\begin{verbatim}
1669>>> string.zfill('12', 5)
1670'00012'
1671>>> string.zfill('-3.14', 7)
1672'-003.14'
1673>>> string.zfill('3.14159265359', 5)
1674'3.14159265359'
1675>>>
1676\end{verbatim}\ecode
1677
1678\chapter{Errors and Exceptions}
1679
1680Until now error messages haven't been more than mentioned, but if you
1681have tried out the examples you have probably seen some. There are
1682(at least) two distinguishable kinds of errors: {\em syntax\ errors}
1683and {\em exceptions}.
1684
1685\section{Syntax Errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001686
1687Syntax errors, also known as parsing errors, are perhaps the most common
Guido van Rossum4410c751991-06-04 20:22:18 +00001688kind of complaint you get while you are still learning Python:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001689
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001690\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001691>>> while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001692Parsing error: file <stdin>, line 1:
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001693while 1 print 'Hello world'
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001694 ^
1695Unhandled exception: run-time error: syntax error
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001696>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001697\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001698%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001699The parser repeats the offending line and displays a little `arrow'
1700pointing at the earliest point in the line where the error was detected.
1701The error is caused by (or at least detected at) the token
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001702{\em preceding}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001703the arrow: in the example, the error is detected at the keyword
1704{\tt print}, since a colon ({\tt :}) is missing before it.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001705File name and line number are printed so you know where to look in case
1706the input came from a script.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001707
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001708\section{Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001709
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001710Even if a statement or expression is syntactically correct, it may
1711cause an error when an attempt is made to execute it.
1712Errors detected during execution are called {\em exceptions} and are
1713not unconditionally fatal: you will soon learn how to handle them in
1714Python programs. Most exceptions are not handled by programs,
1715however, and result in error messages as shown here:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001716
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001717\bcode\small\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001718>>> 10 * (1/0)
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001719Unhandled exception: run-time error: integer division by zero
1720Stack backtrace (innermost last):
1721 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001722>>> 4 + foo*3
1723Unhandled exception: undefined name: foo
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001724Stack backtrace (innermost last):
1725 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001726>>> '2' + 2
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001727Unhandled exception: type error: illegal argument type for built-in operation
1728Stack backtrace (innermost last):
1729 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001730>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001731\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001732%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001733The first line of the error message indicates what happened.
1734Exceptions come in different types, and the type is printed as part of
1735the message: the types in the example are
1736{\tt run-time error},
1737{\tt undefined name}
1738and
1739{\tt type error}.
1740The rest of the line is a detail whose interpretation depends on the
1741exception type.
1742
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001743The rest of the error message shows the context where the
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001744exception happened.
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001745In general it contains a stack backtrace listing source lines; however,
1746it will not display lines read from standard input.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001747
1748Here is a summary of the most common exceptions:
1749\begin{itemize}
1750\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001751{\em Run-time\ errors}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001752are generally caused by wrong data used by the program; this can be the
1753programmer's fault or caused by bad input.
1754The detail states the cause of the error in more detail.
1755\item
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001756{\em Undefined\ name}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001757errors are more serious: these are usually caused by misspelled
1758identifiers.%
1759\footnote{
1760 The parser does not check whether names used in a program are at
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001761 all defined elsewhere in the program; such checks are
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001762 postponed until run-time. The same holds for type checking.
1763}
1764The detail is the offending identifier.
1765\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001766{\em Type\ errors} are also pretty serious: this is another case of
1767using wrong data (or better, using data the wrong way), but here the
1768error can be gleaned from the object type(s) alone. The detail shows
1769in what context the error was detected.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001770\end{itemize}
1771
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001772\section{Handling Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001773
1774It is possible to write programs that handle selected exceptions.
1775Look at the following example, which prints a table of inverses of
1776some floating point numbers:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001777
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001778\bcode\begin{verbatim}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001779>>> numbers = [0.3333, 2.5, 0, 10]
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001780>>> for x in numbers:
1781... print x,
1782... try:
1783... print 1.0 / x
1784... except RuntimeError:
1785... print '*** has no inverse ***'
1786...
17870.3333 3.00030003
17882.5 0.4
17890 *** has no inverse ***
179010 0.1
1791>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001792\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001793%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001794The {\tt try} statement works as follows.
1795\begin{itemize}
1796\item
1797First, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001798{\em try\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001799(the statement(s) between the {\tt try} and {\tt except} keywords) is
1800executed.
1801\item
1802If no exception occurs, the
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001803{\em except\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001804is skipped and execution of the {\tt try} statement is finished.
1805\item
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001806If an exception occurs during execution of the try clause,
1807the rest of the clause is skipped. Then if
1808its type matches the exception named after the {\tt except} keyword,
1809the rest of the try clause is skipped, the except clause is executed,
1810and then execution continues after the {\tt try} statement.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001811\item
1812If an exception occurs which does not match the exception named in the
1813except clause, it is passed on to outer try statements; if no handler is
1814found, it is an
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001815{\em unhandled\ exception}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001816and execution stops with a message as shown above.
1817\end{itemize}
1818A {\tt try} statement may have more than one except clause, to specify
1819handlers for different exceptions.
1820At most one handler will be executed.
1821Handlers only handle exceptions that occur in the corresponding try
1822clause, not in other handlers of the same {\tt try} statement.
1823An except clause may name multiple exceptions as a parenthesized list,
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001824e.g.:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001825
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001826\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001827... except (RuntimeError, TypeError, NameError):
1828... pass
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001829\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001830%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001831The last except clause may omit the exception name(s), to serve as a
1832wildcard.
1833Use this with extreme caution!
1834
1835When an exception occurs, it may have an associated value, also known as
1836the exceptions's
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001837{\em argument}.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001838The presence and type of the argument depend on the exception type.
1839For exception types which have an argument, the except clause may
1840specify a variable after the exception name (or list) to receive the
1841argument's value, as follows:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001842
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001843\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001844>>> try:
1845... foo()
1846... except NameError, x:
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001847... print 'name', x, 'undefined'
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001848...
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001849name foo undefined
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001850>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001851\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001852%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001853If an exception has an argument, it is printed as the third part
1854(`detail') of the message for unhandled exceptions.
1855
1856Standard exception names are built-in identifiers (not reserved
1857keywords).
1858These are in fact string objects whose
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001859{\em object\ identity}
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001860(not their value!) identifies the exceptions.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001861The string is printed as the second part of the message for unhandled
1862exceptions.
1863Their names and values are:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001864
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001865\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001866EOFError 'end-of-file read'
1867KeyboardInterrupt 'keyboard interrupt'
1868MemoryError 'out of memory' *
1869NameError 'undefined name' *
1870RuntimeError 'run-time error' *
1871SystemError 'system error' *
1872TypeError 'type error' *
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001873\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001874%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001875The meanings should be clear enough.
1876Those exceptions with a {\tt *} in the third column have an argument.
1877
1878Exception handlers don't just handle exceptions if they occur
1879immediately in the try clause, but also if they occur inside functions
1880that are called (even indirectly) in the try clause.
1881For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001882
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001883\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001884>>> def this_fails():
1885... x = 1/0
1886...
1887>>> try:
1888... this_fails()
1889... except RuntimeError, detail:
1890... print 'Handling run-time error:', detail
1891...
Guido van Rossum67fa1601991-04-23 14:14:57 +00001892Handling run-time error: integer division by zero
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001893>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001894\end{verbatim}\ecode
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001895
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001896\section{Raising Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001897
1898The {\tt raise} statement allows the programmer to force a specified
1899exception to occur.
1900For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001901
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001902\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001903>>> raise NameError, 'Hi There!'
1904Unhandled exception: undefined name: Hi There!
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001905Stack backtrace (innermost last):
1906 File "<stdin>", line 1
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001907>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001908\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001909%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001910The first argument to {\tt raise} names the exception to be raised.
1911The optional second argument specifies the exception's argument.
1912
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001913\section{User-defined Exceptions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001914
1915Programs may name their own exceptions by assigning a string to a
1916variable.
1917For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001918
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001919\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001920>>> my_exc = 'nobody likes me!'
1921>>> try:
1922... raise my_exc, 2*2
1923... except my_exc, val:
Guido van Rossum67fa1601991-04-23 14:14:57 +00001924... print 'My exception occurred, value:', val
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001925...
1926My exception occured, value: 4
1927>>> raise my_exc, 1
1928Unhandled exception: nobody likes me!: 1
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001929Stack backtrace (innermost last):
1930 File "<stdin>", line 7
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001931>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001932\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001933%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001934Many standard modules use this to report errors that may occur in
1935functions they define.
1936
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001937\section{Defining Clean-up Actions}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001938
1939The {\tt try} statement has another optional clause which is intended to
1940define clean-up actions that must be executed under all circumstances.
1941For example:
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001942
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001943\bcode\begin{verbatim}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001944>>> try:
1945... raise KeyboardInterrupt
1946... finally:
1947... print 'Goodbye, world!'
1948...
1949Goodbye, world!
1950Unhandled exception: keyboard interrupt
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001951Stack backtrace (innermost last):
1952 File "<stdin>", line 2
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001953>>>
Guido van Rossum5ce78f11991-01-25 13:27:18 +00001954\end{verbatim}\ecode
Guido van Rossuma8d754e1992-01-07 16:44:35 +00001955%
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001956The
Guido van Rossum2292b8e1991-01-23 16:31:24 +00001957{\em finally\ clause}
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001958must follow the except clauses(s), if any.
Guido van Rossum6fc178f1991-08-16 09:13:42 +00001959It is executed whether or not an exception occurred,
1960or whether or not an exception is handled.
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001961If the exception is handled, the finally clause is executed after the
1962handler (and even if another exception occurred in the handler).
1963It is also executed when the {\tt try} statement is left via a
1964{\tt break} or {\tt return} statement.
1965
Guido van Rossumd9bf55d1991-01-11 16:35:08 +00001966\end{document}