@@ -238,7 +238,7 @@ \subsection{Interactive Mode \label{interactive}}
238238When commands are read from a tty, the interpreter is said to be in
239239\emph {interactive mode }. In this mode it prompts for the next command
240240with the \emph {primary prompt }, usually three greater-than signs
241- (\samp {>> >~}); for continuation lines it prompts with the
241+ (\samp {>\code {>} >~}); for continuation lines it prompts with the
242242\emph {secondary prompt }, by default three dots (\samp {...~}).
243243The interpreter prints a welcome message stating its version number
244244and a copyright notice before printing the first prompt, e.g.:
@@ -340,7 +340,7 @@ \subsection{The Interactive Startup File \label{startup}}
340340\chapter {An Informal Introduction to Python \label {informal } }
341341
342342In the following examples, input and output are distinguished by the
343- presence or absence of prompts (\samp {>> >~} and \samp {...~}): to repeat
343+ presence or absence of prompts (\samp {>\code {>} >~} and \samp {...~}): to repeat
344344the example, you must type everything after the prompt, when the
345345prompt appears; lines that do not begin with a prompt are output from
346346the interpreter. %
@@ -372,7 +372,7 @@ \chapter{An Informal Introduction to Python \label{informal}}
372372\section {Using Python as a Calculator \label {calculator } }
373373
374374Let's try some simple Python commands. Start the interpreter and wait
375- for the primary prompt, \samp {>>> }. (It shouldn't take long.)
375+ for the primary prompt, \samp {>\code {>}>~ }. (It shouldn't take long.)
376376
377377\subsection {Numbers \label {numbers } }
378378
@@ -420,7 +420,7 @@ \subsection{Numbers \label{numbers}}
420420>>> z
4214210
422422\end {verbatim }
423- %
423+
424424There is full support for floating point; operators with mixed type
425425operands convert the integer operand to floating point:
426426
@@ -430,7 +430,7 @@ \subsection{Numbers \label{numbers}}
430430>>> 7.0 / 2
4314313.5
432432\end {verbatim }
433- %
433+
434434Complex numbers are also supported; imaginary numbers are written with
435435a suffix of \samp {j} or \samp {J}. Complex numbers with a nonzero
436436real component are written as \samp {(\var {real}+\var {imag}j)}, or can
@@ -448,7 +448,7 @@ \subsection{Numbers \label{numbers}}
448448>>> (1+2j)/(1+1j)
449449(1.5+0.5j)
450450\end {verbatim }
451- %
451+
452452Complex numbers are always represented as two floating point numbers,
453453the real and imaginary part. To extract these parts from a complex
454454number \var {z}, use \code {\var {z}.real} and \code {\var {z}.imag}.
@@ -460,7 +460,7 @@ \subsection{Numbers \label{numbers}}
460460>>> a.imag
4614610.5
462462\end {verbatim }
463- %
463+
464464The conversion functions to floating point and integer
465465(\function {float()}, \function {int()} and \function {long()}) don't
466466work for complex numbers --- there is no one correct way to convert a
@@ -478,7 +478,7 @@ \subsection{Numbers \label{numbers}}
478478>>> abs(a)
4794791.58113883008
480480\end {verbatim }
481- %
481+
482482In interactive mode, the last printed expression is assigned to the
483483variable \code {_}. This means that when you are using Python as a
484484desk calculator, it is somewhat easier to continue calculations, for
@@ -749,9 +749,9 @@ \subsection{Unicode Strings \label{unicodeStrings}}
749749were only 256 possible ordinals for script characters and texts were
750750typically bound to a code page which mapped the ordinals to script
751751characters. This lead to very much confusion especially with respect
752- to internalization (usually written as \samp {i18n} --- \character {i} +
753- 18 characters + \character {n}) of software. Unicode solves these
754- problems by defining one code page for all scripts.
752+ to internationalization (usually written as \samp {i18n} ---
753+ \character {i} + 18 characters + \character {n}) of software. Unicode
754+ solves these problems by defining one code page for all scripts.
755755
756756Creating Unicode strings in Python is just as simple as creating
757757normal strings:
@@ -1167,6 +1167,7 @@ \section{\keyword{break} and \keyword{continue} Statements, and
116711679 equals 3 * 3
11681168\end {verbatim }
11691169
1170+
11701171\section {\keyword {pass} Statements \label {pass } }
11711172
11721173The \keyword {pass} statement does nothing.
@@ -1180,6 +1181,7 @@ \section{\keyword{pass} Statements \label{pass}}
11801181...
11811182\end {verbatim }
11821183
1184+
11831185\section {Defining Functions \label {functions } }
11841186
11851187We can create a function that writes the Fibonacci series to an
@@ -1277,7 +1279,7 @@ \section{Defining Functions \label{functions}}
12771279>>> f100 # write the result
12781280[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
12791281\end {verbatim }
1280- %
1282+
12811283This example, as usual, demonstrates some new Python features:
12821284
12831285\begin {itemize }
@@ -1467,6 +1469,7 @@ \subsection{Keyword Arguments \label{keywordArgs}}
14671469sketch : Cheese Shop Sketch
14681470\end {verbatim }
14691471
1472+
14701473\subsection {Arbitrary Argument Lists \label {arbitraryArgs } }
14711474
14721475Finally, the least frequently used option is to specify that a
@@ -1753,6 +1756,7 @@ \subsection{Functional Programming Tools \label{functional}}
175317560
17541757\end {verbatim }
17551758
1759+
17561760\subsection {List Comprehensions }
17571761
17581762List comprehensions provide a concise way to create lists without resorting
@@ -1795,6 +1799,7 @@ \subsection{List Comprehensions}
17951799[6, 5, -7, 8, 7, -5, 10, 9, -3]
17961800\end {verbatim }
17971801
1802+
17981803\section {The \keyword {del} statement \label {del } }
17991804
18001805There is a way to remove an item from a list given its index instead
@@ -1823,6 +1828,7 @@ \section{The \keyword{del} statement \label{del}}
18231828another value is assigned to it). We'll find other uses for
18241829\keyword {del} later.
18251830
1831+
18261832\section {Tuples and Sequences \label {tuples } }
18271833
18281834We saw that lists and strings have many common properties, e.g.,
@@ -1855,7 +1861,8 @@ \section{Tuples and Sequences \label{tuples}}
18551861from a database, etc. Tuples, like strings, are immutable: it is not
18561862possible to assign to the individual items of a tuple (you can
18571863simulate much of the same effect with slicing and concatenation,
1858- though).
1864+ though). It is also possible to create tuples which contain mutable
1865+ objects, such as lists.
18591866
18601867A special problem is the construction of tuples containing 0 or 1
18611868items: the syntax has some extra quirks to accommodate these. Empty
@@ -1884,24 +1891,17 @@ \section{Tuples and Sequences \label{tuples}}
18841891>>> x, y, z = t
18851892\end {verbatim }
18861893
1887- This is called, appropriately enough, \emph {tuple unpacking }. Tuple
1888- unpacking requires that the list of variables on the left have the same
1889- number of elements as the length of the tuple . Note that multiple
1890- assignment is really just a combination of tuple packing and tuple
1891- unpacking!
1894+ This is called, appropriately enough, \emph {sequence unpacking }.
1895+ Sequence unpacking requires that the list of variables on the left
1896+ have the same number of elements as the length of the sequence . Note
1897+ that multiple assignment is really just a combination of tuple packing
1898+ and sequence unpacking!
18921899
1893- % XXX This is no longer necessary!
1894- Occasionally, the corresponding operation on lists is useful: \emph {list
1895- unpacking }. This is supported by enclosing the list of variables in
1896- square brackets:
1897-
1898- \begin {verbatim }
1899- >>> a = ['spam', 'eggs', 100, 1234]
1900- >>> [a1, a2, a3, a4] = a
1901- \end {verbatim }
1900+ There is a small bit of asymmetry here: packing multiple values
1901+ always creates a tuple, and unpacking works for any sequence.
19021902
19031903% XXX Add a bit on the difference between tuples and lists.
1904- % XXX Also explain that a tuple can *contain* a mutable object!
1904+
19051905
19061906\section {Dictionaries \label {dictionaries } }
19071907
@@ -1911,11 +1911,14 @@ \section{Dictionaries \label{dictionaries}}
19111911indexed by a range of numbers, dictionaries are indexed by \emph {keys },
19121912which can be any immutable type; strings and numbers can always be
19131913keys. Tuples can be used as keys if they contain only strings,
1914- numbers, or tuples. You can't use lists as keys, since lists can be
1915- modified in place using their \code {append()} method.
1914+ numbers, or tuples; if a tuple contains any mutable object either
1915+ directly or indirectly, it cannot be used as a key. You can't use
1916+ lists as keys, since lists can be modified in place using their
1917+ \method {append()} and \method {extend()} methods, as well as slice and
1918+ indexed assignments.
19161919
19171920It is best to think of a dictionary as an unordered set of
1918- \emph {key:value } pairs, with the requirement that the keys are unique
1921+ \emph {key: value } pairs, with the requirement that the keys are unique
19191922(within one dictionary).
19201923A pair of braces creates an empty dictionary: \code {\{\} }.
19211924Placing a comma-separated list of key:value pairs within the
@@ -2001,6 +2004,7 @@ \section{More on Conditions \label{conditions}}
20012004problems encountered in C programs: typing \code {=} in an expression when
20022005\code {==} was intended.
20032006
2007+
20042008\section {Comparing Sequences and Other Types \label {comparing } }
20052009
20062010Sequence objects may be compared to other objects with the same
@@ -2102,7 +2106,7 @@ \chapter{Modules \label{modules}}
21022106>>> fibo.__name__
21032107'fibo'
21042108\end {verbatim }
2105- %
2109+
21062110If you intend to use a function often you can assign it to a local name:
21072111
21082112\begin {verbatim }
@@ -2164,9 +2168,8 @@ \section{More on Modules \label{moreModules}}
21642168This imports all names except those beginning with an underscore
21652169(\code {_}).
21662170
2167- \subsection {The Module Search Path \label {searchPath } }
21682171
2169- % XXX Need to document that a lone .pyc/.pyo is acceptable too!
2172+ \subsection { The Module Search Path \label { searchPath } }
21702173
21712174\indexiii {module}{search}{path}
21722175When a module named \module {spam} is imported, the interpreter searches
@@ -2238,13 +2241,14 @@ \subsection{``Compiled'' Python files}
22382241bytecode for the script is never written to a \file {.pyc} or
22392242\file {.pyo} file. Thus, the startup time of a script may be reduced
22402243by moving most of its code to a module and having a small bootstrap
2241- script that imports that module.
2244+ script that imports that module. It is also possible to name a
2245+ \file {.pyc} or \file {.pyo} file directly on the command line.
22422246
22432247\item
22442248It is possible to have a file called \file {spam.pyc} (or
2245- \file {spam.pyo} when \programopt {-O} is used) without a module
2246- \file {spam.py} in the same module. This can be used to distribute
2247- a library of Python code in a form that is moderately hard to reverse
2249+ \file {spam.pyo} when \programopt {-O} is used) without a file
2250+ \file {spam.py} for the same module. This can be used to distribute a
2251+ library of Python code in a form that is moderately hard to reverse
22482252engineer.
22492253
22502254\item
@@ -2343,6 +2347,7 @@ \section{The \function{dir()} Function \label{dir}}
23432347'reduce', 'reload', 'repr', 'round', 'setattr', 'str', 'type', 'xrange']
23442348\end {verbatim }
23452349
2350+
23462351\section {Packages \label {packages } }
23472352
23482353Packages are a way of structuring Python's module namespace
@@ -2392,6 +2397,7 @@ \section{Packages \label{packages}}
23922397 karaoke.py
23932398 ...
23942399\end {verbatim }
2400+
23952401The \file {__init__.py} files are required to make Python treat the
23962402directories as containing packages; this is done to prevent
23972403directories with a common name, such as \samp {string}, from
@@ -2406,17 +2412,20 @@ \section{Packages \label{packages}}
24062412\begin {verbatim }
24072413import Sound.Effects.echo
24082414\end {verbatim }
2415+
24092416This loads the submodule \module {Sound.Effects.echo}. It must be referenced
24102417with its full name, e.g.
24112418
24122419\begin {verbatim }
24132420Sound.Effects.echo.echofilter(input, output, delay=0.7, atten=4)
24142421\end {verbatim }
2422+
24152423An alternative way of importing the submodule is:
24162424
24172425\begin {verbatim }
24182426from Sound.Effects import echo
24192427\end {verbatim }
2428+
24202429This also loads the submodule \module {echo}, and makes it available without
24212430its package prefix, so it can be used as follows:
24222431
@@ -2500,7 +2509,6 @@ \subsection{Importing * From a Package \label{pkg-import-star}}
25002509from Sound.Effects import *
25012510\end {verbatim }
25022511
2503-
25042512In this example, the echo and surround modules are imported in the
25052513current namespace because they are defined in the
25062514\module {Sound.Effects} package when the \code {from...import} statement
@@ -2664,7 +2672,7 @@ \section{Fancier Output Formatting \label{formatting}}
26642672>>> string.zfill('3.14159265359', 5)
26652673'3.14159265359'
26662674\end {verbatim }
2667- %
2675+
26682676Using the \code {\% } operator looks like this:
26692677
26702678\begin {verbatim }
@@ -3630,7 +3638,6 @@ \section{Random Remarks \label{remarks}}
36303638 self.add(x)
36313639\end {verbatim }
36323640
3633-
36343641Methods may reference global names in the same way as ordinary
36353642functions. The global scope associated with a method is the module
36363643containing the class definition. (The class itself is never used as a
@@ -3790,20 +3797,6 @@ \section{Private Variables \label{private}}
37903797 self.__vdict[name] = value
37913798\end {verbatim }
37923799
3793- % \emph{Warning: this is an experimental feature.} To avoid all
3794- % potential problems, refrain from using identifiers starting with
3795- % double underscore except for predefined uses like \samp{__init__}. To
3796- % use private names while maintaining future compatibility: refrain from
3797- % using the same private name in classes related via subclassing; avoid
3798- % explicit (manual) mangling/unmangling; and assume that at some point
3799- % in the future, leading double underscore will revert to being just a
3800- % naming convention. Discussion on extensive compile-time declarations
3801- % are currently underway, and it is impossible to predict what solution
3802- % will eventually be chosen for private names. Double leading
3803- % underscore is still a candidate, of course --- just not the only one.
3804- % It is placed in the distribution in the belief that it is useful, and
3805- % so that widespread experience with its use can be gained. It will not
3806- % be removed without providing a better solution and a migration path.
38073800
38083801\section {Odds and Ends \label {odds } }
38093802
@@ -3823,7 +3816,6 @@ \section{Odds and Ends \label{odds}}
38233816john.salary = 1000
38243817\end {verbatim }
38253818
3826-
38273819A piece of Python code that expects a particular abstract data type
38283820can often be passed a class that emulates the methods of that data
38293821type instead. For instance, if you have a function that formats some
0 commit comments