Skip to content

Commit ad13af4

Browse files
committed
Version 2.4
1 parent 3b8cc88 commit ad13af4

2 files changed

Lines changed: 65 additions & 76 deletions

File tree

book/book.tex

Lines changed: 43 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
\title{Think Python}
3333
\author{Allen B. Downey}
3434
\newcommand{\thetitle}{Think Python: How to Think Like a Computer Scientist}
35-
\newcommand{\theversion}{2nd Edition, Version 2.2.23}
35+
\newcommand{\theversion}{2nd Edition, Version 2.4.0}
3636
\newcommand{\thedate}{}
3737

3838
% these styles get translated in CSS for the HTML version
@@ -399,7 +399,7 @@ \section*{Contributor List}
399399
and an improvement in style in Chapter 1, and he initiated discussion
400400
on the technical aspects of interpreters.
401401

402-
\item Benoit Girard sent in a
402+
\item Beno\^{i}t Girard sent in a
403403
correction to a humorous mistake in Section 5.6.
404404

405405
\item Courtney Gleason and Katherine Smith wrote {\tt horsebet.py},
@@ -538,7 +538,7 @@ \section*{Contributor List}
538538
\item Rob Black sent in a passel of corrections, including some
539539
changes for Python 2.2.
540540

541-
\item Jean-Philippe Rey at Ecole Centrale
541+
\item Jean-Philippe Rey at \'{E}cole Centrale
542542
Paris sent a number of patches, including some updates for Python 2.2
543543
and other thoughtful improvements.
544544

@@ -720,6 +720,8 @@ \section*{Contributor List}
720720

721721
\item Per Starb{\"a}ck brought me up to date on universal newlines in Python 3.
722722

723+
\item Laurent Rosenfeld and Michaela R. (aka Mishulnya) translated this book into French. Along the way, they sent many corrections and suggestions.
724+
723725
% ENDCONTRIB
724726

725727
In addition, people who spotted typos or made corrections include
@@ -1098,8 +1100,8 @@ \section{Formal and natural languages}
10981100
\index{structure}
10991101

11001102
The second type of syntax rule pertains to the way tokens are
1101-
combined. The equation $3 += 3$ is illegal because even though $+$
1102-
and $=$ are legal tokens, you can't have one right after the other.
1103+
combined. The equation $3 +/ 3$ is illegal because even though $+$
1104+
and $/$ are legal tokens, you can't have one right after the other.
11031105
Similarly, in a chemical formula the subscript comes after the element
11041106
name, not before.
11051107

@@ -1333,8 +1335,8 @@ \section{Exercises}
13331335
{\tt -2}. What happens if you put a plus sign before a number?
13341336
What about {\tt 2++2}?
13351337

1336-
\item In math notation, leading zeros are ok, as in {\tt 02}.
1337-
What happens if you try this in Python?
1338+
\item In math notation, leading zeros are ok, as in {\tt 09}.
1339+
What happens if you try this in Python? What about {\tt 011}?
13381340

13391341
\item What happens if you have two values with no operator
13401342
between them?
@@ -1550,38 +1552,18 @@ \section{Script mode}
15501552
marathon is about 42 kilometers.
15511553

15521554
But if you type the same code into a script and run it, you get no
1553-
output at all. In script mode an expression, all by itself, has no
1554-
visible effect. Python actually evaluates the expression, but it doesn't
1555-
display the value unless you tell it to:
1555+
output at all.
1556+
In script mode an expression, all by itself, has no
1557+
visible effect. Python evaluates the expression, but it doesn't
1558+
display the result.
1559+
To display the result, you need a {\tt print} statement like this:
15561560

15571561
\begin{verbatim}
15581562
miles = 26.2
15591563
print(miles * 1.61)
15601564
\end{verbatim}
15611565

15621566
This behavior can be confusing at first.
1563-
1564-
A script usually contains a sequence of statements. If there
1565-
is more than one statement, the results appear one at a time
1566-
as the statements execute.
1567-
1568-
For example, the script
1569-
1570-
\begin{verbatim}
1571-
print(1)
1572-
x = 2
1573-
print(x)
1574-
\end{verbatim}
1575-
%
1576-
produces the output
1577-
1578-
\begin{verbatim}
1579-
1
1580-
2
1581-
\end{verbatim}
1582-
%
1583-
The assignment statement produces no output.
1584-
15851567
To check your understanding, type the following statements in the
15861568
Python interpreter and see what they do:
15871569

@@ -1596,7 +1578,6 @@ \section{Script mode}
15961578
expression into a print statement and then run it again.
15971579

15981580

1599-
16001581
\section{Order of operations}
16011582
\index{order of operations}
16021583
\index{PEMDAS}
@@ -2028,8 +2009,7 @@ \section{Math functions}
20282009
\index{trigonometric function}
20292010
\index{function, trigonometric}
20302011

2031-
The second example finds the sine of {\tt radians}. The name of the
2032-
variable is a hint that {\tt sin} and the other trigonometric
2012+
The second example finds the sine of {\tt radians}. The variable name {\tt radians} is a hint that {\tt sin} and the other trigonometric
20332013
functions ({\tt cos}, {\tt tan}, etc.) take arguments in radians. To
20342014
convert from degrees to radians, divide by 180 and multiply by
20352015
$\pi$:
@@ -4579,7 +4559,7 @@ \section{Return values}
45794559
\index{abs function}
45804560
\index{function!abs}
45814561

4582-
As an exercise, write a {\tt compare} function
4562+
As an exercise, write a {\tt compare} function that
45834563
takes two values, {\tt x} and {\tt y}, and returns {\tt 1} if {\tt x > y},
45844564
{\tt 0} if {\tt x == y}, and {\tt -1} if {\tt x < y}.
45854565
\index{compare function}
@@ -5122,8 +5102,7 @@ \section{Checking types}
51225102
None
51235103
\end{verbatim}
51245104
%
5125-
If we get past both checks, we know that $n$ is positive or
5126-
zero, so we can prove that the recursion terminates.
5105+
If we get past both checks, we know that $n$ is a non-negative integer, so we can prove that the recursion terminates.
51275106
\index{guardian pattern}
51285107
\index{pattern!guardian}
51295108

@@ -6926,8 +6905,8 @@ \section{Exercises}
69266905
Write a function called \verb"has_no_e" that returns {\tt True} if
69276906
the given word doesn't have the letter ``e'' in it.
69286907

6929-
Modify your program from the previous section to print only the words
6930-
that have no ``e'' and compute the percentage of the words in the list
6908+
Write a program that reads {\tt words.txt} and prints only the words
6909+
that have no ``e''. Compute the percentage of words in the list
69316910
that have no ``e''.
69326911
\index{lipogram}
69336912

@@ -6941,8 +6920,8 @@ \section{Exercises}
69416920
that returns {\tt True} if the word doesn't use any of the forbidden
69426921
letters.
69436922

6944-
Modify your program to prompt the user to enter a string
6945-
of forbidden letters and then print the number of words that
6923+
Write a program that prompts the user to enter a string
6924+
of forbidden letters and then prints the number of words that
69466925
don't contain any of them.
69476926
Can you find a combination of 5 forbidden letters that
69486927
excludes the smallest number of words?
@@ -6956,7 +6935,7 @@ \section{Exercises}
69566935
Write a function named \verb"uses_only" that takes a word and a
69576936
string of letters, and that returns {\tt True} if the word contains
69586937
only letters in the list. Can you make a sentence using only the
6959-
letters {\tt acefhlo}? Other than ``Hoe alfalfa?''
6938+
letters {\tt acefhlo}? Other than ``Hoe alfalfa''?
69606939

69616940
\end{exercise}
69626941

@@ -8526,7 +8505,7 @@ \section{Exercises}
85268505
85278506
Because the words are in alphabetical order, we can speed things up
85288507
with a bisection search (also known as binary search), which is
8529-
similar to what you do when you look a word up in the dictionary. You
8508+
similar to what you do when you look a word up in the dictionary (the book, not the data structure). You
85308509
start in the middle and check to see whether the word you are looking
85318510
for comes before the word in the middle of the list. If so, you
85328511
search the first half of the list the same way. Otherwise you search
@@ -8720,8 +8699,8 @@ \section{A dictionary is a mapping}
87208699
order, as in Section~\ref{find}. As the list gets longer, the search
87218700
time gets longer in direct proportion.
87228701
8723-
For dictionaries, Python uses an
8724-
algorithm called a {\bf hashtable} that has a remarkable property: the
8702+
Python dictionaries use a data structure
8703+
called a {\bf hashtable} that has a remarkable property: the
87258704
{\tt in} operator takes about the same amount of time no matter how
87268705
many items are in the dictionary. I explain how that's possible
87278706
in Section~\ref{hashtable}, but the explanation might not make
@@ -8816,7 +8795,7 @@ \section{Dictionary as a collection of counters}
88168795
{'a': 1}
88178796
>>> h.get('a', 0)
88188797
1
8819-
>>> h.get('b', 0)
8798+
>>> h.get('c', 0)
88208799
0
88218800
\end{verbatim}
88228801
%
@@ -8936,8 +8915,7 @@ \section{Reverse lookup}
89368915
\index{optional argument}
89378916
\index{argument!optional}
89388917
8939-
The {\tt raise} statement can take a detailed error message as an
8940-
optional argument. For example:
8918+
When you raise an exception, you can provide a detailed error message as an optional argument. For example:
89418919
89428920
\begin{verbatim}
89438921
>>> raise LookupError('value does not appear in the dictionary')
@@ -9837,7 +9815,7 @@ \section{Variable-length argument tuples}
98379815
TypeError: sum expected at most 2 arguments, got 3
98389816
\end{verbatim}
98399817
%
9840-
As an exercise, write a function called {\tt sumall} that takes any number
9818+
As an exercise, write a function called \verb"sum_all" that takes any number
98419819
of arguments and returns their sum.
98429820
98439821
@@ -9846,9 +9824,8 @@ \section{Lists and tuples}
98469824
\index{function!zip}
98479825
98489826
{\tt zip} is a built-in function that takes two or more sequences and
9849-
returns a list of tuples where each tuple contains one
9850-
element from each sequence. The name of the function refers to
9851-
a zipper, which joins and interleaves two rows of teeth.
9827+
interleaves them. The name of the function refers to
9828+
a zipper, which interleaves two rows of teeth.
98529829
98539830
This example zips a string and a list:
98549831
@@ -10217,12 +10194,10 @@ \section{Glossary}
1021710194
\index{tuple assignment}
1021810195
\index{assignment!tuple}
1021910196
10220-
\item[gather:] The operation of assembling a variable-length
10221-
argument tuple.
10197+
\item[gather:] An operation that collects multiple arguments into a tuple.
1022210198
\index{gather}
1022310199
10224-
\item[scatter:] The operation of treating a sequence as a list of
10225-
arguments.
10200+
\item[scatter:] An operation that makes a sequence behave like multiple arguments.
1022610201
\index{scatter}
1022710202
1022810203
\item[zip object:] The result of calling a built-in function {\tt zip};
@@ -10295,7 +10270,6 @@ \section{Exercises}
1029510270
\item In Scrabble a ``bingo'' is when you play all seven tiles in
1029610271
your rack, along with a letter on the board, to form an eight-letter
1029710272
word. What collection of 8 letters forms the most possible bingos?
10298-
Hint: there are seven.
1029910273
1030010274
% (7, ['angriest', 'astringe', 'ganister', 'gantries', 'granites',
1030110275
% 'ingrates', 'rangiest'])
@@ -10984,7 +10958,7 @@ \section{Markov analysis}
1098410958
1098510959
\end{exercise}
1098610960
10987-
You should attempt this exercise before you go on; then you can can
10961+
You should attempt this exercise before you go on; then you can
1098810962
download my solution from \url{http://thinkpython2.com/code/markov.py}.
1098910963
You will also need \url{http://thinkpython2.com/code/emma.txt}.
1099010964
@@ -12228,9 +12202,9 @@ \section{Attributes}
1222812202
As a noun, ``AT-trib-ute'' is pronounced with emphasis on the first
1222912203
syllable, as opposed to ``a-TRIB-ute'', which is a verb.
1223012204
12231-
The following diagram shows the result of these assignments.
12205+
Figure~\ref{fig.point} is a state diagram that shows the result of these assignments.
1223212206
A state diagram that shows an object and its attributes is
12233-
called an {\bf object diagram}; see Figure~\ref{fig.point}.
12207+
called an {\bf object diagram}.
1223412208
\index{state diagram}
1223512209
\index{diagram!state}
1223612210
\index{object diagram}
@@ -13170,7 +13144,7 @@ \section{Exercises}
1317013144
1317113145
\item For two people born on different days, there is a day when one
1317213146
is twice as old as the other. That's their Double Day. Write a
13173-
program that takes two birthdays and computes their Double Day.
13147+
program that takes two birth dates and computes their Double Day.
1317413148
1317513149
\item For a little more challenge, write the more general version that
1317613150
computes the day when one person is $n$ times older than the other.
@@ -13910,11 +13884,6 @@ \section{Glossary}
1391013884
than one type.
1391113885
\index{polymorphism}
1391213886
13913-
\item[information hiding:] The principle that the interface provided
13914-
by an object should not depend on its implementation, in particular
13915-
the representation of its attributes.
13916-
\index{information hiding}
13917-
1391813887
\end{description}
1391913888
1392013889
@@ -14604,7 +14573,7 @@ \section{Debugging}
1460414573
\begin{verbatim}
1460514574
>>> hand = Hand()
1460614575
>>> find_defining_class(hand, 'shuffle')
14607-
<class 'Card.Deck'>
14576+
<class '__main__.Deck'>
1460814577
\end{verbatim}
1460914578
%
1461014579
So the {\tt shuffle} method for this Hand is the one in {\tt Deck}.
@@ -15294,7 +15263,7 @@ \section{Sets}
1529415263
return set(word) <= set(available)
1529515264
\end{verbatim}
1529615265
%
15297-
The \verb"<=" operator checks whether one set is a subset or another,
15266+
The \verb"<=" operator checks whether one set is a subset of another,
1529815267
including the possibility that they are equal, which is true if all
1529915268
the letters in {\tt word} appear in {\tt available}.
1530015269
\index{subset}
@@ -16460,7 +16429,7 @@ \section{Order of growth}
1646016429
10 & 1 001 & 111 \\
1646116430
100 & 10 001 & 10 101 \\
1646216431
1 000 & 100 001 & 1 001 001 \\
16463-
10 000 & 1 000 001 & $> 10^{10}$ \\
16432+
10 000 & 1 000 001 & 100 010 001 \\
1646416433
\hline
1646516434
\end{tabular}
1646616435
@@ -16557,7 +16526,7 @@ \section{Order of growth}
1655716526
you start multiplying, remember that you only need the leading term.
1655816527
1655916528
\item If $f$ is in $O(g)$, for some unspecified function $g$, what can
16560-
we say about $af+b$?
16529+
we say about $af+b$, where $a$ and $b$ are constants?
1656116530
1656216531
\item If $f_1$ and $f_2$ are in $O(g)$, what can we say about $f_1 + f_2$?
1656316532
@@ -16576,7 +16545,7 @@ \section{Order of growth}
1657616545
coefficients and the non-leading terms make a real difference.
1657716546
Sometimes the details of the hardware, the programming language, and
1657816547
the characteristics of the input make a big difference. And for small
16579-
problems asymptotic behavior is irrelevant.
16548+
problems, order of growth is irrelevant.
1658016549
1658116550
But if you keep those caveats in mind, algorithmic analysis is a
1658216551
useful tool. At least for large problems, the ``better'' algorithm
@@ -16926,9 +16895,7 @@ \section{Hashtables}
1692616895
self.maps = new_maps
1692716896
\end{verbatim}
1692816897
16929-
Each {\tt HashMap} contains a {\tt BetterMap}; \verb"__init__" starts
16930-
with just 2 LinearMaps and initializes {\tt num}, which keeps track of
16931-
the number of items.
16898+
\verb"__init__" creates a {\tt BetterMap} and initializes {\tt num}, which keeps track of the number of items.
1693216899
1693316900
{\tt get} just dispatches to {\tt BetterMap}. The real work happens
1693416901
in {\tt add}, which checks the number of items and the size of the
@@ -16984,7 +16951,7 @@ \section{Hashtables}
1698416951
Figure~\ref{fig.hash} shows how this works graphically. Each
1698516952
block represents a unit of work. The columns show the total
1698616953
work for each add in order from left to right: the first two
16987-
{\tt adds} cost 1 units, the third costs 3 units, etc.
16954+
{\tt adds} cost 1 unit each, the third costs 3 units, etc.
1698816955
1698916956
\begin{figure}
1699016957
\centerline{\includegraphics[width=5.5in]{figs/towers.pdf}}

book/footer.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@
55

66
<td width=130 valign="top">
77

8+
<h4>Contribute</h4>
9+
If you would like to make a contribution to support my books,
10+
you can use the button below and pay with PayPal. Thank you!
11+
12+
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
13+
<input type="hidden" name="cmd" value="_s-xclick">
14+
<input type="hidden" name="hosted_button_id" value="RAAYAZ7Y48S2A">
15+
<table>
16+
<tr><td><input type="hidden" name="on0" value="Pay what you want!">Pay what you want:</td></tr><tr><td><select name="os0">
17+
<option value="Small">Small $1.00 USD</option>
18+
<option value="Medium">Medium $5.00 USD</option>
19+
<option value="Large">Large $10.00 USD</option>
20+
<option value="X-Large">X-Large $20.00 USD</option>
21+
<option value="XX-Large">XX-Large $50.00 USD</option>
22+
</select> </td></tr>
23+
</table>
24+
<input type="hidden" name="currency_code" value="USD">
25+
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynow_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
26+
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
27+
</form>
28+
29+
830
<p>
931
<h4>Are you using one of our books in a class?</h4> We'd like to know
1032
about it. Please consider filling out <a href="http://spreadsheets.google.com/viewform?formkey=dC0tNUZkMjBEdXVoRGljNm9FRmlTMHc6MA" onClick="javascript: pageTracker._trackPageview('/outbound/survey');">this short survey</a>.

0 commit comments

Comments
 (0)