Changed code environment into \bcode and \ecode macros.

Small lay-out improvements.
Took out a ref or two to "python -s".
This commit is contained in:
Guido van Rossum 1991-01-25 13:27:18 +00:00
parent d38b7648c0
commit 5ce78f11fc
2 changed files with 234 additions and 274 deletions

View File

@ -1,7 +1,7 @@
% Format this file with latex. % Format this file with latex.
\documentstyle[palatino,11pt,myformat]{article} %\documentstyle[palatino,11pt,myformat]{article}
%\documentstyle[11pt,myformat]{article} \documentstyle[11pt,myformat]{article}
\title{\bf \title{\bf
Python Tutorial \\ Python Tutorial \\
@ -144,9 +144,9 @@ on those machines where it is available; putting
{\tt /usr/local} {\tt /usr/local}
in your \UNIX\ shell's search path makes it possible to start it by in your \UNIX\ shell's search path makes it possible to start it by
typing the command typing the command
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
python python
\end{verbatim}\end{code} \end{verbatim}\ecode
to the shell. to the shell.
Since the choice of the directory where the interpreter lives is an Since the choice of the directory where the interpreter lives is an
installation option, other places instead of installation option, other places instead of
@ -237,34 +237,17 @@ is not set, an installation-dependent default path is used, usually
{\tt PYTHONPATH} or from the installation-dependent default. {\tt PYTHONPATH} or from the installation-dependent default.
See the section on Standard Modules later. See the section on Standard Modules later.
} }
The built-in module
{\tt stdwin},
if supported at all, is only available if the interpreter is started
with the
{\bf --s}
flag.
If this flag is given, stdwin is initialized as soon as the interpreter
is started, and in the case of X11 stdwin certain command line arguments
(like
{\bf --display} )
are consumed by stdwin.
On BSD'ish \UNIX\ systems, \Python\ scripts can be made directly executable, On BSD'ish \UNIX\ systems, \Python\ scripts can be made directly executable,
like shell scripts, by putting the line like shell scripts, by putting the line
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
#! /usr/local/python #! /usr/local/python
\end{verbatim}\end{code} \end{verbatim}\ecode
(assuming that's the name of the interpreter) at the beginning of the (assuming that's the name of the interpreter) at the beginning of the
script and giving the file an executable mode. script and giving the file an executable mode.
(The (The
{\tt \#!} {\tt \#!}
must be the first two characters of the file.) must be the first two characters of the file.)
For scripts that use the built-in module
{\tt stdwin},
use
\begin{code}\begin{verbatim}
#! /usr/local/python -s
\end{verbatim}\end{code}
\subsection{Interactive Input Editing and History Substitution} \subsection{Interactive Input Editing and History Substitution}
@ -312,15 +295,15 @@ The key bindings and some other parameters of the Readline library can
be customized by placing commands in an initialization file called be customized by placing commands in an initialization file called
{\tt \$HOME/.initrc}. {\tt \$HOME/.initrc}.
Key bindings have the form Key bindings have the form
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
key-name: function-name key-name: function-name
\end{verbatim}\end{code} \end{verbatim}\ecode
and options can be set with and options can be set with
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
set option-name value set option-name value
\end{verbatim}\end{code} \end{verbatim}\ecode
Example: Example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
# I prefer vi-style editing: # I prefer vi-style editing:
set editing-mode vi set editing-mode vi
# Edit using a single line: # Edit using a single line:
@ -328,13 +311,13 @@ set horizontal-scroll-mode On
# Rebind some keys: # Rebind some keys:
Meta-h: backward-kill-word Meta-h: backward-kill-word
Control-u: universal-argument Control-u: universal-argument
\end{verbatim}\end{code} \end{verbatim}\ecode
Note that the default binding for TAB in \Python\ is to insert a TAB Note that the default binding for TAB in \Python\ is to insert a TAB
instead of Readline's default filename completion function. instead of Readline's default filename completion function.
If you insist, you can override this by putting If you insist, you can override this by putting
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
TAB: complete TAB: complete
\end{verbatim}\end{code} \end{verbatim}\ecode
in your in your
{\tt \$HOME/.inputrc}. {\tt \$HOME/.inputrc}.
(Of course, this makes it hard to type indented continuation lines.) (Of course, this makes it hard to type indented continuation lines.)
@ -374,7 +357,7 @@ and
work just as in most other languages (e.g., Pascal or C); parentheses work just as in most other languages (e.g., Pascal or C); parentheses
can be used for grouping. can be used for grouping.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> # This is a comment >>> # This is a comment
>>> 2+2 >>> 2+2
4 4
@ -385,48 +368,46 @@ For example:
>>> 7/3 >>> 7/3
2 2
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
As in C, the equal sign ({\tt =}) is used to assign a value to a variable. As in C, the equal sign ({\tt =}) is used to assign a value to a variable.
The value of an assignment is not written: The value of an assignment is not written:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> width = 20 >>> width = 20
>>> height = 5*9 >>> height = 5*9
>>> width * height >>> width * height
900 900
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
There is some support for floating point, but you can't mix floating There is some support for floating point, but you can't mix floating
point and integral numbers in expression (yet): point and integral numbers in expression (yet):
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> 10.0 / 3.3 >>> 10.0 / 3.3
3.0303030303 3.0303030303
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Besides numbers, \Python\ can also manipulate strings, enclosed in single Besides numbers, \Python\ can also manipulate strings, enclosed in single
quotes: quotes:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> 'foo bar' >>> 'foo bar'
'foo bar' 'foo bar'
>>> 'doesn\'t' >>> 'doesn\'t'
'doesn\'t' 'doesn\'t'
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Strings are written inside quotes and with quotes and other funny Strings are written inside quotes and with quotes and other funny
characters escaped by backslashes, to show the precise value. characters escaped by backslashes, to show the precise value.
(There is also a way to write strings without quotes and escapes.) (There is also a way to write strings without quotes and escapes.)
Strings can be concatenated (glued together) with the Strings can be concatenated (glued together) with the
{\tt +} {\tt +}
operator, and repeated with operator, and repeated with~{\tt *}:
{\tt *}: \bcode\begin{verbatim}
\begin{code}\begin{verbatim}
>>> word = 'Help' + 'A' >>> word = 'Help' + 'A'
>>> word >>> word
'HelpA' 'HelpA'
>>> '<' + word*5 + '>' >>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>' '<HelpAHelpAHelpAHelpAHelpA>'
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Strings can be subscripted; as in C, the first character of a string has Strings can be subscripted; as in C, the first character of a string has
subscript 0. subscript 0.
There is no separate character type; a character is simply a string of There is no separate character type; a character is simply a string of
@ -434,7 +415,7 @@ size one.
As in Icon, substrings can be specified with the As in Icon, substrings can be specified with the
{\em slice} {\em slice}
notation: two subscripts (indices) separated by a colon. notation: two subscripts (indices) separated by a colon.
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> word[4] >>> word[4]
'A' 'A'
>>> word[0:2] >>> word[0:2]
@ -450,11 +431,11 @@ notation: two subscripts (indices) separated by a colon.
>>> word[:3] + word[3:] >>> word[:3] + word[3:]
'HelpA' 'HelpA'
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Degenerate cases are handled gracefully: an index that is too large is Degenerate cases are handled gracefully: an index that is too large is
replaced by the string size, an upper bound smaller than the lower bound replaced by the string size, an upper bound smaller than the lower bound
returns an empty string. returns an empty string.
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> word[1:100] >>> word[1:100]
'elpA' 'elpA'
>>> word[10:] >>> word[10:]
@ -462,11 +443,11 @@ returns an empty string.
>>> word[2:1] >>> word[2:1]
'' ''
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Slice indices (but not simple subscripts) may be negative numbers, to Slice indices (but not simple subscripts) may be negative numbers, to
start counting from the right. start counting from the right.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> word[-2:] # Take last two characters >>> word[-2:] # Take last two characters
'pA' 'pA'
>>> word[:-2] # Drop last two characters >>> word[:-2] # Drop last two characters
@ -475,7 +456,7 @@ For example:
>>> word[-0:] # (since -0 equals 0) >>> word[-0:] # (since -0 equals 0)
'HelpA' 'HelpA'
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The best way to remember how slices work is to think of the indices as The best way to remember how slices work is to think of the indices as
pointing pointing
{\em between} {\em between}
@ -485,13 +466,13 @@ Then the right edge of the last character of a string of
characters has index characters has index
{\tt n}, {\tt n},
for example: for example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
+---+---+---+---+---+ +---+---+---+---+---+
| H | e | l | p | A | | H | e | l | p | A |
+---+---+---+---+---+ +---+---+---+---+---+
0 1 2 3 4 5 0 1 2 3 4 5
-5 -4 -3 -2 -1 -5 -4 -3 -2 -1
\end{verbatim}\end{code} \end{verbatim}\ecode
The first row of numbers gives the position of the indices 0...5 in the The first row of numbers gives the position of the indices 0...5 in the
string; the second row gives the corresponding negative indices. string; the second row gives the corresponding negative indices.
For nonnegative indices, the length of a slice is the difference of the For nonnegative indices, the length of a slice is the difference of the
@ -503,13 +484,12 @@ is 3--1 = 2.
Finally, the built-in function {\tt len()} computes the length of a Finally, the built-in function {\tt len()} computes the length of a
string: string:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> s = 'supercalifragilisticexpialidocious' >>> s = 'supercalifragilisticexpialidocious'
>>> len(s) >>> len(s)
34 34
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\Python\ knows a number of \Python\ knows a number of
{\em compound} {\em compound}
data types, used to group together other values. data types, used to group together other values.
@ -517,42 +497,42 @@ The most versatile is the
{\em list}, {\em list},
which can be written as a list of comma-separated values between square which can be written as a list of comma-separated values between square
brackets: brackets:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a = ['foo', 'bar', 100, 1234] >>> a = ['foo', 'bar', 100, 1234]
>>> a >>> a
['foo', 'bar', 100, 1234] ['foo', 'bar', 100, 1234]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
As for strings, list subscripts start at 0: As for strings, list subscripts start at 0:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a[0] >>> a[0]
'foo' 'foo'
>>> a[3] >>> a[3]
1234 1234
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Lists can be sliced and concatenated like strings: Lists can be sliced and concatenated like strings:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a[1:3] >>> a[1:3]
['bar', 100] ['bar', 100]
>>> a[:2] + ['bletch', 2*2] >>> a[:2] + ['bletch', 2*2]
['foo', 'bar', 'bletch', 4] ['foo', 'bar', 'bletch', 4]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Unlike strings, which are Unlike strings, which are
{\em immutable}, {\em immutable},
it is possible to change individual elements of a list: it is possible to change individual elements of a list:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a >>> a
['foo', 'bar', 100, 1234] ['foo', 'bar', 100, 1234]
>>> a[2] = a[2] + 23 >>> a[2] = a[2] + 23
>>> a >>> a
['foo', 'bar', 123, 1234] ['foo', 'bar', 123, 1234]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Assignment to slices is also possible, and this may even change the size Assignment to slices is also possible, and this may even change the size
of the list: of the list:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> # Replace some items: >>> # Replace some items:
>>> a[0:2] = [1, 12] >>> a[0:2] = [1, 12]
>>> a >>> a
@ -566,13 +546,13 @@ of the list:
>>> a >>> a
[123, 'bletch', 'xyzzy', 1234] [123, 'bletch', 'xyzzy', 1234]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The built-in function {\tt len()} also applies to lists: The built-in function {\tt len()} also applies to lists:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> len(a) >>> len(a)
4 4
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsection{Tuples and Sequences} \subsection{Tuples and Sequences}
@ -585,7 +565,7 @@ and two together.
For instance, we can write an initial subsequence of the For instance, we can write an initial subsequence of the
{\em Fibonacci} {\em Fibonacci}
series as follows: series as follows:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> # Fibonacci series: >>> # Fibonacci series:
>>> # the sum of two elements defines the next >>> # the sum of two elements defines the next
>>> a, b = 0, 1 >>> a, b = 0, 1
@ -605,7 +585,7 @@ series as follows:
55 55
89 89
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
This example introduces several new features. This example introduces several new features.
\begin{itemize} \begin{itemize}
\item \item
@ -661,14 +641,14 @@ earlier in the calculator examples) in the way it handles multiple
expressions and strings. expressions and strings.
Strings are written without quotes and a space is inserted between Strings are written without quotes and a space is inserted between
items, so you can format things nicely, like this: items, so you can format things nicely, like this:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> i = 256*256 >>> i = 256*256
>>> print 'The value of i is', i >>> print 'The value of i is', i
The value of i is 65536 The value of i is 65536
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
A trailing comma avoids the newline after the output: A trailing comma avoids the newline after the output:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a, b = 0, 1 >>> a, b = 0, 1
>>> while b < 1000: >>> while b < 1000:
... print b, ... print b,
@ -676,7 +656,7 @@ A trailing comma avoids the newline after the output:
... ...
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Note that the interpreter inserts a newline before it prints the next Note that the interpreter inserts a newline before it prints the next
prompt if the last line was not completed. prompt if the last line was not completed.
\end{itemize} \end{itemize}
@ -691,7 +671,7 @@ twists.
Perhaps the most well-known statement type is the {\tt if} statement. Perhaps the most well-known statement type is the {\tt if} statement.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> if x < 0: >>> if x < 0:
... x = 0 ... x = 0
... print 'Negative changed to zero' ... print 'Negative changed to zero'
@ -702,7 +682,7 @@ For example:
... else: ... else:
... print 'More' ... print 'More'
... ...
\end{verbatim}\end{code} \end{verbatim}\ecode
There can be zero or more {\tt elif} parts, and the {\tt else} part is There can be zero or more {\tt elif} parts, and the {\tt else} part is
optional. optional.
The keyword `{\tt elif}' is short for `{\tt else if}', and is useful to The keyword `{\tt elif}' is short for `{\tt else if}', and is useful to
@ -719,7 +699,7 @@ Rather than always iterating over an arithmetic progression of numbers
and step (as C), \Python's {\tt for} statement iterates over the items and step (as C), \Python's {\tt for} statement iterates over the items
of any sequence (e.g., a list or a string). of any sequence (e.g., a list or a string).
For example (no pun intended): For example (no pun intended):
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> # Measure some strings: >>> # Measure some strings:
>>> a = ['cat', 'window', 'defenestrate'] >>> a = ['cat', 'window', 'defenestrate']
>>> for x in a: >>> for x in a:
@ -729,7 +709,7 @@ cat 3
window 6 window 6
defenestrate 12 defenestrate 12
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{The {\tt range()} Function} \subsubsection{The {\tt range()} Function}
@ -737,17 +717,17 @@ If you do need to iterate over a sequence of numbers, the built-in
function {\tt range()} comes in handy. function {\tt range()} comes in handy.
It generates lists containing arithmetic progressions, It generates lists containing arithmetic progressions,
e.g.: e.g.:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> range(10) >>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The given end point is never part of the generated list; The given end point is never part of the generated list;
{\tt range(10)} generates a list of 10 values, {\tt range(10)} generates a list of 10 values,
exactly the legal indices for items of a sequence of length 10. exactly the legal indices for items of a sequence of length 10.
It is possible to let the range start at another number, or to specify a It is possible to let the range start at another number, or to specify a
different increment (even negative): different increment (even negative):
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> range(5, 10) >>> range(5, 10)
[5, 6, 7, 8, 9] [5, 6, 7, 8, 9]
>>> range(0, 10, 3) >>> range(0, 10, 3)
@ -755,10 +735,10 @@ different increment (even negative):
>>> range(-10, -100, -30) >>> range(-10, -100, -30)
[-10, -40, -70] [-10, -40, -70]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
To iterate over the indices of a sequence, combine {\tt range()} To iterate over the indices of a sequence, combine {\tt range()}
and {\tt len()} as follows: and {\tt len()} as follows:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a = ['Mary', 'had', 'a', 'little', 'boy'] >>> a = ['Mary', 'had', 'a', 'little', 'boy']
>>> for i in range(len(a)): >>> for i in range(len(a)):
... print i, a[i] ... print i, a[i]
@ -769,7 +749,7 @@ and {\tt len()} as follows:
3 little 3 little
4 boy 4 boy
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{Break Statements and Else Clauses on Loops} \subsubsection{Break Statements and Else Clauses on Loops}
@ -781,7 +761,7 @@ the condition becomes false (with {\tt while}) but not when the loop is
terminated by a {\tt break} statement. terminated by a {\tt break} statement.
This is exemplified by the following loop, which searches for a list This is exemplified by the following loop, which searches for a list
item of value 0: item of value 0:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> for n in range(2, 10): >>> for n in range(2, 10):
... for x in range(2, n): ... for x in range(2, n):
... if n % x = 0: ... if n % x = 0:
@ -799,7 +779,7 @@ item of value 0:
8 equals 2 * 4 8 equals 2 * 4
9 equals 3 * 3 9 equals 3 * 3
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{Pass Statements} \subsubsection{Pass Statements}
@ -807,11 +787,11 @@ The {\tt pass} statement does nothing.
It can be used when a statement is required syntactically but the It can be used when a statement is required syntactically but the
program requires no action. program requires no action.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> while 1: >>> while 1:
... pass # Busy-wait for keyboard interrupt ... pass # Busy-wait for keyboard interrupt
... ...
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{Conditions Revisited} \subsubsection{Conditions Revisited}
@ -821,7 +801,7 @@ XXX To Be Done.
We can create a function that writes the Fibonacci series to an We can create a function that writes the Fibonacci series to an
arbitrary boundary: arbitrary boundary:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> def fib(n): # write Fibonacci series up to n >>> def fib(n): # write Fibonacci series up to n
... a, b = 0, 1 ... a, b = 0, 1
... while b <= n: ... while b <= n:
@ -832,7 +812,7 @@ arbitrary boundary:
>>> fib(2000) >>> fib(2000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The keyword The keyword
{\tt def} {\tt def}
introduces a function introduces a function
@ -872,14 +852,14 @@ user-defined function.
This value can be assigned to another name which can then also be used This value can be assigned to another name which can then also be used
as a function. as a function.
This serves as a general renaming mechanism: This serves as a general renaming mechanism:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> fib >>> fib
<function object at 10042ed0> <function object at 10042ed0>
>>> f = fib >>> f = fib
>>> f(100) >>> f(100)
1 1 2 3 5 8 13 21 34 55 89 1 1 2 3 5 8 13 21 34 55 89
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
You might object that You might object that
{\tt fib} {\tt fib}
is not a function but a procedure. is not a function but a procedure.
@ -891,14 +871,14 @@ This value is called {\tt None} (it's a built-in name).
Writing the value {\tt None} is normally suppressed by the interpreter Writing the value {\tt None} is normally suppressed by the interpreter
if it would be the only value written. if it would be the only value written.
You can see it if you really want to: You can see it if you really want to:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> print fib(0) >>> print fib(0)
None None
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
It is simple to write a function that returns a list of the numbers of It is simple to write a function that returns a list of the numbers of
the Fibonacci series, instead of printing it: the Fibonacci series, instead of printing it:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> def fib2(n): # return Fibonacci series up to n >>> def fib2(n): # return Fibonacci series up to n
... result = [] ... result = []
... a, b = 0, 1 ... a, b = 0, 1
@ -911,7 +891,7 @@ the Fibonacci series, instead of printing it:
>>> f100 # write the result >>> f100 # write the result
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
This example, as usual, demonstrates some new \Python\ features: This example, as usual, demonstrates some new \Python\ features:
\begin{itemize} \begin{itemize}
\item \item
@ -963,7 +943,7 @@ so {\tt a.insert(0, x)} inserts at the front of the list, and
Sorts the elements of the list. Sorts the elements of the list.
\end{description} \end{description}
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a = [10, 100, 1, 1000] >>> a = [10, 100, 1, 1000]
>>> a.insert(2, -1) >>> a.insert(2, -1)
>>> a >>> a
@ -977,7 +957,7 @@ For example:
>>> b >>> b
['Mary', 'a', 'boy', 'had', 'little'] ['Mary', 'a', 'boy', 'had', 'little']
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsection{Modules} \subsection{Modules}
@ -1010,7 +990,7 @@ appended.
For instance, use your favorite text editor to create a file called For instance, use your favorite text editor to create a file called
{\tt fibo.py} {\tt fibo.py}
in the current directory with the following contents: in the current directory with the following contents:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
# Fibonacci numbers module # Fibonacci numbers module
def fib(n): # write Fibonacci series up to n def fib(n): # write Fibonacci series up to n
@ -1026,33 +1006,33 @@ def fib2(n): # return Fibonacci series up to n
ret.append(b) ret.append(b)
a, b = b, a+b a, b = b, a+b
return ret return ret
\end{verbatim}\end{code} \end{verbatim}\ecode
Now enter the \Python\ interpreter and import this module with the Now enter the \Python\ interpreter and import this module with the
following command: following command:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> import fibo >>> import fibo
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
This does not enter the names of the functions defined in This does not enter the names of the functions defined in
{\tt fibo} {\tt fibo}
directly in the symbol table; it only enters the module name directly in the symbol table; it only enters the module name
{\tt fibo} {\tt fibo}
there. there.
Using the module name you can access the functions: Using the module name you can access the functions:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> fibo.fib(1000) >>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100) >>> fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
If you intend to use a function often you can assign it to a local name: If you intend to use a function often you can assign it to a local name:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> fib = fibo.fib >>> fib = fibo.fib
>>> fib(500) >>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{More on Modules} \subsubsection{More on Modules}
@ -1090,23 +1070,23 @@ There is a variant of the
statement that imports names from a module directly into the importing statement that imports names from a module directly into the importing
module's symbol table. module's symbol table.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> from fibo import fib, fib2 >>> from fibo import fib, fib2
>>> fib(500) >>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
This does not introduce the module name from which the imports are taken This does not introduce the module name from which the imports are taken
in the local symbol table (so in the example, {\tt fibo} is not in the local symbol table (so in the example, {\tt fibo} is not
defined). defined).
There is even a variant to import all names that a module defines: There is even a variant to import all names that a module defines:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> from fibo import * >>> from fibo import *
>>> fib(500) >>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
This imports all names except those beginning with an underscore This imports all names except those beginning with an underscore
({\tt \_}). ({\tt \_}).
@ -1130,7 +1110,7 @@ The variables
and and
{\tt sys.ps2} {\tt sys.ps2}
define the strings used as primary and secondary prompts: define the strings used as primary and secondary prompts:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> import sys >>> import sys
>>> sys.ps1 >>> sys.ps1
'>>> ' '>>> '
@ -1140,7 +1120,7 @@ define the strings used as primary and secondary prompts:
C> print 'Yuck!' C> print 'Yuck!'
Yuck! Yuck!
C> C>
\end{verbatim}\end{code} \end{verbatim}\ecode
These two variables are only defined if the interpreter is in These two variables are only defined if the interpreter is in
interactive mode. interactive mode.
@ -1154,11 +1134,11 @@ or from a built-in default if
{\tt PYTHONPATH} {\tt PYTHONPATH}
is not set. is not set.
You can modify it using standard list operations, e.g.: You can modify it using standard list operations, e.g.:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> import sys >>> import sys
>>> sys.path.append('/ufs/guido/lib/python') >>> sys.path.append('/ufs/guido/lib/python')
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsection{Errors and Exceptions} \subsection{Errors and Exceptions}
@ -1173,14 +1153,14 @@ and
Syntax errors, also known as parsing errors, are perhaps the most common Syntax errors, also known as parsing errors, are perhaps the most common
kind of complaint you get while you are still learning \Python: kind of complaint you get while you are still learning \Python:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> while 1 print 'Hello world' >>> while 1 print 'Hello world'
Parsing error: file <stdin>, line 1: Parsing error: file <stdin>, line 1:
while 1 print 'Hello world' while 1 print 'Hello world'
^ ^
Unhandled exception: run-time error: syntax error Unhandled exception: run-time error: syntax error
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The parser repeats the offending line and displays a little `arrow' The parser repeats the offending line and displays a little `arrow'
pointing at the earliest point in the line where the error was detected. pointing at the earliest point in the line where the error was detected.
The error is caused by (or at least detected at) the token The error is caused by (or at least detected at) the token
@ -1194,7 +1174,7 @@ the input came from a script.
Even if a statement or expression is syntactically correct, it may cause Even if a statement or expression is syntactically correct, it may cause
an error when an attempt is made to execute it: an error when an attempt is made to execute it:
\begin{code}\begin{verbatim} \bcode\small\begin{verbatim}
>>> 10 * (1/0) >>> 10 * (1/0)
Unhandled exception: run-time error: integer division by zero Unhandled exception: run-time error: integer division by zero
Stack backtrace (innermost last): Stack backtrace (innermost last):
@ -1208,7 +1188,7 @@ Unhandled exception: type error: illegal argument type for built-in operation
Stack backtrace (innermost last): Stack backtrace (innermost last):
File "<stdin>", line 1 File "<stdin>", line 1
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Errors detected during execution are called Errors detected during execution are called
{\em exceptions} {\em exceptions}
and are not unconditionally fatal: you will soon learn how to handle and are not unconditionally fatal: you will soon learn how to handle
@ -1261,7 +1241,7 @@ The detail shows in what context the error was detected.
It is possible to write programs that handle selected exceptions. It is possible to write programs that handle selected exceptions.
Look at the following example, which prints a table of inverses of Look at the following example, which prints a table of inverses of
some floating point numbers: some floating point numbers:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> numbers = [0.3333, 2.5, 0.0, 10.0] >>> numbers = [0.3333, 2.5, 0.0, 10.0]
>>> for x in numbers: >>> for x in numbers:
... print x, ... print x,
@ -1275,7 +1255,7 @@ some floating point numbers:
0 *** has no inverse *** 0 *** has no inverse ***
10 0.1 10 0.1
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The {\tt try} statement works as follows. The {\tt try} statement works as follows.
\begin{itemize} \begin{itemize}
\item \item
@ -1306,10 +1286,10 @@ Handlers only handle exceptions that occur in the corresponding try
clause, not in other handlers of the same {\tt try} statement. clause, not in other handlers of the same {\tt try} statement.
An except clause may name multiple exceptions as a parenthesized list, An except clause may name multiple exceptions as a parenthesized list,
e.g.: e.g.:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
... except (RuntimeError, TypeError, NameError): ... except (RuntimeError, TypeError, NameError):
... pass ... pass
\end{verbatim}\end{code} \end{verbatim}\ecode
The last except clause may omit the exception name(s), to serve as a The last except clause may omit the exception name(s), to serve as a
wildcard. wildcard.
Use this with extreme caution! Use this with extreme caution!
@ -1321,7 +1301,7 @@ The presence and type of the argument depend on the exception type.
For exception types which have an argument, the except clause may For exception types which have an argument, the except clause may
specify a variable after the exception name (or list) to receive the specify a variable after the exception name (or list) to receive the
argument's value, as follows: argument's value, as follows:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> try: >>> try:
... foo() ... foo()
... except NameError, x: ... except NameError, x:
@ -1329,7 +1309,7 @@ argument's value, as follows:
... ...
name foo undefined name foo undefined
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
If an exception has an argument, it is printed as the third part If an exception has an argument, it is printed as the third part
(`detail') of the message for unhandled exceptions. (`detail') of the message for unhandled exceptions.
@ -1346,7 +1326,7 @@ These are in fact string objects whose
The string is printed as the second part of the message for unhandled The string is printed as the second part of the message for unhandled
exceptions. exceptions.
Their names and values are: Their names and values are:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
EOFError 'end-of-file read' EOFError 'end-of-file read'
KeyboardInterrupt 'keyboard interrupt' KeyboardInterrupt 'keyboard interrupt'
MemoryError 'out of memory' * MemoryError 'out of memory' *
@ -1354,7 +1334,7 @@ NameError 'undefined name' *
RuntimeError 'run-time error' * RuntimeError 'run-time error' *
SystemError 'system error' * SystemError 'system error' *
TypeError 'type error' * TypeError 'type error' *
\end{verbatim}\end{code} \end{verbatim}\ecode
The meanings should be clear enough. The meanings should be clear enough.
Those exceptions with a {\tt *} in the third column have an argument. Those exceptions with a {\tt *} in the third column have an argument.
@ -1362,7 +1342,7 @@ Exception handlers don't just handle exceptions if they occur
immediately in the try clause, but also if they occur inside functions immediately in the try clause, but also if they occur inside functions
that are called (even indirectly) in the try clause. that are called (even indirectly) in the try clause.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> def this_fails(): >>> def this_fails():
... x = 1/0 ... x = 1/0
... ...
@ -1373,20 +1353,20 @@ For example:
... ...
Handling run-time error: domain error or zero division Handling run-time error: domain error or zero division
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{Raising Exceptions} \subsubsection{Raising Exceptions}
The {\tt raise} statement allows the programmer to force a specified The {\tt raise} statement allows the programmer to force a specified
exception to occur. exception to occur.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> raise NameError, 'Hi There!' >>> raise NameError, 'Hi There!'
Unhandled exception: undefined name: Hi There! Unhandled exception: undefined name: Hi There!
Stack backtrace (innermost last): Stack backtrace (innermost last):
File "<stdin>", line 1 File "<stdin>", line 1
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The first argument to {\tt raise} names the exception to be raised. The first argument to {\tt raise} names the exception to be raised.
The optional second argument specifies the exception's argument. The optional second argument specifies the exception's argument.
@ -1395,7 +1375,7 @@ The optional second argument specifies the exception's argument.
Programs may name their own exceptions by assigning a string to a Programs may name their own exceptions by assigning a string to a
variable. variable.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> my_exc = 'nobody likes me!' >>> my_exc = 'nobody likes me!'
>>> try: >>> try:
... raise my_exc, 2*2 ... raise my_exc, 2*2
@ -1408,7 +1388,7 @@ Unhandled exception: nobody likes me!: 1
Stack backtrace (innermost last): Stack backtrace (innermost last):
File "<stdin>", line 7 File "<stdin>", line 7
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Many standard modules use this to report errors that may occur in Many standard modules use this to report errors that may occur in
functions they define. functions they define.
@ -1417,7 +1397,7 @@ functions they define.
The {\tt try} statement has another optional clause which is intended to The {\tt try} statement has another optional clause which is intended to
define clean-up actions that must be executed under all circumstances. define clean-up actions that must be executed under all circumstances.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> try: >>> try:
... raise KeyboardInterrupt ... raise KeyboardInterrupt
... finally: ... finally:
@ -1428,7 +1408,7 @@ Unhandled exception: keyboard interrupt
Stack backtrace (innermost last): Stack backtrace (innermost last):
File "<stdin>", line 2 File "<stdin>", line 2
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The The
{\em finally\ clause} {\em finally\ clause}
must follow the except clauses(s), if any. must follow the except clauses(s), if any.
@ -1489,7 +1469,7 @@ Consider the following example, which defines a class {\tt Set}
representing a (finite) mathematical set with operations to add and representing a (finite) mathematical set with operations to add and
remove elements, a membership test, and a request for the size of the remove elements, a membership test, and a request for the size of the
set. set.
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
class Set(): class Set():
def new(self): def new(self):
self.elements = [] self.elements = []
@ -1507,7 +1487,7 @@ class Set():
return e in self.elements return e in self.elements
def size(self): def size(self):
return len(self.elements) return len(self.elements)
\end{verbatim}\end{code} \end{verbatim}\ecode
Note that the class definition looks like a big compound statement, Note that the class definition looks like a big compound statement,
with all the function definitons indented repective to the with all the function definitons indented repective to the
{\tt class} {\tt class}
@ -1518,7 +1498,7 @@ Let's assume that this
is the only contents of the module file is the only contents of the module file
{\tt SetClass.py}. {\tt SetClass.py}.
We can then use it in a \Python\ program as follows: We can then use it in a \Python\ program as follows:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> from SetClass import Set >>> from SetClass import Set
>>> a = Set().new() # create a Set object >>> a = Set().new() # create a Set object
>>> a.add(2) >>> a.add(2)
@ -1538,7 +1518,7 @@ a has 3 elements
>>> >>>
now a has 2 elements now a has 2 elements
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
From the example we learn in the first place that the functions defined From the example we learn in the first place that the functions defined
in the class (e.g., in the class (e.g.,
{\tt add}) {\tt add})

View File

@ -1,7 +1,7 @@
% Format this file with latex. % Format this file with latex.
\documentstyle[palatino,11pt,myformat]{article} %\documentstyle[palatino,11pt,myformat]{article}
%\documentstyle[11pt,myformat]{article} \documentstyle[11pt,myformat]{article}
\title{\bf \title{\bf
Python Tutorial \\ Python Tutorial \\
@ -144,9 +144,9 @@ on those machines where it is available; putting
{\tt /usr/local} {\tt /usr/local}
in your \UNIX\ shell's search path makes it possible to start it by in your \UNIX\ shell's search path makes it possible to start it by
typing the command typing the command
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
python python
\end{verbatim}\end{code} \end{verbatim}\ecode
to the shell. to the shell.
Since the choice of the directory where the interpreter lives is an Since the choice of the directory where the interpreter lives is an
installation option, other places instead of installation option, other places instead of
@ -237,34 +237,17 @@ is not set, an installation-dependent default path is used, usually
{\tt PYTHONPATH} or from the installation-dependent default. {\tt PYTHONPATH} or from the installation-dependent default.
See the section on Standard Modules later. See the section on Standard Modules later.
} }
The built-in module
{\tt stdwin},
if supported at all, is only available if the interpreter is started
with the
{\bf --s}
flag.
If this flag is given, stdwin is initialized as soon as the interpreter
is started, and in the case of X11 stdwin certain command line arguments
(like
{\bf --display} )
are consumed by stdwin.
On BSD'ish \UNIX\ systems, \Python\ scripts can be made directly executable, On BSD'ish \UNIX\ systems, \Python\ scripts can be made directly executable,
like shell scripts, by putting the line like shell scripts, by putting the line
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
#! /usr/local/python #! /usr/local/python
\end{verbatim}\end{code} \end{verbatim}\ecode
(assuming that's the name of the interpreter) at the beginning of the (assuming that's the name of the interpreter) at the beginning of the
script and giving the file an executable mode. script and giving the file an executable mode.
(The (The
{\tt \#!} {\tt \#!}
must be the first two characters of the file.) must be the first two characters of the file.)
For scripts that use the built-in module
{\tt stdwin},
use
\begin{code}\begin{verbatim}
#! /usr/local/python -s
\end{verbatim}\end{code}
\subsection{Interactive Input Editing and History Substitution} \subsection{Interactive Input Editing and History Substitution}
@ -312,15 +295,15 @@ The key bindings and some other parameters of the Readline library can
be customized by placing commands in an initialization file called be customized by placing commands in an initialization file called
{\tt \$HOME/.initrc}. {\tt \$HOME/.initrc}.
Key bindings have the form Key bindings have the form
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
key-name: function-name key-name: function-name
\end{verbatim}\end{code} \end{verbatim}\ecode
and options can be set with and options can be set with
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
set option-name value set option-name value
\end{verbatim}\end{code} \end{verbatim}\ecode
Example: Example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
# I prefer vi-style editing: # I prefer vi-style editing:
set editing-mode vi set editing-mode vi
# Edit using a single line: # Edit using a single line:
@ -328,13 +311,13 @@ set horizontal-scroll-mode On
# Rebind some keys: # Rebind some keys:
Meta-h: backward-kill-word Meta-h: backward-kill-word
Control-u: universal-argument Control-u: universal-argument
\end{verbatim}\end{code} \end{verbatim}\ecode
Note that the default binding for TAB in \Python\ is to insert a TAB Note that the default binding for TAB in \Python\ is to insert a TAB
instead of Readline's default filename completion function. instead of Readline's default filename completion function.
If you insist, you can override this by putting If you insist, you can override this by putting
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
TAB: complete TAB: complete
\end{verbatim}\end{code} \end{verbatim}\ecode
in your in your
{\tt \$HOME/.inputrc}. {\tt \$HOME/.inputrc}.
(Of course, this makes it hard to type indented continuation lines.) (Of course, this makes it hard to type indented continuation lines.)
@ -374,7 +357,7 @@ and
work just as in most other languages (e.g., Pascal or C); parentheses work just as in most other languages (e.g., Pascal or C); parentheses
can be used for grouping. can be used for grouping.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> # This is a comment >>> # This is a comment
>>> 2+2 >>> 2+2
4 4
@ -385,48 +368,46 @@ For example:
>>> 7/3 >>> 7/3
2 2
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
As in C, the equal sign ({\tt =}) is used to assign a value to a variable. As in C, the equal sign ({\tt =}) is used to assign a value to a variable.
The value of an assignment is not written: The value of an assignment is not written:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> width = 20 >>> width = 20
>>> height = 5*9 >>> height = 5*9
>>> width * height >>> width * height
900 900
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
There is some support for floating point, but you can't mix floating There is some support for floating point, but you can't mix floating
point and integral numbers in expression (yet): point and integral numbers in expression (yet):
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> 10.0 / 3.3 >>> 10.0 / 3.3
3.0303030303 3.0303030303
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Besides numbers, \Python\ can also manipulate strings, enclosed in single Besides numbers, \Python\ can also manipulate strings, enclosed in single
quotes: quotes:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> 'foo bar' >>> 'foo bar'
'foo bar' 'foo bar'
>>> 'doesn\'t' >>> 'doesn\'t'
'doesn\'t' 'doesn\'t'
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Strings are written inside quotes and with quotes and other funny Strings are written inside quotes and with quotes and other funny
characters escaped by backslashes, to show the precise value. characters escaped by backslashes, to show the precise value.
(There is also a way to write strings without quotes and escapes.) (There is also a way to write strings without quotes and escapes.)
Strings can be concatenated (glued together) with the Strings can be concatenated (glued together) with the
{\tt +} {\tt +}
operator, and repeated with operator, and repeated with~{\tt *}:
{\tt *}: \bcode\begin{verbatim}
\begin{code}\begin{verbatim}
>>> word = 'Help' + 'A' >>> word = 'Help' + 'A'
>>> word >>> word
'HelpA' 'HelpA'
>>> '<' + word*5 + '>' >>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>' '<HelpAHelpAHelpAHelpAHelpA>'
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Strings can be subscripted; as in C, the first character of a string has Strings can be subscripted; as in C, the first character of a string has
subscript 0. subscript 0.
There is no separate character type; a character is simply a string of There is no separate character type; a character is simply a string of
@ -434,7 +415,7 @@ size one.
As in Icon, substrings can be specified with the As in Icon, substrings can be specified with the
{\em slice} {\em slice}
notation: two subscripts (indices) separated by a colon. notation: two subscripts (indices) separated by a colon.
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> word[4] >>> word[4]
'A' 'A'
>>> word[0:2] >>> word[0:2]
@ -450,11 +431,11 @@ notation: two subscripts (indices) separated by a colon.
>>> word[:3] + word[3:] >>> word[:3] + word[3:]
'HelpA' 'HelpA'
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Degenerate cases are handled gracefully: an index that is too large is Degenerate cases are handled gracefully: an index that is too large is
replaced by the string size, an upper bound smaller than the lower bound replaced by the string size, an upper bound smaller than the lower bound
returns an empty string. returns an empty string.
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> word[1:100] >>> word[1:100]
'elpA' 'elpA'
>>> word[10:] >>> word[10:]
@ -462,11 +443,11 @@ returns an empty string.
>>> word[2:1] >>> word[2:1]
'' ''
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Slice indices (but not simple subscripts) may be negative numbers, to Slice indices (but not simple subscripts) may be negative numbers, to
start counting from the right. start counting from the right.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> word[-2:] # Take last two characters >>> word[-2:] # Take last two characters
'pA' 'pA'
>>> word[:-2] # Drop last two characters >>> word[:-2] # Drop last two characters
@ -475,7 +456,7 @@ For example:
>>> word[-0:] # (since -0 equals 0) >>> word[-0:] # (since -0 equals 0)
'HelpA' 'HelpA'
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The best way to remember how slices work is to think of the indices as The best way to remember how slices work is to think of the indices as
pointing pointing
{\em between} {\em between}
@ -485,13 +466,13 @@ Then the right edge of the last character of a string of
characters has index characters has index
{\tt n}, {\tt n},
for example: for example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
+---+---+---+---+---+ +---+---+---+---+---+
| H | e | l | p | A | | H | e | l | p | A |
+---+---+---+---+---+ +---+---+---+---+---+
0 1 2 3 4 5 0 1 2 3 4 5
-5 -4 -3 -2 -1 -5 -4 -3 -2 -1
\end{verbatim}\end{code} \end{verbatim}\ecode
The first row of numbers gives the position of the indices 0...5 in the The first row of numbers gives the position of the indices 0...5 in the
string; the second row gives the corresponding negative indices. string; the second row gives the corresponding negative indices.
For nonnegative indices, the length of a slice is the difference of the For nonnegative indices, the length of a slice is the difference of the
@ -503,13 +484,12 @@ is 3--1 = 2.
Finally, the built-in function {\tt len()} computes the length of a Finally, the built-in function {\tt len()} computes the length of a
string: string:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> s = 'supercalifragilisticexpialidocious' >>> s = 'supercalifragilisticexpialidocious'
>>> len(s) >>> len(s)
34 34
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\Python\ knows a number of \Python\ knows a number of
{\em compound} {\em compound}
data types, used to group together other values. data types, used to group together other values.
@ -517,42 +497,42 @@ The most versatile is the
{\em list}, {\em list},
which can be written as a list of comma-separated values between square which can be written as a list of comma-separated values between square
brackets: brackets:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a = ['foo', 'bar', 100, 1234] >>> a = ['foo', 'bar', 100, 1234]
>>> a >>> a
['foo', 'bar', 100, 1234] ['foo', 'bar', 100, 1234]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
As for strings, list subscripts start at 0: As for strings, list subscripts start at 0:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a[0] >>> a[0]
'foo' 'foo'
>>> a[3] >>> a[3]
1234 1234
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Lists can be sliced and concatenated like strings: Lists can be sliced and concatenated like strings:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a[1:3] >>> a[1:3]
['bar', 100] ['bar', 100]
>>> a[:2] + ['bletch', 2*2] >>> a[:2] + ['bletch', 2*2]
['foo', 'bar', 'bletch', 4] ['foo', 'bar', 'bletch', 4]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Unlike strings, which are Unlike strings, which are
{\em immutable}, {\em immutable},
it is possible to change individual elements of a list: it is possible to change individual elements of a list:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a >>> a
['foo', 'bar', 100, 1234] ['foo', 'bar', 100, 1234]
>>> a[2] = a[2] + 23 >>> a[2] = a[2] + 23
>>> a >>> a
['foo', 'bar', 123, 1234] ['foo', 'bar', 123, 1234]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Assignment to slices is also possible, and this may even change the size Assignment to slices is also possible, and this may even change the size
of the list: of the list:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> # Replace some items: >>> # Replace some items:
>>> a[0:2] = [1, 12] >>> a[0:2] = [1, 12]
>>> a >>> a
@ -566,13 +546,13 @@ of the list:
>>> a >>> a
[123, 'bletch', 'xyzzy', 1234] [123, 'bletch', 'xyzzy', 1234]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The built-in function {\tt len()} also applies to lists: The built-in function {\tt len()} also applies to lists:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> len(a) >>> len(a)
4 4
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsection{Tuples and Sequences} \subsection{Tuples and Sequences}
@ -585,7 +565,7 @@ and two together.
For instance, we can write an initial subsequence of the For instance, we can write an initial subsequence of the
{\em Fibonacci} {\em Fibonacci}
series as follows: series as follows:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> # Fibonacci series: >>> # Fibonacci series:
>>> # the sum of two elements defines the next >>> # the sum of two elements defines the next
>>> a, b = 0, 1 >>> a, b = 0, 1
@ -605,7 +585,7 @@ series as follows:
55 55
89 89
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
This example introduces several new features. This example introduces several new features.
\begin{itemize} \begin{itemize}
\item \item
@ -661,14 +641,14 @@ earlier in the calculator examples) in the way it handles multiple
expressions and strings. expressions and strings.
Strings are written without quotes and a space is inserted between Strings are written without quotes and a space is inserted between
items, so you can format things nicely, like this: items, so you can format things nicely, like this:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> i = 256*256 >>> i = 256*256
>>> print 'The value of i is', i >>> print 'The value of i is', i
The value of i is 65536 The value of i is 65536
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
A trailing comma avoids the newline after the output: A trailing comma avoids the newline after the output:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a, b = 0, 1 >>> a, b = 0, 1
>>> while b < 1000: >>> while b < 1000:
... print b, ... print b,
@ -676,7 +656,7 @@ A trailing comma avoids the newline after the output:
... ...
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Note that the interpreter inserts a newline before it prints the next Note that the interpreter inserts a newline before it prints the next
prompt if the last line was not completed. prompt if the last line was not completed.
\end{itemize} \end{itemize}
@ -691,7 +671,7 @@ twists.
Perhaps the most well-known statement type is the {\tt if} statement. Perhaps the most well-known statement type is the {\tt if} statement.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> if x < 0: >>> if x < 0:
... x = 0 ... x = 0
... print 'Negative changed to zero' ... print 'Negative changed to zero'
@ -702,7 +682,7 @@ For example:
... else: ... else:
... print 'More' ... print 'More'
... ...
\end{verbatim}\end{code} \end{verbatim}\ecode
There can be zero or more {\tt elif} parts, and the {\tt else} part is There can be zero or more {\tt elif} parts, and the {\tt else} part is
optional. optional.
The keyword `{\tt elif}' is short for `{\tt else if}', and is useful to The keyword `{\tt elif}' is short for `{\tt else if}', and is useful to
@ -719,7 +699,7 @@ Rather than always iterating over an arithmetic progression of numbers
and step (as C), \Python's {\tt for} statement iterates over the items and step (as C), \Python's {\tt for} statement iterates over the items
of any sequence (e.g., a list or a string). of any sequence (e.g., a list or a string).
For example (no pun intended): For example (no pun intended):
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> # Measure some strings: >>> # Measure some strings:
>>> a = ['cat', 'window', 'defenestrate'] >>> a = ['cat', 'window', 'defenestrate']
>>> for x in a: >>> for x in a:
@ -729,7 +709,7 @@ cat 3
window 6 window 6
defenestrate 12 defenestrate 12
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{The {\tt range()} Function} \subsubsection{The {\tt range()} Function}
@ -737,17 +717,17 @@ If you do need to iterate over a sequence of numbers, the built-in
function {\tt range()} comes in handy. function {\tt range()} comes in handy.
It generates lists containing arithmetic progressions, It generates lists containing arithmetic progressions,
e.g.: e.g.:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> range(10) >>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The given end point is never part of the generated list; The given end point is never part of the generated list;
{\tt range(10)} generates a list of 10 values, {\tt range(10)} generates a list of 10 values,
exactly the legal indices for items of a sequence of length 10. exactly the legal indices for items of a sequence of length 10.
It is possible to let the range start at another number, or to specify a It is possible to let the range start at another number, or to specify a
different increment (even negative): different increment (even negative):
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> range(5, 10) >>> range(5, 10)
[5, 6, 7, 8, 9] [5, 6, 7, 8, 9]
>>> range(0, 10, 3) >>> range(0, 10, 3)
@ -755,10 +735,10 @@ different increment (even negative):
>>> range(-10, -100, -30) >>> range(-10, -100, -30)
[-10, -40, -70] [-10, -40, -70]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
To iterate over the indices of a sequence, combine {\tt range()} To iterate over the indices of a sequence, combine {\tt range()}
and {\tt len()} as follows: and {\tt len()} as follows:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a = ['Mary', 'had', 'a', 'little', 'boy'] >>> a = ['Mary', 'had', 'a', 'little', 'boy']
>>> for i in range(len(a)): >>> for i in range(len(a)):
... print i, a[i] ... print i, a[i]
@ -769,7 +749,7 @@ and {\tt len()} as follows:
3 little 3 little
4 boy 4 boy
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{Break Statements and Else Clauses on Loops} \subsubsection{Break Statements and Else Clauses on Loops}
@ -781,7 +761,7 @@ the condition becomes false (with {\tt while}) but not when the loop is
terminated by a {\tt break} statement. terminated by a {\tt break} statement.
This is exemplified by the following loop, which searches for a list This is exemplified by the following loop, which searches for a list
item of value 0: item of value 0:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> for n in range(2, 10): >>> for n in range(2, 10):
... for x in range(2, n): ... for x in range(2, n):
... if n % x = 0: ... if n % x = 0:
@ -799,7 +779,7 @@ item of value 0:
8 equals 2 * 4 8 equals 2 * 4
9 equals 3 * 3 9 equals 3 * 3
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{Pass Statements} \subsubsection{Pass Statements}
@ -807,11 +787,11 @@ The {\tt pass} statement does nothing.
It can be used when a statement is required syntactically but the It can be used when a statement is required syntactically but the
program requires no action. program requires no action.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> while 1: >>> while 1:
... pass # Busy-wait for keyboard interrupt ... pass # Busy-wait for keyboard interrupt
... ...
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{Conditions Revisited} \subsubsection{Conditions Revisited}
@ -821,7 +801,7 @@ XXX To Be Done.
We can create a function that writes the Fibonacci series to an We can create a function that writes the Fibonacci series to an
arbitrary boundary: arbitrary boundary:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> def fib(n): # write Fibonacci series up to n >>> def fib(n): # write Fibonacci series up to n
... a, b = 0, 1 ... a, b = 0, 1
... while b <= n: ... while b <= n:
@ -832,7 +812,7 @@ arbitrary boundary:
>>> fib(2000) >>> fib(2000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The keyword The keyword
{\tt def} {\tt def}
introduces a function introduces a function
@ -872,14 +852,14 @@ user-defined function.
This value can be assigned to another name which can then also be used This value can be assigned to another name which can then also be used
as a function. as a function.
This serves as a general renaming mechanism: This serves as a general renaming mechanism:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> fib >>> fib
<function object at 10042ed0> <function object at 10042ed0>
>>> f = fib >>> f = fib
>>> f(100) >>> f(100)
1 1 2 3 5 8 13 21 34 55 89 1 1 2 3 5 8 13 21 34 55 89
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
You might object that You might object that
{\tt fib} {\tt fib}
is not a function but a procedure. is not a function but a procedure.
@ -891,14 +871,14 @@ This value is called {\tt None} (it's a built-in name).
Writing the value {\tt None} is normally suppressed by the interpreter Writing the value {\tt None} is normally suppressed by the interpreter
if it would be the only value written. if it would be the only value written.
You can see it if you really want to: You can see it if you really want to:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> print fib(0) >>> print fib(0)
None None
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
It is simple to write a function that returns a list of the numbers of It is simple to write a function that returns a list of the numbers of
the Fibonacci series, instead of printing it: the Fibonacci series, instead of printing it:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> def fib2(n): # return Fibonacci series up to n >>> def fib2(n): # return Fibonacci series up to n
... result = [] ... result = []
... a, b = 0, 1 ... a, b = 0, 1
@ -911,7 +891,7 @@ the Fibonacci series, instead of printing it:
>>> f100 # write the result >>> f100 # write the result
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
This example, as usual, demonstrates some new \Python\ features: This example, as usual, demonstrates some new \Python\ features:
\begin{itemize} \begin{itemize}
\item \item
@ -963,7 +943,7 @@ so {\tt a.insert(0, x)} inserts at the front of the list, and
Sorts the elements of the list. Sorts the elements of the list.
\end{description} \end{description}
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> a = [10, 100, 1, 1000] >>> a = [10, 100, 1, 1000]
>>> a.insert(2, -1) >>> a.insert(2, -1)
>>> a >>> a
@ -977,7 +957,7 @@ For example:
>>> b >>> b
['Mary', 'a', 'boy', 'had', 'little'] ['Mary', 'a', 'boy', 'had', 'little']
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsection{Modules} \subsection{Modules}
@ -1010,7 +990,7 @@ appended.
For instance, use your favorite text editor to create a file called For instance, use your favorite text editor to create a file called
{\tt fibo.py} {\tt fibo.py}
in the current directory with the following contents: in the current directory with the following contents:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
# Fibonacci numbers module # Fibonacci numbers module
def fib(n): # write Fibonacci series up to n def fib(n): # write Fibonacci series up to n
@ -1026,33 +1006,33 @@ def fib2(n): # return Fibonacci series up to n
ret.append(b) ret.append(b)
a, b = b, a+b a, b = b, a+b
return ret return ret
\end{verbatim}\end{code} \end{verbatim}\ecode
Now enter the \Python\ interpreter and import this module with the Now enter the \Python\ interpreter and import this module with the
following command: following command:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> import fibo >>> import fibo
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
This does not enter the names of the functions defined in This does not enter the names of the functions defined in
{\tt fibo} {\tt fibo}
directly in the symbol table; it only enters the module name directly in the symbol table; it only enters the module name
{\tt fibo} {\tt fibo}
there. there.
Using the module name you can access the functions: Using the module name you can access the functions:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> fibo.fib(1000) >>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100) >>> fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
If you intend to use a function often you can assign it to a local name: If you intend to use a function often you can assign it to a local name:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> fib = fibo.fib >>> fib = fibo.fib
>>> fib(500) >>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{More on Modules} \subsubsection{More on Modules}
@ -1090,23 +1070,23 @@ There is a variant of the
statement that imports names from a module directly into the importing statement that imports names from a module directly into the importing
module's symbol table. module's symbol table.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> from fibo import fib, fib2 >>> from fibo import fib, fib2
>>> fib(500) >>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
This does not introduce the module name from which the imports are taken This does not introduce the module name from which the imports are taken
in the local symbol table (so in the example, {\tt fibo} is not in the local symbol table (so in the example, {\tt fibo} is not
defined). defined).
There is even a variant to import all names that a module defines: There is even a variant to import all names that a module defines:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> from fibo import * >>> from fibo import *
>>> fib(500) >>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
This imports all names except those beginning with an underscore This imports all names except those beginning with an underscore
({\tt \_}). ({\tt \_}).
@ -1130,7 +1110,7 @@ The variables
and and
{\tt sys.ps2} {\tt sys.ps2}
define the strings used as primary and secondary prompts: define the strings used as primary and secondary prompts:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> import sys >>> import sys
>>> sys.ps1 >>> sys.ps1
'>>> ' '>>> '
@ -1140,7 +1120,7 @@ define the strings used as primary and secondary prompts:
C> print 'Yuck!' C> print 'Yuck!'
Yuck! Yuck!
C> C>
\end{verbatim}\end{code} \end{verbatim}\ecode
These two variables are only defined if the interpreter is in These two variables are only defined if the interpreter is in
interactive mode. interactive mode.
@ -1154,11 +1134,11 @@ or from a built-in default if
{\tt PYTHONPATH} {\tt PYTHONPATH}
is not set. is not set.
You can modify it using standard list operations, e.g.: You can modify it using standard list operations, e.g.:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> import sys >>> import sys
>>> sys.path.append('/ufs/guido/lib/python') >>> sys.path.append('/ufs/guido/lib/python')
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsection{Errors and Exceptions} \subsection{Errors and Exceptions}
@ -1173,14 +1153,14 @@ and
Syntax errors, also known as parsing errors, are perhaps the most common Syntax errors, also known as parsing errors, are perhaps the most common
kind of complaint you get while you are still learning \Python: kind of complaint you get while you are still learning \Python:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> while 1 print 'Hello world' >>> while 1 print 'Hello world'
Parsing error: file <stdin>, line 1: Parsing error: file <stdin>, line 1:
while 1 print 'Hello world' while 1 print 'Hello world'
^ ^
Unhandled exception: run-time error: syntax error Unhandled exception: run-time error: syntax error
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The parser repeats the offending line and displays a little `arrow' The parser repeats the offending line and displays a little `arrow'
pointing at the earliest point in the line where the error was detected. pointing at the earliest point in the line where the error was detected.
The error is caused by (or at least detected at) the token The error is caused by (or at least detected at) the token
@ -1194,7 +1174,7 @@ the input came from a script.
Even if a statement or expression is syntactically correct, it may cause Even if a statement or expression is syntactically correct, it may cause
an error when an attempt is made to execute it: an error when an attempt is made to execute it:
\begin{code}\begin{verbatim} \bcode\small\begin{verbatim}
>>> 10 * (1/0) >>> 10 * (1/0)
Unhandled exception: run-time error: integer division by zero Unhandled exception: run-time error: integer division by zero
Stack backtrace (innermost last): Stack backtrace (innermost last):
@ -1208,7 +1188,7 @@ Unhandled exception: type error: illegal argument type for built-in operation
Stack backtrace (innermost last): Stack backtrace (innermost last):
File "<stdin>", line 1 File "<stdin>", line 1
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Errors detected during execution are called Errors detected during execution are called
{\em exceptions} {\em exceptions}
and are not unconditionally fatal: you will soon learn how to handle and are not unconditionally fatal: you will soon learn how to handle
@ -1261,7 +1241,7 @@ The detail shows in what context the error was detected.
It is possible to write programs that handle selected exceptions. It is possible to write programs that handle selected exceptions.
Look at the following example, which prints a table of inverses of Look at the following example, which prints a table of inverses of
some floating point numbers: some floating point numbers:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> numbers = [0.3333, 2.5, 0.0, 10.0] >>> numbers = [0.3333, 2.5, 0.0, 10.0]
>>> for x in numbers: >>> for x in numbers:
... print x, ... print x,
@ -1275,7 +1255,7 @@ some floating point numbers:
0 *** has no inverse *** 0 *** has no inverse ***
10 0.1 10 0.1
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The {\tt try} statement works as follows. The {\tt try} statement works as follows.
\begin{itemize} \begin{itemize}
\item \item
@ -1306,10 +1286,10 @@ Handlers only handle exceptions that occur in the corresponding try
clause, not in other handlers of the same {\tt try} statement. clause, not in other handlers of the same {\tt try} statement.
An except clause may name multiple exceptions as a parenthesized list, An except clause may name multiple exceptions as a parenthesized list,
e.g.: e.g.:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
... except (RuntimeError, TypeError, NameError): ... except (RuntimeError, TypeError, NameError):
... pass ... pass
\end{verbatim}\end{code} \end{verbatim}\ecode
The last except clause may omit the exception name(s), to serve as a The last except clause may omit the exception name(s), to serve as a
wildcard. wildcard.
Use this with extreme caution! Use this with extreme caution!
@ -1321,7 +1301,7 @@ The presence and type of the argument depend on the exception type.
For exception types which have an argument, the except clause may For exception types which have an argument, the except clause may
specify a variable after the exception name (or list) to receive the specify a variable after the exception name (or list) to receive the
argument's value, as follows: argument's value, as follows:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> try: >>> try:
... foo() ... foo()
... except NameError, x: ... except NameError, x:
@ -1329,7 +1309,7 @@ argument's value, as follows:
... ...
name foo undefined name foo undefined
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
If an exception has an argument, it is printed as the third part If an exception has an argument, it is printed as the third part
(`detail') of the message for unhandled exceptions. (`detail') of the message for unhandled exceptions.
@ -1346,7 +1326,7 @@ These are in fact string objects whose
The string is printed as the second part of the message for unhandled The string is printed as the second part of the message for unhandled
exceptions. exceptions.
Their names and values are: Their names and values are:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
EOFError 'end-of-file read' EOFError 'end-of-file read'
KeyboardInterrupt 'keyboard interrupt' KeyboardInterrupt 'keyboard interrupt'
MemoryError 'out of memory' * MemoryError 'out of memory' *
@ -1354,7 +1334,7 @@ NameError 'undefined name' *
RuntimeError 'run-time error' * RuntimeError 'run-time error' *
SystemError 'system error' * SystemError 'system error' *
TypeError 'type error' * TypeError 'type error' *
\end{verbatim}\end{code} \end{verbatim}\ecode
The meanings should be clear enough. The meanings should be clear enough.
Those exceptions with a {\tt *} in the third column have an argument. Those exceptions with a {\tt *} in the third column have an argument.
@ -1362,7 +1342,7 @@ Exception handlers don't just handle exceptions if they occur
immediately in the try clause, but also if they occur inside functions immediately in the try clause, but also if they occur inside functions
that are called (even indirectly) in the try clause. that are called (even indirectly) in the try clause.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> def this_fails(): >>> def this_fails():
... x = 1/0 ... x = 1/0
... ...
@ -1373,20 +1353,20 @@ For example:
... ...
Handling run-time error: domain error or zero division Handling run-time error: domain error or zero division
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
\subsubsection{Raising Exceptions} \subsubsection{Raising Exceptions}
The {\tt raise} statement allows the programmer to force a specified The {\tt raise} statement allows the programmer to force a specified
exception to occur. exception to occur.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> raise NameError, 'Hi There!' >>> raise NameError, 'Hi There!'
Unhandled exception: undefined name: Hi There! Unhandled exception: undefined name: Hi There!
Stack backtrace (innermost last): Stack backtrace (innermost last):
File "<stdin>", line 1 File "<stdin>", line 1
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The first argument to {\tt raise} names the exception to be raised. The first argument to {\tt raise} names the exception to be raised.
The optional second argument specifies the exception's argument. The optional second argument specifies the exception's argument.
@ -1395,7 +1375,7 @@ The optional second argument specifies the exception's argument.
Programs may name their own exceptions by assigning a string to a Programs may name their own exceptions by assigning a string to a
variable. variable.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> my_exc = 'nobody likes me!' >>> my_exc = 'nobody likes me!'
>>> try: >>> try:
... raise my_exc, 2*2 ... raise my_exc, 2*2
@ -1408,7 +1388,7 @@ Unhandled exception: nobody likes me!: 1
Stack backtrace (innermost last): Stack backtrace (innermost last):
File "<stdin>", line 7 File "<stdin>", line 7
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
Many standard modules use this to report errors that may occur in Many standard modules use this to report errors that may occur in
functions they define. functions they define.
@ -1417,7 +1397,7 @@ functions they define.
The {\tt try} statement has another optional clause which is intended to The {\tt try} statement has another optional clause which is intended to
define clean-up actions that must be executed under all circumstances. define clean-up actions that must be executed under all circumstances.
For example: For example:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> try: >>> try:
... raise KeyboardInterrupt ... raise KeyboardInterrupt
... finally: ... finally:
@ -1428,7 +1408,7 @@ Unhandled exception: keyboard interrupt
Stack backtrace (innermost last): Stack backtrace (innermost last):
File "<stdin>", line 2 File "<stdin>", line 2
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
The The
{\em finally\ clause} {\em finally\ clause}
must follow the except clauses(s), if any. must follow the except clauses(s), if any.
@ -1489,7 +1469,7 @@ Consider the following example, which defines a class {\tt Set}
representing a (finite) mathematical set with operations to add and representing a (finite) mathematical set with operations to add and
remove elements, a membership test, and a request for the size of the remove elements, a membership test, and a request for the size of the
set. set.
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
class Set(): class Set():
def new(self): def new(self):
self.elements = [] self.elements = []
@ -1507,7 +1487,7 @@ class Set():
return e in self.elements return e in self.elements
def size(self): def size(self):
return len(self.elements) return len(self.elements)
\end{verbatim}\end{code} \end{verbatim}\ecode
Note that the class definition looks like a big compound statement, Note that the class definition looks like a big compound statement,
with all the function definitons indented repective to the with all the function definitons indented repective to the
{\tt class} {\tt class}
@ -1518,7 +1498,7 @@ Let's assume that this
is the only contents of the module file is the only contents of the module file
{\tt SetClass.py}. {\tt SetClass.py}.
We can then use it in a \Python\ program as follows: We can then use it in a \Python\ program as follows:
\begin{code}\begin{verbatim} \bcode\begin{verbatim}
>>> from SetClass import Set >>> from SetClass import Set
>>> a = Set().new() # create a Set object >>> a = Set().new() # create a Set object
>>> a.add(2) >>> a.add(2)
@ -1538,7 +1518,7 @@ a has 3 elements
>>> >>>
now a has 2 elements now a has 2 elements
>>> >>>
\end{verbatim}\end{code} \end{verbatim}\ecode
From the example we learn in the first place that the functions defined From the example we learn in the first place that the functions defined
in the class (e.g., in the class (e.g.,
{\tt add}) {\tt add})