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}
399399and an improvement in style in Chapter 1, and he initiated discussion
400400on the technical aspects of interpreters.
401401
402- \item Benoit Girard sent in a
402+ \item Beno \^ {i}t Girard sent in a
403403correction 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
539539changes for Python 2.2.
540540
541- \item Jean-Philippe Rey at Ecole Centrale
541+ \item Jean-Philippe Rey at \' {E}cole Centrale
542542Paris sent a number of patches, including some updates for Python 2.2
543543and 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
725727In addition, people who spotted typos or made corrections include
@@ -1098,8 +1100,8 @@ \section{Formal and natural languages}
10981100\index {structure}
10991101
11001102The 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.
11031105Similarly, in a chemical formula the subscript comes after the element
11041106name, not before.
11051107
@@ -1333,8 +1335,8 @@ \section{Exercises}
13331335{\tt -2}. What happens if you put a plus sign before a number?
13341336What 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
13401342between them?
@@ -1550,38 +1552,18 @@ \section{Script mode}
15501552marathon is about 42 kilometers.
15511553
15521554But 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 }
15581562miles = 26.2
15591563print(miles * 1.61)
15601564\end {verbatim }
15611565
15621566This 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-
15851567To check your understanding, type the following statements in the
15861568Python interpreter and see what they do:
15871569
@@ -1596,7 +1578,6 @@ \section{Script mode}
15961578expression 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
20332013functions ({\tt cos}, {\tt tan}, etc.) take arguments in radians. To
20342014convert 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
45834563takes 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}
51225102None
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}
69266905Write a function called \verb "has_no_e " that returns {\tt True} if
69276906the 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
69316910that have no `` e'' .
69326911\index {lipogram}
69336912
@@ -6941,8 +6920,8 @@ \section{Exercises}
69416920that returns {\tt True} if the word doesn't use any of the forbidden
69426921letters.
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
69466925don't contain any of them.
69476926Can you find a combination of 5 forbidden letters that
69486927excludes the smallest number of words?
@@ -6956,7 +6935,7 @@ \section{Exercises}
69566935Write a function named \verb "uses_only " that takes a word and a
69576936string of letters, and that returns {\tt True} if the word contains
69586937only 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
85278506Because the words are in alphabetical order, we can speed things up
85288507with 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
85308509start in the middle and check to see whether the word you are looking
85318510for comes before the word in the middle of the list. If so, you
85328511search the first half of the list the same way. Otherwise you search
@@ -8720,8 +8699,8 @@ \section{A dictionary is a mapping}
87208699order, as in Section~\ref {find }. As the list gets longer, the search
87218700time 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
87268705many items are in the dictionary. I explain how that's possible
87278706in 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)
881887971
8819- >>> h.get('b ', 0)
8798+ >>> h.get('c ', 0)
882087990
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}
98379815TypeError: 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
98419819of 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
98539830This 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
1029610271your rack, along with a letter on the board, to form an eight-letter
1029710272word. 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
1098810962download my solution from \url {http://thinkpython2.com/code/markov.py}.
1098910963You will also need \url {http://thinkpython2.com/code/emma.txt}.
1099010964
@@ -12228,9 +12202,9 @@ \section{Attributes}
1222812202As a noun, `` AT-trib-ute'' is pronounced with emphasis on the first
1222912203syllable, 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.
1223212206A 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%
1461014579So 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,
1529815267including the possibility that they are equal, which is true if all
1529915268the letters in {\tt word} appear in {\tt available}.
1530015269\index {subset}
@@ -16460,7 +16429,7 @@ \section{Order of growth}
164601642910 & 1 001 & 111 \\
1646116430100 & 10 001 & 10 101 \\
16462164311 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}
1657616545coefficients and the non-leading terms make a real difference.
1657716546Sometimes the details of the hardware, the programming language, and
1657816547the 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
1658116550But if you keep those caveats in mind, algorithmic analysis is a
1658216551useful 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
1693416901in {\tt add}, which checks the number of items and the size of the
@@ -16984,7 +16951,7 @@ \section{Hashtables}
1698416951Figure~\ref {fig.hash } shows how this works graphically. Each
1698516952block represents a unit of work. The columns show the total
1698616953work 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}}
0 commit comments