Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
dfbccb71
Commit
dfbccb71
authored
Feb 26, 1998
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added \label{}s for logical addressing.
parent
122a6d16
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
284 additions
and
142 deletions
+284
-142
Doc/tut.tex
Doc/tut.tex
+142
-71
Doc/tut/tut.tex
Doc/tut/tut.tex
+142
-71
No files found.
Doc/tut.tex
View file @
dfbccb71
...
@@ -66,7 +66,8 @@ modules described in the \emph{Python Library Reference}.
...
@@ -66,7 +66,8 @@ modules described in the \emph{Python Library Reference}.
\chapter
{
Whetting Your Appetite
}
\chapter
{
Whetting Your Appetite
}
\section
{
Introduction
}
%\section{Introduction}
\label
{
intro
}
If you ever wrote a large shell script, you probably know this
If you ever wrote a large shell script, you probably know this
feeling: you'd love to add yet another feature, but it's already so
feeling: you'd love to add yet another feature, but it's already so
...
@@ -138,6 +139,7 @@ references to Monty Python skits in documentation is not only allowed,
...
@@ -138,6 +139,7 @@ references to Monty Python skits in documentation is not only allowed,
it is encouraged!
it is encouraged!
\section
{
Where From Here
}
\section
{
Where From Here
}
\label
{
where
}
Now that you are all excited about Python, you'll want to examine it
Now that you are all excited about Python, you'll want to examine it
in some more detail. Since the best way to learn a language is
in some more detail. Since the best way to learn a language is
...
@@ -154,8 +156,10 @@ and finally touching upon advanced concepts like exceptions
...
@@ -154,8 +156,10 @@ and finally touching upon advanced concepts like exceptions
and user-defined classes.
and user-defined classes.
\chapter
{
Using the Python Interpreter
}
\chapter
{
Using the Python Interpreter
}
\label
{
using
}
\section
{
Invoking the Interpreter
}
\section
{
Invoking the Interpreter
}
\label
{
invoking
}
The Python interpreter is usually installed as
\file
{
/usr/local/bin/python
}
The Python interpreter is usually installed as
\file
{
/usr/local/bin/python
}
on those machines where it is available; putting
\file
{
/usr/local/bin
}
in
on those machines where it is available; putting
\file
{
/usr/local/bin
}
in
...
@@ -165,7 +169,7 @@ typing the command
...
@@ -165,7 +169,7 @@ typing the command
\begin{verbatim}
\begin{verbatim}
python
python
\end{verbatim}
\end{verbatim}
%
to the shell. Since the choice of the directory where the interpreter
to the shell. Since the choice of the directory where the interpreter
lives is an installation option, other places are possible; check with
lives is an installation option, other places are possible; check with
your local Python guru or system administrator. (E.g.,
your local Python guru or system administrator. (E.g.,
...
@@ -217,6 +221,7 @@ is read from standard input, for the same reason as explained in the
...
@@ -217,6 +221,7 @@ is read from standard input, for the same reason as explained in the
previous paragraph.)
previous paragraph.)
\subsection
{
Argument Passing
}
\subsection
{
Argument Passing
}
\label
{
argPassing
}
When known to the interpreter, the script name and additional
When known to the interpreter, the script name and additional
arguments thereafter are passed to the script in the variable
arguments thereafter are passed to the script in the variable
...
@@ -230,6 +235,7 @@ interpreter's option processing but left in \code{sys.argv} for the
...
@@ -230,6 +235,7 @@ interpreter's option processing but left in \code{sys.argv} for the
command to handle.
command to handle.
\subsection
{
Interactive Mode
}
\subsection
{
Interactive Mode
}
\label
{
interactive
}
When commands are read from a tty, the interpreter is said to be in
When commands are read from a tty, the interpreter is said to be in
\emph
{
interactive mode
}
. In this mode it prompts for the next command
\emph
{
interactive mode
}
. In this mode it prompts for the next command
...
@@ -249,8 +255,10 @@ Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
...
@@ -249,8 +255,10 @@ Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
\end{verbatim}
\end{verbatim}
\section
{
The Interpreter and its Environment
}
\section
{
The Interpreter and its Environment
}
\label
{
interp
}
\subsection
{
Error Handling
}
\subsection
{
Error Handling
}
\label
{
error
}
When an error occurs, the interpreter prints an error
When an error occurs, the interpreter prints an error
message and a stack trace. In interactive mode, it then returns to
message and a stack trace. In interactive mode, it then returns to
...
@@ -275,6 +283,7 @@ Typing an interrupt while a command is executing raises the
...
@@ -275,6 +283,7 @@ Typing an interrupt while a command is executing raises the
\code
{
try
}
statement.
\code
{
try
}
statement.
\subsection
{
Executable Python scripts
}
\subsection
{
Executable Python scripts
}
\label
{
scripts
}
On BSD'ish
\UNIX
{}
systems, Python scripts can be made directly
On BSD'ish
\UNIX
{}
systems, Python scripts can be made directly
executable, like shell scripts, by putting the line
executable, like shell scripts, by putting the line
...
@@ -282,12 +291,13 @@ executable, like shell scripts, by putting the line
...
@@ -282,12 +291,13 @@ executable, like shell scripts, by putting the line
\begin{verbatim}
\begin{verbatim}
#! /usr/bin/env python
#! /usr/bin/env python
\end{verbatim}
\end{verbatim}
%
(assuming that the interpreter is on the user's PATH) at the beginning
(assuming that the interpreter is on the user's PATH) at the beginning
of the script and giving the file an executable mode. The
\samp
{
\#
!
}
of the script and giving the file an executable mode. The
\samp
{
\#
!
}
must be the first two characters of the file.
must be the first two characters of the file.
\subsection
{
The Interactive Startup File
}
\subsection
{
The Interactive Startup File
}
\label
{
startup
}
% XXX This should probably be dumped in an appendix, since most people
% XXX This should probably be dumped in an appendix, since most people
% don't use Python interactively in non-trivial ways.
% don't use Python interactively in non-trivial ways.
...
@@ -319,6 +329,7 @@ execfile(os.environ['PYTHONSTARTUP'])
...
@@ -319,6 +329,7 @@ execfile(os.environ['PYTHONSTARTUP'])
\end{verbatim}
\end{verbatim}
\chapter
{
An Informal Introduction to Python
}
\chapter
{
An Informal Introduction to Python
}
\label
{
informal
}
In the following examples, input and output are distinguished by the
In the following examples, input and output are distinguished by the
presence or absence of prompts (
\samp
{
>>>
}
and
\samp
{
...
}
): to repeat
presence or absence of prompts (
\samp
{
>>>
}
and
\samp
{
...
}
): to repeat
...
@@ -334,11 +345,13 @@ Note that a secondary prompt on a line by itself in an example means
...
@@ -334,11 +345,13 @@ Note that a secondary prompt on a line by itself in an example means
you must type a blank line; this is used to end a multi-line command.
you must type a blank line; this is used to end a multi-line command.
\section
{
Using Python as a Calculator
}
\section
{
Using Python as a Calculator
}
\label
{
calculator
}
Let's try some simple Python commands. Start the interpreter and wait
Let's try some simple Python commands. Start the interpreter and wait
for the primary prompt,
\samp
{
>>>
}
. (It shouldn't take long.)
for the primary prompt,
\samp
{
>>>
}
. (It shouldn't take long.)
\subsection
{
Numbers
}
\subsection
{
Numbers
}
\label
{
numbers
}
The interpreter acts as a simple calculator: you can type an
The interpreter acts as a simple calculator: you can type an
expression at it and it will write the value. Expression syntax is
expression at it and it will write the value. Expression syntax is
...
@@ -362,7 +375,7 @@ can be used for grouping. For example:
...
@@ -362,7 +375,7 @@ can be used for grouping. For example:
>>> 7/-3
>>> 7/-3
-3
-3
\end{verbatim}
\end{verbatim}
%
Like in
\C
{}
, the equal sign (
\code
{
=
}
) is used to assign a value to a
Like in
\C
{}
, the equal sign (
\code
{
=
}
) is used to assign a value to a
variable. The value of an assignment is not written:
variable. The value of an assignment is not written:
...
@@ -465,6 +478,7 @@ local variable with the same name masking the built-in variable with
...
@@ -465,6 +478,7 @@ local variable with the same name masking the built-in variable with
its magic behavior.
its magic behavior.
\subsection
{
Strings
}
\subsection
{
Strings
}
\label
{
strings
}
Besides numbers, Python can also manipulate strings, which can be
Besides numbers, Python can also manipulate strings, which can be
expressed in several ways. They can be enclosed in single quotes or
expressed in several ways. They can be enclosed in single quotes or
...
@@ -484,7 +498,7 @@ double quotes:
...
@@ -484,7 +498,7 @@ double quotes:
>>> '"Isn
\'
t," she said.'
>>> '"Isn
\'
t," she said.'
'"Isn
\'
t," she said.'
'"Isn
\'
t," she said.'
\end{verbatim}
\end{verbatim}
%
String literals can span multiple lines in several ways. Newlines can
String literals can span multiple lines in several ways. Newlines can
be escaped with backslashes, e.g.:
be escaped with backslashes, e.g.:
...
@@ -523,7 +537,7 @@ Usage: thingy [OPTIONS]
...
@@ -523,7 +537,7 @@ Usage: thingy [OPTIONS]
-h Display this usage message
-h Display this usage message
-H hostname Hostname to connect to
-H hostname Hostname to connect to
\end{verbatim}
\end{verbatim}
%
The interpreter prints the result of string operations in the same way
The interpreter prints the result of string operations in the same way
as they are typed for input: inside quotes, and with quotes and other
as they are typed for input: inside quotes, and with quotes and other
funny characters escaped by backslashes, to show the precise
funny characters escaped by backslashes, to show the precise
...
@@ -542,7 +556,7 @@ operator, and repeated with \code{*}:
...
@@ -542,7 +556,7 @@ operator, and repeated with \code{*}:
>>> '<' + word*5 + '>'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
'<HelpAHelpAHelpAHelpAHelpA>'
\end{verbatim}
\end{verbatim}
%
Two string literals next to each other are automatically concatenated;
Two string literals next to each other are automatically concatenated;
the first line above could also have been written
\samp
{
word = 'Help'
the first line above could also have been written
\samp
{
word = 'Help'
'A'
}
; this only works with two literals, not with arbitrary string expressions.
'A'
}
; this only works with two literals, not with arbitrary string expressions.
...
@@ -561,7 +575,7 @@ separated by a colon.
...
@@ -561,7 +575,7 @@ separated by a colon.
>>> word[2:4]
>>> word[2:4]
'lp'
'lp'
\end{verbatim}
\end{verbatim}
%
Slice indices have useful defaults; an omitted first index defaults to
Slice indices have useful defaults; an omitted first index defaults to
zero, an omitted second index defaults to the size of the string being
zero, an omitted second index defaults to the size of the string being
sliced.
sliced.
...
@@ -572,7 +586,7 @@ sliced.
...
@@ -572,7 +586,7 @@ sliced.
>>> word[2:] # All but the first two characters
>>> word[2:] # All but the first two characters
'lpA'
'lpA'
\end{verbatim}
\end{verbatim}
%
Here's a useful invariant of slice operations:
\code
{
s[:i] + s[i:]
}
Here's a useful invariant of slice operations:
\code
{
s[:i] + s[i:]
}
equals
\code
{
s
}
.
equals
\code
{
s
}
.
...
@@ -582,7 +596,7 @@ equals \code{s}.
...
@@ -582,7 +596,7 @@ equals \code{s}.
>>> word[:3] + word[3:]
>>> word[:3] + word[3:]
'HelpA'
'HelpA'
\end{verbatim}
\end{verbatim}
%
Degenerate slice indices are handled gracefully: an index that is too
Degenerate slice indices are handled gracefully: an index that is too
large is replaced by the string size, an upper bound smaller than the
large is replaced by the string size, an upper bound smaller than the
lower bound returns an empty string.
lower bound returns an empty string.
...
@@ -595,7 +609,7 @@ lower bound returns an empty string.
...
@@ -595,7 +609,7 @@ lower bound returns an empty string.
>>> word[2:1]
>>> word[2:1]
''
''
\end{verbatim}
\end{verbatim}
%
Indices may be negative numbers, to start counting from the right.
Indices may be negative numbers, to start counting from the right.
For example:
For example:
...
@@ -609,7 +623,7 @@ For example:
...
@@ -609,7 +623,7 @@ For example:
>>> word[:-2] # All but the last two characters
>>> word[:-2] # All but the last two characters
'Hel'
'Hel'
\end{verbatim}
\end{verbatim}
%
But note that -0 is really the same as 0, so it does not count from
But note that -0 is really the same as 0, so it does not count from
the right!
the right!
...
@@ -617,7 +631,7 @@ the right!
...
@@ -617,7 +631,7 @@ the right!
>>> word[-0] # (since -0 equals 0)
>>> word[-0] # (since -0 equals 0)
'H'
'H'
\end{verbatim}
\end{verbatim}
%
Out-of-range negative slice indices are truncated, but don't try this
Out-of-range negative slice indices are truncated, but don't try this
for single-element (non-slice) indices:
for single-element (non-slice) indices:
...
@@ -629,7 +643,7 @@ Traceback (innermost last):
...
@@ -629,7 +643,7 @@ Traceback (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
IndexError: string index out of range
IndexError: string index out of range
\end{verbatim}
\end{verbatim}
%
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
\emph
{
between
}
characters, with the left edge of the first
pointing
\emph
{
between
}
characters, with the left edge of the first
character numbered 0. Then the right edge of the last character of a
character numbered 0. Then the right edge of the last character of a
...
@@ -642,7 +656,7 @@ string of \var{n} characters has index \var{n}, for example:
...
@@ -642,7 +656,7 @@ string of \var{n} characters has index \var{n}, for example:
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{verbatim}
%
The first row of numbers gives the position of the indices 0...5 in
The first row of numbers gives the position of the indices 0...5 in
the string; the second row gives the corresponding negative indices.
the string; the second row gives the corresponding negative indices.
The slice from
\var
{
i
}
to
\var
{
j
}
consists of all characters between
The slice from
\var
{
i
}
to
\var
{
j
}
consists of all characters between
...
@@ -661,6 +675,7 @@ The built-in function \function{len()} returns the length of a string:
...
@@ -661,6 +675,7 @@ The built-in function \function{len()} returns the length of a string:
\end{verbatim}
\end{verbatim}
\subsection
{
Lists
}
\subsection
{
Lists
}
\label
{
lists
}
Python knows a number of
\emph
{
compound
}
data types, used to group
Python knows a number of
\emph
{
compound
}
data types, used to group
together other values. The most versatile is the
\emph
{
list
}
, which
together other values. The most versatile is the
\emph
{
list
}
, which
...
@@ -672,7 +687,7 @@ square brackets. List items need not all have the same type.
...
@@ -672,7 +687,7 @@ square brackets. List items need not all have the same type.
>>> a
>>> a
['spam', 'eggs', 100, 1234]
['spam', 'eggs', 100, 1234]
\end{verbatim}
\end{verbatim}
%
Like string indices, list indices start at 0, and lists can be sliced,
Like string indices, list indices start at 0, and lists can be sliced,
concatenated and so on:
concatenated and so on:
...
@@ -690,7 +705,7 @@ concatenated and so on:
...
@@ -690,7 +705,7 @@ concatenated and so on:
>>> 3*a[:3] + ['Boe!']
>>> 3*a[:3] + ['Boe!']
['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!']
['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!']
\end{verbatim}
\end{verbatim}
%
Unlike strings, which are
\emph
{
immutable
}
, it is possible to change
Unlike strings, which are
\emph
{
immutable
}
, it is possible to change
individual elements of a list:
individual elements of a list:
...
@@ -701,7 +716,7 @@ individual elements of a list:
...
@@ -701,7 +716,7 @@ individual elements of a list:
>>> a
>>> a
['spam', 'eggs', 123, 1234]
['spam', 'eggs', 123, 1234]
\end{verbatim}
\end{verbatim}
%
Assignment to slices is also possible, and this can even change the size
Assignment to slices is also possible, and this can even change the size
of the list:
of the list:
...
@@ -722,14 +737,14 @@ of the list:
...
@@ -722,14 +737,14 @@ of the list:
>>> a
>>> a
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
\end{verbatim}
\end{verbatim}
%
The built-in function
\function
{
len()
}
also applies to lists:
The built-in function
\function
{
len()
}
also applies to lists:
\begin{verbatim}
\begin{verbatim}
>>> len(a)
>>> len(a)
8
8
\end{verbatim}
\end{verbatim}
%
It is possible to nest lists (create lists containing other lists),
It is possible to nest lists (create lists containing other lists),
for example:
for example:
...
@@ -748,11 +763,12 @@ for example:
...
@@ -748,11 +763,12 @@ for example:
>>> q
>>> q
[2, 3, 'xtra']
[2, 3, 'xtra']
\end{verbatim}
\end{verbatim}
%
Note that in the last example,
\code
{
p[1]
}
and
\code
{
q
}
really refer to
Note that in the last example,
\code
{
p[1]
}
and
\code
{
q
}
really refer to
the same object! We'll come back to
\emph
{
object semantics
}
later.
the same object! We'll come back to
\emph
{
object semantics
}
later.
\section
{
First Steps Towards Programming
}
\section
{
First Steps Towards Programming
}
\label
{
firstSteps
}
Of course, we can use Python for more complicated tasks than adding
Of course, we can use Python for more complicated tasks than adding
two and two together. For instance, we can write an initial
two and two together. For instance, we can write an initial
...
@@ -773,7 +789,7 @@ subsequence of the \emph{Fibonacci} series as follows:
...
@@ -773,7 +789,7 @@ subsequence of the \emph{Fibonacci} series as follows:
5
5
8
8
\end{verbatim}
\end{verbatim}
%
This example introduces several new features.
This example introduces several new features.
\begin{itemize}
\begin{itemize}
...
@@ -819,7 +835,7 @@ like this:
...
@@ -819,7 +835,7 @@ like this:
>>> 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{verbatim}
%
A trailing comma avoids the newline after the output:
A trailing comma avoids the newline after the output:
\begin{verbatim}
\begin{verbatim}
...
@@ -830,7 +846,7 @@ A trailing comma avoids the newline after the output:
...
@@ -830,7 +846,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{verbatim}
%
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.
...
@@ -838,12 +854,14 @@ prompt if the last line was not completed.
...
@@ -838,12 +854,14 @@ prompt if the last line was not completed.
\chapter
{
More Control Flow Tools
}
\chapter
{
More Control Flow Tools
}
\label
{
moreControl
}
Besides the
\keyword
{
while
}
statement just introduced, Python knows
Besides the
\keyword
{
while
}
statement just introduced, Python knows
the usual control flow statements known from other languages, with
the usual control flow statements known from other languages, with
some twists.
some twists.
\section
{
If Statements
}
\section
{
If Statements
}
\label
{
if
}
Perhaps the most well-known statement type is the
\keyword
{
if
}
Perhaps the most well-known statement type is the
\keyword
{
if
}
statement. For example:
statement. For example:
...
@@ -860,7 +878,7 @@ statement. For example:
...
@@ -860,7 +878,7 @@ statement. For example:
... print 'More'
... print 'More'
...
...
\end{verbatim}
\end{verbatim}
%
There can be zero or more
\keyword
{
elif
}
parts, and the
\keyword
{
else
}
There can be zero or more
\keyword
{
elif
}
parts, and the
\keyword
{
else
}
part is optional. The keyword `
\keyword
{
elif
}
' is short for `else
part is optional. The keyword `
\keyword
{
elif
}
' is short for `else
if', and is useful to avoid excessive indentation. An
if', and is useful to avoid excessive indentation. An
...
@@ -872,6 +890,7 @@ if', and is useful to avoid excessive indentation. An
...
@@ -872,6 +890,7 @@ if', and is useful to avoid excessive indentation. An
\emph
{
case
}
statements found in other languages.
\emph
{
case
}
statements found in other languages.
\section
{
For Statements
}
\section
{
For Statements
}
\label
{
for
}
The
\keyword
{
for
}
statement in Python differs a bit from what you may be
The
\keyword
{
for
}
statement in Python differs a bit from what you may be
used to in
\C
{}
or Pascal. Rather than always iterating over an
used to in
\C
{}
or Pascal. Rather than always iterating over an
...
@@ -891,7 +910,7 @@ cat 3
...
@@ -891,7 +910,7 @@ cat 3
window 6
window 6
defenestrate 12
defenestrate 12
\end{verbatim}
\end{verbatim}
%
It is not safe to modify the sequence being iterated over in the loop
It is not safe to modify the sequence being iterated over in the loop
(this can only happen for mutable sequence types, i.e., lists). If
(this can only happen for mutable sequence types, i.e., lists). If
you need to modify the list you are iterating over, e.g., duplicate
you need to modify the list you are iterating over, e.g., duplicate
...
@@ -907,6 +926,7 @@ makes this particularly convenient:
...
@@ -907,6 +926,7 @@ makes this particularly convenient:
\end{verbatim}
\end{verbatim}
\section
{
The
\sectcode
{
range()
}
Function
}
\section
{
The
\sectcode
{
range()
}
Function
}
\label
{
range
}
If you do need to iterate over a sequence of numbers, the built-in
If you do need to iterate over a sequence of numbers, the built-in
function
\function
{
range()
}
comes in handy. It generates lists
function
\function
{
range()
}
comes in handy. It generates lists
...
@@ -916,7 +936,7 @@ containing arithmetic progressions, e.g.:
...
@@ -916,7 +936,7 @@ containing arithmetic progressions, e.g.:
>>> 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{verbatim}
%
The given end point is never part of the generated list;
The given end point is never part of the generated list;
\code
{
range(10)
}
generates a list of 10 values, exactly the legal
\code
{
range(10)
}
generates a list of 10 values, exactly the legal
indices for items of a sequence of length 10. It is possible to let
indices for items of a sequence of length 10. It is possible to let
...
@@ -931,7 +951,7 @@ the range start at another number, or to specify a different increment
...
@@ -931,7 +951,7 @@ the range start at another number, or to specify a different increment
>>> range(-10, -100, -30)
>>> range(-10, -100, -30)
[-10, -40, -70]
[-10, -40, -70]
\end{verbatim}
\end{verbatim}
%
To iterate over the indices of a sequence, combine
\function
{
range()
}
To iterate over the indices of a sequence, combine
\function
{
range()
}
and
\function
{
len()
}
as follows:
and
\function
{
len()
}
as follows:
...
@@ -948,6 +968,7 @@ and \function{len()} as follows:
...
@@ -948,6 +968,7 @@ and \function{len()} as follows:
\end{verbatim}
\end{verbatim}
\section
{
Break and Continue Statements, and Else Clauses on Loops
}
\section
{
Break and Continue Statements, and Else Clauses on Loops
}
\label
{
break
}
The
\keyword
{
break
}
statement, like in
\C
{}
, breaks out of the smallest
The
\keyword
{
break
}
statement, like in
\C
{}
, breaks out of the smallest
enclosing
\keyword
{
for
}
or
\keyword
{
while
}
loop.
enclosing
\keyword
{
for
}
or
\keyword
{
while
}
loop.
...
@@ -982,6 +1003,7 @@ which searches for prime numbers:
...
@@ -982,6 +1003,7 @@ which searches for prime numbers:
\end{verbatim}
\end{verbatim}
\section
{
Pass Statements
}
\section
{
Pass Statements
}
\label
{
pass
}
The
\keyword
{
pass
}
statement does nothing.
The
\keyword
{
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
...
@@ -995,6 +1017,7 @@ For example:
...
@@ -995,6 +1017,7 @@ For example:
\end{verbatim}
\end{verbatim}
\section
{
Defining Functions
}
\section
{
Defining Functions
}
\label
{
functions
}
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:
...
@@ -1011,7 +1034,7 @@ arbitrary boundary:
...
@@ -1011,7 +1034,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{verbatim}
%
The keyword
\keyword
{
def
}
introduces a function
\emph
{
definition
}
. It
The keyword
\keyword
{
def
}
introduces a function
\emph
{
definition
}
. It
must be followed by the function name and the parenthesized list of
must be followed by the function name and the parenthesized list of
formal parameters. The statements that form the body of the function
formal parameters. The statements that form the body of the function
...
@@ -1058,7 +1081,7 @@ mechanism:
...
@@ -1058,7 +1081,7 @@ mechanism:
>>> 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{verbatim}
%
You might object that
\code
{
fib
}
is not a function but a procedure. In
You might object that
\code
{
fib
}
is not a function but a procedure. In
Python, like in
\C
{}
, procedures are just functions that don't return a
Python, like in
\C
{}
, procedures are just functions that don't return a
value. In fact, technically speaking, procedures do return a value,
value. In fact, technically speaking, procedures do return a value,
...
@@ -1071,7 +1094,7 @@ if you really want to:
...
@@ -1071,7 +1094,7 @@ if you really want to:
>>> print fib(0)
>>> print fib(0)
None
None
\end{verbatim}
\end{verbatim}
%
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:
...
@@ -1118,11 +1141,13 @@ efficient.
...
@@ -1118,11 +1141,13 @@ efficient.
\end{itemize}
\end{itemize}
\section
{
More on Defining Functions
}
\section
{
More on Defining Functions
}
\label
{
defining
}
It is also possible to define functions with a variable number of
It is also possible to define functions with a variable number of
arguments. There are three forms, which can be combined.
arguments. There are three forms, which can be combined.
\subsection
{
Default Argument Values
}
\subsection
{
Default Argument Values
}
\label
{
defaultArgs
}
The most useful form is to specify a default value for one or more
The most useful form is to specify a default value for one or more
arguments. This creates a function that can be called with fewer
arguments. This creates a function that can be called with fewer
...
@@ -1156,6 +1181,7 @@ f()
...
@@ -1156,6 +1181,7 @@ f()
will print
\code
{
5
}
.
will print
\code
{
5
}
.
\subsection
{
Keyword Arguments
}
\subsection
{
Keyword Arguments
}
\label
{
keywordArgs
}
Functions can also be called using
Functions can also be called using
keyword arguments of the form
\samp
{
\var
{
keyword
}
=
\var
{
value
}}
. For
keyword arguments of the form
\samp
{
\var
{
keyword
}
=
\var
{
value
}}
. For
...
@@ -1236,6 +1262,7 @@ sketch : Cheese Shop Sketch
...
@@ -1236,6 +1262,7 @@ sketch : Cheese Shop Sketch
\end{verbatim}
\end{verbatim}
\subsection
{
Arbitrary Argument Lists
}
\subsection
{
Arbitrary Argument Lists
}
\label
{
arbitraryArgs
}
Finally, the least frequently used option is to specify that a
Finally, the least frequently used option is to specify that a
function can be called with an arbitrary number of arguments. These
function can be called with an arbitrary number of arguments. These
...
@@ -1248,11 +1275,13 @@ def fprintf(file, format, *args):
...
@@ -1248,11 +1275,13 @@ def fprintf(file, format, *args):
\end{verbatim}
\end{verbatim}
\chapter
{
Data Structures
}
\chapter
{
Data Structures
}
\label
{
structures
}
This chapter describes some things you've learned about already in
This chapter describes some things you've learned about already in
more detail, and adds some new things as well.
more detail, and adds some new things as well.
\section
{
More on Lists
}
\section
{
More on Lists
}
\label
{
moreLists
}
The list data type has some more methods. Here are all of the methods
The list data type has some more methods. Here are all of the methods
of list objects:
of list objects:
...
@@ -1311,6 +1340,7 @@ An example that uses all list methods:
...
@@ -1311,6 +1340,7 @@ An example that uses all list methods:
\end{verbatim}
\end{verbatim}
\subsection
{
Functional Programming Tools
}
\subsection
{
Functional Programming Tools
}
\label
{
functional
}
There are three built-in functions that are very useful when used with
There are three built-in functions that are very useful when used with
lists:
\function
{
filter()
}
,
\function
{
map()
}
, and
\function
{
reduce()
}
.
lists:
\function
{
filter()
}
,
\function
{
map()
}
, and
\function
{
reduce()
}
.
...
@@ -1389,6 +1419,7 @@ item, then to the result and the next item, and so on. For example,
...
@@ -1389,6 +1419,7 @@ item, then to the result and the next item, and so on. For example,
\end{verbatim}
\end{verbatim}
\section
{
The
\sectcode
{
del
}
statement
}
\section
{
The
\sectcode
{
del
}
statement
}
\label
{
del
}
There is a way to remove an item from a list given its index instead
There is a way to remove an item from a list given its index instead
of its value: the
\code
{
del
}
statement. This can also be used to
of its value: the
\code
{
del
}
statement. This can also be used to
...
@@ -1405,18 +1436,19 @@ empty list to the slice). For example:
...
@@ -1405,18 +1436,19 @@ empty list to the slice). For example:
>>> a
>>> a
[1, 66.6, 1234.5]
[1, 66.6, 1234.5]
\end{verbatim}
\end{verbatim}
%
\
code
{
del
}
can also be used to delete entire variables:
\
keyword
{
del
}
can also be used to delete entire variables:
\begin{verbatim}
\begin{verbatim}
>>> del a
>>> del a
\end{verbatim}
\end{verbatim}
%
Referencing the name
\code
{
a
}
hereafter is an error (at least until
Referencing the name
\code
{
a
}
hereafter is an error (at least until
another value is assigned to it). We'll find other uses for
\code
{
del
}
another value is assigned to it). We'll find other uses for
later.
\keyword
{
del
}
later.
\section
{
Tuples and Sequences
}
\section
{
Tuples and Sequences
}
\label
{
tuples
}
We saw that lists and strings have many common properties, e.g.,
We saw that lists and strings have many common properties, e.g.,
indexing and slicing operations. They are two examples of
indexing and slicing operations. They are two examples of
...
@@ -1438,7 +1470,7 @@ instance:
...
@@ -1438,7 +1470,7 @@ instance:
>>> u
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
\end{verbatim}
\end{verbatim}
%
As you see, on output tuples are alway enclosed in parentheses, so
As you see, on output tuples are alway enclosed in parentheses, so
that nested tuples are interpreted correctly; they may be input with
that nested tuples are interpreted correctly; they may be input with
or without surrounding parentheses, although often parentheses are
or without surrounding parentheses, although often parentheses are
...
@@ -1467,7 +1499,7 @@ Ugly, but effective. For example:
...
@@ -1467,7 +1499,7 @@ Ugly, but effective. For example:
>>> singleton
>>> singleton
('hello',)
('hello',)
\end{verbatim}
\end{verbatim}
%
The statement
\code
{
t = 12345, 54321, 'hello!'
}
is an example of
The statement
\code
{
t = 12345, 54321, 'hello!'
}
is an example of
\emph
{
tuple packing
}
: the values
\code
{
12345
}
,
\code
{
54321
}
and
\emph
{
tuple packing
}
: the values
\code
{
12345
}
,
\code
{
54321
}
and
\code
{
'hello!'
}
are packed together in a tuple. The reverse operation
\code
{
'hello!'
}
are packed together in a tuple. The reverse operation
...
@@ -1476,7 +1508,7 @@ is also possible, e.g.:
...
@@ -1476,7 +1508,7 @@ is also possible, e.g.:
\begin{verbatim}
\begin{verbatim}
>>> x, y, z = t
>>> x, y, z = t
\end{verbatim}
\end{verbatim}
%
This is called, appropriately enough,
\emph
{
tuple unpacking
}
. Tuple
This is called, appropriately enough,
\emph
{
tuple unpacking
}
. Tuple
unpacking requires that the list of variables on the left has the same
unpacking requires that the list of variables on the left has the same
number of elements as the length of the tuple. Note that multiple
number of elements as the length of the tuple. Note that multiple
...
@@ -1493,6 +1525,7 @@ square brackets:
...
@@ -1493,6 +1525,7 @@ square brackets:
\end{verbatim}
\end{verbatim}
\section
{
Dictionaries
}
\section
{
Dictionaries
}
\label
{
dictionaries
}
Another useful data type built into Python is the
\emph
{
dictionary
}
.
Another useful data type built into Python is the
\emph
{
dictionary
}
.
Dictionaries are sometimes found in other languages as ``associative
Dictionaries are sometimes found in other languages as ``associative
...
@@ -1545,6 +1578,7 @@ Here is a small example using a dictionary:
...
@@ -1545,6 +1578,7 @@ Here is a small example using a dictionary:
\end{verbatim}
\end{verbatim}
\section
{
More on Conditions
}
\section
{
More on Conditions
}
\label
{
conditions
}
The conditions used in
\code
{
while
}
and
\code
{
if
}
statements above can
The conditions used in
\code
{
while
}
and
\code
{
if
}
statements above can
contain other operators besides comparisons.
contain other operators besides comparisons.
...
@@ -1584,10 +1618,11 @@ expression to a variable. For example,
...
@@ -1584,10 +1618,11 @@ expression to a variable. For example,
>>> non
_
null
>>> non
_
null
'Trondheim'
'Trondheim'
\end{verbatim}
\end{verbatim}
%
Note that in Python, unlike
\C
{}
, assignment cannot occur inside expressions.
Note that in Python, unlike
\C
{}
, assignment cannot occur inside expressions.
\section
{
Comparing Sequences and Other Types
}
\section
{
Comparing Sequences and Other Types
}
\label
{
comparing
}
Sequence objects may be compared to other objects with the same
Sequence objects may be compared to other objects with the same
sequence type. The comparison uses
\emph
{
lexicographical
}
ordering:
sequence type. The comparison uses
\emph
{
lexicographical
}
ordering:
...
@@ -1611,7 +1646,7 @@ examples of comparisons between sequences with the same types:
...
@@ -1611,7 +1646,7 @@ examples of comparisons between sequences with the same types:
(1, 2, 3) = (1.0, 2.0, 3.0)
(1, 2, 3) = (1.0, 2.0, 3.0)
(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
\end{verbatim}
\end{verbatim}
%
Note that comparing objects of different types is legal. The outcome
Note that comparing objects of different types is legal. The outcome
is deterministic but arbitrary: the types are ordered by their name.
is deterministic but arbitrary: the types are ordered by their name.
Thus, a list is always smaller than a string, a string is always
Thus, a list is always smaller than a string, a string is always
...
@@ -1625,6 +1660,7 @@ to their numeric value, so 0 equals 0.0, etc.%
...
@@ -1625,6 +1660,7 @@ to their numeric value, so 0 equals 0.0, etc.%
\chapter
{
Modules
}
\chapter
{
Modules
}
\label
{
modules
}
If you quit from the Python interpreter and enter it again, the
If you quit from the Python interpreter and enter it again, the
definitions you have made (functions and variables) are lost.
definitions you have made (functions and variables) are lost.
...
@@ -1668,14 +1704,14 @@ def fib2(n): # return Fibonacci series up to n
...
@@ -1668,14 +1704,14 @@ def fib2(n): # return Fibonacci series up to n
a, b = b, a+b
a, b = b, a+b
return result
return result
\end{verbatim}
\end{verbatim}
%
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{verbatim}
\begin{verbatim}
>>> import fibo
>>> import fibo
\end{verbatim}
\end{verbatim}
%
This does not enter the names of the functions defined in
This does not enter the names of the functions defined in
\code
{
fibo
}
\code
{
fibo
}
directly in the current symbol table; it only enters the module name
directly in the current symbol table; it only enters the module name
...
@@ -1702,6 +1738,7 @@ If you intend to use a function often you can assign it to a local name:
...
@@ -1702,6 +1738,7 @@ If you intend to use a function often you can assign it to a local name:
\section
{
More on Modules
}
\section
{
More on Modules
}
\label
{
moreModules
}
A module can contain executable statements as well as function
A module can contain executable statements as well as function
definitions.
definitions.
...
@@ -1743,7 +1780,7 @@ For example:
...
@@ -1743,7 +1780,7 @@ For example:
>>> 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{verbatim}
%
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,
\code
{
fibo
}
is not
in the local symbol table (so in the example,
\code
{
fibo
}
is not
defined).
defined).
...
@@ -1755,11 +1792,12 @@ There is even a variant to import all names that a module defines:
...
@@ -1755,11 +1792,12 @@ There is even a variant to import all names that a module defines:
>>> 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{verbatim}
%
This imports all names except those beginning with an underscore
This imports all names except those beginning with an underscore
(
\code
{_}
).
(
\code
{_}
).
\subsection
{
The Module Search Path
}
\subsection
{
The Module Search Path
}
\label
{
searchPath
}
When a module named
\module
{
spam
}
is imported, the interpreter searches
When a module named
\module
{
spam
}
is imported, the interpreter searches
for a file named
\file
{
spam.py
}
in the current directory,
for a file named
\file
{
spam.py
}
in the current directory,
...
@@ -1801,6 +1839,7 @@ modules.)
...
@@ -1801,6 +1839,7 @@ modules.)
% XXX Should optimization with -O be covered here?
% XXX Should optimization with -O be covered here?
\section
{
Standard Modules
}
\section
{
Standard Modules
}
\label
{
standardModules
}
Python comes with a library of standard modules, described in a separate
Python comes with a library of standard modules, described in a separate
document, the
\emph
{
Python Library Reference
}
(``Library Reference''
document, the
\emph
{
Python Library Reference
}
(``Library Reference''
...
@@ -1826,7 +1865,7 @@ C> print 'Yuck!'
...
@@ -1826,7 +1865,7 @@ C> print 'Yuck!'
Yuck!
Yuck!
C>
C>
\end{verbatim}
\end{verbatim}
%
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.
...
@@ -1847,6 +1886,7 @@ You can modify it using standard list operations, e.g.:
...
@@ -1847,6 +1886,7 @@ You can modify it using standard list operations, e.g.:
\end{verbatim}
\end{verbatim}
\section
{
The
\sectcode
{
dir()
}
function
}
\section
{
The
\sectcode
{
dir()
}
function
}
\label
{
dir
}
The built-in function
\function
{
dir()
}
is used to find out which names
The built-in function
\function
{
dir()
}
is used to find out which names
a module defines. It returns a sorted list of strings:
a module defines. It returns a sorted list of strings:
...
@@ -1860,7 +1900,7 @@ a module defines. It returns a sorted list of strings:
...
@@ -1860,7 +1900,7 @@ a module defines. It returns a sorted list of strings:
'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace',
'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace',
'stderr', 'stdin', 'stdout', 'version']
'stderr', 'stdin', 'stdout', 'version']
\end{verbatim}
\end{verbatim}
%
Without arguments,
\function
{
dir()
}
lists the names you have defined
Without arguments,
\function
{
dir()
}
lists the names you have defined
currently:
currently:
...
@@ -1871,7 +1911,7 @@ currently:
...
@@ -1871,7 +1911,7 @@ currently:
>>> dir()
>>> dir()
['
__
name
__
', 'a', 'fib', 'fibo', 'sys']
['
__
name
__
', 'a', 'fib', 'fibo', 'sys']
\end{verbatim}
\end{verbatim}
%
Note that it lists all types of names: variables, modules, functions, etc.
Note that it lists all types of names: variables, modules, functions, etc.
\function
{
dir()
}
does not list the names of built-in functions and
\function
{
dir()
}
does not list the names of built-in functions and
...
@@ -1894,6 +1934,7 @@ standard module \module{__builtin__}:
...
@@ -1894,6 +1934,7 @@ standard module \module{__builtin__}:
\chapter
{
Input and Output
}
\chapter
{
Input and Output
}
\label
{
io
}
There are several ways to present the output of a program; data can be
There are several ways to present the output of a program; data can be
printed in a human-readable form, or written to a file for future use.
printed in a human-readable form, or written to a file for future use.
...
@@ -1943,7 +1984,7 @@ The value of x is 31.4, and y is 40000...
...
@@ -1943,7 +1984,7 @@ The value of x is 31.4, and y is 40000...
... `x, y, ('spam', 'eggs')`
... `x, y, ('spam', 'eggs')`
"(31.4, 40000, ('spam', 'eggs'))"
"(31.4, 40000, ('spam', 'eggs'))"
\end{verbatim}
\end{verbatim}
%
Here are two ways to write a table of squares and cubes:
Here are two ways to write a table of squares and cubes:
\begin{verbatim}
\begin{verbatim}
...
@@ -1977,7 +2018,7 @@ Here are two ways to write a table of squares and cubes:
...
@@ -1977,7 +2018,7 @@ Here are two ways to write a table of squares and cubes:
9 81 729
9 81 729
10 100 1000
10 100 1000
\end{verbatim}
\end{verbatim}
%
(Note that one space between each column was added by the way
(Note that one space between each column was added by the way
\keyword
{
print
}
works: it always adds spaces between its arguments.)
\keyword
{
print
}
works: it always adds spaces between its arguments.)
...
@@ -2050,6 +2091,8 @@ This is particularly useful in combination with the new built-in
...
@@ -2050,6 +2091,8 @@ This is particularly useful in combination with the new built-in
local variables.
local variables.
\section
{
Reading and Writing Files
}
\section
{
Reading and Writing Files
}
\label
{
files
}
% Opening files
% Opening files
\function
{
open()
}
returns a file object, and is most commonly used with
\function
{
open()
}
returns a file object, and is most commonly used with
two arguments:
\samp
{
open(
\var
{
filename
}
,
\var
{
mode
}
)
}
.
two arguments:
\samp
{
open(
\var
{
filename
}
,
\var
{
mode
}
)
}
.
...
@@ -2059,7 +2102,7 @@ two arguments: \samp{open(\var{filename}, \var{mode})}.
...
@@ -2059,7 +2102,7 @@ two arguments: \samp{open(\var{filename}, \var{mode})}.
>>> print f
>>> print f
<open file '/tmp/workfile', mode 'w' at 80a0960>
<open file '/tmp/workfile', mode 'w' at 80a0960>
\end{verbatim}
\end{verbatim}
%
The first argument is a string containing the filename. The second
The first argument is a string containing the filename. The second
argument is another string containing a few characters describing the
argument is another string containing a few characters describing the
way in which the file will be used.
\var
{
mode
}
can be
\code
{
'r'
}
when
way in which the file will be used.
\var
{
mode
}
can be
\code
{
'r'
}
when
...
@@ -2081,6 +2124,7 @@ written. This behind-the-scenes modification to file data is fine for
...
@@ -2081,6 +2124,7 @@ written. This behind-the-scenes modification to file data is fine for
writing such files.
writing such files.
\subsection
{
Methods of file objects
}
\subsection
{
Methods of file objects
}
\label
{
fileMethods
}
The rest of the examples in this section will assume that a file
The rest of the examples in this section will assume that a file
object called
\code
{
f
}
has already been created.
object called
\code
{
f
}
has already been created.
...
@@ -2099,7 +2143,7 @@ string (\code {""}).
...
@@ -2099,7 +2143,7 @@ string (\code {""}).
>>> f.read()
>>> f.read()
''
''
\end{verbatim}
\end{verbatim}
%
\code
{
f.readline()
}
reads a single line from the file; a newline
\code
{
f.readline()
}
reads a single line from the file; a newline
character (
\code
{
\e
n
}
) is left at the end of the string, and is only
character (
\code
{
\e
n
}
) is left at the end of the string, and is only
omitted on the last line of the file if the file doesn't end in a
omitted on the last line of the file if the file doesn't end in a
...
@@ -2116,7 +2160,7 @@ string containing only a single newline.
...
@@ -2116,7 +2160,7 @@ string containing only a single newline.
>>> f.readline()
>>> f.readline()
''
''
\end{verbatim}
\end{verbatim}
%
\code
{
f.readlines()
}
uses
\code
{
f.readline()
}
repeatedly, and returns
\code
{
f.readlines()
}
uses
\code
{
f.readline()
}
repeatedly, and returns
a list containing all the lines of data in the file.
a list containing all the lines of data in the file.
...
@@ -2124,14 +2168,14 @@ a list containing all the lines of data in the file.
...
@@ -2124,14 +2168,14 @@ a list containing all the lines of data in the file.
>>> f.readlines()
>>> f.readlines()
['This is the first line of the file.
\0
12', 'Second line of the file
\0
12']
['This is the first line of the file.
\0
12', 'Second line of the file
\0
12']
\end{verbatim}
\end{verbatim}
%
\code
{
f.write(
\var
{
string
}
)
}
writes the contents of
\var
{
string
}
to
\code
{
f.write(
\var
{
string
}
)
}
writes the contents of
\var
{
string
}
to
the file, returning
\code
{
None
}
.
the file, returning
\code
{
None
}
.
\begin{verbatim}
\begin{verbatim}
>>> f.write('This is a test
\n
')
>>> f.write('This is a test
\n
')
\end{verbatim}
\end{verbatim}
%
\code
{
f.tell()
}
returns an integer giving the file object's current
\code
{
f.tell()
}
returns an integer giving the file object's current
position in the file, measured in bytes from the beginning of the
position in the file, measured in bytes from the beginning of the
file. To change the file object's position, use
file. To change the file object's position, use
...
@@ -2153,7 +2197,7 @@ of the file as the reference point.
...
@@ -2153,7 +2197,7 @@ of the file as the reference point.
>>> f.read(1)
>>> f.read(1)
'd'
'd'
\end{verbatim}
\end{verbatim}
%
When you're done with a file, call
\code
{
f.close()
}
to close it and
When you're done with a file, call
\code
{
f.close()
}
to close it and
free up any system resources taken up by the open file. After calling
free up any system resources taken up by the open file. After calling
\code
{
f.close()
}
, attempts to use the file object will automatically fail.
\code
{
f.close()
}
, attempts to use the file object will automatically fail.
...
@@ -2165,12 +2209,13 @@ Traceback (innermost last):
...
@@ -2165,12 +2209,13 @@ Traceback (innermost last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in ?
ValueError: I/O operation on closed file
ValueError: I/O operation on closed file
\end{verbatim}
\end{verbatim}
%
File objects have some additional methods, such as
\method
{
isatty()
}
File objects have some additional methods, such as
\method
{
isatty()
}
and
\method
{
truncate()
}
which are less frequently used; consult the
and
\method
{
truncate()
}
which are less frequently used; consult the
Library Reference for a complete guide to file objects.
Library Reference for a complete guide to file objects.
\subsection
{
The pickle module
}
\subsection
{
The pickle module
}
\label
{
pickle
}
Strings can easily be written to and read from a file. Numbers take a
Strings can easily be written to and read from a file. Numbers take a
bit more effort, since the
\method
{
read()
}
method only returns
bit more effort, since the
\method
{
read()
}
method only returns
...
@@ -2197,14 +2242,14 @@ one line of code:
...
@@ -2197,14 +2242,14 @@ one line of code:
\begin{verbatim}
\begin{verbatim}
pickle.dump(x, f)
pickle.dump(x, f)
\end{verbatim}
\end{verbatim}
%
To unpickle the object again, if
\code
{
f
}
is a file object which has
To unpickle the object again, if
\code
{
f
}
is a file object which has
been opened for reading:
been opened for reading:
\begin{verbatim}
\begin{verbatim}
x = pickle.load(f)
x = pickle.load(f)
\end{verbatim}
\end{verbatim}
%
(There are other variants of this, used when pickling many objects or
(There are other variants of this, used when pickling many objects or
when you don't want to write the pickled data to a file; consult the
when you don't want to write the pickled data to a file; consult the
complete documentation for
\module
{
pickle
}
in the Library Reference.)
complete documentation for
\module
{
pickle
}
in the Library Reference.)
...
@@ -2220,6 +2265,7 @@ unpickled.
...
@@ -2220,6 +2265,7 @@ unpickled.
\chapter
{
Errors and Exceptions
}
\chapter
{
Errors and Exceptions
}
\label
{
errors
}
Until now error messages haven't been more than mentioned, but if you
Until now error messages haven't been more than mentioned, but if you
have tried out the examples you have probably seen some. There are
have tried out the examples you have probably seen some. There are
...
@@ -2227,6 +2273,7 @@ have tried out the examples you have probably seen some. There are
...
@@ -2227,6 +2273,7 @@ have tried out the examples you have probably seen some. There are
and
\emph
{
exceptions
}
.
and
\emph
{
exceptions
}
.
\section
{
Syntax Errors
}
\section
{
Syntax Errors
}
\label
{
syntaxErrors
}
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:
...
@@ -2238,7 +2285,7 @@ kind of complaint you get while you are still learning Python:
...
@@ -2238,7 +2285,7 @@ kind of complaint you get while you are still learning Python:
^
^
SyntaxError: invalid syntax
SyntaxError: invalid syntax
\end{verbatim}
\end{verbatim}
%
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
...
@@ -2249,6 +2296,7 @@ File name and line number are printed so you know where to look in case
...
@@ -2249,6 +2296,7 @@ File name and line number are printed so you know where to look in case
the input came from a script.
the input came from a script.
\section
{
Exceptions
}
\section
{
Exceptions
}
\label
{
exceptions
}
Even if a statement or expression is syntactically correct, it may
Even if a statement or expression is syntactically correct, it may
cause an error when an attempt is made to execute it.
cause an error when an attempt is made to execute it.
...
@@ -2271,7 +2319,7 @@ Traceback (innermost last):
...
@@ -2271,7 +2319,7 @@ Traceback (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
TypeError: illegal argument type for built-in operation
TypeError: illegal argument type for built-in operation
\end{verbatim}
\end{verbatim}
%
The last line of the error message indicates what happened.
The last line of the error message indicates what happened.
Exceptions come in different types, and the type is printed as part of
Exceptions come in different types, and the type is printed as part of
the message: the types in the example are
the message: the types in the example are
...
@@ -2298,6 +2346,7 @@ The Library Reference lists the built-in exceptions and their
...
@@ -2298,6 +2346,7 @@ The Library Reference lists the built-in exceptions and their
meanings.
meanings.
\section
{
Handling Exceptions
}
\section
{
Handling Exceptions
}
\label
{
handling
}
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
...
@@ -2317,7 +2366,7 @@ some floating point numbers:
...
@@ -2317,7 +2366,7 @@ some floating point numbers:
0 *** has no inverse ***
0 *** has no inverse ***
10 0.1
10 0.1
\end{verbatim}
\end{verbatim}
%
The
\keyword
{
try
}
statement works as follows.
The
\keyword
{
try
}
statement works as follows.
\begin{itemize}
\begin{itemize}
\item
\item
...
@@ -2352,7 +2401,7 @@ e.g.:
...
@@ -2352,7 +2401,7 @@ e.g.:
... except (RuntimeError, TypeError, NameError):
... except (RuntimeError, TypeError, NameError):
... pass
... pass
\end{verbatim}
\end{verbatim}
%
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, since it is easy to mask a real
Use this with extreme caution, since it is easy to mask a real
...
@@ -2390,7 +2439,7 @@ argument's value, as follows:
...
@@ -2390,7 +2439,7 @@ argument's value, as follows:
...
...
name spam undefined
name spam undefined
\end{verbatim}
\end{verbatim}
%
If an exception has an argument, it is printed as the last part
If an exception has an argument, it is printed as the last part
(`detail') of the message for unhandled exceptions.
(`detail') of the message for unhandled exceptions.
...
@@ -2410,9 +2459,10 @@ For example:
...
@@ -2410,9 +2459,10 @@ For example:
...
...
Handling run-time error: integer division or modulo
Handling run-time error: integer division or modulo
\end{verbatim}
\end{verbatim}
%
\section
{
Raising Exceptions
}
\section
{
Raising Exceptions
}
\label
{
raising
}
The
\keyword
{
raise
}
statement allows the programmer to force a
The
\keyword
{
raise
}
statement allows the programmer to force a
specified exception to occur.
specified exception to occur.
...
@@ -2424,14 +2474,14 @@ Traceback (innermost last):
...
@@ -2424,14 +2474,14 @@ Traceback (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
NameError: HiThere
NameError: HiThere
\end{verbatim}
\end{verbatim}
%
The first argument to
\keyword
{
raise
}
names the exception to be
The first argument to
\keyword
{
raise
}
names the exception to be
raised. The optional second argument specifies the exception's
raised. The optional second argument specifies the exception's
argument.
argument.
%
\section
{
User-defined Exceptions
}
\section
{
User-defined Exceptions
}
\label
{
userExceptions
}
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.
...
@@ -2450,13 +2500,13 @@ Traceback (innermost last):
...
@@ -2450,13 +2500,13 @@ Traceback (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
my
_
exc: 1
my
_
exc: 1
\end{verbatim}
\end{verbatim}
%
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.
%
\section
{
Defining Clean-up Actions
}
\section
{
Defining Clean-up Actions
}
\label
{
cleanup
}
The
\keyword
{
try
}
statement has another optional clause which is
The
\keyword
{
try
}
statement has another optional clause which is
intended to define clean-up actions that must be executed under all
intended to define clean-up actions that must be executed under all
...
@@ -2473,7 +2523,7 @@ Traceback (innermost last):
...
@@ -2473,7 +2523,7 @@ Traceback (innermost last):
File "<stdin>", line 2
File "<stdin>", line 2
KeyboardInterrupt
KeyboardInterrupt
\end{verbatim}
\end{verbatim}
%
A
\emph
{
finally clause
}
is executed whether or not an exception has
A
\emph
{
finally clause
}
is executed whether or not an exception has
occurred in the try clause. When an exception has occurred, it is
occurred in the try clause. When an exception has occurred, it is
re-raised after the finally clause is executed. The finally clause is
re-raised after the finally clause is executed. The finally clause is
...
@@ -2484,6 +2534,7 @@ A \keyword{try} statement must either have one or more except clauses
...
@@ -2484,6 +2534,7 @@ A \keyword{try} statement must either have one or more except clauses
or one finally clause, but not both.
or one finally clause, but not both.
\chapter
{
Classes
}
\chapter
{
Classes
}
\label
{
classes
}
Python's class mechanism adds classes to the language with a minimum
Python's class mechanism adds classes to the language with a minimum
of new syntax and semantics. It is a mixture of the class mechanisms
of new syntax and semantics. It is a mixture of the class mechanisms
...
@@ -2511,6 +2562,7 @@ built-in operators with special syntax (arithmetic operators,
...
@@ -2511,6 +2562,7 @@ built-in operators with special syntax (arithmetic operators,
subscripting etc.) can be redefined for class members.
subscripting etc.) can be redefined for class members.
\section
{
A word about terminology
}
\section
{
A word about terminology
}
\label
{
terminology
}
Lacking universally accepted terminology to talk about classes, I'll
Lacking universally accepted terminology to talk about classes, I'll
make occasional use of Smalltalk and
\Cpp
{}
terms. (I'd use Modula-3
make occasional use of Smalltalk and
\Cpp
{}
terms. (I'd use Modula-3
...
@@ -2543,6 +2595,7 @@ Pascal.
...
@@ -2543,6 +2595,7 @@ Pascal.
\section
{
Python scopes and name spaces
}
\section
{
Python scopes and name spaces
}
\label
{
scopes
}
Before introducing classes, I first have to tell you something about
Before introducing classes, I first have to tell you something about
Python's scope rules. Class definitions play some neat tricks with
Python's scope rules. Class definitions play some neat tricks with
...
@@ -2646,12 +2699,14 @@ variables live in the global scope.)
...
@@ -2646,12 +2699,14 @@ variables live in the global scope.)
\section
{
A first look at classes
}
\section
{
A first look at classes
}
\label
{
firstClasses
}
Classes introduce a little bit of new syntax, three new object types,
Classes introduce a little bit of new syntax, three new object types,
and some new semantics.
and some new semantics.
\subsection
{
Class definition syntax
}
\subsection
{
Class definition syntax
}
\label
{
classDefinition
}
The simplest form of class definition looks like this:
The simplest form of class definition looks like this:
...
@@ -2691,6 +2746,7 @@ the class definition header (\code{ClassName} in the example).
...
@@ -2691,6 +2746,7 @@ the class definition header (\code{ClassName} in the example).
\subsection
{
Class objects
}
\subsection
{
Class objects
}
\label
{
classObjects
}
Class objects support two kinds of operations: attribute references
Class objects support two kinds of operations: attribute references
and instantiation.
and instantiation.
...
@@ -2729,6 +2785,7 @@ the local variable \code{x}.
...
@@ -2729,6 +2785,7 @@ the local variable \code{x}.
\subsection
{
Instance objects
}
\subsection
{
Instance objects
}
\label
{
instanceObjects
}
Now what can we do with instance objects? The only operations
Now what can we do with instance objects? The only operations
understood by instance objects are attribute references. There are
understood by instance objects are attribute references. There are
...
@@ -2769,6 +2826,7 @@ object.
...
@@ -2769,6 +2826,7 @@ object.
\subsection
{
Method objects
}
\subsection
{
Method objects
}
\label
{
methodObjects
}
Usually, a method is called immediately, e.g.:
Usually, a method is called immediately, e.g.:
...
@@ -2817,7 +2875,7 @@ list, and the function object is called with this new argument list.
...
@@ -2817,7 +2875,7 @@ list, and the function object is called with this new argument list.
\section
{
Random remarks
}
\section
{
Random remarks
}
\label
{
remarks
}
[These should perhaps be placed more carefully...]
[These should perhaps be placed more carefully...]
...
@@ -2951,6 +3009,7 @@ reasons why a method would want to reference its own class!
...
@@ -2951,6 +3009,7 @@ reasons why a method would want to reference its own class!
\section
{
Inheritance
}
\section
{
Inheritance
}
\label
{
inheritance
}
Of course, a language feature would not be worthy of the name ``class''
Of course, a language feature would not be worthy of the name ``class''
without supporting inheritance. The syntax for a derived class
without supporting inheritance. The syntax for a derived class
...
@@ -3003,6 +3062,7 @@ the base class is defined or imported directly in the global scope.)
...
@@ -3003,6 +3062,7 @@ the base class is defined or imported directly in the global scope.)
\subsection
{
Multiple inheritance
}
\subsection
{
Multiple inheritance
}
\label
{
multiple
}
Python supports a limited form of multiple inheritance as well. A
Python supports a limited form of multiple inheritance as well. A
class definition with multiple base classes looks as follows:
class definition with multiple base classes looks as follows:
...
@@ -3043,6 +3103,7 @@ not clear that these semantics are in any way useful.
...
@@ -3043,6 +3103,7 @@ not clear that these semantics are in any way useful.
\section
{
Private variables through name mangling
}
\section
{
Private variables through name mangling
}
\label
{
private
}
There is now limited support for class-private
There is now limited support for class-private
identifiers. Any identifier of the form
\code
{__
spam
}
(at least two
identifiers. Any identifier of the form
\code
{__
spam
}
(at least two
...
@@ -3112,6 +3173,7 @@ class VirtualAttributes:
...
@@ -3112,6 +3173,7 @@ class VirtualAttributes:
%be removed without providing a better solution and a migration path.
%be removed without providing a better solution and a migration path.
\section
{
Odds and ends
}
\section
{
Odds and ends
}
\label
{
odds
}
Sometimes it is useful to have a data type similar to the Pascal
Sometimes it is useful to have a data type similar to the Pascal
``record'' or
\C
{}
``struct'', bundling together a couple of named data
``record'' or
\C
{}
``struct'', bundling together a couple of named data
...
@@ -3148,6 +3210,7 @@ object of which the method is an instance, and \code{m.im_func} is the
...
@@ -3148,6 +3210,7 @@ object of which the method is an instance, and \code{m.im_func} is the
function object corresponding to the method.
function object corresponding to the method.
\subsection
{
Exceptions Can Be Classes
}
\subsection
{
Exceptions Can Be Classes
}
\label
{
exceptionClasses
}
User-defined exceptions are no longer limited to being string objects
User-defined exceptions are no longer limited to being string objects
--- they can be identified by classes as well. Using this mechanism it
--- they can be identified by classes as well. Using this mechanism it
...
@@ -3206,6 +3269,7 @@ finally the instance converted to a string using the built-in function
...
@@ -3206,6 +3269,7 @@ finally the instance converted to a string using the built-in function
In this release, the built-in exceptions are still strings.
In this release, the built-in exceptions are still strings.
\chapter
{
What Now?
}
\chapter
{
What Now?
}
\label
{
whatNow
}
Hopefully reading this tutorial has reinforced your interest in using
Hopefully reading this tutorial has reinforced your interest in using
Python. Now what should you do?
Python. Now what should you do?
...
@@ -3254,6 +3318,7 @@ information on how to join.
...
@@ -3254,6 +3318,7 @@ information on how to join.
% found or it elsewhere in the Tutorial?
% found or it elsewhere in the Tutorial?
\section
{
Lambda Forms
}
\section
{
Lambda Forms
}
\label
{
lambda
}
% XXX Where to put this? Or just leave it out?
% XXX Where to put this? Or just leave it out?
...
@@ -3274,6 +3339,7 @@ def make_incrementor(n):
...
@@ -3274,6 +3339,7 @@ def make_incrementor(n):
\end{verbatim}
\end{verbatim}
\section
{
Documentation Strings
}
\section
{
Documentation Strings
}
\label
{
docstrings
}
% XXX Where to put this? Or just leave it out?
% XXX Where to put this? Or just leave it out?
...
@@ -3310,6 +3376,7 @@ tested after expansion of tabs (to 8 spaces, normally).
...
@@ -3310,6 +3376,7 @@ tested after expansion of tabs (to 8 spaces, normally).
\appendix\chapter
{
Interactive Input Editing and History Substitution
}
\appendix\chapter
{
Interactive Input Editing and History Substitution
}
\label
{
interacting
}
Some versions of the Python interpreter support editing of the current
Some versions of the Python interpreter support editing of the current
input line and history substitution, similar to facilities found in
input line and history substitution, similar to facilities found in
...
@@ -3319,6 +3386,7 @@ editing. This library has its own documentation which I won't
...
@@ -3319,6 +3386,7 @@ editing. This library has its own documentation which I won't
duplicate here; however, the basics are easily explained.
duplicate here; however, the basics are easily explained.
\section
{
Line Editing
}
\section
{
Line Editing
}
\label
{
lineEditing
}
If supported, input line editing is active whenever the interpreter
If supported, input line editing is active whenever the interpreter
prints a primary or secondary prompt. The current line can be edited
prints a primary or secondary prompt. The current line can be edited
...
@@ -3332,6 +3400,7 @@ string. C-underscore undoes the last change you made; it can be
...
@@ -3332,6 +3400,7 @@ string. C-underscore undoes the last change you made; it can be
repeated for cumulative effect.
repeated for cumulative effect.
\section
{
History Substitution
}
\section
{
History Substitution
}
\label
{
history
}
History substitution works as follows. All non-empty input lines
History substitution works as follows. All non-empty input lines
issued are saved in a history buffer, and when a new prompt is given
issued are saved in a history buffer, and when a new prompt is given
...
@@ -3343,6 +3412,7 @@ key passes the current line to the interpreter. C-R starts an
...
@@ -3343,6 +3412,7 @@ key passes the current line to the interpreter. C-R starts an
incremental reverse search; C-S starts a forward search.
incremental reverse search; C-S starts a forward search.
\section
{
Key Bindings
}
\section
{
Key Bindings
}
\label
{
keyBindings
}
The key bindings and some other parameters of the Readline library can
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
...
@@ -3389,6 +3459,7 @@ in your \file{\$HOME/.inputrc}. (Of course, this makes it hard to type
...
@@ -3389,6 +3459,7 @@ in your \file{\$HOME/.inputrc}. (Of course, this makes it hard to type
indented continuation lines...)
indented continuation lines...)
\section
{
Commentary
}
\section
{
Commentary
}
\label
{
commentary
}
This facility is an enormous step forward compared to previous
This facility is an enormous step forward compared to previous
versions of the interpreter; however, some wishes are left: It would
versions of the interpreter; however, some wishes are left: It would
...
...
Doc/tut/tut.tex
View file @
dfbccb71
...
@@ -66,7 +66,8 @@ modules described in the \emph{Python Library Reference}.
...
@@ -66,7 +66,8 @@ modules described in the \emph{Python Library Reference}.
\chapter
{
Whetting Your Appetite
}
\chapter
{
Whetting Your Appetite
}
\section
{
Introduction
}
%\section{Introduction}
\label
{
intro
}
If you ever wrote a large shell script, you probably know this
If you ever wrote a large shell script, you probably know this
feeling: you'd love to add yet another feature, but it's already so
feeling: you'd love to add yet another feature, but it's already so
...
@@ -138,6 +139,7 @@ references to Monty Python skits in documentation is not only allowed,
...
@@ -138,6 +139,7 @@ references to Monty Python skits in documentation is not only allowed,
it is encouraged!
it is encouraged!
\section
{
Where From Here
}
\section
{
Where From Here
}
\label
{
where
}
Now that you are all excited about Python, you'll want to examine it
Now that you are all excited about Python, you'll want to examine it
in some more detail. Since the best way to learn a language is
in some more detail. Since the best way to learn a language is
...
@@ -154,8 +156,10 @@ and finally touching upon advanced concepts like exceptions
...
@@ -154,8 +156,10 @@ and finally touching upon advanced concepts like exceptions
and user-defined classes.
and user-defined classes.
\chapter
{
Using the Python Interpreter
}
\chapter
{
Using the Python Interpreter
}
\label
{
using
}
\section
{
Invoking the Interpreter
}
\section
{
Invoking the Interpreter
}
\label
{
invoking
}
The Python interpreter is usually installed as
\file
{
/usr/local/bin/python
}
The Python interpreter is usually installed as
\file
{
/usr/local/bin/python
}
on those machines where it is available; putting
\file
{
/usr/local/bin
}
in
on those machines where it is available; putting
\file
{
/usr/local/bin
}
in
...
@@ -165,7 +169,7 @@ typing the command
...
@@ -165,7 +169,7 @@ typing the command
\begin{verbatim}
\begin{verbatim}
python
python
\end{verbatim}
\end{verbatim}
%
to the shell. Since the choice of the directory where the interpreter
to the shell. Since the choice of the directory where the interpreter
lives is an installation option, other places are possible; check with
lives is an installation option, other places are possible; check with
your local Python guru or system administrator. (E.g.,
your local Python guru or system administrator. (E.g.,
...
@@ -217,6 +221,7 @@ is read from standard input, for the same reason as explained in the
...
@@ -217,6 +221,7 @@ is read from standard input, for the same reason as explained in the
previous paragraph.)
previous paragraph.)
\subsection
{
Argument Passing
}
\subsection
{
Argument Passing
}
\label
{
argPassing
}
When known to the interpreter, the script name and additional
When known to the interpreter, the script name and additional
arguments thereafter are passed to the script in the variable
arguments thereafter are passed to the script in the variable
...
@@ -230,6 +235,7 @@ interpreter's option processing but left in \code{sys.argv} for the
...
@@ -230,6 +235,7 @@ interpreter's option processing but left in \code{sys.argv} for the
command to handle.
command to handle.
\subsection
{
Interactive Mode
}
\subsection
{
Interactive Mode
}
\label
{
interactive
}
When commands are read from a tty, the interpreter is said to be in
When commands are read from a tty, the interpreter is said to be in
\emph
{
interactive mode
}
. In this mode it prompts for the next command
\emph
{
interactive mode
}
. In this mode it prompts for the next command
...
@@ -249,8 +255,10 @@ Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
...
@@ -249,8 +255,10 @@ Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
\end{verbatim}
\end{verbatim}
\section
{
The Interpreter and its Environment
}
\section
{
The Interpreter and its Environment
}
\label
{
interp
}
\subsection
{
Error Handling
}
\subsection
{
Error Handling
}
\label
{
error
}
When an error occurs, the interpreter prints an error
When an error occurs, the interpreter prints an error
message and a stack trace. In interactive mode, it then returns to
message and a stack trace. In interactive mode, it then returns to
...
@@ -275,6 +283,7 @@ Typing an interrupt while a command is executing raises the
...
@@ -275,6 +283,7 @@ Typing an interrupt while a command is executing raises the
\code
{
try
}
statement.
\code
{
try
}
statement.
\subsection
{
Executable Python scripts
}
\subsection
{
Executable Python scripts
}
\label
{
scripts
}
On BSD'ish
\UNIX
{}
systems, Python scripts can be made directly
On BSD'ish
\UNIX
{}
systems, Python scripts can be made directly
executable, like shell scripts, by putting the line
executable, like shell scripts, by putting the line
...
@@ -282,12 +291,13 @@ executable, like shell scripts, by putting the line
...
@@ -282,12 +291,13 @@ executable, like shell scripts, by putting the line
\begin{verbatim}
\begin{verbatim}
#! /usr/bin/env python
#! /usr/bin/env python
\end{verbatim}
\end{verbatim}
%
(assuming that the interpreter is on the user's PATH) at the beginning
(assuming that the interpreter is on the user's PATH) at the beginning
of the script and giving the file an executable mode. The
\samp
{
\#
!
}
of the script and giving the file an executable mode. The
\samp
{
\#
!
}
must be the first two characters of the file.
must be the first two characters of the file.
\subsection
{
The Interactive Startup File
}
\subsection
{
The Interactive Startup File
}
\label
{
startup
}
% XXX This should probably be dumped in an appendix, since most people
% XXX This should probably be dumped in an appendix, since most people
% don't use Python interactively in non-trivial ways.
% don't use Python interactively in non-trivial ways.
...
@@ -319,6 +329,7 @@ execfile(os.environ['PYTHONSTARTUP'])
...
@@ -319,6 +329,7 @@ execfile(os.environ['PYTHONSTARTUP'])
\end{verbatim}
\end{verbatim}
\chapter
{
An Informal Introduction to Python
}
\chapter
{
An Informal Introduction to Python
}
\label
{
informal
}
In the following examples, input and output are distinguished by the
In the following examples, input and output are distinguished by the
presence or absence of prompts (
\samp
{
>>>
}
and
\samp
{
...
}
): to repeat
presence or absence of prompts (
\samp
{
>>>
}
and
\samp
{
...
}
): to repeat
...
@@ -334,11 +345,13 @@ Note that a secondary prompt on a line by itself in an example means
...
@@ -334,11 +345,13 @@ Note that a secondary prompt on a line by itself in an example means
you must type a blank line; this is used to end a multi-line command.
you must type a blank line; this is used to end a multi-line command.
\section
{
Using Python as a Calculator
}
\section
{
Using Python as a Calculator
}
\label
{
calculator
}
Let's try some simple Python commands. Start the interpreter and wait
Let's try some simple Python commands. Start the interpreter and wait
for the primary prompt,
\samp
{
>>>
}
. (It shouldn't take long.)
for the primary prompt,
\samp
{
>>>
}
. (It shouldn't take long.)
\subsection
{
Numbers
}
\subsection
{
Numbers
}
\label
{
numbers
}
The interpreter acts as a simple calculator: you can type an
The interpreter acts as a simple calculator: you can type an
expression at it and it will write the value. Expression syntax is
expression at it and it will write the value. Expression syntax is
...
@@ -362,7 +375,7 @@ can be used for grouping. For example:
...
@@ -362,7 +375,7 @@ can be used for grouping. For example:
>>> 7/-3
>>> 7/-3
-3
-3
\end{verbatim}
\end{verbatim}
%
Like in
\C
{}
, the equal sign (
\code
{
=
}
) is used to assign a value to a
Like in
\C
{}
, the equal sign (
\code
{
=
}
) is used to assign a value to a
variable. The value of an assignment is not written:
variable. The value of an assignment is not written:
...
@@ -465,6 +478,7 @@ local variable with the same name masking the built-in variable with
...
@@ -465,6 +478,7 @@ local variable with the same name masking the built-in variable with
its magic behavior.
its magic behavior.
\subsection
{
Strings
}
\subsection
{
Strings
}
\label
{
strings
}
Besides numbers, Python can also manipulate strings, which can be
Besides numbers, Python can also manipulate strings, which can be
expressed in several ways. They can be enclosed in single quotes or
expressed in several ways. They can be enclosed in single quotes or
...
@@ -484,7 +498,7 @@ double quotes:
...
@@ -484,7 +498,7 @@ double quotes:
>>> '"Isn
\'
t," she said.'
>>> '"Isn
\'
t," she said.'
'"Isn
\'
t," she said.'
'"Isn
\'
t," she said.'
\end{verbatim}
\end{verbatim}
%
String literals can span multiple lines in several ways. Newlines can
String literals can span multiple lines in several ways. Newlines can
be escaped with backslashes, e.g.:
be escaped with backslashes, e.g.:
...
@@ -523,7 +537,7 @@ Usage: thingy [OPTIONS]
...
@@ -523,7 +537,7 @@ Usage: thingy [OPTIONS]
-h Display this usage message
-h Display this usage message
-H hostname Hostname to connect to
-H hostname Hostname to connect to
\end{verbatim}
\end{verbatim}
%
The interpreter prints the result of string operations in the same way
The interpreter prints the result of string operations in the same way
as they are typed for input: inside quotes, and with quotes and other
as they are typed for input: inside quotes, and with quotes and other
funny characters escaped by backslashes, to show the precise
funny characters escaped by backslashes, to show the precise
...
@@ -542,7 +556,7 @@ operator, and repeated with \code{*}:
...
@@ -542,7 +556,7 @@ operator, and repeated with \code{*}:
>>> '<' + word*5 + '>'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
'<HelpAHelpAHelpAHelpAHelpA>'
\end{verbatim}
\end{verbatim}
%
Two string literals next to each other are automatically concatenated;
Two string literals next to each other are automatically concatenated;
the first line above could also have been written
\samp
{
word = 'Help'
the first line above could also have been written
\samp
{
word = 'Help'
'A'
}
; this only works with two literals, not with arbitrary string expressions.
'A'
}
; this only works with two literals, not with arbitrary string expressions.
...
@@ -561,7 +575,7 @@ separated by a colon.
...
@@ -561,7 +575,7 @@ separated by a colon.
>>> word[2:4]
>>> word[2:4]
'lp'
'lp'
\end{verbatim}
\end{verbatim}
%
Slice indices have useful defaults; an omitted first index defaults to
Slice indices have useful defaults; an omitted first index defaults to
zero, an omitted second index defaults to the size of the string being
zero, an omitted second index defaults to the size of the string being
sliced.
sliced.
...
@@ -572,7 +586,7 @@ sliced.
...
@@ -572,7 +586,7 @@ sliced.
>>> word[2:] # All but the first two characters
>>> word[2:] # All but the first two characters
'lpA'
'lpA'
\end{verbatim}
\end{verbatim}
%
Here's a useful invariant of slice operations:
\code
{
s[:i] + s[i:]
}
Here's a useful invariant of slice operations:
\code
{
s[:i] + s[i:]
}
equals
\code
{
s
}
.
equals
\code
{
s
}
.
...
@@ -582,7 +596,7 @@ equals \code{s}.
...
@@ -582,7 +596,7 @@ equals \code{s}.
>>> word[:3] + word[3:]
>>> word[:3] + word[3:]
'HelpA'
'HelpA'
\end{verbatim}
\end{verbatim}
%
Degenerate slice indices are handled gracefully: an index that is too
Degenerate slice indices are handled gracefully: an index that is too
large is replaced by the string size, an upper bound smaller than the
large is replaced by the string size, an upper bound smaller than the
lower bound returns an empty string.
lower bound returns an empty string.
...
@@ -595,7 +609,7 @@ lower bound returns an empty string.
...
@@ -595,7 +609,7 @@ lower bound returns an empty string.
>>> word[2:1]
>>> word[2:1]
''
''
\end{verbatim}
\end{verbatim}
%
Indices may be negative numbers, to start counting from the right.
Indices may be negative numbers, to start counting from the right.
For example:
For example:
...
@@ -609,7 +623,7 @@ For example:
...
@@ -609,7 +623,7 @@ For example:
>>> word[:-2] # All but the last two characters
>>> word[:-2] # All but the last two characters
'Hel'
'Hel'
\end{verbatim}
\end{verbatim}
%
But note that -0 is really the same as 0, so it does not count from
But note that -0 is really the same as 0, so it does not count from
the right!
the right!
...
@@ -617,7 +631,7 @@ the right!
...
@@ -617,7 +631,7 @@ the right!
>>> word[-0] # (since -0 equals 0)
>>> word[-0] # (since -0 equals 0)
'H'
'H'
\end{verbatim}
\end{verbatim}
%
Out-of-range negative slice indices are truncated, but don't try this
Out-of-range negative slice indices are truncated, but don't try this
for single-element (non-slice) indices:
for single-element (non-slice) indices:
...
@@ -629,7 +643,7 @@ Traceback (innermost last):
...
@@ -629,7 +643,7 @@ Traceback (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
IndexError: string index out of range
IndexError: string index out of range
\end{verbatim}
\end{verbatim}
%
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
\emph
{
between
}
characters, with the left edge of the first
pointing
\emph
{
between
}
characters, with the left edge of the first
character numbered 0. Then the right edge of the last character of a
character numbered 0. Then the right edge of the last character of a
...
@@ -642,7 +656,7 @@ string of \var{n} characters has index \var{n}, for example:
...
@@ -642,7 +656,7 @@ string of \var{n} characters has index \var{n}, for example:
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{verbatim}
%
The first row of numbers gives the position of the indices 0...5 in
The first row of numbers gives the position of the indices 0...5 in
the string; the second row gives the corresponding negative indices.
the string; the second row gives the corresponding negative indices.
The slice from
\var
{
i
}
to
\var
{
j
}
consists of all characters between
The slice from
\var
{
i
}
to
\var
{
j
}
consists of all characters between
...
@@ -661,6 +675,7 @@ The built-in function \function{len()} returns the length of a string:
...
@@ -661,6 +675,7 @@ The built-in function \function{len()} returns the length of a string:
\end{verbatim}
\end{verbatim}
\subsection
{
Lists
}
\subsection
{
Lists
}
\label
{
lists
}
Python knows a number of
\emph
{
compound
}
data types, used to group
Python knows a number of
\emph
{
compound
}
data types, used to group
together other values. The most versatile is the
\emph
{
list
}
, which
together other values. The most versatile is the
\emph
{
list
}
, which
...
@@ -672,7 +687,7 @@ square brackets. List items need not all have the same type.
...
@@ -672,7 +687,7 @@ square brackets. List items need not all have the same type.
>>> a
>>> a
['spam', 'eggs', 100, 1234]
['spam', 'eggs', 100, 1234]
\end{verbatim}
\end{verbatim}
%
Like string indices, list indices start at 0, and lists can be sliced,
Like string indices, list indices start at 0, and lists can be sliced,
concatenated and so on:
concatenated and so on:
...
@@ -690,7 +705,7 @@ concatenated and so on:
...
@@ -690,7 +705,7 @@ concatenated and so on:
>>> 3*a[:3] + ['Boe!']
>>> 3*a[:3] + ['Boe!']
['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!']
['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!']
\end{verbatim}
\end{verbatim}
%
Unlike strings, which are
\emph
{
immutable
}
, it is possible to change
Unlike strings, which are
\emph
{
immutable
}
, it is possible to change
individual elements of a list:
individual elements of a list:
...
@@ -701,7 +716,7 @@ individual elements of a list:
...
@@ -701,7 +716,7 @@ individual elements of a list:
>>> a
>>> a
['spam', 'eggs', 123, 1234]
['spam', 'eggs', 123, 1234]
\end{verbatim}
\end{verbatim}
%
Assignment to slices is also possible, and this can even change the size
Assignment to slices is also possible, and this can even change the size
of the list:
of the list:
...
@@ -722,14 +737,14 @@ of the list:
...
@@ -722,14 +737,14 @@ of the list:
>>> a
>>> a
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
\end{verbatim}
\end{verbatim}
%
The built-in function
\function
{
len()
}
also applies to lists:
The built-in function
\function
{
len()
}
also applies to lists:
\begin{verbatim}
\begin{verbatim}
>>> len(a)
>>> len(a)
8
8
\end{verbatim}
\end{verbatim}
%
It is possible to nest lists (create lists containing other lists),
It is possible to nest lists (create lists containing other lists),
for example:
for example:
...
@@ -748,11 +763,12 @@ for example:
...
@@ -748,11 +763,12 @@ for example:
>>> q
>>> q
[2, 3, 'xtra']
[2, 3, 'xtra']
\end{verbatim}
\end{verbatim}
%
Note that in the last example,
\code
{
p[1]
}
and
\code
{
q
}
really refer to
Note that in the last example,
\code
{
p[1]
}
and
\code
{
q
}
really refer to
the same object! We'll come back to
\emph
{
object semantics
}
later.
the same object! We'll come back to
\emph
{
object semantics
}
later.
\section
{
First Steps Towards Programming
}
\section
{
First Steps Towards Programming
}
\label
{
firstSteps
}
Of course, we can use Python for more complicated tasks than adding
Of course, we can use Python for more complicated tasks than adding
two and two together. For instance, we can write an initial
two and two together. For instance, we can write an initial
...
@@ -773,7 +789,7 @@ subsequence of the \emph{Fibonacci} series as follows:
...
@@ -773,7 +789,7 @@ subsequence of the \emph{Fibonacci} series as follows:
5
5
8
8
\end{verbatim}
\end{verbatim}
%
This example introduces several new features.
This example introduces several new features.
\begin{itemize}
\begin{itemize}
...
@@ -819,7 +835,7 @@ like this:
...
@@ -819,7 +835,7 @@ like this:
>>> 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{verbatim}
%
A trailing comma avoids the newline after the output:
A trailing comma avoids the newline after the output:
\begin{verbatim}
\begin{verbatim}
...
@@ -830,7 +846,7 @@ A trailing comma avoids the newline after the output:
...
@@ -830,7 +846,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{verbatim}
%
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.
...
@@ -838,12 +854,14 @@ prompt if the last line was not completed.
...
@@ -838,12 +854,14 @@ prompt if the last line was not completed.
\chapter
{
More Control Flow Tools
}
\chapter
{
More Control Flow Tools
}
\label
{
moreControl
}
Besides the
\keyword
{
while
}
statement just introduced, Python knows
Besides the
\keyword
{
while
}
statement just introduced, Python knows
the usual control flow statements known from other languages, with
the usual control flow statements known from other languages, with
some twists.
some twists.
\section
{
If Statements
}
\section
{
If Statements
}
\label
{
if
}
Perhaps the most well-known statement type is the
\keyword
{
if
}
Perhaps the most well-known statement type is the
\keyword
{
if
}
statement. For example:
statement. For example:
...
@@ -860,7 +878,7 @@ statement. For example:
...
@@ -860,7 +878,7 @@ statement. For example:
... print 'More'
... print 'More'
...
...
\end{verbatim}
\end{verbatim}
%
There can be zero or more
\keyword
{
elif
}
parts, and the
\keyword
{
else
}
There can be zero or more
\keyword
{
elif
}
parts, and the
\keyword
{
else
}
part is optional. The keyword `
\keyword
{
elif
}
' is short for `else
part is optional. The keyword `
\keyword
{
elif
}
' is short for `else
if', and is useful to avoid excessive indentation. An
if', and is useful to avoid excessive indentation. An
...
@@ -872,6 +890,7 @@ if', and is useful to avoid excessive indentation. An
...
@@ -872,6 +890,7 @@ if', and is useful to avoid excessive indentation. An
\emph
{
case
}
statements found in other languages.
\emph
{
case
}
statements found in other languages.
\section
{
For Statements
}
\section
{
For Statements
}
\label
{
for
}
The
\keyword
{
for
}
statement in Python differs a bit from what you may be
The
\keyword
{
for
}
statement in Python differs a bit from what you may be
used to in
\C
{}
or Pascal. Rather than always iterating over an
used to in
\C
{}
or Pascal. Rather than always iterating over an
...
@@ -891,7 +910,7 @@ cat 3
...
@@ -891,7 +910,7 @@ cat 3
window 6
window 6
defenestrate 12
defenestrate 12
\end{verbatim}
\end{verbatim}
%
It is not safe to modify the sequence being iterated over in the loop
It is not safe to modify the sequence being iterated over in the loop
(this can only happen for mutable sequence types, i.e., lists). If
(this can only happen for mutable sequence types, i.e., lists). If
you need to modify the list you are iterating over, e.g., duplicate
you need to modify the list you are iterating over, e.g., duplicate
...
@@ -907,6 +926,7 @@ makes this particularly convenient:
...
@@ -907,6 +926,7 @@ makes this particularly convenient:
\end{verbatim}
\end{verbatim}
\section
{
The
\sectcode
{
range()
}
Function
}
\section
{
The
\sectcode
{
range()
}
Function
}
\label
{
range
}
If you do need to iterate over a sequence of numbers, the built-in
If you do need to iterate over a sequence of numbers, the built-in
function
\function
{
range()
}
comes in handy. It generates lists
function
\function
{
range()
}
comes in handy. It generates lists
...
@@ -916,7 +936,7 @@ containing arithmetic progressions, e.g.:
...
@@ -916,7 +936,7 @@ containing arithmetic progressions, e.g.:
>>> 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{verbatim}
%
The given end point is never part of the generated list;
The given end point is never part of the generated list;
\code
{
range(10)
}
generates a list of 10 values, exactly the legal
\code
{
range(10)
}
generates a list of 10 values, exactly the legal
indices for items of a sequence of length 10. It is possible to let
indices for items of a sequence of length 10. It is possible to let
...
@@ -931,7 +951,7 @@ the range start at another number, or to specify a different increment
...
@@ -931,7 +951,7 @@ the range start at another number, or to specify a different increment
>>> range(-10, -100, -30)
>>> range(-10, -100, -30)
[-10, -40, -70]
[-10, -40, -70]
\end{verbatim}
\end{verbatim}
%
To iterate over the indices of a sequence, combine
\function
{
range()
}
To iterate over the indices of a sequence, combine
\function
{
range()
}
and
\function
{
len()
}
as follows:
and
\function
{
len()
}
as follows:
...
@@ -948,6 +968,7 @@ and \function{len()} as follows:
...
@@ -948,6 +968,7 @@ and \function{len()} as follows:
\end{verbatim}
\end{verbatim}
\section
{
Break and Continue Statements, and Else Clauses on Loops
}
\section
{
Break and Continue Statements, and Else Clauses on Loops
}
\label
{
break
}
The
\keyword
{
break
}
statement, like in
\C
{}
, breaks out of the smallest
The
\keyword
{
break
}
statement, like in
\C
{}
, breaks out of the smallest
enclosing
\keyword
{
for
}
or
\keyword
{
while
}
loop.
enclosing
\keyword
{
for
}
or
\keyword
{
while
}
loop.
...
@@ -982,6 +1003,7 @@ which searches for prime numbers:
...
@@ -982,6 +1003,7 @@ which searches for prime numbers:
\end{verbatim}
\end{verbatim}
\section
{
Pass Statements
}
\section
{
Pass Statements
}
\label
{
pass
}
The
\keyword
{
pass
}
statement does nothing.
The
\keyword
{
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
...
@@ -995,6 +1017,7 @@ For example:
...
@@ -995,6 +1017,7 @@ For example:
\end{verbatim}
\end{verbatim}
\section
{
Defining Functions
}
\section
{
Defining Functions
}
\label
{
functions
}
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:
...
@@ -1011,7 +1034,7 @@ arbitrary boundary:
...
@@ -1011,7 +1034,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{verbatim}
%
The keyword
\keyword
{
def
}
introduces a function
\emph
{
definition
}
. It
The keyword
\keyword
{
def
}
introduces a function
\emph
{
definition
}
. It
must be followed by the function name and the parenthesized list of
must be followed by the function name and the parenthesized list of
formal parameters. The statements that form the body of the function
formal parameters. The statements that form the body of the function
...
@@ -1058,7 +1081,7 @@ mechanism:
...
@@ -1058,7 +1081,7 @@ mechanism:
>>> 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{verbatim}
%
You might object that
\code
{
fib
}
is not a function but a procedure. In
You might object that
\code
{
fib
}
is not a function but a procedure. In
Python, like in
\C
{}
, procedures are just functions that don't return a
Python, like in
\C
{}
, procedures are just functions that don't return a
value. In fact, technically speaking, procedures do return a value,
value. In fact, technically speaking, procedures do return a value,
...
@@ -1071,7 +1094,7 @@ if you really want to:
...
@@ -1071,7 +1094,7 @@ if you really want to:
>>> print fib(0)
>>> print fib(0)
None
None
\end{verbatim}
\end{verbatim}
%
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:
...
@@ -1118,11 +1141,13 @@ efficient.
...
@@ -1118,11 +1141,13 @@ efficient.
\end{itemize}
\end{itemize}
\section
{
More on Defining Functions
}
\section
{
More on Defining Functions
}
\label
{
defining
}
It is also possible to define functions with a variable number of
It is also possible to define functions with a variable number of
arguments. There are three forms, which can be combined.
arguments. There are three forms, which can be combined.
\subsection
{
Default Argument Values
}
\subsection
{
Default Argument Values
}
\label
{
defaultArgs
}
The most useful form is to specify a default value for one or more
The most useful form is to specify a default value for one or more
arguments. This creates a function that can be called with fewer
arguments. This creates a function that can be called with fewer
...
@@ -1156,6 +1181,7 @@ f()
...
@@ -1156,6 +1181,7 @@ f()
will print
\code
{
5
}
.
will print
\code
{
5
}
.
\subsection
{
Keyword Arguments
}
\subsection
{
Keyword Arguments
}
\label
{
keywordArgs
}
Functions can also be called using
Functions can also be called using
keyword arguments of the form
\samp
{
\var
{
keyword
}
=
\var
{
value
}}
. For
keyword arguments of the form
\samp
{
\var
{
keyword
}
=
\var
{
value
}}
. For
...
@@ -1236,6 +1262,7 @@ sketch : Cheese Shop Sketch
...
@@ -1236,6 +1262,7 @@ sketch : Cheese Shop Sketch
\end{verbatim}
\end{verbatim}
\subsection
{
Arbitrary Argument Lists
}
\subsection
{
Arbitrary Argument Lists
}
\label
{
arbitraryArgs
}
Finally, the least frequently used option is to specify that a
Finally, the least frequently used option is to specify that a
function can be called with an arbitrary number of arguments. These
function can be called with an arbitrary number of arguments. These
...
@@ -1248,11 +1275,13 @@ def fprintf(file, format, *args):
...
@@ -1248,11 +1275,13 @@ def fprintf(file, format, *args):
\end{verbatim}
\end{verbatim}
\chapter
{
Data Structures
}
\chapter
{
Data Structures
}
\label
{
structures
}
This chapter describes some things you've learned about already in
This chapter describes some things you've learned about already in
more detail, and adds some new things as well.
more detail, and adds some new things as well.
\section
{
More on Lists
}
\section
{
More on Lists
}
\label
{
moreLists
}
The list data type has some more methods. Here are all of the methods
The list data type has some more methods. Here are all of the methods
of list objects:
of list objects:
...
@@ -1311,6 +1340,7 @@ An example that uses all list methods:
...
@@ -1311,6 +1340,7 @@ An example that uses all list methods:
\end{verbatim}
\end{verbatim}
\subsection
{
Functional Programming Tools
}
\subsection
{
Functional Programming Tools
}
\label
{
functional
}
There are three built-in functions that are very useful when used with
There are three built-in functions that are very useful when used with
lists:
\function
{
filter()
}
,
\function
{
map()
}
, and
\function
{
reduce()
}
.
lists:
\function
{
filter()
}
,
\function
{
map()
}
, and
\function
{
reduce()
}
.
...
@@ -1389,6 +1419,7 @@ item, then to the result and the next item, and so on. For example,
...
@@ -1389,6 +1419,7 @@ item, then to the result and the next item, and so on. For example,
\end{verbatim}
\end{verbatim}
\section
{
The
\sectcode
{
del
}
statement
}
\section
{
The
\sectcode
{
del
}
statement
}
\label
{
del
}
There is a way to remove an item from a list given its index instead
There is a way to remove an item from a list given its index instead
of its value: the
\code
{
del
}
statement. This can also be used to
of its value: the
\code
{
del
}
statement. This can also be used to
...
@@ -1405,18 +1436,19 @@ empty list to the slice). For example:
...
@@ -1405,18 +1436,19 @@ empty list to the slice). For example:
>>> a
>>> a
[1, 66.6, 1234.5]
[1, 66.6, 1234.5]
\end{verbatim}
\end{verbatim}
%
\
code
{
del
}
can also be used to delete entire variables:
\
keyword
{
del
}
can also be used to delete entire variables:
\begin{verbatim}
\begin{verbatim}
>>> del a
>>> del a
\end{verbatim}
\end{verbatim}
%
Referencing the name
\code
{
a
}
hereafter is an error (at least until
Referencing the name
\code
{
a
}
hereafter is an error (at least until
another value is assigned to it). We'll find other uses for
\code
{
del
}
another value is assigned to it). We'll find other uses for
later.
\keyword
{
del
}
later.
\section
{
Tuples and Sequences
}
\section
{
Tuples and Sequences
}
\label
{
tuples
}
We saw that lists and strings have many common properties, e.g.,
We saw that lists and strings have many common properties, e.g.,
indexing and slicing operations. They are two examples of
indexing and slicing operations. They are two examples of
...
@@ -1438,7 +1470,7 @@ instance:
...
@@ -1438,7 +1470,7 @@ instance:
>>> u
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
\end{verbatim}
\end{verbatim}
%
As you see, on output tuples are alway enclosed in parentheses, so
As you see, on output tuples are alway enclosed in parentheses, so
that nested tuples are interpreted correctly; they may be input with
that nested tuples are interpreted correctly; they may be input with
or without surrounding parentheses, although often parentheses are
or without surrounding parentheses, although often parentheses are
...
@@ -1467,7 +1499,7 @@ Ugly, but effective. For example:
...
@@ -1467,7 +1499,7 @@ Ugly, but effective. For example:
>>> singleton
>>> singleton
('hello',)
('hello',)
\end{verbatim}
\end{verbatim}
%
The statement
\code
{
t = 12345, 54321, 'hello!'
}
is an example of
The statement
\code
{
t = 12345, 54321, 'hello!'
}
is an example of
\emph
{
tuple packing
}
: the values
\code
{
12345
}
,
\code
{
54321
}
and
\emph
{
tuple packing
}
: the values
\code
{
12345
}
,
\code
{
54321
}
and
\code
{
'hello!'
}
are packed together in a tuple. The reverse operation
\code
{
'hello!'
}
are packed together in a tuple. The reverse operation
...
@@ -1476,7 +1508,7 @@ is also possible, e.g.:
...
@@ -1476,7 +1508,7 @@ is also possible, e.g.:
\begin{verbatim}
\begin{verbatim}
>>> x, y, z = t
>>> x, y, z = t
\end{verbatim}
\end{verbatim}
%
This is called, appropriately enough,
\emph
{
tuple unpacking
}
. Tuple
This is called, appropriately enough,
\emph
{
tuple unpacking
}
. Tuple
unpacking requires that the list of variables on the left has the same
unpacking requires that the list of variables on the left has the same
number of elements as the length of the tuple. Note that multiple
number of elements as the length of the tuple. Note that multiple
...
@@ -1493,6 +1525,7 @@ square brackets:
...
@@ -1493,6 +1525,7 @@ square brackets:
\end{verbatim}
\end{verbatim}
\section
{
Dictionaries
}
\section
{
Dictionaries
}
\label
{
dictionaries
}
Another useful data type built into Python is the
\emph
{
dictionary
}
.
Another useful data type built into Python is the
\emph
{
dictionary
}
.
Dictionaries are sometimes found in other languages as ``associative
Dictionaries are sometimes found in other languages as ``associative
...
@@ -1545,6 +1578,7 @@ Here is a small example using a dictionary:
...
@@ -1545,6 +1578,7 @@ Here is a small example using a dictionary:
\end{verbatim}
\end{verbatim}
\section
{
More on Conditions
}
\section
{
More on Conditions
}
\label
{
conditions
}
The conditions used in
\code
{
while
}
and
\code
{
if
}
statements above can
The conditions used in
\code
{
while
}
and
\code
{
if
}
statements above can
contain other operators besides comparisons.
contain other operators besides comparisons.
...
@@ -1584,10 +1618,11 @@ expression to a variable. For example,
...
@@ -1584,10 +1618,11 @@ expression to a variable. For example,
>>> non
_
null
>>> non
_
null
'Trondheim'
'Trondheim'
\end{verbatim}
\end{verbatim}
%
Note that in Python, unlike
\C
{}
, assignment cannot occur inside expressions.
Note that in Python, unlike
\C
{}
, assignment cannot occur inside expressions.
\section
{
Comparing Sequences and Other Types
}
\section
{
Comparing Sequences and Other Types
}
\label
{
comparing
}
Sequence objects may be compared to other objects with the same
Sequence objects may be compared to other objects with the same
sequence type. The comparison uses
\emph
{
lexicographical
}
ordering:
sequence type. The comparison uses
\emph
{
lexicographical
}
ordering:
...
@@ -1611,7 +1646,7 @@ examples of comparisons between sequences with the same types:
...
@@ -1611,7 +1646,7 @@ examples of comparisons between sequences with the same types:
(1, 2, 3) = (1.0, 2.0, 3.0)
(1, 2, 3) = (1.0, 2.0, 3.0)
(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
\end{verbatim}
\end{verbatim}
%
Note that comparing objects of different types is legal. The outcome
Note that comparing objects of different types is legal. The outcome
is deterministic but arbitrary: the types are ordered by their name.
is deterministic but arbitrary: the types are ordered by their name.
Thus, a list is always smaller than a string, a string is always
Thus, a list is always smaller than a string, a string is always
...
@@ -1625,6 +1660,7 @@ to their numeric value, so 0 equals 0.0, etc.%
...
@@ -1625,6 +1660,7 @@ to their numeric value, so 0 equals 0.0, etc.%
\chapter
{
Modules
}
\chapter
{
Modules
}
\label
{
modules
}
If you quit from the Python interpreter and enter it again, the
If you quit from the Python interpreter and enter it again, the
definitions you have made (functions and variables) are lost.
definitions you have made (functions and variables) are lost.
...
@@ -1668,14 +1704,14 @@ def fib2(n): # return Fibonacci series up to n
...
@@ -1668,14 +1704,14 @@ def fib2(n): # return Fibonacci series up to n
a, b = b, a+b
a, b = b, a+b
return result
return result
\end{verbatim}
\end{verbatim}
%
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{verbatim}
\begin{verbatim}
>>> import fibo
>>> import fibo
\end{verbatim}
\end{verbatim}
%
This does not enter the names of the functions defined in
This does not enter the names of the functions defined in
\code
{
fibo
}
\code
{
fibo
}
directly in the current symbol table; it only enters the module name
directly in the current symbol table; it only enters the module name
...
@@ -1702,6 +1738,7 @@ If you intend to use a function often you can assign it to a local name:
...
@@ -1702,6 +1738,7 @@ If you intend to use a function often you can assign it to a local name:
\section
{
More on Modules
}
\section
{
More on Modules
}
\label
{
moreModules
}
A module can contain executable statements as well as function
A module can contain executable statements as well as function
definitions.
definitions.
...
@@ -1743,7 +1780,7 @@ For example:
...
@@ -1743,7 +1780,7 @@ For example:
>>> 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{verbatim}
%
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,
\code
{
fibo
}
is not
in the local symbol table (so in the example,
\code
{
fibo
}
is not
defined).
defined).
...
@@ -1755,11 +1792,12 @@ There is even a variant to import all names that a module defines:
...
@@ -1755,11 +1792,12 @@ There is even a variant to import all names that a module defines:
>>> 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{verbatim}
%
This imports all names except those beginning with an underscore
This imports all names except those beginning with an underscore
(
\code
{_}
).
(
\code
{_}
).
\subsection
{
The Module Search Path
}
\subsection
{
The Module Search Path
}
\label
{
searchPath
}
When a module named
\module
{
spam
}
is imported, the interpreter searches
When a module named
\module
{
spam
}
is imported, the interpreter searches
for a file named
\file
{
spam.py
}
in the current directory,
for a file named
\file
{
spam.py
}
in the current directory,
...
@@ -1801,6 +1839,7 @@ modules.)
...
@@ -1801,6 +1839,7 @@ modules.)
% XXX Should optimization with -O be covered here?
% XXX Should optimization with -O be covered here?
\section
{
Standard Modules
}
\section
{
Standard Modules
}
\label
{
standardModules
}
Python comes with a library of standard modules, described in a separate
Python comes with a library of standard modules, described in a separate
document, the
\emph
{
Python Library Reference
}
(``Library Reference''
document, the
\emph
{
Python Library Reference
}
(``Library Reference''
...
@@ -1826,7 +1865,7 @@ C> print 'Yuck!'
...
@@ -1826,7 +1865,7 @@ C> print 'Yuck!'
Yuck!
Yuck!
C>
C>
\end{verbatim}
\end{verbatim}
%
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.
...
@@ -1847,6 +1886,7 @@ You can modify it using standard list operations, e.g.:
...
@@ -1847,6 +1886,7 @@ You can modify it using standard list operations, e.g.:
\end{verbatim}
\end{verbatim}
\section
{
The
\sectcode
{
dir()
}
function
}
\section
{
The
\sectcode
{
dir()
}
function
}
\label
{
dir
}
The built-in function
\function
{
dir()
}
is used to find out which names
The built-in function
\function
{
dir()
}
is used to find out which names
a module defines. It returns a sorted list of strings:
a module defines. It returns a sorted list of strings:
...
@@ -1860,7 +1900,7 @@ a module defines. It returns a sorted list of strings:
...
@@ -1860,7 +1900,7 @@ a module defines. It returns a sorted list of strings:
'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace',
'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace',
'stderr', 'stdin', 'stdout', 'version']
'stderr', 'stdin', 'stdout', 'version']
\end{verbatim}
\end{verbatim}
%
Without arguments,
\function
{
dir()
}
lists the names you have defined
Without arguments,
\function
{
dir()
}
lists the names you have defined
currently:
currently:
...
@@ -1871,7 +1911,7 @@ currently:
...
@@ -1871,7 +1911,7 @@ currently:
>>> dir()
>>> dir()
['
__
name
__
', 'a', 'fib', 'fibo', 'sys']
['
__
name
__
', 'a', 'fib', 'fibo', 'sys']
\end{verbatim}
\end{verbatim}
%
Note that it lists all types of names: variables, modules, functions, etc.
Note that it lists all types of names: variables, modules, functions, etc.
\function
{
dir()
}
does not list the names of built-in functions and
\function
{
dir()
}
does not list the names of built-in functions and
...
@@ -1894,6 +1934,7 @@ standard module \module{__builtin__}:
...
@@ -1894,6 +1934,7 @@ standard module \module{__builtin__}:
\chapter
{
Input and Output
}
\chapter
{
Input and Output
}
\label
{
io
}
There are several ways to present the output of a program; data can be
There are several ways to present the output of a program; data can be
printed in a human-readable form, or written to a file for future use.
printed in a human-readable form, or written to a file for future use.
...
@@ -1943,7 +1984,7 @@ The value of x is 31.4, and y is 40000...
...
@@ -1943,7 +1984,7 @@ The value of x is 31.4, and y is 40000...
... `x, y, ('spam', 'eggs')`
... `x, y, ('spam', 'eggs')`
"(31.4, 40000, ('spam', 'eggs'))"
"(31.4, 40000, ('spam', 'eggs'))"
\end{verbatim}
\end{verbatim}
%
Here are two ways to write a table of squares and cubes:
Here are two ways to write a table of squares and cubes:
\begin{verbatim}
\begin{verbatim}
...
@@ -1977,7 +2018,7 @@ Here are two ways to write a table of squares and cubes:
...
@@ -1977,7 +2018,7 @@ Here are two ways to write a table of squares and cubes:
9 81 729
9 81 729
10 100 1000
10 100 1000
\end{verbatim}
\end{verbatim}
%
(Note that one space between each column was added by the way
(Note that one space between each column was added by the way
\keyword
{
print
}
works: it always adds spaces between its arguments.)
\keyword
{
print
}
works: it always adds spaces between its arguments.)
...
@@ -2050,6 +2091,8 @@ This is particularly useful in combination with the new built-in
...
@@ -2050,6 +2091,8 @@ This is particularly useful in combination with the new built-in
local variables.
local variables.
\section
{
Reading and Writing Files
}
\section
{
Reading and Writing Files
}
\label
{
files
}
% Opening files
% Opening files
\function
{
open()
}
returns a file object, and is most commonly used with
\function
{
open()
}
returns a file object, and is most commonly used with
two arguments:
\samp
{
open(
\var
{
filename
}
,
\var
{
mode
}
)
}
.
two arguments:
\samp
{
open(
\var
{
filename
}
,
\var
{
mode
}
)
}
.
...
@@ -2059,7 +2102,7 @@ two arguments: \samp{open(\var{filename}, \var{mode})}.
...
@@ -2059,7 +2102,7 @@ two arguments: \samp{open(\var{filename}, \var{mode})}.
>>> print f
>>> print f
<open file '/tmp/workfile', mode 'w' at 80a0960>
<open file '/tmp/workfile', mode 'w' at 80a0960>
\end{verbatim}
\end{verbatim}
%
The first argument is a string containing the filename. The second
The first argument is a string containing the filename. The second
argument is another string containing a few characters describing the
argument is another string containing a few characters describing the
way in which the file will be used.
\var
{
mode
}
can be
\code
{
'r'
}
when
way in which the file will be used.
\var
{
mode
}
can be
\code
{
'r'
}
when
...
@@ -2081,6 +2124,7 @@ written. This behind-the-scenes modification to file data is fine for
...
@@ -2081,6 +2124,7 @@ written. This behind-the-scenes modification to file data is fine for
writing such files.
writing such files.
\subsection
{
Methods of file objects
}
\subsection
{
Methods of file objects
}
\label
{
fileMethods
}
The rest of the examples in this section will assume that a file
The rest of the examples in this section will assume that a file
object called
\code
{
f
}
has already been created.
object called
\code
{
f
}
has already been created.
...
@@ -2099,7 +2143,7 @@ string (\code {""}).
...
@@ -2099,7 +2143,7 @@ string (\code {""}).
>>> f.read()
>>> f.read()
''
''
\end{verbatim}
\end{verbatim}
%
\code
{
f.readline()
}
reads a single line from the file; a newline
\code
{
f.readline()
}
reads a single line from the file; a newline
character (
\code
{
\e
n
}
) is left at the end of the string, and is only
character (
\code
{
\e
n
}
) is left at the end of the string, and is only
omitted on the last line of the file if the file doesn't end in a
omitted on the last line of the file if the file doesn't end in a
...
@@ -2116,7 +2160,7 @@ string containing only a single newline.
...
@@ -2116,7 +2160,7 @@ string containing only a single newline.
>>> f.readline()
>>> f.readline()
''
''
\end{verbatim}
\end{verbatim}
%
\code
{
f.readlines()
}
uses
\code
{
f.readline()
}
repeatedly, and returns
\code
{
f.readlines()
}
uses
\code
{
f.readline()
}
repeatedly, and returns
a list containing all the lines of data in the file.
a list containing all the lines of data in the file.
...
@@ -2124,14 +2168,14 @@ a list containing all the lines of data in the file.
...
@@ -2124,14 +2168,14 @@ a list containing all the lines of data in the file.
>>> f.readlines()
>>> f.readlines()
['This is the first line of the file.
\0
12', 'Second line of the file
\0
12']
['This is the first line of the file.
\0
12', 'Second line of the file
\0
12']
\end{verbatim}
\end{verbatim}
%
\code
{
f.write(
\var
{
string
}
)
}
writes the contents of
\var
{
string
}
to
\code
{
f.write(
\var
{
string
}
)
}
writes the contents of
\var
{
string
}
to
the file, returning
\code
{
None
}
.
the file, returning
\code
{
None
}
.
\begin{verbatim}
\begin{verbatim}
>>> f.write('This is a test
\n
')
>>> f.write('This is a test
\n
')
\end{verbatim}
\end{verbatim}
%
\code
{
f.tell()
}
returns an integer giving the file object's current
\code
{
f.tell()
}
returns an integer giving the file object's current
position in the file, measured in bytes from the beginning of the
position in the file, measured in bytes from the beginning of the
file. To change the file object's position, use
file. To change the file object's position, use
...
@@ -2153,7 +2197,7 @@ of the file as the reference point.
...
@@ -2153,7 +2197,7 @@ of the file as the reference point.
>>> f.read(1)
>>> f.read(1)
'd'
'd'
\end{verbatim}
\end{verbatim}
%
When you're done with a file, call
\code
{
f.close()
}
to close it and
When you're done with a file, call
\code
{
f.close()
}
to close it and
free up any system resources taken up by the open file. After calling
free up any system resources taken up by the open file. After calling
\code
{
f.close()
}
, attempts to use the file object will automatically fail.
\code
{
f.close()
}
, attempts to use the file object will automatically fail.
...
@@ -2165,12 +2209,13 @@ Traceback (innermost last):
...
@@ -2165,12 +2209,13 @@ Traceback (innermost last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in ?
ValueError: I/O operation on closed file
ValueError: I/O operation on closed file
\end{verbatim}
\end{verbatim}
%
File objects have some additional methods, such as
\method
{
isatty()
}
File objects have some additional methods, such as
\method
{
isatty()
}
and
\method
{
truncate()
}
which are less frequently used; consult the
and
\method
{
truncate()
}
which are less frequently used; consult the
Library Reference for a complete guide to file objects.
Library Reference for a complete guide to file objects.
\subsection
{
The pickle module
}
\subsection
{
The pickle module
}
\label
{
pickle
}
Strings can easily be written to and read from a file. Numbers take a
Strings can easily be written to and read from a file. Numbers take a
bit more effort, since the
\method
{
read()
}
method only returns
bit more effort, since the
\method
{
read()
}
method only returns
...
@@ -2197,14 +2242,14 @@ one line of code:
...
@@ -2197,14 +2242,14 @@ one line of code:
\begin{verbatim}
\begin{verbatim}
pickle.dump(x, f)
pickle.dump(x, f)
\end{verbatim}
\end{verbatim}
%
To unpickle the object again, if
\code
{
f
}
is a file object which has
To unpickle the object again, if
\code
{
f
}
is a file object which has
been opened for reading:
been opened for reading:
\begin{verbatim}
\begin{verbatim}
x = pickle.load(f)
x = pickle.load(f)
\end{verbatim}
\end{verbatim}
%
(There are other variants of this, used when pickling many objects or
(There are other variants of this, used when pickling many objects or
when you don't want to write the pickled data to a file; consult the
when you don't want to write the pickled data to a file; consult the
complete documentation for
\module
{
pickle
}
in the Library Reference.)
complete documentation for
\module
{
pickle
}
in the Library Reference.)
...
@@ -2220,6 +2265,7 @@ unpickled.
...
@@ -2220,6 +2265,7 @@ unpickled.
\chapter
{
Errors and Exceptions
}
\chapter
{
Errors and Exceptions
}
\label
{
errors
}
Until now error messages haven't been more than mentioned, but if you
Until now error messages haven't been more than mentioned, but if you
have tried out the examples you have probably seen some. There are
have tried out the examples you have probably seen some. There are
...
@@ -2227,6 +2273,7 @@ have tried out the examples you have probably seen some. There are
...
@@ -2227,6 +2273,7 @@ have tried out the examples you have probably seen some. There are
and
\emph
{
exceptions
}
.
and
\emph
{
exceptions
}
.
\section
{
Syntax Errors
}
\section
{
Syntax Errors
}
\label
{
syntaxErrors
}
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:
...
@@ -2238,7 +2285,7 @@ kind of complaint you get while you are still learning Python:
...
@@ -2238,7 +2285,7 @@ kind of complaint you get while you are still learning Python:
^
^
SyntaxError: invalid syntax
SyntaxError: invalid syntax
\end{verbatim}
\end{verbatim}
%
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
...
@@ -2249,6 +2296,7 @@ File name and line number are printed so you know where to look in case
...
@@ -2249,6 +2296,7 @@ File name and line number are printed so you know where to look in case
the input came from a script.
the input came from a script.
\section
{
Exceptions
}
\section
{
Exceptions
}
\label
{
exceptions
}
Even if a statement or expression is syntactically correct, it may
Even if a statement or expression is syntactically correct, it may
cause an error when an attempt is made to execute it.
cause an error when an attempt is made to execute it.
...
@@ -2271,7 +2319,7 @@ Traceback (innermost last):
...
@@ -2271,7 +2319,7 @@ Traceback (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
TypeError: illegal argument type for built-in operation
TypeError: illegal argument type for built-in operation
\end{verbatim}
\end{verbatim}
%
The last line of the error message indicates what happened.
The last line of the error message indicates what happened.
Exceptions come in different types, and the type is printed as part of
Exceptions come in different types, and the type is printed as part of
the message: the types in the example are
the message: the types in the example are
...
@@ -2298,6 +2346,7 @@ The Library Reference lists the built-in exceptions and their
...
@@ -2298,6 +2346,7 @@ The Library Reference lists the built-in exceptions and their
meanings.
meanings.
\section
{
Handling Exceptions
}
\section
{
Handling Exceptions
}
\label
{
handling
}
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
...
@@ -2317,7 +2366,7 @@ some floating point numbers:
...
@@ -2317,7 +2366,7 @@ some floating point numbers:
0 *** has no inverse ***
0 *** has no inverse ***
10 0.1
10 0.1
\end{verbatim}
\end{verbatim}
%
The
\keyword
{
try
}
statement works as follows.
The
\keyword
{
try
}
statement works as follows.
\begin{itemize}
\begin{itemize}
\item
\item
...
@@ -2352,7 +2401,7 @@ e.g.:
...
@@ -2352,7 +2401,7 @@ e.g.:
... except (RuntimeError, TypeError, NameError):
... except (RuntimeError, TypeError, NameError):
... pass
... pass
\end{verbatim}
\end{verbatim}
%
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, since it is easy to mask a real
Use this with extreme caution, since it is easy to mask a real
...
@@ -2390,7 +2439,7 @@ argument's value, as follows:
...
@@ -2390,7 +2439,7 @@ argument's value, as follows:
...
...
name spam undefined
name spam undefined
\end{verbatim}
\end{verbatim}
%
If an exception has an argument, it is printed as the last part
If an exception has an argument, it is printed as the last part
(`detail') of the message for unhandled exceptions.
(`detail') of the message for unhandled exceptions.
...
@@ -2410,9 +2459,10 @@ For example:
...
@@ -2410,9 +2459,10 @@ For example:
...
...
Handling run-time error: integer division or modulo
Handling run-time error: integer division or modulo
\end{verbatim}
\end{verbatim}
%
\section
{
Raising Exceptions
}
\section
{
Raising Exceptions
}
\label
{
raising
}
The
\keyword
{
raise
}
statement allows the programmer to force a
The
\keyword
{
raise
}
statement allows the programmer to force a
specified exception to occur.
specified exception to occur.
...
@@ -2424,14 +2474,14 @@ Traceback (innermost last):
...
@@ -2424,14 +2474,14 @@ Traceback (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
NameError: HiThere
NameError: HiThere
\end{verbatim}
\end{verbatim}
%
The first argument to
\keyword
{
raise
}
names the exception to be
The first argument to
\keyword
{
raise
}
names the exception to be
raised. The optional second argument specifies the exception's
raised. The optional second argument specifies the exception's
argument.
argument.
%
\section
{
User-defined Exceptions
}
\section
{
User-defined Exceptions
}
\label
{
userExceptions
}
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.
...
@@ -2450,13 +2500,13 @@ Traceback (innermost last):
...
@@ -2450,13 +2500,13 @@ Traceback (innermost last):
File "<stdin>", line 1
File "<stdin>", line 1
my
_
exc: 1
my
_
exc: 1
\end{verbatim}
\end{verbatim}
%
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.
%
\section
{
Defining Clean-up Actions
}
\section
{
Defining Clean-up Actions
}
\label
{
cleanup
}
The
\keyword
{
try
}
statement has another optional clause which is
The
\keyword
{
try
}
statement has another optional clause which is
intended to define clean-up actions that must be executed under all
intended to define clean-up actions that must be executed under all
...
@@ -2473,7 +2523,7 @@ Traceback (innermost last):
...
@@ -2473,7 +2523,7 @@ Traceback (innermost last):
File "<stdin>", line 2
File "<stdin>", line 2
KeyboardInterrupt
KeyboardInterrupt
\end{verbatim}
\end{verbatim}
%
A
\emph
{
finally clause
}
is executed whether or not an exception has
A
\emph
{
finally clause
}
is executed whether or not an exception has
occurred in the try clause. When an exception has occurred, it is
occurred in the try clause. When an exception has occurred, it is
re-raised after the finally clause is executed. The finally clause is
re-raised after the finally clause is executed. The finally clause is
...
@@ -2484,6 +2534,7 @@ A \keyword{try} statement must either have one or more except clauses
...
@@ -2484,6 +2534,7 @@ A \keyword{try} statement must either have one or more except clauses
or one finally clause, but not both.
or one finally clause, but not both.
\chapter
{
Classes
}
\chapter
{
Classes
}
\label
{
classes
}
Python's class mechanism adds classes to the language with a minimum
Python's class mechanism adds classes to the language with a minimum
of new syntax and semantics. It is a mixture of the class mechanisms
of new syntax and semantics. It is a mixture of the class mechanisms
...
@@ -2511,6 +2562,7 @@ built-in operators with special syntax (arithmetic operators,
...
@@ -2511,6 +2562,7 @@ built-in operators with special syntax (arithmetic operators,
subscripting etc.) can be redefined for class members.
subscripting etc.) can be redefined for class members.
\section
{
A word about terminology
}
\section
{
A word about terminology
}
\label
{
terminology
}
Lacking universally accepted terminology to talk about classes, I'll
Lacking universally accepted terminology to talk about classes, I'll
make occasional use of Smalltalk and
\Cpp
{}
terms. (I'd use Modula-3
make occasional use of Smalltalk and
\Cpp
{}
terms. (I'd use Modula-3
...
@@ -2543,6 +2595,7 @@ Pascal.
...
@@ -2543,6 +2595,7 @@ Pascal.
\section
{
Python scopes and name spaces
}
\section
{
Python scopes and name spaces
}
\label
{
scopes
}
Before introducing classes, I first have to tell you something about
Before introducing classes, I first have to tell you something about
Python's scope rules. Class definitions play some neat tricks with
Python's scope rules. Class definitions play some neat tricks with
...
@@ -2646,12 +2699,14 @@ variables live in the global scope.)
...
@@ -2646,12 +2699,14 @@ variables live in the global scope.)
\section
{
A first look at classes
}
\section
{
A first look at classes
}
\label
{
firstClasses
}
Classes introduce a little bit of new syntax, three new object types,
Classes introduce a little bit of new syntax, three new object types,
and some new semantics.
and some new semantics.
\subsection
{
Class definition syntax
}
\subsection
{
Class definition syntax
}
\label
{
classDefinition
}
The simplest form of class definition looks like this:
The simplest form of class definition looks like this:
...
@@ -2691,6 +2746,7 @@ the class definition header (\code{ClassName} in the example).
...
@@ -2691,6 +2746,7 @@ the class definition header (\code{ClassName} in the example).
\subsection
{
Class objects
}
\subsection
{
Class objects
}
\label
{
classObjects
}
Class objects support two kinds of operations: attribute references
Class objects support two kinds of operations: attribute references
and instantiation.
and instantiation.
...
@@ -2729,6 +2785,7 @@ the local variable \code{x}.
...
@@ -2729,6 +2785,7 @@ the local variable \code{x}.
\subsection
{
Instance objects
}
\subsection
{
Instance objects
}
\label
{
instanceObjects
}
Now what can we do with instance objects? The only operations
Now what can we do with instance objects? The only operations
understood by instance objects are attribute references. There are
understood by instance objects are attribute references. There are
...
@@ -2769,6 +2826,7 @@ object.
...
@@ -2769,6 +2826,7 @@ object.
\subsection
{
Method objects
}
\subsection
{
Method objects
}
\label
{
methodObjects
}
Usually, a method is called immediately, e.g.:
Usually, a method is called immediately, e.g.:
...
@@ -2817,7 +2875,7 @@ list, and the function object is called with this new argument list.
...
@@ -2817,7 +2875,7 @@ list, and the function object is called with this new argument list.
\section
{
Random remarks
}
\section
{
Random remarks
}
\label
{
remarks
}
[These should perhaps be placed more carefully...]
[These should perhaps be placed more carefully...]
...
@@ -2951,6 +3009,7 @@ reasons why a method would want to reference its own class!
...
@@ -2951,6 +3009,7 @@ reasons why a method would want to reference its own class!
\section
{
Inheritance
}
\section
{
Inheritance
}
\label
{
inheritance
}
Of course, a language feature would not be worthy of the name ``class''
Of course, a language feature would not be worthy of the name ``class''
without supporting inheritance. The syntax for a derived class
without supporting inheritance. The syntax for a derived class
...
@@ -3003,6 +3062,7 @@ the base class is defined or imported directly in the global scope.)
...
@@ -3003,6 +3062,7 @@ the base class is defined or imported directly in the global scope.)
\subsection
{
Multiple inheritance
}
\subsection
{
Multiple inheritance
}
\label
{
multiple
}
Python supports a limited form of multiple inheritance as well. A
Python supports a limited form of multiple inheritance as well. A
class definition with multiple base classes looks as follows:
class definition with multiple base classes looks as follows:
...
@@ -3043,6 +3103,7 @@ not clear that these semantics are in any way useful.
...
@@ -3043,6 +3103,7 @@ not clear that these semantics are in any way useful.
\section
{
Private variables through name mangling
}
\section
{
Private variables through name mangling
}
\label
{
private
}
There is now limited support for class-private
There is now limited support for class-private
identifiers. Any identifier of the form
\code
{__
spam
}
(at least two
identifiers. Any identifier of the form
\code
{__
spam
}
(at least two
...
@@ -3112,6 +3173,7 @@ class VirtualAttributes:
...
@@ -3112,6 +3173,7 @@ class VirtualAttributes:
%be removed without providing a better solution and a migration path.
%be removed without providing a better solution and a migration path.
\section
{
Odds and ends
}
\section
{
Odds and ends
}
\label
{
odds
}
Sometimes it is useful to have a data type similar to the Pascal
Sometimes it is useful to have a data type similar to the Pascal
``record'' or
\C
{}
``struct'', bundling together a couple of named data
``record'' or
\C
{}
``struct'', bundling together a couple of named data
...
@@ -3148,6 +3210,7 @@ object of which the method is an instance, and \code{m.im_func} is the
...
@@ -3148,6 +3210,7 @@ object of which the method is an instance, and \code{m.im_func} is the
function object corresponding to the method.
function object corresponding to the method.
\subsection
{
Exceptions Can Be Classes
}
\subsection
{
Exceptions Can Be Classes
}
\label
{
exceptionClasses
}
User-defined exceptions are no longer limited to being string objects
User-defined exceptions are no longer limited to being string objects
--- they can be identified by classes as well. Using this mechanism it
--- they can be identified by classes as well. Using this mechanism it
...
@@ -3206,6 +3269,7 @@ finally the instance converted to a string using the built-in function
...
@@ -3206,6 +3269,7 @@ finally the instance converted to a string using the built-in function
In this release, the built-in exceptions are still strings.
In this release, the built-in exceptions are still strings.
\chapter
{
What Now?
}
\chapter
{
What Now?
}
\label
{
whatNow
}
Hopefully reading this tutorial has reinforced your interest in using
Hopefully reading this tutorial has reinforced your interest in using
Python. Now what should you do?
Python. Now what should you do?
...
@@ -3254,6 +3318,7 @@ information on how to join.
...
@@ -3254,6 +3318,7 @@ information on how to join.
% found or it elsewhere in the Tutorial?
% found or it elsewhere in the Tutorial?
\section
{
Lambda Forms
}
\section
{
Lambda Forms
}
\label
{
lambda
}
% XXX Where to put this? Or just leave it out?
% XXX Where to put this? Or just leave it out?
...
@@ -3274,6 +3339,7 @@ def make_incrementor(n):
...
@@ -3274,6 +3339,7 @@ def make_incrementor(n):
\end{verbatim}
\end{verbatim}
\section
{
Documentation Strings
}
\section
{
Documentation Strings
}
\label
{
docstrings
}
% XXX Where to put this? Or just leave it out?
% XXX Where to put this? Or just leave it out?
...
@@ -3310,6 +3376,7 @@ tested after expansion of tabs (to 8 spaces, normally).
...
@@ -3310,6 +3376,7 @@ tested after expansion of tabs (to 8 spaces, normally).
\appendix\chapter
{
Interactive Input Editing and History Substitution
}
\appendix\chapter
{
Interactive Input Editing and History Substitution
}
\label
{
interacting
}
Some versions of the Python interpreter support editing of the current
Some versions of the Python interpreter support editing of the current
input line and history substitution, similar to facilities found in
input line and history substitution, similar to facilities found in
...
@@ -3319,6 +3386,7 @@ editing. This library has its own documentation which I won't
...
@@ -3319,6 +3386,7 @@ editing. This library has its own documentation which I won't
duplicate here; however, the basics are easily explained.
duplicate here; however, the basics are easily explained.
\section
{
Line Editing
}
\section
{
Line Editing
}
\label
{
lineEditing
}
If supported, input line editing is active whenever the interpreter
If supported, input line editing is active whenever the interpreter
prints a primary or secondary prompt. The current line can be edited
prints a primary or secondary prompt. The current line can be edited
...
@@ -3332,6 +3400,7 @@ string. C-underscore undoes the last change you made; it can be
...
@@ -3332,6 +3400,7 @@ string. C-underscore undoes the last change you made; it can be
repeated for cumulative effect.
repeated for cumulative effect.
\section
{
History Substitution
}
\section
{
History Substitution
}
\label
{
history
}
History substitution works as follows. All non-empty input lines
History substitution works as follows. All non-empty input lines
issued are saved in a history buffer, and when a new prompt is given
issued are saved in a history buffer, and when a new prompt is given
...
@@ -3343,6 +3412,7 @@ key passes the current line to the interpreter. C-R starts an
...
@@ -3343,6 +3412,7 @@ key passes the current line to the interpreter. C-R starts an
incremental reverse search; C-S starts a forward search.
incremental reverse search; C-S starts a forward search.
\section
{
Key Bindings
}
\section
{
Key Bindings
}
\label
{
keyBindings
}
The key bindings and some other parameters of the Readline library can
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
...
@@ -3389,6 +3459,7 @@ in your \file{\$HOME/.inputrc}. (Of course, this makes it hard to type
...
@@ -3389,6 +3459,7 @@ in your \file{\$HOME/.inputrc}. (Of course, this makes it hard to type
indented continuation lines...)
indented continuation lines...)
\section
{
Commentary
}
\section
{
Commentary
}
\label
{
commentary
}
This facility is an enormous step forward compared to previous
This facility is an enormous step forward compared to previous
versions of the interpreter; however, some wishes are left: It would
versions of the interpreter; however, some wishes are left: It would
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment