diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..2dd9680 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index f24cd99..4375f01 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ pip-log.txt #Mr Developer .mr.developer.cfg + diff --git a/book/.gitignore b/book/.gitignore new file mode 100644 index 0000000..b995c16 --- /dev/null +++ b/book/.gitignore @@ -0,0 +1,10 @@ +# LaTeX related files +.DS_ignore +auto/ +*.aux +*.idx +*.ilg +*.ind +*.log +*.out +*.toc diff --git a/book/Makefile b/book/Makefile index a4f841b..bfe7378 100644 --- a/book/Makefile +++ b/book/Makefile @@ -17,9 +17,10 @@ PDFFLAGS = -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress \ ps2pdf $(PDFFLAGS) $< all: book.tex - pdflatex book + xelatex book + xelatex book makeindex book.idx - pdflatex book + xelatex book mv book.pdf thinkpython2.pdf hevea: book.tex header.html footer.html @@ -28,6 +29,8 @@ hevea: book.tex header.html footer.html latex thinkpython2 rm -rf html mkdir html + sed -i 's#\\begin{lstlisting}#\\begin{verbatim}#g' thinkpython2.tex + sed -i 's#\\end{lstlisting}#\\end{verbatim}#g' thinkpython2.tex hevea -fix -O -e latexonly htmlonly thinkpython2 # the following greps are a kludge to prevent imagen from seeing # the definitions in latexonly, and to avoid headers on the images diff --git a/book/book.tex b/book/book.tex index a6c500f..d529504 100644 --- a/book/book.tex +++ b/book/book.tex @@ -5,14 +5,42 @@ % http://creativecommons.org/licenses/by-nc/3.0/ % +\newif\ifxelatex + \ifx\XeTeXglyph\undefined + \xelatexfalse + \else + \xelatextrue + \fi + + +\ifxelatex %\documentclass[10pt,b5paper]{book} -\documentclass[10pt]{book} -\usepackage[width=5.5in,height=8.5in,hmarginratio=3:2,vmarginratio=1:1]{geometry} +\documentclass[ +DIV=11, +fontsize=12, +twoside, +headinclude=false, +titlepage=firstiscover, +abstract=true, +headsepline=true, +footsepline=true, +chapterprefix=true, %this allows for editing of the chapter titles +headings=big, +bibliography=totoc,%adds unnumbered Bibliography chapter to toc +captions=tableheading +]{scrbook} +\else +\documentclass[10pt,b5paper]{book} +\fi +\usepackage[left=2cm,right=2cm,top=2.5cm,bottom=3cm,paperheight=11in,paperwidth=8.5in]{geometry} % for some of these packages, you might have to install % texlive-latex-extra (in Ubuntu) +\ifxelatex \usepackage[T1]{fontenc} +\usepackage{fontspec} +\fi \usepackage{textcomp} \usepackage{mathpazo} \usepackage{url} @@ -21,13 +49,15 @@ \usepackage{amsmath} \usepackage{amsthm} %\usepackage{amssymb} -\usepackage{exercise} % texlive-latex-extra +\usepackage{answers} % texlive-latex-extra \usepackage{makeidx} \usepackage{setspace} \usepackage{hevea} \usepackage{upquote} \usepackage{appendix} +\usepackage[svgnames,x11names]{xcolor} \usepackage[bookmarks]{hyperref} +\usepackage{listings} \title{Think Python} \author{Allen B. Downey} @@ -35,10 +65,79 @@ \newcommand{\theversion}{2nd Edition, Version 2.2.20} \newcommand{\thedate}{} + +% Hyperref setup +\hypersetup{ + colorlinks, + linkcolor={red!50!black}, + citecolor={blue!50!black}, + urlcolor={blue!80!black} +} + +% For Listings +% Setting up lstlisting +\definecolor{mygreen}{rgb}{0,0.6,0} +\definecolor{mygray}{rgb}{0.5,0.5,0.5} +\definecolor{mymauve}{rgb}{0.58,0,0.82} + +\lstset{ % + backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor} + basicstyle=\ttfamily\footnotesize, % the size of the fonts that are used for the code + breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace + breaklines=true, % sets automatic line breaking + captionpos=b, % sets the caption-position to bottom + commentstyle=\color{mygreen}, % comment style + deletekeywords={...}, % if you want to delete keywords from the given language + escapeinside={\%*}{*)}, % if you want to add LaTeX within your code + extendedchars=true, % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8 + %frame=single, % adds a frame around the code + keepspaces=true, % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible) + keywordstyle=\color{blue}, % keyword style + language=Python, % the language of the code + otherkeywords={*,...}, % if you want to add more keywords to the set + numbers=none, % where to put the line-numbers; possible values are (none, left, right) + numbersep=5pt, % how far the line-numbers are from the code + numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers + rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here)) + showspaces=false, % show spaces everywhere adding particular underscores; it overrides 'showstringspaces' + showstringspaces=false, % underline spaces within strings only + showtabs=false, % show tabs within strings adding particular underscores + stepnumber=2, % the step between two line-numbers. If it's 1, each line will be numbered + stringstyle=\color{mymauve}, % string literal style + tabsize=2, % sets default tabsize to 2 spaces +% title=\lstname % show the filename of files included with \lstinputlisting; also try caption instead of title +} + + +\ifxelatex +% Fonts for the book +% Fonts to be used +\setromanfont[Ligatures=TeX]{Minion Pro} +\setsansfont[Ligatures=TeX,Scale=MatchLowercase]{Myriad Pro} +\setmonofont[Scale=MatchLowercase]{Menlo} + +% Fixes for koma-script + +% Chapter Style +%%% CHAPTER STYLING +\addtokomafont{chapterprefix}{\raggedleft \linespread{1}} +\setkomafont{chapter}{\color{DodgerBlue4}\fontsize{40}{30}\selectfont} + +\addtokomafont{section}{\color{DodgerBlue4}\fontsize{20}{24}\selectfont} +\addtokomafont{subsection}{\color{DodgerBlue4}\fontsize{18}{22}\selectfont} +\addtokomafont{subsubsection}{\color{DodgerBlue4}\fontsize{16}{19}\selectfont} +\addtokomafont{paragraph}{\color{DodgerBlue4}\fontsize{15}{19}\selectfont} + +\renewcommand*{\chapterformat}{% + \mbox{\scalebox{0.80}{\chapappifchapterprefix{\nobreakspace}}% + \scalebox{3.5}{\color{DeepSkyBlue2}\thechapter}\enskip}} + + % these styles get translated in CSS for the HTML version \newstyle{a:link}{color:black;} \newstyle{p+p}{margin-top:1em;margin-bottom:1em} \newstyle{img}{border:0px} +\fi % change the arrows \setlinkstext @@ -51,6 +150,8 @@ \newif\ifplastex \plastexfalse +\theoremstyle{definition} + \begin{document} \frontmatter @@ -314,7 +415,7 @@ \section*{The strange history of this book} to deal with installing Python until you want to. \item For Chapter~\ref{turtle} I switched from my own turtle graphics - package, called Swampy, to a more standard Python module, {\tt + package, called Swampy, to a more standard Python module, {\texttt turtle}, which is easier to install and more powerful. \item I added a new chapter called ``The Goodies'', which introduces @@ -373,7 +474,7 @@ \section*{Contributor List} huge help. If you have a suggestion or correction, please send email to -{\tt feedback@thinkpython.com}. If I make a change based on your +{\texttt feedback@thinkpython.com}. If I make a change based on your feedback, I will add you to the contributor list (unless you ask to be omitted). @@ -402,7 +503,7 @@ \section*{Contributor List} \item Benoit Girard sent in a correction to a humorous mistake in Section 5.6. -\item Courtney Gleason and Katherine Smith wrote {\tt horsebet.py}, +\item Courtney Gleason and Katherine Smith wrote {\texttt horsebet.py}, which was used as a case study in an earlier version of the book. Their program can now be found on the website. @@ -413,7 +514,7 @@ \section*{Contributor List} \item James Kaylin is a student using the text. He has submitted numerous corrections. -\item David Kershaw fixed the broken {\tt catTwice} function in Section +\item David Kershaw fixed the broken {\texttt catTwice} function in Section 3.10. \item Eddie Lam has sent in numerous corrections to Chapters @@ -435,7 +536,7 @@ \section*{Contributor List} in numerous corrections and suggestions to the book. \item Simon Dicon Montford reported a missing function definition and -several typos in Chapter 3. He also found errors in the {\tt increment} +several typos in Chapter 3. He also found errors in the {\texttt increment} function in Chapter 13. \item John Ouzts corrected the definition of ``return value" @@ -513,8 +614,8 @@ \section*{Contributor List} \item Julie Peters caught a typo in the Preface. -\item Florin Oprina sent in an improvement in {\tt makeTime}, -a correction in {\tt printTime}, and a nice typo. +\item Florin Oprina sent in an improvement in {\texttt makeTime}, +a correction in {\texttt printTime}, and a nice typo. \item D.~J.~Webre suggested a clarification in Chapter 3. @@ -570,7 +671,7 @@ \section*{Contributor List} \item Wim Champagne found a brain-o in a dictionary example. \item Douglas Wright pointed out a problem with floor division in -{\tt arc}. +{\texttt arc}. \item Jared Spindor found some jetsam at the end of a sentence. @@ -592,18 +693,18 @@ \section*{Contributor List} \item Gordon Shephard sent in several corrections, all in separate emails. -\item Andrew Turner {\tt spot}ted an error in Chapter 8. +\item Andrew Turner {\texttt spot}ted an error in Chapter 8. -\item Adam Hobart fixed a problem with floor division in {\tt arc}. +\item Adam Hobart fixed a problem with floor division in {\texttt arc}. \item Daryl Hammond and Sarah Zimmerman pointed out that I served -up {\tt math.pi} too early. And Zim spotted a typo. +up {\texttt math.pi} too early. And Zim spotted a typo. \item George Sass found a bug in a Debugging section. \item Brian Bingham suggested Exercise~\ref{exrotatepairs}. -\item Leah Engelbert-Fenton pointed out that I used {\tt tuple} +\item Leah Engelbert-Fenton pointed out that I used {\texttt tuple} as a variable name, contrary to my own advice. And then found a bunch of typos and a ``use before def''. @@ -678,7 +779,7 @@ \section*{Contributor List} \item Daniel Neilson corrected an error about the order of operations. -\item Will McGinnis pointed out that {\tt polyline} was defined +\item Will McGinnis pointed out that {\texttt polyline} was defined differently in two places. \item Frank Hecker pointed out an exercise that was under-specified, and @@ -696,10 +797,10 @@ \section*{Contributor List} \item Martin Nordsletten found a bug in an exercise solution. -\item Sven Hoexter pointed out that a variable named {\tt input} +\item Sven Hoexter pointed out that a variable named {\texttt input} shadows a build-in function. -\item Stephen Gregory pointed out the problem with {\tt cmp} +\item Stephen Gregory pointed out the problem with {\texttt cmp} in Python 3. \item Ishwar Bhat corrected my statement of Fermat's last theorem. @@ -757,7 +858,7 @@ \chapter{The way of the program} Like scientists, they observe the behavior of complex systems, form hypotheses, and test predictions. \index{problem solving} -The single most important skill for a computer scientist is {\bf +The single most important skill for a computer scientist is {\textbf problem solving}. Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. As it turns out, the process of learning to @@ -772,7 +873,7 @@ \chapter{The way of the program} \section{What is a program?} -A {\bf program} is a sequence of instructions that specifies how to +A {\textbf program} is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of equations or finding the roots of a polynomial, but it can also be a symbolic computation, such @@ -841,37 +942,37 @@ \section{Running Python} about Python 2. \index{Python 2} -The Python {\bf interpreter} is a program that reads and executes +The Python {\textbf interpreter} is a program that reads and executes Python code. Depending on your environment, you might start the -interpreter by clicking on an icon, or by typing {\tt python} on +interpreter by clicking on an icon, or by typing {\texttt python} on a command line. When it starts, you should see output like this: \index{interpreter} -\begin{verbatim} +\begin{lstlisting} Python 3.4.0 (default, Jun 19 2015, 14:20:21) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> -\end{verbatim} +\end{lstlisting} % The first three lines contain information about the interpreter and the operating system it's running on, so it might be different for you. But you should check that the version number, which is -{\tt 3.4.0} in this example, begins with 3, which indicates that +{\texttt 3.4.0} in this example, begins with 3, which indicates that you are running Python 3. If it begins with 2, you are running (you guessed it) Python 2. -The last line is a {\bf prompt} that indicates that the interpreter is +The last line is a {\textbf prompt} that indicates that the interpreter is ready for you to enter code. If you type a line of code and hit Enter, the interpreter displays the result: \index{prompt} -\begin{verbatim} +\begin{lstlisting} >>> 1 + 1 2 -\end{verbatim} +\end{lstlisting} % Now you're ready to get started. From here on, I assume that you know how to start the Python @@ -886,17 +987,17 @@ \section{The first program} is called ``Hello, World!'' because all it does is display the words ``Hello, World!''. In Python, it looks like this: -\begin{verbatim} +\begin{lstlisting} >>> print('Hello, World!') -\end{verbatim} +\end{lstlisting} % -This is an example of a {\bf print statement}, although it +This is an example of a {\textbf print statement}, although it doesn't actually print anything on paper. It displays a result on the screen. In this case, the result is the words -\begin{verbatim} +\begin{lstlisting} Hello, World! -\end{verbatim} +\end{lstlisting} % The quotation marks in the program mark the beginning and end of the text to be displayed; they don't appear in the result. @@ -904,7 +1005,7 @@ \section{The first program} \index{print statement} \index{statement!print} -The parentheses indicate that {\tt print} is a function. We'll get +The parentheses indicate that {\texttt print} is a function. We'll get to functions in Chapter~\ref{funcchap}. \index{function} \index{print function} @@ -912,9 +1013,9 @@ \section{The first program} a function, so it doesn't use parentheses. \index{Python 2} -\begin{verbatim} +\begin{lstlisting} >>> print 'Hello, World!' -\end{verbatim} +\end{lstlisting} % This distinction will make more sense soon, but that's enough to get started. @@ -925,47 +1026,47 @@ \section{Arithmetic operators} \index{arithmetic operator} After ``Hello, World'', the next step is arithmetic. Python provides -{\bf operators}, which are special symbols that represent computations +{\textbf operators}, which are special symbols that represent computations like addition and multiplication. -The operators {\tt +}, {\tt -}, and {\tt *} perform addition, +The operators {\texttt +}, {\texttt -}, and {\texttt *} perform addition, subtraction, and multiplication, as in the following examples: -\begin{verbatim} +\begin{lstlisting} >>> 40 + 2 42 >>> 43 - 1 42 >>> 6 * 7 42 -\end{verbatim} +\end{lstlisting} % -The operator {\tt /} performs division: +The operator {\texttt /} performs division: -\begin{verbatim} +\begin{lstlisting} >>> 84 / 2 42.0 -\end{verbatim} +\end{lstlisting} % -You might wonder why the result is {\tt 42.0} instead of {\tt 42}. +You might wonder why the result is {\texttt 42.0} instead of {\texttt 42}. I'll explain in the next section. -Finally, the operator {\tt **} performs exponentiation; that is, +Finally, the operator {\texttt **} performs exponentiation; that is, it raises a number to a power: -\begin{verbatim} +\begin{lstlisting} >>> 6**2 + 6 42 -\end{verbatim} +\end{lstlisting} % In some other languages, \verb"^" is used for exponentiation, but in Python it is a bitwise operator called XOR. If you are not familiar with bitwise operators, the result will surprise you: -\begin{verbatim} +\begin{lstlisting} >>> 6 ^ 2 4 -\end{verbatim} +\end{lstlisting} % I won't cover bitwise operators in this book, but you can read about @@ -979,13 +1080,13 @@ \section{Values and types} \index{type} \index{string} -A {\bf value} is one of the basic things a program works with, like a -letter or a number. Some values we have seen so far are {\tt 2}, -{\tt 42.0}, and \verb"'Hello, World!'". +A {\textbf value} is one of the basic things a program works with, like a +letter or a number. Some values we have seen so far are {\texttt 2}, +{\texttt 42.0}, and \verb"'Hello, World!'". -These values belong to different {\bf types}: -{\tt 2} is an {\bf integer}, {\tt 42.0} is a {\bf floating-point number}, -and \verb"'Hello, World!'" is a {\bf string}, +These values belong to different {\textbf types}: +{\texttt 2} is an {\textbf integer}, {\texttt 42.0} is a {\textbf floating-point number}, +and \verb"'Hello, World!'" is a {\textbf string}, so-called because the letters it contains are strung together. \index{integer} \index{floating-point} @@ -993,22 +1094,22 @@ \section{Values and types} If you are not sure what type a value has, the interpreter can tell you: -\begin{verbatim} +\begin{lstlisting} >>> type(2) >>> type(42.0) >>> type('Hello, World!') -\end{verbatim} +\end{lstlisting} % In these results, the word ``class'' is used in the sense of a category; a type is a category of values. \index{class} -Not surprisingly, integers belong to the type {\tt int}, -strings belong to {\tt str} and floating-point -numbers belong to {\tt float}. +Not surprisingly, integers belong to the type {\texttt int}, +strings belong to {\texttt str} and floating-point +numbers belong to {\texttt float}. \index{type} \index{string type} \index{type!str} @@ -1022,25 +1123,25 @@ \section{Values and types} strings. \index{quotation mark} -\begin{verbatim} +\begin{lstlisting} >>> type('2') >>> type('42.0') -\end{verbatim} +\end{lstlisting} % They're strings. When you type a large integer, you might be tempted to use commas -between groups of digits, as in {\tt 1,000,000}. This is not a +between groups of digits, as in {\texttt 1,000,000}. This is not a legal {\em integer} in Python, but it is legal: -\begin{verbatim} +\begin{lstlisting} >>> 1,000,000 (1, 0, 0) -\end{verbatim} +\end{lstlisting} % -That's not what we expected at all! Python interprets {\tt +That's not what we expected at all! Python interprets {\texttt 1,000,000} as a comma-separated sequence of integers. We'll learn more about this kind of sequence later. \index{sequence} @@ -1061,12 +1162,12 @@ \section{Formal and natural languages} \index{language!formal} \index{language!natural} -{\bf Natural languages} are the languages people speak, +{\textbf Natural languages} are the languages people speak, such as English, Spanish, and French. They were not designed by people (although people try to impose some order on them); they evolved naturally. -{\bf Formal languages} are languages that are designed by people for +{\textbf Formal languages} are languages that are designed by people for specific applications. For example, the notation that mathematicians use is a formal language that is particularly good at denoting relationships among numbers and symbols. Chemists use a formal @@ -1074,11 +1175,11 @@ \section{Formal and natural languages} most importantly: \begin{quote} -{\bf Programming languages are formal languages that have been +{\textbf Programming languages are formal languages that have been designed to express computations.} \end{quote} -Formal languages tend to have strict {\bf syntax} rules that +Formal languages tend to have strict {\textbf syntax} rules that govern the structure of statements. For example, in mathematics the statement $3 + 3 = 6$ has correct syntax, but @@ -1086,7 +1187,7 @@ \section{Formal and natural languages} $H_2O$ is a syntactically correct formula, but $_2Zz$ is not. \index{syntax} -Syntax rules come in two flavors, pertaining to {\bf tokens} and +Syntax rules come in two flavors, pertaining to {\textbf tokens} and structure. Tokens are the basic elements of the language, such as words, numbers, and chemical elements. One of the problems with $3 += 3 \$ 6$ is that \( \$ \) is not a legal token in mathematics @@ -1108,7 +1209,7 @@ \section{Formal and natural languages} When you read a sentence in English or a statement in a formal language, you have to figure out the structure (although in a natural language you do this subconsciously). This -process is called {\bf parsing}. +process is called {\textbf parsing}. \index{parse} Although formal and natural languages have many features in @@ -1176,8 +1277,8 @@ \section{Debugging} \index{debugging} Programmers make mistakes. For whimsical reasons, programming errors -are called {\bf bugs} and the process of tracking them down is called -{\bf debugging}. +are called {\textbf bugs} and the process of tracking them down is called +{\textbf debugging}. \index{debugging} \index{bug} @@ -1189,7 +1290,7 @@ \section{Debugging} they were people. When they work well, we think of them as teammates, and when they are obstinate or rude, we respond to them the same way we respond to rude, -obstinate people (Reeves and Nass, {\it The Media +obstinate people (Reeves and Nass, {\textit The Media Equation: How People Treat Computers, Television, and New Media Like Real People and Places}). \index{debugging!emotional response} @@ -1259,8 +1360,8 @@ \section{Glossary} \index{value} \item[type:] A category of values. The types we have seen so far -are integers (type {\tt int}), floating-point numbers (type {\tt -float}), and strings (type {\tt str}). +are integers (type {\texttt int}), floating-point numbers (type {\texttt +float}), and strings (type {\texttt str}). \index{type} \item[integer:] A type that represents whole numbers. @@ -1304,6 +1405,7 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont \normalfont It is a good idea to read this book in front of a computer so you can try out the examples as you go. @@ -1311,7 +1413,7 @@ \section{Exercises} Whenever you are experimenting with a new feature, you should try to make mistakes. For example, in the ``Hello, world!'' program, what happens if you leave out one of the quotation marks? What -if you leave out both? What if you spell {\tt print} wrong? +if you leave out both? What if you spell {\texttt print} wrong? \index{error message} This kind of experiment helps you remember what you read; it also @@ -1328,10 +1430,10 @@ \section{Exercises} leave out one of the quotation marks, or both? \item You can use a minus sign to make a negative number like -{\tt -2}. What happens if you put a plus sign before a number? -What about {\tt 2++2}? +{\texttt -2}. What happens if you put a plus sign before a number? +What about {\texttt 2++2}? -\item In math notation, leading zeros are ok, as in {\tt 02}. +\item In math notation, leading zeros are ok, as in {\texttt 02}. What happens if you try this in Python? \item What happens if you have two values with no operator @@ -1344,6 +1446,7 @@ \section{Exercises} \begin{exercise} +\normalfont \normalfont Start the Python interpreter and use it as a calculator. @@ -1371,7 +1474,7 @@ \section{Exercises} \chapter{Variables, expressions and statements} One of the most powerful features of a programming language is the -ability to manipulate {\bf variables}. A variable is a name that +ability to manipulate {\textbf variables}. A variable is a name that refers to a value. \index{variable} @@ -1381,25 +1484,25 @@ \section{Assignment statements} \index{assignment statement} \index{statement!assignment} -An {\bf assignment statement} creates a new variable and gives +An {\textbf assignment statement} creates a new variable and gives it a value: -\begin{verbatim} +\begin{lstlisting} >>> message = 'And now for something completely different' >>> n = 17 >>> pi = 3.141592653589793 -\end{verbatim} +\end{lstlisting} % This example makes three assignments. The first assigns a string -to a new variable named {\tt message}; -the second gives the integer {\tt 17} to {\tt n}; the third -assigns the (approximate) value of $\pi$ to {\tt pi}. +to a new variable named {\texttt message}; +the second gives the integer {\texttt 17} to {\texttt n}; the third +assigns the (approximate) value of $\pi$ to {\texttt pi}. \index{state diagram} \index{diagram!state} A common way to represent variables on paper is to write the name with an arrow pointing to its value. This kind of figure is -called a {\bf state diagram} because it shows what state each of the +called a {\textbf state diagram} because it shows what state each of the variables is in (think of it as the variable's state of mind). Figure~\ref{fig.state2} shows the result of the previous example. @@ -1430,27 +1533,27 @@ \section{Variable names} If you give a variable an illegal name, you get a syntax error: -\begin{verbatim} +\begin{lstlisting} >>> 76trombones = 'big parade' SyntaxError: invalid syntax >>> more@ = 1000000 SyntaxError: invalid syntax >>> class = 'Advanced Theoretical Zymurgy' SyntaxError: invalid syntax -\end{verbatim} +\end{lstlisting} % -{\tt 76trombones} is illegal because it begins with a number. -{\tt more@} is illegal because it contains an illegal character, {\tt -@}. But what's wrong with {\tt class}? +{\texttt 76trombones} is illegal because it begins with a number. +{\texttt more@} is illegal because it contains an illegal character, {\texttt +@}. But what's wrong with {\texttt class}? -It turns out that {\tt class} is one of Python's {\bf keywords}. The +It turns out that {\texttt class} is one of Python's {\textbf keywords}. The interpreter uses keywords to recognize the structure of the program, and they cannot be used as variable names. \index{keyword} Python 3 has these keywords: -\begin{verbatim} +\begin{lstlisting} False class finally is return None continue for lambda try True def from nonlocal while @@ -1458,7 +1561,7 @@ \section{Variable names} as elif if or yield assert else import pass break except in raise -\end{verbatim} +\end{lstlisting} % You don't have to memorize this list. In most development environments, keywords are displayed in a different color; if you try to use one @@ -1467,41 +1570,41 @@ \section{Variable names} \section{Expressions and statements} -An {\bf expression} is a combination of values, variables, and operators. +An {\textbf expression} is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions: \index{expression} -\begin{verbatim} +\begin{lstlisting} >>> 42 42 >>> n 17 >>> n + 25 42 -\end{verbatim} +\end{lstlisting} % When you type an expression at the prompt, the interpreter -{\bf evaluates} it, which means that it finds the value of +{\textbf evaluates} it, which means that it finds the value of the expression. -In this example, {\tt n} has the value 17 and -{\tt n + 25} has the value 42. +In this example, {\texttt n} has the value 17 and +{\texttt n + 25} has the value 42. \index{evaluate} -A {\bf statement} is a unit of code that has an effect, like +A {\textbf statement} is a unit of code that has an effect, like creating a variable or displaying a value. \index{statement} -\begin{verbatim} +\begin{lstlisting} >>> n = 17 >>> print(n) -\end{verbatim} +\end{lstlisting} % The first line is an assignment statement that gives a value to -{\tt n}. The second line is a print statement that displays the -value of {\tt n}. +{\texttt n}. The second line is a print statement that displays the +value of {\texttt n}. -When you type a statement, the interpreter {\bf executes} it, +When you type a statement, the interpreter {\textbf executes} it, which means that it does whatever the statement says. In general, statements don't have values. \index{execute} @@ -1509,16 +1612,16 @@ \section{Expressions and statements} \section{Script mode} -So far we have run Python in {\bf interactive mode}, which +So far we have run Python in {\textbf interactive mode}, which means that you interact directly with the interpreter. Interactive mode is a good way to get started, but if you are working with more than a few lines of code, it can be clumsy. \index{interactive mode} -The alternative is to save code in a file called a {\bf script} and -then run the interpreter in {\bf script mode} to execute the script. By -convention, Python scripts have names that end with {\tt .py}. +The alternative is to save code in a file called a {\textbf script} and +then run the interpreter in {\textbf script mode} to execute the script. By +convention, Python scripts have names that end with {\texttt .py}. \index{script} \index{script mode} @@ -1536,13 +1639,13 @@ \section{Script mode} For example, if you are using Python as a calculator, you might type -\begin{verbatim} +\begin{lstlisting} >>> miles = 26.2 >>> miles * 1.61 42.182 -\end{verbatim} +\end{lstlisting} -The first line assigns a value to {\tt miles}, but it has no visible +The first line assigns a value to {\texttt miles}, but it has no visible effect. The second line is an expression, so the interpreter evaluates it and displays the result. It turns out that a marathon is about 42 kilometers. @@ -1552,10 +1655,10 @@ \section{Script mode} visible effect. Python actually evaluates the expression, but it doesn't display the value unless you tell it to: -\begin{verbatim} +\begin{lstlisting} miles = 26.2 print(miles * 1.61) -\end{verbatim} +\end{lstlisting} This behavior can be confusing at first. @@ -1565,29 +1668,29 @@ \section{Script mode} For example, the script -\begin{verbatim} +\begin{lstlisting} print(1) x = 2 print(x) -\end{verbatim} +\end{lstlisting} % produces the output -\begin{verbatim} +\begin{lstlisting} 1 2 -\end{verbatim} +\end{lstlisting} % The assignment statement produces no output. To check your understanding, type the following statements in the Python interpreter and see what they do: -\begin{verbatim} +\begin{lstlisting} 5 x = 5 x + 1 -\end{verbatim} +\end{lstlisting} Now put the same statements in a script and run it. What is the output? Modify the script by transforming each @@ -1600,32 +1703,32 @@ \section{Order of operations} \index{PEMDAS} When an expression contains more than one operator, the order of -evaluation depends on the {\bf order of operations}. For +evaluation depends on the {\textbf order of operations}. For mathematical operators, Python follows mathematical convention. -The acronym {\bf PEMDAS} is a useful way to +The acronym {\textbf PEMDAS} is a useful way to remember the rules: \begin{itemize} -\item {\bf P}arentheses have the highest precedence and can be used +\item {\textbf P}arentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since -expressions in parentheses are evaluated first, {\tt 2 * (3-1)} is 4, -and {\tt (1+1)**(5-2)} is 8. You can also use parentheses to make an -expression easier to read, as in {\tt (minute * 100) / 60}, even +expressions in parentheses are evaluated first, {\texttt 2 * (3-1)} is 4, +and {\texttt (1+1)**(5-2)} is 8. You can also use parentheses to make an +expression easier to read, as in {\texttt (minute * 100) / 60}, even if it doesn't change the result. -\item {\bf E}xponentiation has the next highest precedence, so -{\tt 1 + 2**3} is 9, not 27, and {\tt 2 * 3**2} is 18, not 36. +\item {\textbf E}xponentiation has the next highest precedence, so +{\texttt 1 + 2**3} is 9, not 27, and {\texttt 2 * 3**2} is 18, not 36. -\item {\bf M}ultiplication and {\bf D}ivision have higher precedence - than {\bf A}ddition and {\bf S}ubtraction. So {\tt 2*3-1} is 5, not - 4, and {\tt 6+4/2} is 8, not 5. +\item {\textbf M}ultiplication and {\textbf D}ivision have higher precedence + than {\textbf A}ddition and {\textbf S}ubtraction. So {\texttt 2*3-1} is 5, not + 4, and {\texttt 6+4/2} is 8, not 5. \item Operators with the same precedence are evaluated from left to - right (except exponentiation). So in the expression {\tt degrees / + right (except exponentiation). So in the expression {\texttt degrees / 2 * pi}, the division happens first and the result is multiplied - by {\tt pi}. To divide by $2 \pi$, you can use parentheses or write - {\tt degrees / 2 / pi}. + by {\texttt pi}. To divide by $2 \pi$, you can use parentheses or write + {\texttt degrees / 2 / pi}. \end{itemize} @@ -1641,30 +1744,30 @@ \section{String operations} In general, you can't perform mathematical operations on strings, even if the strings look like numbers, so the following are illegal: -\begin{verbatim} +\begin{lstlisting} '2'-'1' 'eggs'/'easy' 'third'*'a charm' -\end{verbatim} +\end{lstlisting} % -But there are two exceptions, {\tt +} and {\tt *}. +But there are two exceptions, {\texttt +} and {\texttt *}. -The {\tt +} operator performs {\bf string concatenation}, which means +The {\texttt +} operator performs {\textbf string concatenation}, which means it joins the strings by linking them end-to-end. For example: \index{concatenation} -\begin{verbatim} +\begin{lstlisting} >>> first = 'throat' >>> second = 'warbler' >>> first + second throatwarbler -\end{verbatim} +\end{lstlisting} % -The {\tt *} operator also works on strings; it performs repetition. +The {\texttt *} operator also works on strings; it performs repetition. For example, \verb"'Spam'*3" is \verb"'SpamSpamSpam'". If one of the values is a string, the other has to be an integer. -This use of {\tt +} and {\tt *} makes sense by -analogy with addition and multiplication. Just as {\tt 4*3} is -equivalent to {\tt 4+4+4}, we expect \verb"'Spam'*3" to be the same as +This use of {\texttt +} and {\texttt *} makes sense by +analogy with addition and multiplication. Just as {\texttt 4*3} is +equivalent to {\texttt 4+4+4}, we expect \verb"'Spam'*3" to be the same as \verb"'Spam'+'Spam'+'Spam'", and it is. On the other hand, there is a significant way in which string concatenation and repetition are different from integer addition and multiplication. @@ -1682,21 +1785,21 @@ \section{Comments} For this reason, it is a good idea to add notes to your programs to explain in natural language what the program is doing. These notes are called -{\bf comments}, and they start with the \verb"#" symbol: +{\textbf comments}, and they start with the \verb"#" symbol: -\begin{verbatim} +\begin{lstlisting} # compute the percentage of the hour that has elapsed percentage = (minute * 100) / 60 -\end{verbatim} +\end{lstlisting} % In this case, the comment appears on a line by itself. You can also put comments at the end of a line: -\begin{verbatim} +\begin{lstlisting} percentage = (minute * 100) / 60 # percentage of an hour -\end{verbatim} +\end{lstlisting} % -Everything from the {\tt \#} to the end of the line is ignored---it +Everything from the {\texttt \#} to the end of the line is ignored---it has no effect on the execution of the program. Comments are most useful when they document non-obvious features of @@ -1705,15 +1808,15 @@ \section{Comments} This comment is redundant with the code and useless: -\begin{verbatim} +\begin{lstlisting} v = 5 # assign 5 to v -\end{verbatim} +\end{lstlisting} % This comment contains useful information that is not in the code: -\begin{verbatim} +\begin{lstlisting} v = 5 # velocity in meters/second. -\end{verbatim} +\end{lstlisting} % Good variable names can reduce the need for comments, but long names can make complex expressions hard to read, so there is @@ -1732,8 +1835,8 @@ \section{Debugging} \item[Syntax error:] ``Syntax'' refers to the structure of a program and the rules about that structure. For example, parentheses have - to come in matching pairs, so {\tt (1 + 2)} is legal, but {\tt 8)} - is a {\bf syntax error}. \index{syntax error} \index{error!syntax} + to come in matching pairs, so {\texttt (1 + 2)} is legal, but {\texttt 8)} + is a {\textbf syntax error}. \index{syntax error} \index{error!syntax} \index{error message} \index{syntax} @@ -1747,7 +1850,7 @@ \section{Debugging} \item[Runtime error:] The second type of error is a runtime error, so called because the error does not appear until after the program has - started running. These errors are also called {\bf exceptions} + started running. These errors are also called {\textbf exceptions} because they usually indicate that something exceptional (and bad) has happened. \index{runtime error} \index{error!runtime} \index{exception} \index{safe language} \index{language!safe} @@ -1785,7 +1888,7 @@ \section{Glossary} \index{state diagram} \item[keyword:] A reserved word that is used to parse a -program; you cannot use keywords like {\tt if}, {\tt def}, and {\tt while} as +program; you cannot use keywords like {\texttt if}, {\texttt def}, and {\texttt while} as variable names. \index{keyword} @@ -1849,6 +1952,7 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont Repeating my advice from the previous chapter, whenever you learn a new feature, you should try it out in interactive mode and make @@ -1856,11 +1960,11 @@ \section{Exercises} \begin{itemize} -\item We've seen that {\tt n = 42} is legal. What about {\tt 42 = n}? +\item We've seen that {\texttt n = 42} is legal. What about {\texttt 42 = n}? -\item How about {\tt x = y = 1}? +\item How about {\texttt x = y = 1}? -\item In some languages every statement ends with a semi-colon, {\tt ;}. +\item In some languages every statement ends with a semi-colon, {\texttt ;}. What happens if you put a semi-colon at the end of a Python statement? \item What if you put a period at the end of a statement? @@ -1874,6 +1978,7 @@ \section{Exercises} \begin{exercise} +\normalfont Practice using the Python interpreter as a calculator: \index{calculator} @@ -1900,7 +2005,7 @@ \section{Exercises} \chapter{Functions} \label{funcchap} -In the context of programming, a {\bf function} is a named sequence of +In the context of programming, a {\textbf function} is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can ``call'' the function by name. @@ -1910,70 +2015,70 @@ \section{Function calls} \label{functionchap} \index{function call} -We have already seen one example of a {\bf function call}: +We have already seen one example of a {\textbf function call}: -\begin{verbatim} +\begin{lstlisting} >>> type(42) -\end{verbatim} +\end{lstlisting} % -The name of the function is {\tt type}. The expression in parentheses -is called the {\bf argument} of the function. The result, for this +The name of the function is {\texttt type}. The expression in parentheses +is called the {\textbf argument} of the function. The result, for this function, is the type of the argument. \index{parentheses!argument in} It is common to say that a function ``takes'' an argument and ``returns'' -a result. The result is also called the {\bf return value}. +a result. The result is also called the {\textbf return value}. \index{argument} \index{return value} Python provides functions that convert values -from one type to another. The {\tt int} function takes any value and +from one type to another. The {\texttt int} function takes any value and converts it to an integer, if it can, or complains otherwise: \index{conversion!type} \index{type conversion} \index{int function} \index{function!int} -\begin{verbatim} +\begin{lstlisting} >>> int('32') 32 >>> int('Hello') ValueError: invalid literal for int(): Hello -\end{verbatim} +\end{lstlisting} % -{\tt int} can convert floating-point values to integers, but it +{\texttt int} can convert floating-point values to integers, but it doesn't round off; it chops off the fraction part: -\begin{verbatim} +\begin{lstlisting} >>> int(3.99999) 3 >>> int(-2.3) -2 -\end{verbatim} +\end{lstlisting} % -{\tt float} converts integers and strings to floating-point +{\texttt float} converts integers and strings to floating-point numbers: \index{float function} \index{function!float} -\begin{verbatim} +\begin{lstlisting} >>> float(32) 32.0 >>> float('3.14159') 3.14159 -\end{verbatim} +\end{lstlisting} % -Finally, {\tt str} converts its argument to a string: +Finally, {\texttt str} converts its argument to a string: \index{str function} \index{function!str} -\begin{verbatim} +\begin{lstlisting} >>> str(32) '32' >>> str(3.14159) '3.14159' -\end{verbatim} +\end{lstlisting} % \section{Math functions} @@ -1981,44 +2086,44 @@ \section{Math functions} \index{function, math} Python has a math module that provides most of the familiar -mathematical functions. A {\bf module} is a file that contains a +mathematical functions. A {\textbf module} is a file that contains a collection of related functions. \index{module} \index{module object} Before we can use the functions in a module, we have to import it with -an {\bf import statement}: +an {\textbf import statement}: -\begin{verbatim} +\begin{lstlisting} >>> import math -\end{verbatim} +\end{lstlisting} % -This statement creates a {\bf module object} named math. If +This statement creates a {\textbf module object} named math. If you display the module object, you get some information about it: -\begin{verbatim} +\begin{lstlisting} >>> math -\end{verbatim} +\end{lstlisting} % The module object contains the functions and variables defined in the module. To access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also -known as a period). This format is called {\bf dot notation}. +known as a period). This format is called {\textbf dot notation}. \index{dot notation} -\begin{verbatim} +\begin{lstlisting} >>> ratio = signal_power / noise_power >>> decibels = 10 * math.log10(ratio) >>> radians = 0.7 >>> height = math.sin(radians) -\end{verbatim} +\end{lstlisting} % The first example uses \verb"math.log10" to compute a signal-to-noise ratio in decibels (assuming that \verb"signal_power" and -\verb"noise_power" are defined). The math module also provides {\tt log}, -which computes logarithms base {\tt e}. +\verb"noise_power" are defined). The math module also provides {\texttt log}, +which computes logarithms base {\texttt e}. \index{log function} \index{function!log} \index{sine function} @@ -2026,20 +2131,20 @@ \section{Math functions} \index{trigonometric function} \index{function, trigonometric} -The second example finds the sine of {\tt radians}. The name of the -variable is a hint that {\tt sin} and the other trigonometric -functions ({\tt cos}, {\tt tan}, etc.) take arguments in radians. To +The second example finds the sine of {\texttt radians}. The name of the +variable is a hint that {\texttt sin} and the other trigonometric +functions ({\texttt cos}, {\texttt tan}, etc.) take arguments in radians. To convert from degrees to radians, divide by 180 and multiply by $\pi$: -\begin{verbatim} +\begin{lstlisting} >>> degrees = 45 >>> radians = degrees / 180.0 * math.pi >>> math.sin(radians) 0.707106781187 -\end{verbatim} +\end{lstlisting} % -The expression {\tt math.pi} gets the variable {\tt pi} from the math +The expression {\texttt math.pi} gets the variable {\texttt pi} from the math module. Its value is a floating-point approximation of $\pi$, accurate to about 15 digits. \index{pi} @@ -2050,10 +2155,10 @@ \section{Math functions} \index{sqrt function} \index{function!sqrt} -\begin{verbatim} +\begin{lstlisting} >>> math.sqrt(2) / 2.0 0.707106781187 -\end{verbatim} +\end{lstlisting} % \section{Composition} @@ -2064,19 +2169,19 @@ \section{Composition} combine them. One of the most useful features of programming languages is their -ability to take small building blocks and {\bf compose} them. For +ability to take small building blocks and {\textbf compose} them. For example, the argument of a function can be any kind of expression, including arithmetic operators: -\begin{verbatim} +\begin{lstlisting} x = math.sin(degrees / 360.0 * 2 * math.pi) -\end{verbatim} +\end{lstlisting} % And even function calls: -\begin{verbatim} +\begin{lstlisting} x = math.exp(math.log(x+1)) -\end{verbatim} +\end{lstlisting} % Almost anywhere you can put a value, you can put an arbitrary expression, with one exception: the left side of an assignment @@ -2084,11 +2189,11 @@ \section{Composition} side is a syntax error (we will see exceptions to this rule later). -\begin{verbatim} +\begin{lstlisting} >>> minutes = hours * 60 # right >>> hours * 60 = minutes # wrong! SyntaxError: can't assign to operator -\end{verbatim} +\end{lstlisting} % \index{SyntaxError} \index{exception!SyntaxError} @@ -2098,7 +2203,7 @@ \section{Adding new functions} So far, we have only been using the functions that come with Python, but it is also possible to add new functions. -A {\bf function definition} specifies the name of a new function and +A {\textbf function definition} specifies the name of a new function and the sequence of statements that run when the function is called. \index{function} \index{function definition} @@ -2106,13 +2211,13 @@ \section{Adding new functions} Here is an example: -\begin{verbatim} +\begin{lstlisting} def print_lyrics(): print("I'm a lumberjack, and I'm okay.") print("I sleep all night and I work all day.") -\end{verbatim} +\end{lstlisting} % -{\tt def} is a keyword that indicates that this is a function +{\texttt def} is a keyword that indicates that this is a function definition. The name of the function is \verb"print_lyrics". The rules for function names are the same as for variable names: letters, numbers and underscore are legal, but the first character @@ -2131,8 +2236,8 @@ \section{Adding new functions} \index{indentation} \index{colon} -The first line of the function definition is called the {\bf header}; -the rest is called the {\bf body}. The header has to end with a colon +The first line of the function definition is called the {\textbf header}; +the rest is called the {\textbf body}. The header has to end with a colon and the body has to be indented. By convention, indentation is always four spaces. The body can contain any number of statements. @@ -2148,59 +2253,59 @@ \section{Adding new functions} the ones in this sentence, are not legal in Python. If you type a function definition in interactive mode, the interpreter -prints dots ({\tt ...}) to let you know that the definition +prints dots ({\texttt ...}) to let you know that the definition isn't complete: \index{ellipses} -\begin{verbatim} +\begin{lstlisting} >>> def print_lyrics(): ... print("I'm a lumberjack, and I'm okay.") ... print("I sleep all night and I work all day.") ... -\end{verbatim} +\end{lstlisting} % To end the function, you have to enter an empty line. -Defining a function creates a {\bf function object}, which +Defining a function creates a {\textbf function object}, which has type \verb"function": \index{function type} \index{type!function} -\begin{verbatim} +\begin{lstlisting} >>> print(print_lyrics) >>> type(print_lyrics) -\end{verbatim} +\end{lstlisting} % The syntax for calling the new function is the same as for built-in functions: -\begin{verbatim} +\begin{lstlisting} >>> print_lyrics() I'm a lumberjack, and I'm okay. I sleep all night and I work all day. -\end{verbatim} +\end{lstlisting} % Once you have defined a function, you can use it inside another function. For example, to repeat the previous refrain, we could write a function called \verb"repeat_lyrics": -\begin{verbatim} +\begin{lstlisting} def repeat_lyrics(): print_lyrics() print_lyrics() -\end{verbatim} +\end{lstlisting} % And then call \verb"repeat_lyrics": -\begin{verbatim} +\begin{lstlisting} >>> repeat_lyrics() I'm a lumberjack, and I'm okay. I sleep all night and I work all day. I'm a lumberjack, and I'm okay. I sleep all night and I work all day. -\end{verbatim} +\end{lstlisting} % But that's not really how the song goes. @@ -2211,7 +2316,7 @@ \section{Definitions and uses} Pulling together the code fragments from the previous section, the whole program looks like this: -\begin{verbatim} +\begin{lstlisting} def print_lyrics(): print("I'm a lumberjack, and I'm okay.") print("I sleep all night and I work all day.") @@ -2221,7 +2326,7 @@ \section{Definitions and uses} print_lyrics() repeat_lyrics() -\end{verbatim} +\end{lstlisting} % This program contains two function definitions: \verb"print_lyrics" and \verb"repeat_lyrics". Function definitions get executed just like other @@ -2249,7 +2354,7 @@ \section{Flow of execution} To ensure that a function is defined before its first use, you have to know the order statements run in, which is -called the {\bf flow of execution}. +called the {\textbf flow of execution}. Execution always begins at the first statement of the program. Statements are run one at a time, in order from top to bottom. @@ -2287,28 +2392,28 @@ \section{Parameters and arguments} \index{function argument} Some of the functions we have seen require arguments. For -example, when you call {\tt math.sin} you pass a number +example, when you call {\texttt math.sin} you pass a number as an argument. Some functions take more than one argument: -{\tt math.pow} takes two, the base and the exponent. +{\texttt math.pow} takes two, the base and the exponent. Inside the function, the arguments are assigned to -variables called {\bf parameters}. Here is a definition for +variables called {\textbf parameters}. Here is a definition for a function that takes an argument: \index{parentheses!parameters in} -\begin{verbatim} +\begin{lstlisting} def print_twice(bruce): print(bruce) print(bruce) -\end{verbatim} +\end{lstlisting} % This function assigns the argument to a parameter -named {\tt bruce}. When the function is called, it prints the value of +named {\texttt bruce}. When the function is called, it prints the value of the parameter (whatever it is) twice. This function works with any value that can be printed. -\begin{verbatim} +\begin{lstlisting} >>> print_twice('Spam') Spam Spam @@ -2318,7 +2423,7 @@ \section{Parameters and arguments} >>> print_twice(math.pi) 3.14159265359 3.14159265359 -\end{verbatim} +\end{lstlisting} % The same rules of composition that apply to built-in functions also apply to programmer-defined functions, so we can use any kind of expression @@ -2327,75 +2432,75 @@ \section{Parameters and arguments} \index{programmer-defined function} \index{function!programmer defined} -\begin{verbatim} +\begin{lstlisting} >>> print_twice('Spam '*4) Spam Spam Spam Spam Spam Spam Spam Spam >>> print_twice(math.cos(math.pi)) -1.0 -1.0 -\end{verbatim} +\end{lstlisting} % The argument is evaluated before the function is called, so in the examples the expressions \verb"'Spam '*4" and -{\tt math.cos(math.pi)} are only evaluated once. +{\texttt math.cos(math.pi)} are only evaluated once. \index{argument} You can also use a variable as an argument: -\begin{verbatim} +\begin{lstlisting} >>> michael = 'Eric, the half a bee.' >>> print_twice(michael) Eric, the half a bee. Eric, the half a bee. -\end{verbatim} +\end{lstlisting} % -The name of the variable we pass as an argument ({\tt michael}) has -nothing to do with the name of the parameter ({\tt bruce}). It +The name of the variable we pass as an argument ({\texttt michael}) has +nothing to do with the name of the parameter ({\texttt bruce}). It doesn't matter what the value was called back home (in the caller); -here in \verb"print_twice", we call everybody {\tt bruce}. +here in \verb"print_twice", we call everybody {\texttt bruce}. \section{Variables and parameters are local} \index{local variable} \index{variable!local} -When you create a variable inside a function, it is {\bf local}, +When you create a variable inside a function, it is {\textbf local}, which means that it only exists inside the function. For example: \index{parentheses!parameters in} -\begin{verbatim} +\begin{lstlisting} def cat_twice(part1, part2): cat = part1 + part2 print_twice(cat) -\end{verbatim} +\end{lstlisting} % This function takes two arguments, concatenates them, and prints the result twice. Here is an example that uses it: \index{concatenation} -\begin{verbatim} +\begin{lstlisting} >>> line1 = 'Bing tiddle ' >>> line2 = 'tiddle bang.' >>> cat_twice(line1, line2) Bing tiddle tiddle bang. Bing tiddle tiddle bang. -\end{verbatim} +\end{lstlisting} % -When \verb"cat_twice" terminates, the variable {\tt cat} +When \verb"cat_twice" terminates, the variable {\texttt cat} is destroyed. If we try to print it, we get an exception: \index{NameError} \index{exception!NameError} -\begin{verbatim} +\begin{lstlisting} >>> print(cat) NameError: name 'cat' is not defined -\end{verbatim} +\end{lstlisting} % Parameters are also local. For example, outside \verb"print_twice", there is no -such thing as {\tt bruce}. +such thing as {\texttt bruce}. \index{parameter} @@ -2406,13 +2511,13 @@ \section{Stack diagrams} \index{frame} To keep track of which variables can be used where, it is sometimes -useful to draw a {\bf stack diagram}. Like state diagrams, stack +useful to draw a {\textbf stack diagram}. Like state diagrams, stack diagrams show the value of each variable, but they also show the function each variable belongs to. \index{stack diagram} \index{diagram!stack} -Each function is represented by a {\bf frame}. A frame is a box with +Each function is represented by a {\textbf frame}. A frame is a box with the name of a function beside it and the parameters and variables of the function inside it. The stack diagram for the previous example is shown in Figure~\ref{fig.stack}. @@ -2435,19 +2540,19 @@ \section{Stack diagrams} \index{main} Each parameter refers to the same value as its corresponding -argument. So, {\tt part1} has the same value as -{\tt line1}, {\tt part2} has the same value as {\tt line2}, -and {\tt bruce} has the same value as {\tt cat}. +argument. So, {\texttt part1} has the same value as +{\texttt line1}, {\texttt part2} has the same value as {\texttt line2}, +and {\texttt bruce} has the same value as {\texttt cat}. If an error occurs during a function call, Python prints the name of the function, the name of the function that called it, and the name of the function that called {\em that}, all the way back to \verb"__main__". -For example, if you try to access {\tt cat} from within -\verb"print_twice", you get a {\tt NameError}: +For example, if you try to access {\texttt cat} from within +\verb"print_twice", you get a {\texttt NameError}: -\begin{verbatim} +\begin{lstlisting} Traceback (innermost last): File "test.py", line 13, in __main__ cat_twice(line1, line2) @@ -2456,9 +2561,9 @@ \section{Stack diagrams} File "test.py", line 9, in print_twice print(cat) NameError: name 'cat' is not defined -\end{verbatim} +\end{lstlisting} % -This list of functions is called a {\bf traceback}. It tells you what +This list of functions is called a {\textbf traceback}. It tells you what program file the error occurred in, and what line, and what functions were executing at the time. It also shows the line of code that caused the error. @@ -2476,34 +2581,34 @@ \section{Fruitful functions and void functions} \index{function, void} Some of the functions we have used, such as the math functions, return -results; for lack of a better name, I call them {\bf fruitful +results; for lack of a better name, I call them {\textbf fruitful functions}. Other functions, like \verb"print_twice", perform an -action but don't return a value. They are called {\bf void +action but don't return a value. They are called {\textbf void functions}. When you call a fruitful function, you almost always want to do something with the result; for example, you might assign it to a variable or use it as part of an expression: -\begin{verbatim} +\begin{lstlisting} x = math.cos(radians) golden = (math.sqrt(5) + 1) / 2 -\end{verbatim} +\end{lstlisting} % When you call a function in interactive mode, Python displays the result: -\begin{verbatim} +\begin{lstlisting} >>> math.sqrt(5) 2.2360679774997898 -\end{verbatim} +\end{lstlisting} % But in a script, if you call a fruitful function all by itself, the return value is lost forever! -\begin{verbatim} +\begin{lstlisting} math.sqrt(5) -\end{verbatim} +\end{lstlisting} % This script computes the square root of 5, but since it doesn't store or display the result, it is not very useful. @@ -2513,25 +2618,25 @@ \section{Fruitful functions and void functions} Void functions might display something on the screen or have some other effect, but they don't have a return value. If you assign the result to a variable, you get a special value called -{\tt None}. +{\texttt None}. \index{None special value} \index{special value!None} -\begin{verbatim} +\begin{lstlisting} >>> result = print_twice('Bing') Bing Bing >>> print(result) None -\end{verbatim} +\end{lstlisting} % -The value {\tt None} is not the same as the string \verb"'None'". +The value {\texttt None} is not the same as the string \verb"'None'". It is a special value that has its own type: -\begin{verbatim} +\begin{lstlisting} >>> type(None) -\end{verbatim} +\end{lstlisting} % The functions we have written so far are all void. We will start writing fruitful functions in a few chapters. @@ -2651,10 +2756,10 @@ \section{Glossary} \item[fruitful function:] A function that returns a value. \index{fruitful function} -\item[void function:] A function that always returns {\tt None}. +\item[void function:] A function that always returns {\texttt None}. \index{void function} -\item[{\tt None}:] A special value returned by void functions. +\item[{\texttt None}:] A special value returned by void functions. \index{None special value} \index{special value!None} @@ -2667,7 +2772,7 @@ \section{Glossary} \index{import statement} \index{statement!import} -\item[module object:] A value created by an {\tt import} statement +\item[module object:] A value created by an {\texttt import} statement that provides access to the values defined in a module. \index{module} @@ -2703,27 +2808,29 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont \index{len function} \index{function!len} Write a function named \verb"right_justify" that takes a string -named {\tt s} as a parameter and prints the string with enough +named {\texttt s} as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display. -\begin{verbatim} +\begin{lstlisting} >>> right_justify('monty') monty -\end{verbatim} +\end{lstlisting} Hint: Use string concatenation and repetition. Also, -Python provides a built-in function called {\tt len} that +Python provides a built-in function called {\texttt len} that returns the length of a string, so the value of \verb"len('monty')" is 5. \end{exercise} \begin{exercise} +\normalfont \index{function object} \index{object!function} @@ -2731,21 +2838,21 @@ \section{Exercises} or pass as an argument. For example, \verb"do_twice" is a function that takes a function object as an argument and calls it twice: -\begin{verbatim} +\begin{lstlisting} def do_twice(f): f() f() -\end{verbatim} +\end{lstlisting} Here's an example that uses \verb"do_twice" to call a function named \verb"print_spam" twice. -\begin{verbatim} +\begin{lstlisting} def print_spam(): print('spam') do_twice(print_spam) -\end{verbatim} +\end{lstlisting} \begin{enumerate} @@ -2776,6 +2883,7 @@ \section{Exercises} \begin{exercise} +\normalfont Note: This exercise should be done using only the statements and other features we have learned so @@ -2786,7 +2894,7 @@ \section{Exercises} \item Write a function that draws a grid like the following: \index{grid} -\begin{verbatim} +\begin{lstlisting} + - - - - + - - - - + | | | | | | @@ -2798,26 +2906,26 @@ \section{Exercises} | | | | | | + - - - - + - - - - + -\end{verbatim} +\end{lstlisting} % Hint: to print more than one value on a line, you can print a comma-separated sequence of values: -\begin{verbatim} +\begin{lstlisting} print('+', '-') -\end{verbatim} +\end{lstlisting} % -By default, {\tt print} advances to the next line, but you +By default, {\texttt print} advances to the next line, but you can override that behavior and put a space at the end, like this: -\begin{verbatim} +\begin{lstlisting} print('+', end=' ') print('-') -\end{verbatim} +\end{lstlisting} % The output of these statements is \verb"'+ -'". -A {\tt print} statement with no argument ends the current line and +A {\texttt print} statement with no argument ends the current line and goes to the next line. \item Write a function that draws a similar grid @@ -2841,8 +2949,8 @@ \chapter{Case study: interface design} This chapter presents a case study that demonstrates a process for designing functions that work together. -It introduces the {\tt turtle} module, which allows you to -create images using turtle graphics. The {\tt turtle} module is +It introduces the {\texttt turtle} module, which allows you to +create images using turtle graphics. The {\texttt turtle} module is included in most Python installations, but if you are running Python using PythonAnywhere, you won't be able to run the turtle examples (at least you couldn't when I wrote this). @@ -2859,80 +2967,80 @@ \chapter{Case study: interface design} \section{The turtle module} \label{turtle} -To check whether you have the {\tt turtle} module, open the Python +To check whether you have the {\texttt turtle} module, open the Python interpreter and type -\begin{verbatim} +\begin{lstlisting} >>> import turtle >>> bob = turtle.Turtle() -\end{verbatim} +\end{lstlisting} When you run this code, it should create a new window with small arrow that represents the turtle. Close the window. -Create a file named {\tt mypolygon.py} and type in the following +Create a file named {\texttt mypolygon.py} and type in the following code: -\begin{verbatim} +\begin{lstlisting} import turtle bob = turtle.Turtle() print(bob) turtle.mainloop() -\end{verbatim} +\end{lstlisting} % -The {\tt turtle} module (with a lowercase 't') provides a function -called {\tt Turtle} (with an uppercase 'T') that creates a Turtle -object, which we assign to a variable named {\tt bob}. -Printing {\tt bob} displays something like: +The {\texttt turtle} module (with a lowercase 't') provides a function +called {\texttt Turtle} (with an uppercase 'T') that creates a Turtle +object, which we assign to a variable named {\texttt bob}. +Printing {\texttt bob} displays something like: -\begin{verbatim} +\begin{lstlisting} -\end{verbatim} +\end{lstlisting} % -This means that {\tt bob} refers to an object with type -{\tt Turtle} -as defined in module {\tt turtle}. +This means that {\texttt bob} refers to an object with type +{\texttt Turtle} +as defined in module {\texttt turtle}. \verb"mainloop" tells the window to wait for the user to do something, although in this case there's not much for the user to do except close the window. -Once you create a Turtle, you can call a {\bf method} to move it +Once you create a Turtle, you can call a {\textbf method} to move it around the window. A method is similar to a function, but it uses slightly different syntax. For example, to move the turtle forward: -\begin{verbatim} +\begin{lstlisting} bob.fd(100) -\end{verbatim} +\end{lstlisting} % -The method, {\tt fd}, is associated with the turtle -object we're calling {\tt bob}. -Calling a method is like making a request: you are asking {\tt bob} +The method, {\texttt fd}, is associated with the turtle +object we're calling {\texttt bob}. +Calling a method is like making a request: you are asking {\texttt bob} to move forward. -The argument of {\tt fd} is a distance in pixels, so the actual +The argument of {\texttt fd} is a distance in pixels, so the actual size depends on your display. -Other methods you can call on a Turtle are {\tt bk} to move -backward, {\tt lt} for left turn, and {\tt rt} right turn. The -argument for {\tt lt} and {\tt rt} is an angle in degrees. +Other methods you can call on a Turtle are {\texttt bk} to move +backward, {\texttt lt} for left turn, and {\texttt rt} right turn. The +argument for {\texttt lt} and {\texttt rt} is an angle in degrees. Also, each Turtle is holding a pen, which is either down or up; if the pen is down, the Turtle leaves -a trail when it moves. The methods {\tt pu} and {\tt pd} +a trail when it moves. The methods {\texttt pu} and {\texttt pd} stand for ``pen up'' and ``pen down''. To draw a right angle, add these lines to the program -(after creating {\tt bob} and before calling \verb"mainloop"): +(after creating {\texttt bob} and before calling \verb"mainloop"): -\begin{verbatim} +\begin{lstlisting} bob.fd(100) bob.lt(90) bob.fd(100) -\end{verbatim} +\end{lstlisting} % -When you run this program, you should see {\tt bob} move east and then +When you run this program, you should see {\texttt bob} move east and then north, leaving two line segments behind. Now modify the program to draw a square. Don't go on until @@ -2946,7 +3054,7 @@ \section{Simple repetition} Chances are you wrote something like this: -\begin{verbatim} +\begin{lstlisting} bob.fd(100) bob.lt(90) @@ -2957,45 +3065,45 @@ \section{Simple repetition} bob.lt(90) bob.fd(100) -\end{verbatim} +\end{lstlisting} % -We can do the same thing more concisely with a {\tt for} statement. -Add this example to {\tt mypolygon.py} and run it again: +We can do the same thing more concisely with a {\texttt for} statement. +Add this example to {\texttt mypolygon.py} and run it again: \index{for loop} \index{loop!for} \index{statement!for} -\begin{verbatim} +\begin{lstlisting} for i in range(4): print('Hello!') -\end{verbatim} +\end{lstlisting} % You should see something like this: -\begin{verbatim} +\begin{lstlisting} Hello! Hello! Hello! Hello! -\end{verbatim} +\end{lstlisting} % -This is the simplest use of the {\tt for} statement; we will see +This is the simplest use of the {\texttt for} statement; we will see more later. But that should be enough to let you rewrite your square-drawing program. Don't go on until you do. -Here is a {\tt for} statement that draws a square: +Here is a {\texttt for} statement that draws a square: -\begin{verbatim} +\begin{lstlisting} for i in range(4): bob.fd(100) bob.lt(90) -\end{verbatim} +\end{lstlisting} % -The syntax of a {\tt for} statement is similar to a function +The syntax of a {\texttt for} statement is similar to a function definition. It has a header that ends with a colon and an indented body. The body can contain any number of statements. -A {\tt for} statement is also called a {\bf loop} because +A {\texttt for} statement is also called a {\textbf loop} because the flow of execution runs through the body and then loops back to the top. In this case, it runs the body four times. \index{loop} @@ -3019,38 +3127,38 @@ \section{Exercises} \begin{enumerate} -\item Write a function called {\tt square} that takes a parameter -named {\tt t}, which is a turtle. It should use the turtle to draw +\item Write a function called {\texttt square} that takes a parameter +named {\texttt t}, which is a turtle. It should use the turtle to draw a square. -Write a function call that passes {\tt bob} as an argument to -{\tt square}, and then run the program again. +Write a function call that passes {\texttt bob} as an argument to +{\texttt square}, and then run the program again. -\item Add another parameter, named {\tt length}, to {\tt square}. -Modify the body so length of the sides is {\tt length}, and then +\item Add another parameter, named {\texttt length}, to {\texttt square}. +Modify the body so length of the sides is {\texttt length}, and then modify the function call to provide a second argument. Run the -program again. Test your program with a range of values for {\tt +program again. Test your program with a range of values for {\texttt length}. -\item Make a copy of {\tt square} and change the name to {\tt - polygon}. Add another parameter named {\tt n} and modify the body +\item Make a copy of {\texttt square} and change the name to {\texttt + polygon}. Add another parameter named {\texttt n} and modify the body so it draws an n-sided regular polygon. Hint: The exterior angles of an n-sided regular polygon are $360/n$ degrees. \index{polygon function} \index{function!polygon} -\item Write a function called {\tt circle} that takes a turtle, -{\tt t}, and radius, {\tt r}, as parameters and that draws an -approximate circle by calling {\tt polygon} with an appropriate +\item Write a function called {\texttt circle} that takes a turtle, +{\texttt t}, and radius, {\texttt r}, as parameters and that draws an +approximate circle by calling {\texttt polygon} with an appropriate length and number of sides. Test your function with a range of values -of {\tt r}. \index{circle function} \index{function!circle} +of {\texttt r}. \index{circle function} \index{function!circle} Hint: figure out the circumference of the circle and make sure that -{\tt length * n = circumference}. +{\texttt length * n = circumference}. -\item Make a more general version of {\tt circle} called {\tt arc} -that takes an additional parameter {\tt angle}, which determines -what fraction of a circle to draw. {\tt angle} is in units of -degrees, so when {\tt angle=360}, {\tt arc} should draw a complete +\item Make a more general version of {\texttt circle} called {\texttt arc} +that takes an additional parameter {\texttt angle}, which determines +what fraction of a circle to draw. {\texttt angle} is in units of +degrees, so when {\texttt angle=360}, {\texttt arc} should draw a complete circle. \index{arc function} \index{function!arc} @@ -3064,34 +3172,34 @@ \section{Encapsulation} into a function definition and then call the function, passing the turtle as a parameter. Here is a solution: -\begin{verbatim} +\begin{lstlisting} def square(t): for i in range(4): t.fd(100) t.lt(90) square(bob) -\end{verbatim} +\end{lstlisting} % -The innermost statements, {\tt fd} and {\tt lt} are indented twice to -show that they are inside the {\tt for} loop, which is inside the -function definition. The next line, {\tt square(bob)}, is flush with -the left margin, which indicates the end of both the {\tt for} loop +The innermost statements, {\texttt fd} and {\texttt lt} are indented twice to +show that they are inside the {\texttt for} loop, which is inside the +function definition. The next line, {\texttt square(bob)}, is flush with +the left margin, which indicates the end of both the {\texttt for} loop and the function definition. -Inside the function, {\tt t} refers to the same turtle {\tt bob}, so -{\tt t.lt(90)} has the same effect as {\tt bob.lt(90)}. In that +Inside the function, {\texttt t} refers to the same turtle {\texttt bob}, so +{\texttt t.lt(90)} has the same effect as {\texttt bob.lt(90)}. In that case, why not -call the parameter {\tt bob}? The idea is that {\tt t} can be any -turtle, not just {\tt bob}, so you could create a second turtle and -pass it as an argument to {\tt square}: +call the parameter {\texttt bob}? The idea is that {\texttt t} can be any +turtle, not just {\texttt bob}, so you could create a second turtle and +pass it as an argument to {\texttt square}: -\begin{verbatim} +\begin{lstlisting} alice = turtle.Turtle() square(alice) -\end{verbatim} +\end{lstlisting} % -Wrapping a piece of code up in a function is called {\bf +Wrapping a piece of code up in a function is called {\textbf encapsulation}. One of the benefits of encapsulation is that it attaches a name to the code, which serves as a kind of documentation. Another advantage is that if you re-use the code, it is more concise @@ -3101,29 +3209,29 @@ \section{Encapsulation} \section{Generalization} -The next step is to add a {\tt length} parameter to {\tt square}. +The next step is to add a {\texttt length} parameter to {\texttt square}. Here is a solution: -\begin{verbatim} +\begin{lstlisting} def square(t, length): for i in range(4): t.fd(length) t.lt(90) square(bob, 100) -\end{verbatim} +\end{lstlisting} % -Adding a parameter to a function is called {\bf generalization} +Adding a parameter to a function is called {\textbf generalization} because it makes the function more general: in the previous version, the square is always the same size; in this version it can be any size. \index{generalization} The next step is also a generalization. Instead of drawing -squares, {\tt polygon} draws regular polygons with any number of +squares, {\texttt polygon} draws regular polygons with any number of sides. Here is a solution: -\begin{verbatim} +\begin{lstlisting} def polygon(t, n, length): angle = 360 / n for i in range(n): @@ -3131,13 +3239,13 @@ \section{Generalization} t.lt(angle) polygon(bob, 7, 70) -\end{verbatim} +\end{lstlisting} % This example draws a 7-sided polygon with side length 70. -If you are using Python 2, the value of {\tt angle} might be off +If you are using Python 2, the value of {\texttt angle} might be off because of integer division. A simple solution is to compute -{\tt angle = 360.0 / n}. Because the numerator is a floating-point +{\texttt angle = 360.0 / n}. Because the numerator is a floating-point number, the result is floating point. \index{Python 2} @@ -3146,13 +3254,13 @@ \section{Generalization} it is often a good idea to include the names of the parameters in the argument list: -\begin{verbatim} +\begin{lstlisting} polygon(bob, n=7, length=70) -\end{verbatim} +\end{lstlisting} % -These are called {\bf keyword arguments} because they include +These are called {\textbf keyword arguments} because they include the parameter names as ``keywords'' (not to be confused with -Python keywords like {\tt while} and {\tt def}). +Python keywords like {\texttt while} and {\texttt def}). \index{keyword argument} \index{argument!keyword} @@ -3163,11 +3271,11 @@ \section{Generalization} \section{Interface design} -The next step is to write {\tt circle}, which takes a radius, -{\tt r}, as a parameter. Here is a simple solution that uses -{\tt polygon} to draw a 50-sided polygon: +The next step is to write {\texttt circle}, which takes a radius, +{\texttt r}, as a parameter. Here is a simple solution that uses +{\texttt polygon} to draw a 50-sided polygon: -\begin{verbatim} +\begin{lstlisting} import math def circle(t, r): @@ -3175,69 +3283,69 @@ \section{Interface design} n = 50 length = circumference / n polygon(t, n, length) -\end{verbatim} +\end{lstlisting} % The first line computes the circumference of a circle with radius -{\tt r} using the formula $2 \pi r$. Since we use {\tt math.pi}, we -have to import {\tt math}. By convention, {\tt import} statements +{\texttt r} using the formula $2 \pi r$. Since we use {\texttt math.pi}, we +have to import {\texttt math}. By convention, {\texttt import} statements are usually at the beginning of the script. -{\tt n} is the number of line segments in our approximation of a circle, -so {\tt length} is the length of each segment. Thus, {\tt polygon} -draws a 50-sided polygon that approximates a circle with radius {\tt r}. +{\texttt n} is the number of line segments in our approximation of a circle, +so {\texttt length} is the length of each segment. Thus, {\texttt polygon} +draws a 50-sided polygon that approximates a circle with radius {\texttt r}. -One limitation of this solution is that {\tt n} is a constant, which +One limitation of this solution is that {\texttt n} is a constant, which means that for very big circles, the line segments are too long, and for small circles, we waste time drawing very small segments. One -solution would be to generalize the function by taking {\tt n} as -a parameter. This would give the user (whoever calls {\tt circle}) +solution would be to generalize the function by taking {\texttt n} as +a parameter. This would give the user (whoever calls {\texttt circle}) more control, but the interface would be less clean. \index{interface} -The {\bf interface} of a function is a summary of how it is used: what +The {\textbf interface} of a function is a summary of how it is used: what are the parameters? What does the function do? And what is the return value? An interface is ``clean'' if it allows the caller to do what they want without dealing with unnecessary details. -In this example, {\tt r} belongs in the interface because it -specifies the circle to be drawn. {\tt n} is less appropriate +In this example, {\texttt r} belongs in the interface because it +specifies the circle to be drawn. {\texttt n} is less appropriate because it pertains to the details of {\em how} the circle should be rendered. Rather than clutter up the interface, it is better -to choose an appropriate value of {\tt n} -depending on {\tt circumference}: +to choose an appropriate value of {\texttt n} +depending on {\texttt circumference}: -\begin{verbatim} +\begin{lstlisting} def circle(t, r): circumference = 2 * math.pi * r n = int(circumference / 3) + 3 length = circumference / n polygon(t, n, length) -\end{verbatim} +\end{lstlisting} % -Now the number of segments is an integer near {\tt circumference/3}, +Now the number of segments is an integer near {\texttt circumference/3}, so the length of each segment is approximately 3, which is small enough that the circles look good, but big enough to be efficient, and acceptable for any size circle. -Adding 3 to {\tt n} guarantees that the polygon has at least 3 sides. +Adding 3 to {\texttt n} guarantees that the polygon has at least 3 sides. \section{Refactoring} \label{refactoring} \index{refactoring} -When I wrote {\tt circle}, I was able to re-use {\tt polygon} +When I wrote {\texttt circle}, I was able to re-use {\texttt polygon} because a many-sided polygon is a good approximation of a circle. -But {\tt arc} is not as cooperative; we can't use {\tt polygon} -or {\tt circle} to draw an arc. +But {\texttt arc} is not as cooperative; we can't use {\texttt polygon} +or {\texttt circle} to draw an arc. One alternative is to start with a copy -of {\tt polygon} and transform it into {\tt arc}. The result +of {\texttt polygon} and transform it into {\texttt arc}. The result might look like this: -\begin{verbatim} +\begin{lstlisting} def arc(t, r, angle): arc_length = 2 * math.pi * r * angle / 360 n = int(arc_length / 3) + 1 @@ -3247,24 +3355,24 @@ \section{Refactoring} for i in range(n): t.fd(step_length) t.lt(step_angle) -\end{verbatim} +\end{lstlisting} % -The second half of this function looks like {\tt polygon}, but we -can't re-use {\tt polygon} without changing the interface. We could -generalize {\tt polygon} to take an angle as a third argument, -but then {\tt polygon} would no longer be an appropriate name! -Instead, let's call the more general function {\tt polyline}: +The second half of this function looks like {\texttt polygon}, but we +can't re-use {\texttt polygon} without changing the interface. We could +generalize {\texttt polygon} to take an angle as a third argument, +but then {\texttt polygon} would no longer be an appropriate name! +Instead, let's call the more general function {\texttt polyline}: -\begin{verbatim} +\begin{lstlisting} def polyline(t, n, length, angle): for i in range(n): t.fd(length) t.lt(angle) -\end{verbatim} +\end{lstlisting} % -Now we can rewrite {\tt polygon} and {\tt arc} to use {\tt polyline}: +Now we can rewrite {\texttt polygon} and {\texttt arc} to use {\texttt polyline}: -\begin{verbatim} +\begin{lstlisting} def polygon(t, n, length): angle = 360.0 / n polyline(t, n, length, angle) @@ -3275,22 +3383,22 @@ \section{Refactoring} step_length = arc_length / n step_angle = float(angle) / n polyline(t, n, step_length, step_angle) -\end{verbatim} +\end{lstlisting} % -Finally, we can rewrite {\tt circle} to use {\tt arc}: +Finally, we can rewrite {\texttt circle} to use {\texttt arc}: -\begin{verbatim} +\begin{lstlisting} def circle(t, r): arc(t, r, 360) -\end{verbatim} +\end{lstlisting} % This process---rearranging a program to improve -interfaces and facilitate code re-use---is called {\bf refactoring}. -In this case, we noticed that there was similar code in {\tt arc} and -{\tt polygon}, so we ``factored it out'' into {\tt polyline}. +interfaces and facilitate code re-use---is called {\textbf refactoring}. +In this case, we noticed that there was similar code in {\texttt arc} and +{\texttt polygon}, so we ``factored it out'' into {\texttt polyline}. \index{refactoring} -If we had planned ahead, we might have written {\tt polyline} first +If we had planned ahead, we might have written {\texttt polyline} first and avoided refactoring, but often you don't know enough at the beginning of a project to design all the interfaces. Once you start coding, you understand the problem better. Sometimes refactoring is a @@ -3300,7 +3408,7 @@ \section{Refactoring} \section{A development plan} \index{development plan!encapsulation and generalization} -A {\bf development plan} is a process for writing programs. The +A {\textbf development plan} is a process for writing programs. The process we used in this case study is ``encapsulation and generalization''. The steps of this process are: @@ -3332,11 +3440,11 @@ \section{docstring} \label{docstring} \index{docstring} -A {\bf docstring} is a string at the beginning of a function that +A {\textbf docstring} is a string at the beginning of a function that explains the interface (``doc'' is short for ``documentation''). Here is an example: -\begin{verbatim} +\begin{lstlisting} def polyline(t, n, length, angle): """Draws n line segments with the given length and angle (in degrees) between them. t is a turtle. @@ -3344,7 +3452,7 @@ \section{docstring} for i in range(n): t.fd(length) t.lt(angle) -\end{verbatim} +\end{lstlisting} % By convention, all docstrings are triple-quoted strings, also known as multiline strings because the triple quotes allow the string @@ -3376,15 +3484,15 @@ \section{Debugging} The caller agrees to provide certain parameters and the function agrees to do certain work. -For example, {\tt polyline} requires four arguments: {\tt t} has to be -a Turtle; {\tt n} has to be an -integer; {\tt length} should be a positive number; and {\tt +For example, {\texttt polyline} requires four arguments: {\texttt t} has to be +a Turtle; {\texttt n} has to be an +integer; {\texttt length} should be a positive number; and {\texttt angle} has to be a number, which is understood to be in degrees. -These requirements are called {\bf preconditions} because they +These requirements are called {\textbf preconditions} because they are supposed to be true before the function starts executing. Conversely, conditions at the end of the function are -{\bf postconditions}. Postconditions include the intended +{\textbf postconditions}. Postconditions include the intended effect of the function (like drawing line segments) and any side effects (like moving the Turtle or making other changes). \index{precondition} @@ -3453,6 +3561,7 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont Download the code in this chapter from \url{http://thinkpython2.com/code/polygon.py}. @@ -3460,11 +3569,11 @@ \section{Exercises} \begin{enumerate} \item Draw a stack diagram that shows the state of the program -while executing {\tt circle(bob, radius)}. You can do the -arithmetic by hand or add {\tt print} statements to the code. +while executing {\texttt circle(bob, radius)}. You can do the +arithmetic by hand or add {\texttt print} statements to the code. \index{stack diagram} -\item The version of {\tt arc} in Section~\ref{refactoring} is not +\item The version of {\texttt arc} in Section~\ref{refactoring} is not very accurate because the linear approximation of the circle is always outside the true circle. As a result, the Turtle ends up a few pixels away from the correct @@ -3484,6 +3593,7 @@ \section{Exercises} \end{figure} \begin{exercise} +\normalfont \index{flower} Write an appropriately general set of functions that @@ -3503,6 +3613,7 @@ \section{Exercises} \begin{exercise} +\normalfont \index{pie} Write an appropriately general set of functions that @@ -3513,6 +3624,7 @@ \section{Exercises} \end{exercise} \begin{exercise} +\normalfont \index{alphabet} \index{turtle typewriter} \index{typewriter, turtle} @@ -3524,7 +3636,7 @@ \section{Exercises} You should write one function for each letter, with names \verb"draw_a", \verb"draw_b", etc., and put your functions -in a file named {\tt letters.py}. You can download a +in a file named {\texttt letters.py}. You can download a ``turtle typewriter'' from \url{http://thinkpython2.com/code/typewriter.py} to help you test your code. @@ -3535,6 +3647,7 @@ \section{Exercises} \end{exercise} \begin{exercise} +\normalfont Read about spirals at \url{http://en.wikipedia.org/wiki/Spiral}; then write a program that draws an Archimedian spiral (or one of the other @@ -3547,7 +3660,7 @@ \section{Exercises} \chapter{Conditionals and recursion} -The main topic of this chapter is the {\tt if} statement, which +The main topic of this chapter is the {\texttt if} statement, which executes different code depending on the state of the program. But first I want to introduce two new operators: floor division and modulus. @@ -3555,36 +3668,36 @@ \chapter{Conditionals and recursion} \section{Floor division and modulus} -The {\bf floor division} operator, \verb"//", divides +The {\textbf floor division} operator, \verb"//", divides two numbers and rounds down to an integer. For example, suppose the run time of a movie is 105 minutes. You might want to know how long that is in hours. Conventional division returns a floating-point number: -\begin{verbatim} +\begin{lstlisting} >>> minutes = 105 >>> minutes / 60 1.75 -\end{verbatim} +\end{lstlisting} But we don't normally write hours with decimal points. Floor division returns the integer number of hours, dropping the fraction part: -\begin{verbatim} +\begin{lstlisting} >>> minutes = 105 >>> hours = minutes // 60 >>> hours 1 -\end{verbatim} +\end{lstlisting} To get the remainder, you could subtract off one hour in minutes: -\begin{verbatim} +\begin{lstlisting} >>> remainder = minutes - hours * 60 >>> remainder 45 -\end{verbatim} +\end{lstlisting} \index{floor division} \index{floating-point division} @@ -3593,29 +3706,29 @@ \section{Floor division and modulus} \index{modulus operator} \index{operator!modulus} -An alternative is to use the {\bf modulus operator}, \verb"%", which +An alternative is to use the {\textbf modulus operator}, \verb"%", which divides two numbers and returns the remainder. -\begin{verbatim} +\begin{lstlisting} >>> remainder = minutes % 60 >>> remainder 45 -\end{verbatim} +\end{lstlisting} % The modulus operator is more useful than it seems. For example, you can check whether one number is divisible by another---if -{\tt x \% y} is zero, then {\tt x} is divisible by {\tt y}. +{\texttt x \% y} is zero, then {\texttt x} is divisible by {\texttt y}. \index{divisibility} Also, you can extract the right-most digit -or digits from a number. For example, {\tt x \% 10} yields the -right-most digit of {\tt x} (in base 10). Similarly {\tt x \% 100} +or digits from a number. For example, {\texttt x \% 10} yields the +right-most digit of {\texttt x} (in base 10). Similarly {\texttt x \% 100} yields the last two digits. If you are using Python 2, division works differently. The division operator, \verb"/", performs floor division if both operands are integers, and floating-point division if either -operand is a {\tt float}. +operand is a {\texttt float}. \index{Python 2} @@ -3625,20 +3738,20 @@ \section{Boolean expressions} \index{logical operator} \index{operator!logical} -A {\bf boolean expression} is an expression that is either true +A {\textbf boolean expression} is an expression that is either true or false. The following examples use the -operator {\tt ==}, which compares two operands and produces -{\tt True} if they are equal and {\tt False} otherwise: +operator {\texttt ==}, which compares two operands and produces +{\texttt True} if they are equal and {\texttt False} otherwise: -\begin{verbatim} +\begin{lstlisting} >>> 5 == 5 True >>> 5 == 6 False -\end{verbatim} +\end{lstlisting} % -{\tt True} and {\tt False} are special -values that belong to the type {\tt bool}; they are not strings: +{\texttt True} and {\texttt False} are special +values that belong to the type {\texttt bool}; they are not strings: \index{True special value} \index{False special value} \index{special value!True} @@ -3646,30 +3759,30 @@ \section{Boolean expressions} \index{bool type} \index{type!bool} -\begin{verbatim} +\begin{lstlisting} >>> type(True) >>> type(False) -\end{verbatim} +\end{lstlisting} % -The {\tt ==} operator is one of the {\bf relational operators}; the +The {\texttt ==} operator is one of the {\textbf relational operators}; the others are: -\begin{verbatim} +\begin{lstlisting} x != y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y -\end{verbatim} +\end{lstlisting} % Although these operations are probably familiar to you, the Python symbols are different from the mathematical symbols. A common error -is to use a single equal sign ({\tt =}) instead of a double equal sign -({\tt ==}). Remember that {\tt =} is an assignment operator and -{\tt ==} is a relational operator. There is no such thing as -{\tt =<} or {\tt =>}. +is to use a single equal sign ({\texttt =}) instead of a double equal sign +({\texttt ==}). Remember that {\texttt =} is an assignment operator and +{\texttt ==} is a relational operator. There is no such thing as +{\texttt =<} or {\texttt =>}. \index{relational operator} \index{operator!relational} @@ -3678,10 +3791,10 @@ \section{Boolean expressions} \index{logical operator} \index{operator!logical} -There are three {\bf logical operators}: {\tt and}, {\tt -or}, and {\tt not}. The semantics (meaning) of these operators is +There are three {\textbf logical operators}: {\texttt and}, {\texttt +or}, and {\texttt not}. The semantics (meaning) of these operators is similar to their meaning in English. For example, -{\tt x > 0 and x < 10} is true only if {\tt x} is greater than 0 +{\texttt x > 0 and x < 10} is true only if {\texttt x} is greater than 0 {\em and} less than 10. \index{and operator} \index{or operator} @@ -3690,22 +3803,22 @@ \section{Boolean expressions} \index{operator!or} \index{operator!not} -{\tt n\%2 == 0 or n\%3 == 0} is true if {\em either or both} of the +{\texttt n\%2 == 0 or n\%3 == 0} is true if {\em either or both} of the conditions is true, that is, if the number is divisible by 2 {\em or} 3. -Finally, the {\tt not} operator negates a boolean -expression, so {\tt not (x > y)} is true if {\tt x > y} is false, -that is, if {\tt x} is less than or equal to {\tt y}. +Finally, the {\texttt not} operator negates a boolean +expression, so {\texttt not (x > y)} is true if {\texttt x > y} is false, +that is, if {\texttt x} is less than or equal to {\texttt y}. Strictly speaking, the operands of the logical operators should be boolean expressions, but Python is not very strict. -Any nonzero number is interpreted as {\tt True}: +Any nonzero number is interpreted as {\texttt True}: -\begin{verbatim} +\begin{lstlisting} >>> 42 and True True -\end{verbatim} +\end{lstlisting} % This flexibility can be useful, but there are some subtleties to it that might be confusing. You might want to avoid it (unless @@ -3722,37 +3835,37 @@ \section{Conditional execution} \index{conditional execution} In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program -accordingly. {\bf Conditional statements} give us this ability. The -simplest form is the {\tt if} statement: +accordingly. {\textbf Conditional statements} give us this ability. The +simplest form is the {\texttt if} statement: -\begin{verbatim} +\begin{lstlisting} if x > 0: print('x is positive') -\end{verbatim} +\end{lstlisting} % -The boolean expression after {\tt if} is -called the {\bf condition}. If it is true, the indented +The boolean expression after {\texttt if} is +called the {\textbf condition}. If it is true, the indented statement runs. If not, nothing happens. \index{condition} \index{compound statement} \index{statement!compound} -{\tt if} statements have the same structure as function definitions: +{\texttt if} statements have the same structure as function definitions: a header followed by an indented body. Statements like this are -called {\bf compound statements}. +called {\textbf compound statements}. There is no limit on the number of statements that can appear in the body, but there has to be at least one. Occasionally, it is useful to have a body with no statements (usually as a place keeper for code you haven't written yet). In that -case, you can use the {\tt pass} statement, which does nothing. +case, you can use the {\texttt pass} statement, which does nothing. \index{pass statement} \index{statement!pass} -\begin{verbatim} +\begin{lstlisting} if x < 0: pass # TODO: need to handle negative values! -\end{verbatim} +\end{lstlisting} % \section{Alternative execution} @@ -3761,22 +3874,22 @@ \section{Alternative execution} \index{else keyword} \index{keyword!else} -A second form of the {\tt if} statement is ``alternative execution'', +A second form of the {\texttt if} statement is ``alternative execution'', in which there are two possibilities and the condition determines which one runs. The syntax looks like this: -\begin{verbatim} +\begin{lstlisting} if x % 2 == 0: print('x is even') else: print('x is odd') -\end{verbatim} +\end{lstlisting} % -If the remainder when {\tt x} is divided by 2 is 0, then we know that -{\tt x} is even, and the program displays an appropriate message. If +If the remainder when {\texttt x} is divided by 2 is 0, then we know that +{\texttt x} is even, and the program displays an appropriate message. If the condition is false, the second set of statements runs. Since the condition must be true or false, exactly one of the -alternatives will run. The alternatives are called {\bf +alternatives will run. The alternatives are called {\textbf branches}, because they are branches in the flow of execution. \index{branch} @@ -3787,33 +3900,33 @@ \section{Chained conditionals} \index{conditional!chained} Sometimes there are more than two possibilities and we need more than -two branches. One way to express a computation like that is a {\bf +two branches. One way to express a computation like that is a {\textbf chained conditional}: -\begin{verbatim} +\begin{lstlisting} if x < y: print('x is less than y') elif x > y: print('x is greater than y') else: print('x and y are equal') -\end{verbatim} +\end{lstlisting} % -{\tt elif} is an abbreviation of ``else if''. Again, exactly one -branch will run. There is no limit on the number of {\tt -elif} statements. If there is an {\tt else} clause, it has to be +{\texttt elif} is an abbreviation of ``else if''. Again, exactly one +branch will run. There is no limit on the number of {\texttt +elif} statements. If there is an {\texttt else} clause, it has to be at the end, but there doesn't have to be one. \index{elif keyword} \index{keyword!elif} -\begin{verbatim} +\begin{lstlisting} if choice == 'a': draw_a() elif choice == 'b': draw_b() elif choice == 'c': draw_c() -\end{verbatim} +\end{lstlisting} % Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is @@ -3829,7 +3942,7 @@ \section{Nested conditionals} One conditional can also be nested within another. We could have written the example in the previous section like this: -\begin{verbatim} +\begin{lstlisting} if x == y: print('x and y are equal') else: @@ -3837,42 +3950,42 @@ \section{Nested conditionals} print('x is less than y') else: print('x is greater than y') -\end{verbatim} +\end{lstlisting} % The outer conditional contains two branches. The first branch contains a simple statement. The second branch -contains another {\tt if} statement, which has two branches of its +contains another {\texttt if} statement, which has two branches of its own. Those two branches are both simple statements, although they could have been conditional statements as well. Although the indentation of the statements makes the structure -apparent, {\bf nested conditionals} become difficult to read very +apparent, {\textbf nested conditionals} become difficult to read very quickly. It is a good idea to avoid them when you can. Logical operators often provide a way to simplify nested conditional statements. For example, we can rewrite the following code using a single conditional: -\begin{verbatim} +\begin{lstlisting} if 0 < x: if x < 10: print('x is a positive single-digit number.') -\end{verbatim} +\end{lstlisting} % -The {\tt print} statement runs only if we make it past both -conditionals, so we can get the same effect with the {\tt and} operator: +The {\texttt print} statement runs only if we make it past both +conditionals, so we can get the same effect with the {\texttt and} operator: -\begin{verbatim} +\begin{lstlisting} if 0 < x and x < 10: print('x is a positive single-digit number.') -\end{verbatim} +\end{lstlisting} For this kind of condition, Python provides a more concise option: -\begin{verbatim} +\begin{lstlisting} if 0 < x < 10: print('x is a positive single-digit number.') -\end{verbatim} +\end{lstlisting} \section{Recursion} @@ -3885,91 +3998,91 @@ \section{Recursion} magical things a program can do. For example, look at the following function: -\begin{verbatim} +\begin{lstlisting} def countdown(n): if n <= 0: print('Blastoff!') else: print(n) countdown(n-1) -\end{verbatim} +\end{lstlisting} % -If {\tt n} is 0 or negative, it outputs the word, ``Blastoff!'' -Otherwise, it outputs {\tt n} and then calls a function named {\tt -countdown}---itself---passing {\tt n-1} as an argument. +If {\texttt n} is 0 or negative, it outputs the word, ``Blastoff!'' +Otherwise, it outputs {\texttt n} and then calls a function named {\texttt +countdown}---itself---passing {\texttt n-1} as an argument. What happens if we call this function like this? -\begin{verbatim} +\begin{lstlisting} >>> countdown(3) -\end{verbatim} +\end{lstlisting} % -The execution of {\tt countdown} begins with {\tt n=3}, and since -{\tt n} is greater than 0, it outputs the value 3, and then calls itself... +The execution of {\texttt countdown} begins with {\texttt n=3}, and since +{\texttt n} is greater than 0, it outputs the value 3, and then calls itself... \begin{quote} -The execution of {\tt countdown} begins with {\tt n=2}, and since -{\tt n} is greater than 0, it outputs the value 2, and then calls itself... +The execution of {\texttt countdown} begins with {\texttt n=2}, and since +{\texttt n} is greater than 0, it outputs the value 2, and then calls itself... \begin{quote} -The execution of {\tt countdown} begins with {\tt n=1}, and since -{\tt n} is greater than 0, it outputs the value 1, and then calls itself... +The execution of {\texttt countdown} begins with {\texttt n=1}, and since +{\texttt n} is greater than 0, it outputs the value 1, and then calls itself... \begin{quote} -The execution of {\tt countdown} begins with {\tt n=0}, and since {\tt +The execution of {\texttt countdown} begins with {\texttt n=0}, and since {\texttt n} is not greater than 0, it outputs the word, ``Blastoff!'' and then returns. \end{quote} -The {\tt countdown} that got {\tt n=1} returns. +The {\texttt countdown} that got {\texttt n=1} returns. \end{quote} -The {\tt countdown} that got {\tt n=2} returns. +The {\texttt countdown} that got {\texttt n=2} returns. \end{quote} -The {\tt countdown} that got {\tt n=3} returns. +The {\texttt countdown} that got {\texttt n=3} returns. And then you're back in \verb"__main__". So, the total output looks like this: \index{main} -\begin{verbatim} +\begin{lstlisting} 3 2 1 Blastoff! -\end{verbatim} +\end{lstlisting} % -A function that calls itself is {\bf recursive}; the process of -executing it is called {\bf recursion}. +A function that calls itself is {\textbf recursive}; the process of +executing it is called {\textbf recursion}. \index{recursion} \index{function!recursive} As another example, we can write a function that prints a -string {\tt n} times. +string {\texttt n} times. -\begin{verbatim} +\begin{lstlisting} def print_n(s, n): if n <= 0: return print(s) print_n(s, n-1) -\end{verbatim} +\end{lstlisting} % -If {\tt n <= 0} the {\bf return statement} exits the function. The +If {\texttt n <= 0} the {\textbf return statement} exits the function. The flow of execution immediately returns to the caller, and the remaining lines of the function don't run. \index{return statement} \index{statement!return} -The rest of the function is similar to {\tt countdown}: it displays -{\tt s} and then calls itself to display {\tt s} $n-1$ additional -times. So the number of lines of output is {\tt 1 + (n - 1)}, which -adds up to {\tt n}. +The rest of the function is similar to {\texttt countdown}: it displays +{\texttt s} and then calls itself to display {\texttt s} $n-1$ additional +times. So the number of lines of output is {\texttt 1 + (n - 1)}, which +adds up to {\texttt n}. -For simple examples like this, it is probably easier to use a {\tt +For simple examples like this, it is probably easier to use a {\texttt for} loop. But we will see examples later that are hard to write -with a {\tt for} loop and easy to write with recursion, so it is +with a {\texttt for} loop and easy to write with recursion, so it is good to start early. \index{for loop} \index{loop!for} @@ -3990,8 +4103,8 @@ \section{Stack diagrams for recursive functions} For a recursive function, there might be more than one frame on the stack at the same time. -Figure~\ref{fig.stack2} shows a stack diagram for {\tt countdown} called with -{\tt n = 3}. +Figure~\ref{fig.stack2} shows a stack diagram for {\texttt countdown} called with +{\texttt n = 3}. \begin{figure} \centerline @@ -4007,16 +4120,16 @@ \section{Stack diagrams for recursive functions} \index{base case} \index{recursion!base case} -The four {\tt countdown} frames have different values for the -parameter {\tt n}. The bottom of the stack, where {\tt n=0}, is -called the {\bf base case}. It does not make a recursive call, so +The four {\texttt countdown} frames have different values for the +parameter {\texttt n}. The bottom of the stack, where {\texttt n=0}, is +called the {\textbf base case}. It does not make a recursive call, so there are no more frames. As an exercise, draw a stack diagram for \verb"print_n" called with -\verb"s = 'Hello'" and {\tt n=2}. +\verb"s = 'Hello'" and {\texttt n=2}. Then write a function called \verb"do_n" that takes a function -object and a number, {\tt n}, as arguments, and that calls -the given function {\tt n} times. +object and a number, {\texttt n}, as arguments, and that calls +the given function {\texttt n} times. \section{Infinite recursion} @@ -4028,13 +4141,13 @@ \section{Infinite recursion} If a recursion never reaches a base case, it goes on making recursive calls forever, and the program never terminates. This is -known as {\bf infinite recursion}, and it is generally not +known as {\textbf infinite recursion}, and it is generally not a good idea. Here is a minimal program with an infinite recursion: -\begin{verbatim} +\begin{lstlisting} def recurse(): recurse() -\end{verbatim} +\end{lstlisting} % In most programming environments, a program with infinite recursion does not really run forever. Python reports an error @@ -4042,7 +4155,7 @@ \section{Infinite recursion} \index{exception!RuntimeError} \index{RuntimeError} -\begin{verbatim} +\begin{lstlisting} File "", line 2, in recurse File "", line 2, in recurse File "", line 2, in recurse @@ -4051,11 +4164,11 @@ \section{Infinite recursion} . File "", line 2, in recurse RuntimeError: Maximum recursion depth exceeded -\end{verbatim} +\end{lstlisting} % This traceback is a little bigger than the one we saw in the previous chapter. When the error occurs, there are 1000 -{\tt recurse} frames on the stack! +{\texttt recurse} frames on the stack! If you encounter an infinite recursion by accident, review your function to confirm that there is a base case that does not @@ -4069,62 +4182,62 @@ \section{Keyboard input} The programs we have written so far accept no input from the user. They just do the same thing every time. -Python provides a built-in function called {\tt input} that +Python provides a built-in function called {\texttt input} that stops the program and -waits for the user to type something. When the user presses {\sf - Return} or {\sf Enter}, the program resumes and \verb"input" +waits for the user to type something. When the user presses {\textsf + Return} or {\textsf Enter}, the program resumes and \verb"input" returns what the user typed as a string. In Python 2, the same function is called \verb"raw_input". \index{Python 2} \index{input function} \index{function!input} -\begin{verbatim} +\begin{lstlisting} >>> text = input() What are you waiting for? >>> text 'What are you waiting for?' -\end{verbatim} +\end{lstlisting} % Before getting input from the user, it is a good idea to print a prompt telling the user what to type. \verb"input" can take a prompt as an argument: \index{prompt} -\begin{verbatim} +\begin{lstlisting} >>> name = input('What...is your name?\n') What...is your name? Arthur, King of the Britons! >>> name 'Arthur, King of the Britons!' -\end{verbatim} +\end{lstlisting} % -The sequence \verb"\n" at the end of the prompt represents a {\bf +The sequence \verb"\n" at the end of the prompt represents a {\textbf newline}, which is a special character that causes a line break. That's why the user's input appears below the prompt. \index{newline} If you expect the user to type an integer, you can try to convert -the return value to {\tt int}: +the return value to {\texttt int}: -\begin{verbatim} +\begin{lstlisting} >>> prompt = 'What...is the airspeed velocity of an unladen swallow?\n' >>> speed = input(prompt) What...is the airspeed velocity of an unladen swallow? 42 >>> int(speed) 42 -\end{verbatim} +\end{lstlisting} % But if the user types something other than a string of digits, you get an error: -\begin{verbatim} +\begin{lstlisting} >>> speed = input(prompt) What...is the airspeed velocity of an unladen swallow? What do you mean, an African or a European swallow? >>> int(speed) ValueError: invalid literal for int() with base 10 -\end{verbatim} +\end{lstlisting} % We will see how to handle this kind of error later. \index{ValueError} @@ -4153,17 +4266,17 @@ \section{Debugging} tabs are invisible and we are used to ignoring them. \index{whitespace} -\begin{verbatim} +\begin{lstlisting} >>> x = 5 >>> y = 6 File "", line 1 y = 6 ^ IndentationError: unexpected indent -\end{verbatim} +\end{lstlisting} % In this example, the problem is that the second line is indented by -one space. But the error message points to {\tt y}, which is +one space. But the error message points to {\texttt y}, which is misleading. In general, error messages indicate where the problem was discovered, but the actual error might be earlier in the code, sometimes on a previous line. @@ -4175,30 +4288,30 @@ \section{Debugging} is $SNR_{db} = 10 \log_{10} (P_{signal} / P_{noise})$. In Python, you might write something like this: -\begin{verbatim} +\begin{lstlisting} import math signal_power = 9 noise_power = 10 ratio = signal_power // noise_power decibels = 10 * math.log10(ratio) print(decibels) -\end{verbatim} +\end{lstlisting} % When you run this program, you get an exception: % \index{exception!OverflowError} \index{OverflowError} -\begin{verbatim} +\begin{lstlisting} Traceback (most recent call last): File "snr.py", line 5, in ? decibels = 10 * math.log10(ratio) ValueError: math domain error -\end{verbatim} +\end{lstlisting} % The error message indicates line 5, but there is nothing wrong with that line. To find the real error, it might be -useful to print the value of {\tt ratio}, which turns out to +useful to print the value of {\texttt ratio}, which turns out to be 0. The problem is in line 4, which uses floor division instead of floating-point division. \index{floor division} @@ -4212,27 +4325,27 @@ \section{Glossary} \begin{description} -\item[floor division:] An operator, denoted {\tt //}, that divides two +\item[floor division:] An operator, denoted {\texttt //}, that divides two numbers and rounds down (toward negative infinity) to an integer. \index{floor division} \index{division!floor} \item[modulus operator:] An operator, denoted with a percent sign -({\tt \%}), that works on integers and returns the remainder when one +({\texttt \%}), that works on integers and returns the remainder when one number is divided by another. \index{modulus operator} \index{operator!modulus} \item[boolean expression:] An expression whose value is either -{\tt True} or {\tt False}. +{\texttt True} or {\texttt False}. \index{boolean expression} \index{expression!boolean} \item[relational operator:] One of the operators that compares -its operands: {\tt ==}, {\tt !=}, {\tt >}, {\tt <}, {\tt >=}, and {\tt <=}. +its operands: {\texttt ==}, {\texttt !=}, {\texttt >}, {\texttt <}, {\texttt >=}, and {\texttt <=}. \item[logical operator:] One of the operators that combines boolean -expressions: {\tt and}, {\tt or}, and {\tt not}. +expressions: {\texttt and}, {\texttt or}, and {\texttt not}. \item[conditional statement:] A statement that controls the flow of execution depending on some condition. @@ -4283,17 +4396,18 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont -The {\tt time} module provides a function, also named {\tt time}, that +The {\texttt time} module provides a function, also named {\texttt time}, that returns the current Greenwich Mean Time in ``the epoch'', which is an arbitrary time used as a reference point. On UNIX systems, the epoch is 1 January 1970. -\begin{verbatim} +\begin{lstlisting} >>> import time >>> time.time() 1437746094.5735958 -\end{verbatim} +\end{lstlisting} Write a script that reads the current time and converts it to a time of day in hours, minutes, and seconds, plus the number of @@ -4303,6 +4417,7 @@ \section{Exercises} \begin{exercise} +\normalfont \index{Fermat's Last Theorem} Fermat's Last Theorem says that there are no positive integers @@ -4315,7 +4430,7 @@ \section{Exercises} \begin{enumerate} \item Write a function named \verb"check_fermat" that takes four -parameters---{\tt a}, {\tt b}, {\tt c} and {\tt n}---and +parameters---{\texttt a}, {\texttt b}, {\texttt c} and {\texttt n}---and checks to see if Fermat's theorem holds. If $n$ is greater than 2 and @@ -4325,7 +4440,7 @@ \section{Exercises} Otherwise the program should print, ``No, that doesn't work.'' \item Write a function that prompts the user to input values -for {\tt a}, {\tt b}, {\tt c} and {\tt n}, converts them to +for {\texttt a}, {\texttt b}, {\texttt c} and {\texttt n}, converts them to integers, and uses \verb"check_fermat" to check whether they violate Fermat's theorem. @@ -4335,6 +4450,7 @@ \section{Exercises} \begin{exercise} +\normalfont \index{triangle} If you are given three sticks, you may or may not be able to arrange @@ -4367,11 +4483,12 @@ \section{Exercises} \end{exercise} \begin{exercise} +\normalfont What is the output of the following program? Draw a stack diagram that shows the state of the program when it prints the result. -\begin{verbatim} +\begin{lstlisting} def recurse(n, s): if n == 0: print(s) @@ -4379,11 +4496,11 @@ \section{Exercises} recurse(n-1, n+s) recurse(3, 0) -\end{verbatim} +\end{lstlisting} \begin{enumerate} -\item What would happen if you called this function like this: {\tt +\item What would happen if you called this function like this: {\texttt recurse(-1, 0)}? \item Write a docstring that explains everything someone would need to @@ -4394,17 +4511,18 @@ \section{Exercises} \end{exercise} -The following exercises use the {\tt turtle} module, described in +The following exercises use the {\texttt turtle} module, described in Chapter~\ref{turtlechap}: \index{TurtleWorld} \begin{exercise} +\normalfont Read the following function and see if you can figure out what it does (see the examples in Chapter~\ref{turtlechap}). Then run it and see if you got it right. -\begin{verbatim} +\begin{lstlisting} def draw(t, length, n): if n == 0: return @@ -4416,7 +4534,7 @@ \section{Exercises} draw(t, length, n-1) t.lt(angle) t.bk(length*n) -\end{verbatim} +\end{lstlisting} \end{exercise} @@ -4429,6 +4547,7 @@ \section{Exercises} \end{figure} \begin{exercise} +\normalfont \index{Koch curve} The Koch curve is a fractal that looks something like @@ -4458,11 +4577,11 @@ \section{Exercises} \begin{enumerate} -\item Write a function called {\tt koch} that takes a turtle and +\item Write a function called {\texttt koch} that takes a turtle and a length as parameters, and that uses the turtle to draw a Koch curve with the given length. -\item Write a function called {\tt snowflake} that draws three +\item Write a function called {\texttt snowflake} that draws three Koch curves to make the outline of a snowflake. Solution: \url{http://thinkpython2.com/code/koch.py}. @@ -4492,27 +4611,27 @@ \section{Return values} value, which we usually assign to a variable or use as part of an expression. -\begin{verbatim} +\begin{lstlisting} e = math.exp(1.0) height = radius * math.sin(radians) -\end{verbatim} +\end{lstlisting} % The functions we have written so far are void. Speaking casually, they have no return value; more precisely, -their return value is {\tt None}. +their return value is {\texttt None}. In this chapter, we are (finally) going to write fruitful functions. -The first example is {\tt area}, which returns the area of a circle +The first example is {\texttt area}, which returns the area of a circle with the given radius: -\begin{verbatim} +\begin{lstlisting} def area(radius): a = math.pi * radius**2 return a -\end{verbatim} +\end{lstlisting} % -We have seen the {\tt return} statement before, but in a fruitful -function the {\tt return} statement includes +We have seen the {\texttt return} statement before, but in a fruitful +function the {\texttt return} statement includes an expression. This statement means: ``Return immediately from this function and use the following expression as a return value.'' The expression can be arbitrarily complicated, so we could @@ -4520,12 +4639,12 @@ \section{Return values} \index{return statement} \index{statement!return} -\begin{verbatim} +\begin{lstlisting} def area(radius): return math.pi * radius**2 -\end{verbatim} +\end{lstlisting} % -On the other hand, {\bf temporary variables} like {\tt a} can make +On the other hand, {\textbf temporary variables} like {\texttt a} can make debugging easier. \index{temporary variable} \index{variable!temporary} @@ -4533,56 +4652,56 @@ \section{Return values} Sometimes it is useful to have multiple return statements, one in each branch of a conditional: -\begin{verbatim} +\begin{lstlisting} def absolute_value(x): if x < 0: return -x else: return x -\end{verbatim} +\end{lstlisting} % -Since these {\tt return} statements are in an alternative conditional, +Since these {\texttt return} statements are in an alternative conditional, only one runs. As soon as a return statement runs, the function terminates without executing any subsequent statements. -Code that appears after a {\tt return} statement, or any other place -the flow of execution can never reach, is called {\bf dead code}. +Code that appears after a {\texttt return} statement, or any other place +the flow of execution can never reach, is called {\textbf dead code}. \index{dead code} In a fruitful function, it is a good idea to ensure that every possible path through the program hits a -{\tt return} statement. For example: +{\texttt return} statement. For example: -\begin{verbatim} +\begin{lstlisting} def absolute_value(x): if x < 0: return -x if x > 0: return x -\end{verbatim} +\end{lstlisting} % -This function is incorrect because if {\tt x} happens to be 0, +This function is incorrect because if {\texttt x} happens to be 0, neither condition is true, and the function ends without hitting a -{\tt return} statement. If the flow of execution gets to the end -of a function, the return value is {\tt None}, which is not +{\texttt return} statement. If the flow of execution gets to the end +of a function, the return value is {\texttt None}, which is not the absolute value of 0. \index{None special value} \index{special value!None} -\begin{verbatim} +\begin{lstlisting} >>> print(absolute_value(0)) None -\end{verbatim} +\end{lstlisting} % By the way, Python provides a built-in function called -{\tt abs} that computes absolute values. +{\texttt abs} that computes absolute values. \index{abs function} \index{function!abs} -As an exercise, write a {\tt compare} function -takes two values, {\tt x} and {\tt y}, and returns {\tt 1} if {\tt x > y}, -{\tt 0} if {\tt x == y}, and {\tt -1} if {\tt x < y}. +As an exercise, write a {\texttt compare} function +takes two values, {\texttt x} and {\texttt y}, and returns {\texttt 1} if {\texttt x > y}, +{\texttt 0} if {\texttt x == y}, and {\texttt -1} if {\texttt x < y}. \index{compare function} \index{function!compare} @@ -4596,7 +4715,7 @@ \section{Incremental development} To deal with increasingly complex programs, you might want to try a process called -{\bf incremental development}. The goal of incremental development +{\textbf incremental development}. The goal of incremental development is to avoid long debugging sessions by adding and testing only a small amount of code at a time. \index{testing!incremental development} @@ -4610,7 +4729,7 @@ \section{Incremental development} \mathrm{distance} = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} \end{displaymath} % -The first step is to consider what a {\tt distance} function should +The first step is to consider what a {\texttt distance} function should look like in Python. In other words, what are the inputs (parameters) and what is the output (return value)? @@ -4620,10 +4739,10 @@ \section{Incremental development} Immediately you can write an outline of the function: -\begin{verbatim} +\begin{lstlisting} def distance(x1, y1, x2, y2): return 0.0 -\end{verbatim} +\end{lstlisting} % Obviously, this version doesn't compute distances; it always returns zero. But it is syntactically correct, and it runs, which means that @@ -4631,10 +4750,10 @@ \section{Incremental development} To test the new function, call it with sample arguments: -\begin{verbatim} +\begin{lstlisting} >>> distance(1, 2, 4, 6) 0.0 -\end{verbatim} +\end{lstlisting} % I chose these values so that the horizontal distance is 3 and the vertical distance is 4; that way, the result is 5, the hypotenuse @@ -4648,54 +4767,54 @@ \section{Incremental development} $x_2 - x_1$ and $y_2 - y_1$. The next version stores those values in temporary variables and prints them. -\begin{verbatim} +\begin{lstlisting} def distance(x1, y1, x2, y2): dx = x2 - x1 dy = y2 - y1 print('dx is', dx) print('dy is', dy) return 0.0 -\end{verbatim} +\end{lstlisting} % If the function is working, it should display \verb"dx is 3" and \verb"dy is 4". If so, we know that the function is getting the right arguments and performing the first computation correctly. If not, there are only a few lines to check. -Next we compute the sum of squares of {\tt dx} and {\tt dy}: +Next we compute the sum of squares of {\texttt dx} and {\texttt dy}: -\begin{verbatim} +\begin{lstlisting} def distance(x1, y1, x2, y2): dx = x2 - x1 dy = y2 - y1 dsquared = dx**2 + dy**2 print('dsquared is: ', dsquared) return 0.0 -\end{verbatim} +\end{lstlisting} % Again, you would run the program at this stage and check the output (which should be 25). -Finally, you can use {\tt math.sqrt} to compute and return the result: +Finally, you can use {\texttt math.sqrt} to compute and return the result: \index{sqrt} \index{function!sqrt} -\begin{verbatim} +\begin{lstlisting} def distance(x1, y1, x2, y2): dx = x2 - x1 dy = y2 - y1 dsquared = dx**2 + dy**2 result = math.sqrt(dsquared) return result -\end{verbatim} +\end{lstlisting} % If that works correctly, you are done. Otherwise, you might -want to print the value of {\tt result} before the return +want to print the value of {\texttt result} before the return statement. The final version of the function doesn't display anything when it -runs; it only returns a value. The {\tt print} statements we wrote +runs; it only returns a value. The {\texttt print} statements we wrote are useful for debugging, but once you get the function working, you -should remove them. Code like that is called {\bf scaffolding} +should remove them. Code like that is called {\textbf scaffolding} because it is helpful for building the program but is not part of the final product. \index{scaffolding} @@ -4724,7 +4843,7 @@ \section{Incremental development} \end{enumerate} As an exercise, use incremental development to write a function -called {\tt hypotenuse} that returns the length of the hypotenuse of a +called {\texttt hypotenuse} that returns the length of the hypotenuse of a right triangle given the lengths of the other two legs as arguments. Record each stage of the development process as you go. \index{hypotenuse} @@ -4740,41 +4859,41 @@ \section{Composition} the center of the circle and a point on the perimeter, and computes the area of the circle. -Assume that the center point is stored in the variables {\tt xc} and -{\tt yc}, and the perimeter point is in {\tt xp} and {\tt yp}. The +Assume that the center point is stored in the variables {\texttt xc} and +{\texttt yc}, and the perimeter point is in {\texttt xp} and {\texttt yp}. The first step is to find the radius of the circle, which is the distance -between the two points. We just wrote a function, {\tt +between the two points. We just wrote a function, {\texttt distance}, that does that: -\begin{verbatim} +\begin{lstlisting} radius = distance(xc, yc, xp, yp) -\end{verbatim} +\end{lstlisting} % The next step is to find the area of a circle with that radius; we just wrote that, too: -\begin{verbatim} +\begin{lstlisting} result = area(radius) -\end{verbatim} +\end{lstlisting} % Encapsulating these steps in a function, we get: \index{encapsulation} -\begin{verbatim} +\begin{lstlisting} def circle_area(xc, yc, xp, yp): radius = distance(xc, yc, xp, yp) result = area(radius) return result -\end{verbatim} +\end{lstlisting} % -The temporary variables {\tt radius} and {\tt result} are useful for +The temporary variables {\texttt radius} and {\texttt result} are useful for development and debugging, but once the program is working, we can make it more concise by composing the function calls: -\begin{verbatim} +\begin{lstlisting} def circle_area(xc, yc, xp, yp): return area(distance(xc, yc, xp, yp)) -\end{verbatim} +\end{lstlisting} % \section{Boolean functions} @@ -4784,55 +4903,55 @@ \section{Boolean functions} complicated tests inside functions. \index{boolean function} For example: -\begin{verbatim} +\begin{lstlisting} def is_divisible(x, y): if x % y == 0: return True else: return False -\end{verbatim} +\end{lstlisting} % It is common to give boolean functions names that sound like yes/no -questions; \verb"is_divisible" returns either {\tt True} or {\tt False} -to indicate whether {\tt x} is divisible by {\tt y}. +questions; \verb"is_divisible" returns either {\texttt True} or {\texttt False} +to indicate whether {\texttt x} is divisible by {\texttt y}. Here is an example: -\begin{verbatim} +\begin{lstlisting} >>> is_divisible(6, 4) False >>> is_divisible(6, 3) True -\end{verbatim} +\end{lstlisting} % -The result of the {\tt ==} operator is a boolean, so we can write the +The result of the {\texttt ==} operator is a boolean, so we can write the function more concisely by returning it directly: -\begin{verbatim} +\begin{lstlisting} def is_divisible(x, y): return x % y == 0 -\end{verbatim} +\end{lstlisting} % Boolean functions are often used in conditional statements: \index{conditional statement} \index{statement!conditional} -\begin{verbatim} +\begin{lstlisting} if is_divisible(x, y): print('x is divisible by y') -\end{verbatim} +\end{lstlisting} % It might be tempting to write something like: -\begin{verbatim} +\begin{lstlisting} if is_divisible(x, y) == True: print('x is divisible by y') -\end{verbatim} +\end{lstlisting} % But the extra comparison is unnecessary. As an exercise, write a function \verb"is_between(x, y, z)" that -returns {\tt True} if $x \le y \le z$ or {\tt False} otherwise. +returns {\texttt True} if $x \le y \le z$ or {\texttt False} otherwise. \section{More recursion} @@ -4898,25 +5017,25 @@ \section{More recursion} If you can write a recursive definition of something, you can write a Python program to evaluate it. The first step is to decide what the parameters should be. In this case it should be clear -that {\tt factorial} takes an integer: +that {\texttt factorial} takes an integer: -\begin{verbatim} +\begin{lstlisting} def factorial(n): -\end{verbatim} +\end{lstlisting} % If the argument happens to be 0, all we have to do is return 1: -\begin{verbatim} +\begin{lstlisting} def factorial(n): if n == 0: return 1 -\end{verbatim} +\end{lstlisting} % Otherwise, and this is the interesting part, we have to make a recursive call to find the factorial of $n-1$ and then multiply it by $n$: -\begin{verbatim} +\begin{lstlisting} def factorial(n): if n == 0: return 1 @@ -4924,23 +5043,23 @@ \section{More recursion} recurse = factorial(n-1) result = n * recurse return result -\end{verbatim} +\end{lstlisting} % -The flow of execution for this program is similar to the flow of {\tt -countdown} in Section~\ref{recursion}. If we call {\tt factorial} +The flow of execution for this program is similar to the flow of {\texttt +countdown} in Section~\ref{recursion}. If we call {\texttt factorial} with the value 3: Since 3 is not 0, we take the second branch and calculate the factorial -of {\tt n-1}... +of {\texttt n-1}... \begin{quote} Since 2 is not 0, we take the second branch and calculate the factorial of -{\tt n-1}... +{\texttt n-1}... \begin{quote} Since 1 is not 0, we take the second branch and calculate the factorial - of {\tt n-1}... + of {\texttt n-1}... \begin{quote} @@ -4975,13 +5094,13 @@ \section{More recursion} \end{figure} The return values are shown being passed back up the stack. In each -frame, the return value is the value of {\tt result}, which is the -product of {\tt n} and {\tt recurse}. +frame, the return value is the value of {\texttt result}, which is the +product of {\texttt n} and {\texttt recurse}. \index{function frame} \index{frame} In the last frame, the local -variables {\tt recurse} and {\tt result} do not exist, because +variables {\texttt recurse} and {\texttt result} do not exist, because the branch that creates them does not run. @@ -4997,7 +5116,7 @@ \section{Leap of faith} result. In fact, you are already practicing this leap of faith when you use -built-in functions. When you call {\tt math.cos} or {\tt math.exp}, +built-in functions. When you call {\texttt math.cos} or {\texttt math.exp}, you don't examine the bodies of those functions. You just assume that they work because the people who wrote the built-in functions were good programmers. @@ -5027,8 +5146,8 @@ \section{One more example} \index{fibonacci function} \index{function!fibonacci} -After {\tt factorial}, the most common example of a recursively -defined mathematical function is {\tt fibonacci}, which has the +After {\texttt factorial}, the most common example of a recursively +defined mathematical function is {\texttt fibonacci}, which has the following definition (see \url{http://en.wikipedia.org/wiki/Fibonacci_number}): % @@ -5040,7 +5159,7 @@ \section{One more example} % Translated into Python, it looks like this: -\begin{verbatim} +\begin{lstlisting} def fibonacci(n): if n == 0: return 0 @@ -5048,7 +5167,7 @@ \section{One more example} return 1 else: return fibonacci(n-1) + fibonacci(n-2) -\end{verbatim} +\end{lstlisting} % If you try to follow the flow of execution here, even for fairly small values of $n$, your head explodes. But according to the @@ -5061,41 +5180,41 @@ \section{One more example} \section{Checking types} \label{guardian} -What happens if we call {\tt factorial} and give it 1.5 as an argument? +What happens if we call {\texttt factorial} and give it 1.5 as an argument? \index{type checking} \index{error checking} \index{factorial function} \index{RuntimeError} -\begin{verbatim} +\begin{lstlisting} >>> factorial(1.5) RuntimeError: Maximum recursion depth exceeded -\end{verbatim} +\end{lstlisting} % It looks like an infinite recursion. How can that be? The function -has a base case---when {\tt n == 0}. But if {\tt n} is not an integer, +has a base case---when {\texttt n == 0}. But if {\texttt n} is not an integer, we can {\em miss} the base case and recurse forever. \index{infinite recursion} \index{recursion!infinite} -In the first recursive call, the value of {\tt n} is 0.5. +In the first recursive call, the value of {\texttt n} is 0.5. In the next, it is -0.5. From there, it gets smaller (more negative), but it will never be 0. -We have two choices. We can try to generalize the {\tt factorial} -function to work with floating-point numbers, or we can make {\tt +We have two choices. We can try to generalize the {\texttt factorial} +function to work with floating-point numbers, or we can make {\texttt factorial} check the type of its argument. The first option is called the gamma function and it's a little beyond the scope of this book. So we'll go for the second. \index{gamma function} -We can use the built-in function {\tt isinstance} to verify the type +We can use the built-in function {\texttt isinstance} to verify the type of the argument. While we're at it, we can also make sure the argument is positive: \index{isinstance function} \index{function!isinstance} -\begin{verbatim} +\begin{lstlisting} def factorial(n): if not isinstance(n, int): print('Factorial is only defined for integers.') @@ -5107,28 +5226,28 @@ \section{Checking types} return 1 else: return n * factorial(n-1) -\end{verbatim} +\end{lstlisting} % The first base case handles nonintegers; the second handles negative integers. In both cases, the program prints -an error message and returns {\tt None} to indicate that something +an error message and returns {\texttt None} to indicate that something went wrong: -\begin{verbatim} +\begin{lstlisting} >>> print(factorial('fred')) Factorial is only defined for integers. None >>> print(factorial(-2)) Factorial is not defined for negative integers. None -\end{verbatim} +\end{lstlisting} % If we get past both checks, we know that $n$ is positive or zero, so we can prove that the recursion terminates. \index{guardian pattern} \index{pattern!guardian} -This program demonstrates a pattern sometimes called a {\bf guardian}. +This program demonstrates a pattern sometimes called a {\textbf guardian}. The first two conditionals act as guardians, protecting the code that follows from values that might cause an error. The guardians make it possible to prove the correctness of the code. @@ -5158,15 +5277,15 @@ \section{Debugging} \end{itemize} -To rule out the first possibility, you can add a {\tt print} statement +To rule out the first possibility, you can add a {\texttt print} statement at the beginning of the function and display the values of the parameters (and maybe their types). Or you can write code that checks the preconditions explicitly. \index{precondition} \index{postcondition} -If the parameters look good, add a {\tt print} statement before each -{\tt return} statement and display the return value. If +If the parameters look good, add a {\texttt print} statement before each +{\texttt return} statement and display the return value. If possible, check the result by hand. Consider calling the function with values that make it easy to check the result (as in Section~\ref{incremental.development}). @@ -5178,10 +5297,10 @@ \section{Debugging} Adding print statements at the beginning and end of a function can help make the flow of execution more visible. -For example, here is a version of {\tt factorial} with +For example, here is a version of {\texttt factorial} with print statements: -\begin{verbatim} +\begin{lstlisting} def factorial(n): space = ' ' * (4 * n) print(space, 'factorial', n) @@ -5193,12 +5312,12 @@ \section{Debugging} result = n * recurse print(space, 'returning', result) return result -\end{verbatim} +\end{lstlisting} % -{\tt space} is a string of space characters that controls the -indentation of the output. Here is the result of {\tt factorial(4)} : +{\texttt space} is a string of space characters that controls the +indentation of the output. Here is the result of {\texttt factorial(4)} : -\begin{verbatim} +\begin{lstlisting} factorial 4 factorial 3 factorial 2 @@ -5209,7 +5328,7 @@ \section{Debugging} returning 2 returning 6 returning 24 -\end{verbatim} +\end{lstlisting} % If you are confused about the flow of execution, this kind of output can be helpful. It takes some time to develop effective @@ -5226,7 +5345,7 @@ \section{Glossary} \index{variable!temporary} \item[dead code:] Part of a program that can never run, often because -it appears after a {\tt return} statement. +it appears after a {\texttt return} statement. \index{dead code} \item[incremental development:] A program development plan intended to @@ -5250,11 +5369,12 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont Draw a stack diagram for the following program. What does the program print? \index{stack diagram} -\begin{verbatim} +\begin{lstlisting} def b(z): prod = a(z, z) print(z, prod) @@ -5272,12 +5392,13 @@ \section{Exercises} x = 1 y = x + 1 print(c(x, y+3, x+y)) -\end{verbatim} +\end{lstlisting} \end{exercise} \begin{exercise} +\normalfont \label{ackermann} The Ackermann function, $A(m, n)$, is defined: @@ -5291,9 +5412,9 @@ \section{Exercises} \end{eqnarray*} % See \url{http://en.wikipedia.org/wiki/Ackermann_function}. -Write a function named {\tt ack} that evaluates the Ackermann function. -Use your function to evaluate {\tt ack(3, 4)}, which should be 125. -What happens for larger values of {\tt m} and {\tt n}? +Write a function named {\texttt ack} that evaluates the Ackermann function. +Use your function to evaluate {\texttt ack(3, 4)}, which should be 125. +What happens for larger values of {\texttt m} and {\texttt n}? Solution: \url{http://thinkpython2.com/code/ackermann.py}. \index{Ackermann function} \index{function!ack} @@ -5302,6 +5423,7 @@ \section{Exercises} \begin{exercise} +\normalfont \label{palindrome} A palindrome is a word that is spelled the same backward and @@ -5313,7 +5435,7 @@ \section{Exercises} The following are functions that take a string argument and return the first, last, and middle letters: -\begin{verbatim} +\begin{lstlisting} def first(word): return word[0] @@ -5322,21 +5444,21 @@ \section{Exercises} def middle(word): return word[1:-1] -\end{verbatim} +\end{lstlisting} % We'll see how they work in Chapter~\ref{strings}. \begin{enumerate} -\item Type these functions into a file named {\tt palindrome.py} -and test them out. What happens if you call {\tt middle} with +\item Type these functions into a file named {\texttt palindrome.py} +and test them out. What happens if you call {\texttt middle} with a string with two letters? One letter? What about the empty string, which is written \verb"''" and contains no letters? \item Write a function called \verb"is_palindrome" that takes -a string argument and returns {\tt True} if it is a palindrome -and {\tt False} otherwise. Remember that you can use the -built-in function {\tt len} to check the length of a string. +a string argument and returns {\texttt True} if it is a palindrome +and {\texttt False} otherwise. Remember that you can use the +built-in function {\texttt len} to check the length of a string. \end{enumerate} @@ -5345,17 +5467,19 @@ \section{Exercises} \end{exercise} \begin{exercise} +\normalfont A number, $a$, is a power of $b$ if it is divisible by $b$ and $a/b$ is a power of $b$. Write a function called -\verb"is_power" that takes parameters {\tt a} and {\tt b} -and returns {\tt True} if {\tt a} is a power of {\tt b}. +\verb"is_power" that takes parameters {\texttt a} and {\texttt b} +and returns {\texttt True} if {\texttt a} is a power of {\texttt b}. Note: you will have to think about the base case. \end{exercise} \begin{exercise} +\normalfont \index{greatest common divisor (GCD)} \index{GCD (greatest common divisor)} @@ -5367,7 +5491,7 @@ \section{Exercises} b) = gcd(b, r)$. As a base case, we can use $gcd(a, 0) = a$. Write a function called -\verb"gcd" that takes parameters {\tt a} and {\tt b} +\verb"gcd" that takes parameters {\texttt a} and {\texttt b} and returns their greatest common divisor. Credit: This exercise is based on an example from Abelson and @@ -5381,9 +5505,9 @@ \chapter{Iteration} This chapter is about iteration, which is the ability to run a block of statements repeatedly. We saw a kind of iteration, using recursion, in Section~\ref{recursion}. -We saw another kind, using a {\tt for} loop, +We saw another kind, using a {\texttt for} loop, in Section~\ref{repetition}. In this chapter we'll see yet another -kind, using a {\tt while} statement. +kind, using a {\texttt while} statement. But first I want to say a little more about variable assignment. @@ -5396,50 +5520,50 @@ \section{Reassignment} assignment to the same variable. A new assignment makes an existing variable refer to a new value (and stop referring to the old value). -\begin{verbatim} +\begin{lstlisting} >>> x = 5 >>> x 5 >>> x = 7 >>> x 7 -\end{verbatim} +\end{lstlisting} % The first time we display -{\tt x}, its value is 5; the second time, its +{\texttt x}, its value is 5; the second time, its value is 7. -Figure~\ref{fig.assign2} shows what {\bf reassignment} looks +Figure~\ref{fig.assign2} shows what {\textbf reassignment} looks like in a state diagram. \index{state diagram} \index{diagram!state} At this point I want to address a common source of confusion. -Because Python uses the equal sign ({\tt =}) for assignment, it is -tempting to interpret a statement like {\tt a = b} as a +Because Python uses the equal sign ({\texttt =}) for assignment, it is +tempting to interpret a statement like {\texttt a = b} as a mathematical -proposition of equality; that is, the claim that {\tt a} and -{\tt b} are equal. But this interpretation is wrong. +proposition of equality; that is, the claim that {\texttt a} and +{\texttt b} are equal. But this interpretation is wrong. \index{equality and assignment} First, equality is a symmetric relationship and assignment is not. For example, in mathematics, if $a=7$ then $7=a$. But in Python, the -statement {\tt a = 7} is legal and {\tt 7 = a} is not. +statement {\texttt a = 7} is legal and {\texttt 7 = a} is not. Also, in mathematics, a proposition of equality is either true or false for all time. If $a=b$ now, then $a$ will always equal $b$. In Python, an assignment statement can make two variables equal, but they don't have to stay that way: -\begin{verbatim} +\begin{lstlisting} >>> a = 5 >>> b = a # a and b are now equal >>> a = 3 # a and b are no longer equal >>> b 5 -\end{verbatim} +\end{lstlisting} % -The third line changes the value of {\tt a} but does not change the -value of {\tt b}, so they are no longer equal. +The third line changes the value of {\texttt a} but does not change the +value of {\texttt b}, so they are no longer equal. Reassigning variables is often useful, but you should use it with caution. If the values of variables change frequently, it can @@ -5460,43 +5584,43 @@ \section{Updating variables} \index{update} \index{variable!updating} -A common kind of reassignment is an {\bf update}, +A common kind of reassignment is an {\textbf update}, where the new value of the variable depends on the old. -\begin{verbatim} +\begin{lstlisting} >>> x = x + 1 -\end{verbatim} +\end{lstlisting} % -This means ``get the current value of {\tt x}, add one, and then -update {\tt x} with the new value.'' +This means ``get the current value of {\texttt x}, add one, and then +update {\texttt x} with the new value.'' If you try to update a variable that doesn't exist, you get an error, because Python evaluates the right side before it assigns -a value to {\tt x}: +a value to {\texttt x}: -\begin{verbatim} +\begin{lstlisting} >>> x = x + 1 NameError: name 'x' is not defined -\end{verbatim} +\end{lstlisting} % -Before you can update a variable, you have to {\bf initialize} +Before you can update a variable, you have to {\textbf initialize} it, usually with a simple assignment: \index{initialization (before update)} -\begin{verbatim} +\begin{lstlisting} >>> x = 0 >>> x = x + 1 -\end{verbatim} +\end{lstlisting} % -Updating a variable by adding 1 is called an {\bf increment}; -subtracting 1 is called a {\bf decrement}. +Updating a variable by adding 1 is called an {\textbf increment}; +subtracting 1 is called a {\textbf decrement}. \index{increment} \index{decrement} -\section{The {\tt while} statement} +\section{The {\texttt while} statement} \index{statement!while} \index{while loop} \index{loop!while} @@ -5505,38 +5629,38 @@ \section{The {\tt while} statement} Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that computers do well and people do poorly. In a computer program, -repetition is also called {\bf iteration}. +repetition is also called {\textbf iteration}. -We have already seen two functions, {\tt countdown} and +We have already seen two functions, {\texttt countdown} and \verb"print_n", that iterate using recursion. Because iteration is so common, Python provides language features to make it easier. -One is the {\tt for} statement we saw in Section~\ref{repetition}. +One is the {\texttt for} statement we saw in Section~\ref{repetition}. We'll get back to that later. -Another is the {\tt while} statement. Here is a version of {\tt -countdown} that uses a {\tt while} statement: +Another is the {\texttt while} statement. Here is a version of {\texttt +countdown} that uses a {\texttt while} statement: -\begin{verbatim} +\begin{lstlisting} def countdown(n): while n > 0: print(n) n = n - 1 print('Blastoff!') -\end{verbatim} +\end{lstlisting} % -You can almost read the {\tt while} statement as if it were English. -It means, ``While {\tt n} is greater than 0, -display the value of {\tt n} and then decrement -{\tt n}. When you get to 0, display the word {\tt Blastoff!}'' +You can almost read the {\texttt while} statement as if it were English. +It means, ``While {\texttt n} is greater than 0, +display the value of {\texttt n} and then decrement +{\texttt n}. When you get to 0, display the word {\texttt Blastoff!}'' \index{flow of execution} -More formally, here is the flow of execution for a {\tt while} statement: +More formally, here is the flow of execution for a {\texttt while} statement: \begin{enumerate} \item Determine whether the condition is true or false. -\item If false, exit the {\tt while} statement +\item If false, exit the {\texttt while} statement and continue execution at the next statement. \item If the condition is true, run the @@ -5553,20 +5677,20 @@ \section{The {\tt while} statement} The body of the loop should change the value of one or more variables so that the condition becomes false eventually and the loop terminates. Otherwise the loop will repeat forever, which is called -an {\bf infinite loop}. An endless source of amusement for computer +an {\textbf infinite loop}. An endless source of amusement for computer scientists is the observation that the directions on shampoo, ``Lather, rinse, repeat'', are an infinite loop. \index{infinite loop} \index{loop!infinite} -In the case of {\tt countdown}, we can prove that the loop -terminates: if {\tt n} is zero or negative, the loop never runs. -Otherwise, {\tt n} gets smaller each time through the +In the case of {\texttt countdown}, we can prove that the loop +terminates: if {\texttt n} is zero or negative, the loop never runs. +Otherwise, {\texttt n} gets smaller each time through the loop, so eventually we have to get to 0. For some other loops, it is not so easy to tell. For example: -\begin{verbatim} +\begin{lstlisting} def sequence(n): while n != 1: print(n) @@ -5574,28 +5698,28 @@ \section{The {\tt while} statement} n = n / 2 else: # n is odd n = n*3 + 1 -\end{verbatim} +\end{lstlisting} % -The condition for this loop is {\tt n != 1}, so the loop will continue -until {\tt n} is {\tt 1}, which makes the condition false. +The condition for this loop is {\texttt n != 1}, so the loop will continue +until {\texttt n} is {\texttt 1}, which makes the condition false. -Each time through the loop, the program outputs the value of {\tt n} -and then checks whether it is even or odd. If it is even, {\tt n} is -divided by 2. If it is odd, the value of {\tt n} is replaced with -{\tt n*3 + 1}. For example, if the argument passed to {\tt sequence} -is 3, the resulting values of {\tt n} are 3, 10, 5, 16, 8, 4, 2, 1. +Each time through the loop, the program outputs the value of {\texttt n} +and then checks whether it is even or odd. If it is even, {\texttt n} is +divided by 2. If it is odd, the value of {\texttt n} is replaced with +{\texttt n*3 + 1}. For example, if the argument passed to {\texttt sequence} +is 3, the resulting values of {\texttt n} are 3, 10, 5, 16, 8, 4, 2, 1. -Since {\tt n} sometimes increases and sometimes decreases, there is no -obvious proof that {\tt n} will ever reach 1, or that the program -terminates. For some particular values of {\tt n}, we can prove +Since {\texttt n} sometimes increases and sometimes decreases, there is no +obvious proof that {\texttt n} will ever reach 1, or that the program +terminates. For some particular values of {\texttt n}, we can prove termination. For example, if the starting value is a power of two, -{\tt n} will be even every time through the loop +{\texttt n} will be even every time through the loop until it reaches 1. The previous example ends with such a sequence, starting with 16. \index{Collatz conjecture} The hard question is whether we can prove that this program terminates -for {\em all} positive values of {\tt n}. So far, no one has +for {\em all} positive values of {\texttt n}. So far, no one has been able to prove it {\em or} disprove it! (See \url{http://en.wikipedia.org/wiki/Collatz_conjecture}.) @@ -5603,18 +5727,18 @@ \section{The {\tt while} statement} Section~\ref{recursion} using iteration instead of recursion. -\section{{\tt break}} +\section{{\texttt break}} \index{break statement} \index{statement!break} Sometimes you don't know it's time to end a loop until you get half -way through the body. In that case you can use the {\tt break} +way through the body. In that case you can use the {\texttt break} statement to jump out of the loop. For example, suppose you want to take input from the user until they -type {\tt done}. You could write: +type {\texttt done}. You could write: -\begin{verbatim} +\begin{lstlisting} while True: line = input('> ') if line == 'done': @@ -5622,24 +5746,24 @@ \section{{\tt break}} print(line) print('Done!') -\end{verbatim} +\end{lstlisting} % -The loop condition is {\tt True}, which is always true, so the +The loop condition is {\texttt True}, which is always true, so the loop runs until it hits the break statement. Each time through, it prompts the user with an angle bracket. -If the user types {\tt done}, the {\tt break} statement exits +If the user types {\texttt done}, the {\texttt break} statement exits the loop. Otherwise the program echoes whatever the user types and goes back to the top of the loop. Here's a sample run: -\begin{verbatim} +\begin{lstlisting} > not done not done > done Done! -\end{verbatim} +\end{lstlisting} % -This way of writing {\tt while} loops is common because you +This way of writing {\texttt while} loops is common because you can check the condition anywhere in the loop (not just at the top) and you can express the stop condition affirmatively (``stop when this happens'') rather than negatively (``keep going @@ -5664,28 +5788,28 @@ \section{Square roots} % For example, if $a$ is 4 and $x$ is 3: -\begin{verbatim} +\begin{lstlisting} >>> a = 4 >>> x = 3 >>> y = (x + a/x) / 2 >>> y 2.16666666667 -\end{verbatim} +\end{lstlisting} % The result is closer to the correct answer ($\sqrt{4} = 2$). If we repeat the process with the new estimate, it gets even closer: -\begin{verbatim} +\begin{lstlisting} >>> x = y >>> y = (x + a/x) / 2 >>> y 2.00641025641 -\end{verbatim} +\end{lstlisting} % After a few more updates, the estimate is almost exact: \index{update} -\begin{verbatim} +\begin{lstlisting} >>> x = y >>> y = (x + a/x) / 2 >>> y @@ -5694,14 +5818,14 @@ \section{Square roots} >>> y = (x + a/x) / 2 >>> y 2.00000000003 -\end{verbatim} +\end{lstlisting} % In general we don't know ahead of time how many steps it takes to get to the right answer, but we know when we get there because the estimate stops changing: -\begin{verbatim} +\begin{lstlisting} >>> x = y >>> y = (x + a/x) / 2 >>> y @@ -5710,46 +5834,46 @@ \section{Square roots} >>> y = (x + a/x) / 2 >>> y 2.0 -\end{verbatim} +\end{lstlisting} % -When {\tt y == x}, we can stop. Here is a loop that starts -with an initial estimate, {\tt x}, and improves it until it +When {\texttt y == x}, we can stop. Here is a loop that starts +with an initial estimate, {\texttt x}, and improves it until it stops changing: -\begin{verbatim} +\begin{lstlisting} while True: print(x) y = (x + a/x) / 2 if y == x: break x = y -\end{verbatim} +\end{lstlisting} % -For most values of {\tt a} this works fine, but in general it is -dangerous to test {\tt float} equality. +For most values of {\texttt a} this works fine, but in general it is +dangerous to test {\texttt float} equality. Floating-point values are only approximately right: most rational numbers, like $1/3$, and irrational numbers, like -$\sqrt{2}$, can't be represented exactly with a {\tt float}. +$\sqrt{2}$, can't be represented exactly with a {\texttt float}. \index{floating-point} \index{epsilon} -Rather than checking whether {\tt x} and {\tt y} are exactly equal, it -is safer to use the built-in function {\tt abs} to compute the +Rather than checking whether {\texttt x} and {\texttt y} are exactly equal, it +is safer to use the built-in function {\texttt abs} to compute the absolute value, or magnitude, of the difference between them: -\begin{verbatim} +\begin{lstlisting} if abs(y-x) < epsilon: break -\end{verbatim} +\end{lstlisting} % -Where \verb"epsilon" has a value like {\tt 0.0000001} that +Where \verb"epsilon" has a value like {\texttt 0.0000001} that determines how close is close enough. \section{Algorithms} \index{algorithm} -Newton's method is an example of an {\bf algorithm}: it is a +Newton's method is an example of an {\textbf algorithm}: it is a mechanical process for solving a category of problems (in this case, computing square roots). @@ -5800,7 +5924,7 @@ \section{Debugging} Instead, try to break the problem in half. Look at the middle of the program, or near it, for an intermediate value you -can check. Add a {\tt print} statement (or something else +can check. Add a {\texttt print} statement (or something else that has a verifiable effect) and run the program. If the mid-point check is incorrect, there must be a problem in the @@ -5864,18 +5988,19 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont \index{algorithm!square root} Copy the loop from Section~\ref{squareroot} and encapsulate it in a function called -\verb"mysqrt" that takes {\tt a} as a parameter, chooses a -reasonable value of {\tt x}, and returns an estimate of the square -root of {\tt a}. \index{encapsulation} +\verb"mysqrt" that takes {\texttt a} as a parameter, chooses a +reasonable value of {\texttt x}, and returns an estimate of the square +root of {\texttt a}. \index{encapsulation} To test it, write a function named \verb"test_square_root" that prints a table like this: -\begin{verbatim} +\begin{lstlisting} a mysqrt(a) math.sqrt(a) diff - --------- ------------ ---- 1.0 1.0 1.0 0.0 @@ -5887,23 +6012,24 @@ \section{Exercises} 7.0 2.64575131106 2.64575131106 0.0 8.0 2.82842712475 2.82842712475 4.4408920985e-16 9.0 3.0 3.0 0.0 -\end{verbatim} +\end{lstlisting} % The first column is a number, $a$; the second column is the square root of $a$ computed with \verb"mysqrt"; the third column is the -square root computed by {\tt math.sqrt}; the fourth column is the +square root computed by {\texttt math.sqrt}; the fourth column is the absolute value of the difference between the two estimates. \end{exercise} \begin{exercise} +\normalfont \index{eval function} \index{function!eval} -The built-in function {\tt eval} takes a string and evaluates +The built-in function {\texttt eval} takes a string and evaluates it using the Python interpreter. For example: -\begin{verbatim} +\begin{lstlisting} >>> eval('1 + 2 * 3') 7 >>> import math @@ -5911,11 +6037,11 @@ \section{Exercises} 2.2360679774997898 >>> eval('type(math.pi)') -\end{verbatim} +\end{lstlisting} % Write a function called \verb"eval_loop" that iteratively prompts the user, takes the resulting input and evaluates -it using {\tt eval}, and prints the result. +it using {\texttt eval}, and prints the result. It should continue until the user enters \verb"'done'", and then return the value of the last expression it evaluated. @@ -5924,6 +6050,7 @@ \section{Exercises} \begin{exercise} +\normalfont \index{Ramanujan, Srinivasa} The mathematician Srinivasa Ramanujan found an @@ -5936,10 +6063,10 @@ \section{Exercises} \sum^\infty_{k=0} \frac{(4k)!(1103+26390k)}{(k!)^4 396^{4k}} \] Write a function called \verb"estimate_pi" that uses this formula -to compute and return an estimate of $\pi$. It should use a {\tt while} +to compute and return an estimate of $\pi$. It should use a {\texttt while} loop to compute terms of the summation until the last term is -smaller than {\tt 1e-15} (which is Python notation for $10^{-15}$). -You can check the result by comparing it to {\tt math.pi}. +smaller than {\texttt 1e-15} (which is Python notation for $10^{-15}$). +You can check the result by comparing it to {\texttt math.pi}. Solution: \url{http://thinkpython2.com/code/pi.py}. @@ -5950,7 +6077,7 @@ \chapter{Strings} \label{strings} Strings are not like integers, floats, and booleans. A string -is a {\bf sequence}, which means it is +is a {\textbf sequence}, which means it is an ordered collection of other values. In this chapter you'll see how to access the characters that make up a string, and you'll learn about some of the methods strings provide. @@ -5967,38 +6094,38 @@ \section{A string is a sequence} You can access the characters one at a time with the bracket operator: -\begin{verbatim} +\begin{lstlisting} >>> fruit = 'banana' >>> letter = fruit[1] -\end{verbatim} +\end{lstlisting} % -The second statement selects character number 1 from {\tt -fruit} and assigns it to {\tt letter}. +The second statement selects character number 1 from {\texttt +fruit} and assigns it to {\texttt letter}. \index{index} -The expression in brackets is called an {\bf index}. +The expression in brackets is called an {\textbf index}. The index indicates which character in the sequence you want (hence the name). But you might not get what you expect: -\begin{verbatim} +\begin{lstlisting} >>> letter 'a' -\end{verbatim} +\end{lstlisting} % -For most people, the first letter of \verb"'banana'" is {\tt b}, not -{\tt a}. But for computer scientists, the index is an offset from the +For most people, the first letter of \verb"'banana'" is {\texttt b}, not +{\texttt a}. But for computer scientists, the index is an offset from the beginning of the string, and the offset of the first letter is zero. -\begin{verbatim} +\begin{lstlisting} >>> letter = fruit[0] >>> letter 'b' -\end{verbatim} +\end{lstlisting} % -So {\tt b} is the 0th letter (``zero-eth'') of \verb"'banana'", {\tt - a} is the 1th letter (``one-eth''), and {\tt n} is the 2th letter +So {\texttt b} is the 0th letter (``zero-eth'') of \verb"'banana'", {\texttt + a} is the 1th letter (``one-eth''), and {\texttt n} is the 2th letter (``two-eth''). \index{index!starting at zero} \index{zero, index starting at} @@ -6006,13 +6133,13 @@ \section{A string is a sequence} operators: \index{index} -\begin{verbatim} +\begin{lstlisting} >>> i = 1 >>> fruit[i] 'a' >>> fruit[i+1] 'n' -\end{verbatim} +\end{lstlisting} % But the value of the index has to be an integer. Otherwise you @@ -6020,55 +6147,55 @@ \section{A string is a sequence} \index{exception!TypeError} \index{TypeError} -\begin{verbatim} +\begin{lstlisting} >>> letter = fruit[1.5] TypeError: string indices must be integers -\end{verbatim} +\end{lstlisting} % -\section{{\tt len}} +\section{{\texttt len}} \index{len function} \index{function!len} -{\tt len} is a built-in function that returns the number of characters +{\texttt len} is a built-in function that returns the number of characters in a string: -\begin{verbatim} +\begin{lstlisting} >>> fruit = 'banana' >>> len(fruit) 6 -\end{verbatim} +\end{lstlisting} % To get the last letter of a string, you might be tempted to try something like this: \index{exception!IndexError} \index{IndexError} -\begin{verbatim} +\begin{lstlisting} >>> length = len(fruit) >>> last = fruit[length] IndexError: string index out of range -\end{verbatim} +\end{lstlisting} % -The reason for the {\tt IndexError} is that there is no letter in {\tt +The reason for the {\texttt IndexError} is that there is no letter in {\texttt 'banana'} with the index 6. Since we started counting at zero, the six letters are numbered 0 to 5. To get the last character, you have -to subtract 1 from {\tt length}: +to subtract 1 from {\texttt length}: -\begin{verbatim} +\begin{lstlisting} >>> last = fruit[length-1] >>> last 'a' -\end{verbatim} +\end{lstlisting} % Or you can use negative indices, which count backward from -the end of the string. The expression {\tt fruit[-1]} yields the last -letter, {\tt fruit[-2]} yields the second to last, and so on. +the end of the string. The expression {\texttt fruit[-1]} yields the last +letter, {\texttt fruit[-2]} yields the second to last, and so on. \index{index!negative} \index{negative index} -\section{Traversal with a {\tt for} loop} +\section{Traversal with a {\texttt for} loop} \label{for} \index{traversal} \index{loop!traversal} @@ -6080,59 +6207,59 @@ \section{Traversal with a {\tt for} loop} A lot of computations involve processing a string one character at a time. Often they start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of -processing is called a {\bf traversal}. One way to write a traversal -is with a {\tt while} loop: +processing is called a {\textbf traversal}. One way to write a traversal +is with a {\texttt while} loop: -\begin{verbatim} +\begin{lstlisting} index = 0 while index < len(fruit): letter = fruit[index] print(letter) index = index + 1 -\end{verbatim} +\end{lstlisting} % This loop traverses the string and displays each letter on a line by -itself. The loop condition is {\tt index < len(fruit)}, so -when {\tt index} is equal to the length of the string, the +itself. The loop condition is {\texttt index < len(fruit)}, so +when {\texttt index} is equal to the length of the string, the condition is false, and the body of the loop doesn't run. The -last character accessed is the one with the index {\tt len(fruit)-1}, +last character accessed is the one with the index {\texttt len(fruit)-1}, which is the last character in the string. As an exercise, write a function that takes a string as an argument and displays the letters backward, one per line. -Another way to write a traversal is with a {\tt for} loop: +Another way to write a traversal is with a {\texttt for} loop: -\begin{verbatim} +\begin{lstlisting} for letter in fruit: print(letter) -\end{verbatim} +\end{lstlisting} % Each time through the loop, the next character in the string is assigned -to the variable {\tt letter}. The loop continues until no characters are +to the variable {\texttt letter}. The loop continues until no characters are left. \index{concatenation} \index{abecedarian} \index{McCloskey, Robert} The following example shows how to use concatenation (string addition) -and a {\tt for} loop to generate an abecedarian series (that is, in +and a {\texttt for} loop to generate an abecedarian series (that is, in alphabetical order). In Robert McCloskey's book {\em Make Way for Ducklings}, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop outputs these names in order: -\begin{verbatim} +\begin{lstlisting} prefixes = 'JKLMNOPQ' suffix = 'ack' for letter in prefixes: print(letter + suffix) -\end{verbatim} +\end{lstlisting} % The output is: -\begin{verbatim} +\begin{lstlisting} Jack Kack Lack @@ -6141,7 +6268,7 @@ \section{Traversal with a {\tt for} loop} Oack Pack Qack -\end{verbatim} +\end{lstlisting} % Of course, that's not quite right because ``Ouack'' and ``Quack'' are misspelled. As an exercise, modify the program to fix this error. @@ -6153,18 +6280,18 @@ \section{String slices} \index{slice operator} \index{operator!slice} \index{index!slice} \index{string!slice} \index{slice!string} -A segment of a string is called a {\bf slice}. Selecting a slice is +A segment of a string is called a {\textbf slice}. Selecting a slice is similar to selecting a character: -\begin{verbatim} +\begin{lstlisting} >>> s = 'Monty Python' >>> s[0:5] 'Monty' >>> s[6:12] 'Python' -\end{verbatim} +\end{lstlisting} % -The operator {\tt [n:m]} returns the part of the string from the +The operator {\texttt [n:m]} returns the part of the string from the ``n-eth'' character to the ``m-eth'' character, including the first but excluding the last. This behavior is counterintuitive, but it might help to imagine the indices pointing {\em between} the @@ -6181,29 +6308,29 @@ \section{String slices} the beginning of the string. If you omit the second index, the slice goes to the end of the string: -\begin{verbatim} +\begin{lstlisting} >>> fruit = 'banana' >>> fruit[:3] 'ban' >>> fruit[3:] 'ana' -\end{verbatim} +\end{lstlisting} % If the first index is greater than or equal to the second the result -is an {\bf empty string}, represented by two quotation marks: +is an {\textbf empty string}, represented by two quotation marks: \index{quotation mark} -\begin{verbatim} +\begin{lstlisting} >>> fruit = 'banana' >>> fruit[3:3] '' -\end{verbatim} +\end{lstlisting} % An empty string contains no characters and has length 0, but other than that, it is the same as any other string. Continuing this example, what do you think -{\tt fruit[:]} means? Try it and see. +{\texttt fruit[:]} means? Try it and see. \index{copy!slice} \index{slice!copy} @@ -6214,17 +6341,17 @@ \section{Strings are immutable} \index{immutability} \index{string!immutable} -It is tempting to use the {\tt []} operator on the left side of an +It is tempting to use the {\texttt []} operator on the left side of an assignment, with the intention of changing a character in a string. For example: \index{TypeError} \index{exception!TypeError} -\begin{verbatim} +\begin{lstlisting} >>> greeting = 'Hello, world!' >>> greeting[0] = 'J' TypeError: 'str' object does not support item assignment -\end{verbatim} +\end{lstlisting} % The ``object'' in this case is the string and the ``item'' is the character you tried to assign. For now, an object is @@ -6237,19 +6364,19 @@ \section{Strings are immutable} \index{immutability} The reason for the error is that -strings are {\bf immutable}, which means you can't change an +strings are {\textbf immutable}, which means you can't change an existing string. The best you can do is create a new string that is a variation on the original: -\begin{verbatim} +\begin{lstlisting} >>> greeting = 'Hello, world!' >>> new_greeting = 'J' + greeting[1:] >>> new_greeting 'Jello, world!' -\end{verbatim} +\end{lstlisting} % This example concatenates a new first letter onto -a slice of {\tt greeting}. It has no effect on +a slice of {\texttt greeting}. It has no effect on the original string. \index{concatenation} @@ -6261,7 +6388,7 @@ \section{Searching} \index{find function} \index{function!find} -\begin{verbatim} +\begin{lstlisting} def find(word, letter): index = 0 while index < len(word): @@ -6269,29 +6396,29 @@ \section{Searching} return index index = index + 1 return -1 -\end{verbatim} +\end{lstlisting} % -In a sense, {\tt find} is the inverse of the {\tt []} operator. +In a sense, {\texttt find} is the inverse of the {\texttt []} operator. Instead of taking an index and extracting the corresponding character, it takes a character and finds the index where that character -appears. If the character is not found, the function returns {\tt +appears. If the character is not found, the function returns {\texttt -1}. -This is the first example we have seen of a {\tt return} statement -inside a loop. If {\tt word[index] == letter}, the function breaks +This is the first example we have seen of a {\texttt return} statement +inside a loop. If {\texttt word[index] == letter}, the function breaks out of the loop and returns immediately. If the character doesn't appear in the string, the program -exits the loop normally and returns {\tt -1}. +exits the loop normally and returns {\texttt -1}. This pattern of computation---traversing a sequence and returning -when we find what we are looking for---is called a {\bf search}. +when we find what we are looking for---is called a {\textbf search}. \index{traversal} \index{search pattern} \index{pattern!search} -As an exercise, modify {\tt find} so that it has a -third parameter, the index in {\tt word} where it should start +As an exercise, modify {\texttt find} so that it has a +third parameter, the index in {\texttt word} where it should start looking. @@ -6302,31 +6429,31 @@ \section{Looping and counting} \index{looping and counting} \index{looping!with strings} -The following program counts the number of times the letter {\tt a} +The following program counts the number of times the letter {\texttt a} appears in a string: -\begin{verbatim} +\begin{lstlisting} word = 'banana' count = 0 for letter in word: if letter == 'a': count = count + 1 print(count) -\end{verbatim} +\end{lstlisting} % -This program demonstrates another pattern of computation called a {\bf -counter}. The variable {\tt count} is initialized to 0 and then -incremented each time an {\tt a} is found. -When the loop exits, {\tt count} -contains the result---the total number of {\tt a}'s. +This program demonstrates another pattern of computation called a {\textbf +counter}. The variable {\texttt count} is initialized to 0 and then +incremented each time an {\texttt a} is found. +When the loop exits, {\texttt count} +contains the result---the total number of {\texttt a}'s. \index{encapsulation} -As an exercise, encapsulate this code in a function named {\tt +As an exercise, encapsulate this code in a function named {\texttt count}, and generalize it so that it accepts the string and the letter as arguments. Then rewrite the function so that instead of -traversing the string, it uses the three-parameter version of {\tt +traversing the string, it uses the three-parameter version of {\texttt find} from the previous section. @@ -6336,106 +6463,106 @@ \section{String methods} Strings provide methods that perform a variety of useful operations. A method is similar to a function---it takes arguments and returns a value---but the syntax is different. For example, the -method {\tt upper} takes a string and returns a new string with +method {\texttt upper} takes a string and returns a new string with all uppercase letters. \index{method} \index{string!method} -Instead of the function syntax {\tt upper(word)}, it uses -the method syntax {\tt word.upper()}. +Instead of the function syntax {\texttt upper(word)}, it uses +the method syntax {\texttt word.upper()}. -\begin{verbatim} +\begin{lstlisting} >>> word = 'banana' >>> new_word = word.upper() >>> new_word 'BANANA' -\end{verbatim} +\end{lstlisting} % -This form of dot notation specifies the name of the method, {\tt -upper}, and the name of the string to apply the method to, {\tt +This form of dot notation specifies the name of the method, {\texttt +upper}, and the name of the string to apply the method to, {\texttt word}. The empty parentheses indicate that this method takes no arguments. \index{parentheses!empty} \index{dot notation} -A method call is called an {\bf invocation}; in this case, we would -say that we are invoking {\tt upper} on {\tt word}. +A method call is called an {\textbf invocation}; in this case, we would +say that we are invoking {\texttt upper} on {\texttt word}. \index{invocation} -As it turns out, there is a string method named {\tt find} that +As it turns out, there is a string method named {\texttt find} that is remarkably similar to the function we wrote: -\begin{verbatim} +\begin{lstlisting} >>> word = 'banana' >>> index = word.find('a') >>> index 1 -\end{verbatim} +\end{lstlisting} % -In this example, we invoke {\tt find} on {\tt word} and pass +In this example, we invoke {\texttt find} on {\texttt word} and pass the letter we are looking for as a parameter. -Actually, the {\tt find} method is more general than our function; +Actually, the {\texttt find} method is more general than our function; it can find substrings, not just characters: -\begin{verbatim} +\begin{lstlisting} >>> word.find('na') 2 -\end{verbatim} +\end{lstlisting} % -By default, {\tt find} starts at the beginning of the string, but +By default, {\texttt find} starts at the beginning of the string, but it can take a second argument, the index where it should start: \index{optional argument} \index{argument!optional} -\begin{verbatim} +\begin{lstlisting} >>> word.find('na', 3) 4 -\end{verbatim} +\end{lstlisting} % -This is an example of an {\bf optional argument}; -{\tt find} can +This is an example of an {\textbf optional argument}; +{\texttt find} can also take a third argument, the index where it should stop: -\begin{verbatim} +\begin{lstlisting} >>> name = 'bob' >>> name.find('b', 1, 2) -1 -\end{verbatim} +\end{lstlisting} % -This search fails because {\tt b} does not -appear in the index range from {\tt 1} to {\tt 2}, not including {\tt +This search fails because {\texttt b} does not +appear in the index range from {\texttt 1} to {\texttt 2}, not including {\texttt 2}. Searching up to, but not including, the second index makes -{\tt find} consistent with the slice operator. +{\texttt find} consistent with the slice operator. -\section{The {\tt in} operator} +\section{The {\texttt in} operator} \label{inboth} \index{in operator} \index{operator!in} \index{boolean operator} \index{operator!boolean} -The word {\tt in} is a boolean operator that takes two strings and -returns {\tt True} if the first appears as a substring in the second: +The word {\texttt in} is a boolean operator that takes two strings and +returns {\texttt True} if the first appears as a substring in the second: -\begin{verbatim} +\begin{lstlisting} >>> 'a' in 'banana' True >>> 'seed' in 'banana' False -\end{verbatim} +\end{lstlisting} % For example, the following function prints all the -letters from {\tt word1} that also appear in {\tt word2}: +letters from {\texttt word1} that also appear in {\texttt word2}: -\begin{verbatim} +\begin{lstlisting} def in_both(word1, word2): for letter in word1: if letter in word2: print(letter) -\end{verbatim} +\end{lstlisting} % With well-chosen variable names, Python sometimes reads like English. You could read @@ -6444,12 +6571,12 @@ \section{The {\tt in} operator} Here's what you get if you compare apples and oranges: -\begin{verbatim} +\begin{lstlisting} >>> in_both('apples', 'oranges') a e s -\end{verbatim} +\end{lstlisting} % \section{String comparison} @@ -6458,30 +6585,30 @@ \section{String comparison} The relational operators work on strings. To see if two strings are equal: -\begin{verbatim} +\begin{lstlisting} if word == 'banana': print('All right, bananas.') -\end{verbatim} +\end{lstlisting} % Other relational operations are useful for putting words in alphabetical order: -\begin{verbatim} +\begin{lstlisting} if word < 'banana': print('Your word, ' + word + ', comes before banana.') elif word > 'banana': print('Your word, ' + word + ', comes after banana.') else: print('All right, bananas.') -\end{verbatim} +\end{lstlisting} % Python does not handle uppercase and lowercase letters the same way people do. All the uppercase letters come before all the lowercase letters, so: -\begin{verbatim} +\begin{lstlisting} Your word, Pineapple, comes before banana. -\end{verbatim} +\end{lstlisting} % A common way to address this problem is to convert strings to a standard format, such as all lowercase, before performing the @@ -6496,10 +6623,10 @@ \section{Debugging} When you use indices to traverse the values in a sequence, it is tricky to get the beginning and end of the traversal right. Here is a function that is supposed to compare two -words and return {\tt True} if one of the words is the reverse +words and return {\texttt True} if one of the words is the reverse of the other, but it contains two errors: -\begin{verbatim} +\begin{lstlisting} def is_reverse(word1, word2): if len(word1) != len(word2): return False @@ -6514,10 +6641,10 @@ \section{Debugging} j = j-1 return True -\end{verbatim} +\end{lstlisting} % -The first {\tt if} statement checks whether the words are the -same length. If not, we can return {\tt False} immediately. +The first {\texttt if} statement checks whether the words are the +same length. If not, we can return {\texttt False} immediately. Otherwise, for the rest of the function, we can assume that the words are the same length. This is an example of the guardian pattern in Section~\ref{guardian}. @@ -6525,30 +6652,30 @@ \section{Debugging} \index{pattern!guardian} \index{index} -{\tt i} and {\tt j} are indices: {\tt i} traverses {\tt word1} -forward while {\tt j} traverses {\tt word2} backward. If we find -two letters that don't match, we can return {\tt False} immediately. +{\texttt i} and {\texttt j} are indices: {\texttt i} traverses {\texttt word1} +forward while {\texttt j} traverses {\texttt word2} backward. If we find +two letters that don't match, we can return {\texttt False} immediately. If we get through the whole loop and all the letters match, we -return {\tt True}. +return {\texttt True}. If we test this function with the words ``pots'' and ``stop'', we -expect the return value {\tt True}, but we get an IndexError: +expect the return value {\texttt True}, but we get an IndexError: \index{IndexError} \index{exception!IndexError} -\begin{verbatim} +\begin{lstlisting} >>> is_reverse('pots', 'stop') ... File "reverse.py", line 15, in is_reverse if word1[i] != word2[j]: IndexError: string index out of range -\end{verbatim} +\end{lstlisting} % For debugging this kind of error, my first move is to print the values of the indices immediately before the line where the error appears. -\begin{verbatim} +\begin{lstlisting} while j > 0: print(i, j) # print here @@ -6556,31 +6683,31 @@ \section{Debugging} return False i = i+1 j = j-1 -\end{verbatim} +\end{lstlisting} % Now when I run the program again, I get more information: -\begin{verbatim} +\begin{lstlisting} >>> is_reverse('pots', 'stop') 0 4 ... IndexError: string index out of range -\end{verbatim} +\end{lstlisting} % -The first time through the loop, the value of {\tt j} is 4, +The first time through the loop, the value of {\texttt j} is 4, which is out of range for the string \verb"'pots'". The index of the last character is 3, so the -initial value for {\tt j} should be {\tt len(word2)-1}. +initial value for {\texttt j} should be {\texttt len(word2)-1}. If I fix that error and run the program again, I get: -\begin{verbatim} +\begin{lstlisting} >>> is_reverse('pots', 'stop') 0 3 1 2 2 1 True -\end{verbatim} +\end{lstlisting} % This time we get the right answer, but it looks like the loop only ran three times, which is suspicious. To get a better idea of what is @@ -6596,11 +6723,11 @@ \section{Debugging} \end{figure} I took some license by arranging the variables in the frame -and adding dotted lines to show that the values of {\tt i} and -{\tt j} indicate characters in {\tt word1} and {\tt word2}. +and adding dotted lines to show that the values of {\texttt i} and +{\texttt j} indicate characters in {\texttt word1} and {\texttt word2}. Starting with this diagram, run the program on paper, changing the -values of {\tt i} and {\tt j} during each iteration. Find and fix the +values of {\texttt i} and {\texttt j} during each iteration. Find and fix the second error in this function. \label{isreverse} @@ -6663,20 +6790,21 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont \index{string method} \index{method!string} Read the documentation of the string methods at \url{http://docs.python.org/3/library/stdtypes.html#string-methods}. You might want to experiment with some of them to make sure you -understand how they work. {\tt strip} and {\tt replace} are +understand how they work. {\texttt strip} and {\texttt replace} are particularly useful. The documentation uses a syntax that might be confusing. For example, in \verb"find(sub[, start[, end]])", the brackets -indicate optional arguments. So {\tt sub} is required, but -{\tt start} is optional, and if you include {\tt start}, -then {\tt end} is optional. +indicate optional arguments. So {\texttt sub} is required, but +{\texttt start} is optional, and if you include {\texttt start}, +then {\texttt end} is optional. \index{optional argument} \index{argument!optional} @@ -6684,18 +6812,20 @@ \section{Exercises} \begin{exercise} +\normalfont \index{count method} \index{method!count} -There is a string method called {\tt count} that is similar +There is a string method called {\texttt count} that is similar to the function in Section~\ref{counter}. Read the documentation of this method -and write an invocation that counts the number of {\tt a}'s +and write an invocation that counts the number of {\texttt a}'s in \verb"'banana'". \end{exercise} \begin{exercise} +\normalfont \index{step size} \index{slice operator} \index{operator!slice} @@ -6705,11 +6835,11 @@ \section{Exercises} A step size of 2 means every other character; 3 means every third, etc. -\begin{verbatim} +\begin{lstlisting} >>> fruit = 'banana' >>> fruit[0:5:2] 'bnn' -\end{verbatim} +\end{lstlisting} A step size of -1 goes through the word backwards, so the slice \verb"[::-1]" generates a reversed string. @@ -6721,13 +6851,14 @@ \section{Exercises} \begin{exercise} +\normalfont The following functions are all {\em intended} to check whether a string contains any lowercase letters, but at least some of them are wrong. For each function, describe what the function actually does (assuming that the parameter is a string). -\begin{verbatim} +\begin{lstlisting} def any_lowercase1(s): for c in s: if c.islower(): @@ -6758,12 +6889,13 @@ \section{Exercises} if not c.islower(): return False return True -\end{verbatim} +\end{lstlisting} \end{exercise} \begin{exercise} +\normalfont \index{letter rotation} \index{rotation, letter} @@ -6786,15 +6918,15 @@ \section{Exercises} a new string that contains the letters from the original string rotated by the given amount. -You might want to use the built-in function {\tt ord}, which converts -a character to a numeric code, and {\tt chr}, which converts numeric +You might want to use the built-in function {\texttt ord}, which converts +a character to a numeric code, and {\texttt chr}, which converts numeric codes to characters. Letters of the alphabet are encoded in alphabetical order, so for example: -\begin{verbatim} +\begin{lstlisting} >>> ord('c') - ord('a') 2 -\end{verbatim} +\end{lstlisting} Because \verb"'c'" is the two-eth letter of the alphabet. But beware: the numeric codes for upper case letters are different. @@ -6828,16 +6960,16 @@ \section{Reading word lists} lexicon project (see \url{http://wikipedia.org/wiki/Moby_Project}). It is a list of 113,809 official crosswords; that is, words that are considered valid in crossword puzzles and other word games. In the -Moby collection, the filename is {\tt 113809of.fic}; you can download -a copy, with the simpler name {\tt words.txt}, from +Moby collection, the filename is {\texttt 113809of.fic}; you can download +a copy, with the simpler name {\texttt words.txt}, from \url{http://thinkpython2.com/code/words.txt}. \index{Moby Project} \index{crosswords} This file is in plain text, so you can open it with a text editor, but you can also read it from Python. The built-in -function {\tt open} takes the name of the file as a parameter -and returns a {\bf file object} you can use to read the file. +function {\texttt open} takes the name of the file as a parameter +and returns a {\textbf file object} you can use to read the file. \index{open function} \index{function!open} \index{plain text} @@ -6845,20 +6977,20 @@ \section{Reading word lists} \index{object!file} \index{file object} -\begin{verbatim} +\begin{lstlisting} >>> fin = open('words.txt') -\end{verbatim} +\end{lstlisting} % -{\tt fin} is a common name for a file object used for input. The file -object provides several methods for reading, including {\tt readline}, +{\texttt fin} is a common name for a file object used for input. The file +object provides several methods for reading, including {\texttt readline}, which reads characters from the file until it gets to a newline and returns the result as a string: \index{readline method} \index{method!readline} -\begin{verbatim} +\begin{lstlisting} >>> fin.readline() 'aa\r\n' -\end{verbatim} +\end{lstlisting} % The first word in this particular list is ``aa'', which is a kind of lava. The sequence \verb"\r\n" represents two whitespace characters, @@ -6866,39 +6998,39 @@ \section{Reading word lists} next. The file object keeps track of where it is in the file, so -if you call {\tt readline} again, you get the next word: +if you call {\texttt readline} again, you get the next word: -\begin{verbatim} +\begin{lstlisting} >>> fin.readline() 'aah\r\n' -\end{verbatim} +\end{lstlisting} % The next word is ``aah'', which is a perfectly legitimate word, so stop looking at me like that. Or, if it's the whitespace that's bothering you, -we can get rid of it with the string method {\tt strip}: +we can get rid of it with the string method {\texttt strip}: \index{strip method} \index{method!strip} -\begin{verbatim} +\begin{lstlisting} >>> line = fin.readline() >>> word = line.strip() >>> word 'aahed' -\end{verbatim} +\end{lstlisting} % -You can also use a file object as part of a {\tt for} loop. -This program reads {\tt words.txt} and prints each word, one +You can also use a file object as part of a {\texttt for} loop. +This program reads {\texttt words.txt} and prints each word, one per line: \index{open function} \index{function!open} -\begin{verbatim} +\begin{lstlisting} fin = open('words.txt') for line in fin: word = line.strip() print(word) -\end{verbatim} +\end{lstlisting} % \section{Exercises} @@ -6907,13 +7039,15 @@ \section{Exercises} You should at least attempt each one before you read the solutions. \begin{exercise} -Write a program that reads {\tt words.txt} and prints only the +\normalfont +Write a program that reads {\texttt words.txt} and prints only the words with more than 20 characters (not counting whitespace). \index{whitespace} \end{exercise} \begin{exercise} +\normalfont In 1939 Ernest Vincent Wright published a 50,000 word novel called {\em Gadsby} that does not contain the letter ``e''. Since ``e'' is @@ -6925,7 +7059,7 @@ \section{Exercises} All right, I'll stop now. -Write a function called \verb"has_no_e" that returns {\tt True} if +Write a function called \verb"has_no_e" that returns {\texttt True} if the given word doesn't have the letter ``e'' in it. Modify your program from the previous section to print only the words @@ -6936,11 +7070,12 @@ \section{Exercises} \end{exercise} -\begin{exercise} +\begin{exercise} +\normalfont -Write a function named {\tt avoids} +Write a function named {\texttt avoids} that takes a word and a string of forbidden letters, and -that returns {\tt True} if the word doesn't use any of the forbidden +that returns {\texttt True} if the word doesn't use any of the forbidden letters. Modify your program to prompt the user to enter a string @@ -6954,29 +7089,32 @@ \section{Exercises} \begin{exercise} +\normalfont Write a function named \verb"uses_only" that takes a word and a -string of letters, and that returns {\tt True} if the word contains +string of letters, and that returns {\texttt True} if the word contains only letters in the list. Can you make a sentence using only the -letters {\tt acefhlo}? Other than ``Hoe alfalfa?'' +letters {\texttt acefhlo}? Other than ``Hoe alfalfa?'' \end{exercise} -\begin{exercise} +\begin{exercise} +\normalfont Write a function named \verb"uses_all" that takes a word and a -string of required letters, and that returns {\tt True} if the word +string of required letters, and that returns {\texttt True} if the word uses all the required letters at least once. How many words are there -that use all the vowels {\tt aeiou}? How about {\tt aeiouy}? +that use all the vowels {\texttt aeiou}? How about {\texttt aeiouy}? \end{exercise} \begin{exercise} +\normalfont Write a function called \verb"is_abecedarian" that returns -{\tt True} if the letters in a word appear in alphabetical order +{\texttt True} if the letters in a word appear in alphabetical order (double letters are ok). How many abecedarian words are there? @@ -6995,82 +7133,82 @@ \section{Search} in common; they can be solved with the search pattern we saw in Section~\ref{find}. The simplest example is: -\begin{verbatim} +\begin{lstlisting} def has_no_e(word): for letter in word: if letter == 'e': return False return True -\end{verbatim} +\end{lstlisting} % -The {\tt for} loop traverses the characters in {\tt word}. If we find -the letter ``e'', we can immediately return {\tt False}; otherwise we +The {\texttt for} loop traverses the characters in {\texttt word}. If we find +the letter ``e'', we can immediately return {\texttt False}; otherwise we have to go to the next letter. If we exit the loop normally, that -means we didn't find an ``e'', so we return {\tt True}. +means we didn't find an ``e'', so we return {\texttt True}. \index{traversal} \index{in operator} \index{operator!in} -You could write this function more concisely using the {\tt in} +You could write this function more concisely using the {\texttt in} operator, but I started with this version because it demonstrates the logic of the search pattern. \index{generalization} -{\tt avoids} is a more general version of \verb"has_no_e" but it +{\texttt avoids} is a more general version of \verb"has_no_e" but it has the same structure: -\begin{verbatim} +\begin{lstlisting} def avoids(word, forbidden): for letter in word: if letter in forbidden: return False return True -\end{verbatim} +\end{lstlisting} % -We can return {\tt False} as soon as we find a forbidden letter; -if we get to the end of the loop, we return {\tt True}. +We can return {\texttt False} as soon as we find a forbidden letter; +if we get to the end of the loop, we return {\texttt True}. \verb"uses_only" is similar except that the sense of the condition is reversed: -\begin{verbatim} +\begin{lstlisting} def uses_only(word, available): for letter in word: if letter not in available: return False return True -\end{verbatim} +\end{lstlisting} % Instead of a list of forbidden letters, we have a list of available -letters. If we find a letter in {\tt word} that is not in -{\tt available}, we can return {\tt False}. +letters. If we find a letter in {\texttt word} that is not in +{\texttt available}, we can return {\texttt False}. \verb"uses_all" is similar except that we reverse the role of the word and the string of letters: -\begin{verbatim} +\begin{lstlisting} def uses_all(word, required): for letter in required: if letter not in word: return False return True -\end{verbatim} +\end{lstlisting} % -Instead of traversing the letters in {\tt word}, the loop +Instead of traversing the letters in {\texttt word}, the loop traverses the required letters. If any of the required letters -do not appear in the word, we can return {\tt False}. +do not appear in the word, we can return {\texttt False}. \index{traversal} If you were really thinking like a computer scientist, you would have recognized that \verb"uses_all" was an instance of a previously solved problem, and you would have written: -\begin{verbatim} +\begin{lstlisting} def uses_all(word, required): return uses_only(required, word) -\end{verbatim} +\end{lstlisting} % -This is an example of a program development plan called {\bf +This is an example of a program development plan called {\textbf reduction to a previously solved problem}, which means that you recognize the problem you are working on as an instance of a solved problem and apply an existing solution. \index{reduction to a @@ -7081,14 +7219,14 @@ \section{Looping with indices} \index{looping!with indices} \index{index!looping with} -I wrote the functions in the previous section with {\tt for} +I wrote the functions in the previous section with {\texttt for} loops because I only needed the characters in the strings; I didn't have to do anything with the indices. For \verb"is_abecedarian" we have to compare adjacent letters, -which is a little tricky with a {\tt for} loop: +which is a little tricky with a {\texttt for} loop: -\begin{verbatim} +\begin{lstlisting} def is_abecedarian(word): previous = word[0] for c in word: @@ -7096,22 +7234,22 @@ \section{Looping with indices} return False previous = c return True -\end{verbatim} +\end{lstlisting} An alternative is to use recursion: -\begin{verbatim} +\begin{lstlisting} def is_abecedarian(word): if len(word) <= 1: return True if word[0] > word[1]: return False return is_abecedarian(word[1:]) -\end{verbatim} +\end{lstlisting} -Another option is to use a {\tt while} loop: +Another option is to use a {\texttt while} loop: -\begin{verbatim} +\begin{lstlisting} def is_abecedarian(word): i = 0 while i < len(word)-1: @@ -7119,22 +7257,22 @@ \section{Looping with indices} return False i = i+1 return True -\end{verbatim} +\end{lstlisting} % -The loop starts at {\tt i=0} and ends when {\tt i=len(word)-1}. Each +The loop starts at {\texttt i=0} and ends when {\texttt i=len(word)-1}. Each time through the loop, it compares the $i$th character (which you can think of as the current character) to the $i+1$th character (which you can think of as the next). If the next character is less than (alphabetically before) the current one, then we have discovered a break in the abecedarian trend, and -we return {\tt False}. +we return {\texttt False}. If we get to the end of the loop without finding a fault, then the word passes the test. To convince yourself that the loop ends correctly, consider an example like \verb"'flossy'". The length of the word is 6, so -the last time the loop runs is when {\tt i} is 4, which is the +the last time the loop runs is when {\texttt i} is 4, which is the index of the second-to-last character. On the last iteration, it compares the second-to-last character to the last, which is what we want. @@ -7144,7 +7282,7 @@ \section{Looping with indices} Exercise~\ref{palindrome}) that uses two indices; one starts at the beginning and goes up; the other starts at the end and goes down. -\begin{verbatim} +\begin{lstlisting} def is_palindrome(word): i = 0 j = len(word)-1 @@ -7156,17 +7294,17 @@ \section{Looping with indices} j = j-1 return True -\end{verbatim} +\end{lstlisting} Or we could reduce to a previously solved problem and write: \index{reduction to a previously solved problem} \index{development plan!reduction} -\begin{verbatim} +\begin{lstlisting} def is_palindrome(word): return is_reverse(word, word) -\end{verbatim} +\end{lstlisting} % Using \verb"is_reverse" from Section~\ref{isreverse}. @@ -7182,20 +7320,20 @@ \section{Debugging} set of words that test for all possible errors. Taking \verb"has_no_e" as an example, there are two obvious -cases to check: words that have an `e' should return {\tt False}, and -words that don't should return {\tt True}. You should have no +cases to check: words that have an `e' should return {\texttt False}, and +words that don't should return {\texttt True}. You should have no trouble coming up with one of each. Within each case, there are some less obvious subcases. Among the words that have an ``e'', you should test words with an ``e'' at the beginning, the end, and somewhere in the middle. You should test long words, short words, and very short words, like the empty string. The -empty string is an example of a {\bf special case}, which is one of +empty string is an example of a {\textbf special case}, which is one of the non-obvious cases where errors often lurk. \index{special case} In addition to the test cases you generate, you can also test -your program with a word list like {\tt words.txt}. By scanning +your program with a word list like {\texttt words.txt}. By scanning the output, you might be able to catch errors, but be careful: you might catch one kind of error (words that should not be included, but are) and not another (words that should be included, @@ -7239,6 +7377,7 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont \index{Car Talk} \index{Puzzler} \index{double letters} @@ -7265,6 +7404,7 @@ \section{Exercises} \begin{exercise} +\normalfont Here's another {\em Car Talk} Puzzler (\url{http://www.cartalk.com/content/puzzlers}): \index{Car Talk} @@ -7299,6 +7439,7 @@ \section{Exercises} \begin{exercise} +\normalfont Here's another {\em Car Talk} Puzzler you can solve with a search (\url{http://www.cartalk.com/content/puzzlers}): \index{Car Talk} @@ -7321,7 +7462,7 @@ \section{Exercises} \end{quote} Write a Python program that searches for solutions to this Puzzler. -Hint: you might find the string method {\tt zfill} useful. +Hint: you might find the string method {\texttt zfill} useful. Solution: \url{http://thinkpython2.com/code/cartalk3.py}. @@ -7339,9 +7480,9 @@ \chapter{Lists} \section{A list is a sequence} \label{sequence} -Like a string, a {\bf list} is a sequence of values. In a string, the +Like a string, a {\textbf list} is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in -a list are called {\bf elements} or sometimes {\bf items}. +a list are called {\textbf elements} or sometimes {\textbf items}. \index{list} \index{type!list} \index{element} @@ -7351,21 +7492,21 @@ \section{A list is a sequence} There are several ways to create a new list; the simplest is to enclose the elements in square brackets (\verb"[" and \verb"]"): -\begin{verbatim} +\begin{lstlisting} [10, 20, 30, 40] ['crunchy frog', 'ram bladder', 'lark vomit'] -\end{verbatim} +\end{lstlisting} % The first example is a list of four integers. The second is a list of three strings. The elements of a list don't have to be the same type. The following list contains a string, a float, an integer, and (lo!) another list: -\begin{verbatim} +\begin{lstlisting} ['spam', 2.0, 5, [10, 20]] -\end{verbatim} +\end{lstlisting} % -A list within another list is {\bf nested}. +A list within another list is {\textbf nested}. \index{nested list} \index{list!nested} @@ -7377,13 +7518,13 @@ \section{A list is a sequence} As you might expect, you can assign list values to variables: -\begin{verbatim} +\begin{lstlisting} >>> cheeses = ['Cheddar', 'Edam', 'Gouda'] >>> numbers = [42, 123] >>> empty = [] >>> print(cheeses, numbers, empty) ['Cheddar', 'Edam', 'Gouda'] [42, 123] [] -\end{verbatim} +\end{lstlisting} % \index{assignment} @@ -7401,31 +7542,31 @@ \section{Lists are mutable} expression inside the brackets specifies the index. Remember that the indices start at 0: -\begin{verbatim} +\begin{lstlisting} >>> cheeses[0] 'Cheddar' -\end{verbatim} +\end{lstlisting} % Unlike strings, lists are mutable. When the bracket operator appears on the left side of an assignment, it identifies the element of the list that will be assigned. \index{mutability} -\begin{verbatim} +\begin{lstlisting} >>> numbers = [42, 123] >>> numbers[1] = 5 >>> numbers [42, 5] -\end{verbatim} +\end{lstlisting} % -The one-eth element of {\tt numbers}, which +The one-eth element of {\texttt numbers}, which used to be 123, is now 5. \index{index!starting at zero} \index{zero, index starting at} Figure~\ref{fig.liststate} shows -the state diagram for {\tt -cheeses}, {\tt numbers} and {\tt empty}: +the state diagram for {\texttt +cheeses}, {\texttt numbers} and {\texttt empty}: \index{state diagram} \index{diagram!state} @@ -7437,11 +7578,11 @@ \section{Lists are mutable} \end{figure} Lists are represented by boxes with the word ``list'' outside -and the elements of the list inside. {\tt cheeses} refers to +and the elements of the list inside. {\texttt cheeses} refers to a list with three elements indexed 0, 1 and 2. -{\tt numbers} contains two elements; the diagram shows that the +{\texttt numbers} contains two elements; the diagram shows that the value of the second element has been reassigned from 123 to 5. -{\tt empty} refers to a list with no elements. +{\texttt empty} refers to a list with no elements. \index{item assignment} \index{assignment!item} \index{reassignment} @@ -7453,7 +7594,7 @@ \section{Lists are mutable} \item Any integer expression can be used as an index. \item If you try to read or write an element that does not exist, you -get an {\tt IndexError}. +get an {\texttt IndexError}. \index{exception!IndexError} \index{IndexError} @@ -7468,15 +7609,15 @@ \section{Lists are mutable} \index{in operator} \index{operator!in} -The {\tt in} operator also works on lists. +The {\texttt in} operator also works on lists. -\begin{verbatim} +\begin{lstlisting} >>> cheeses = ['Cheddar', 'Edam', 'Gouda'] >>> 'Edam' in cheeses True >>> 'Brie' in cheeses False -\end{verbatim} +\end{lstlisting} \section{Traversing a list} @@ -7487,41 +7628,41 @@ \section{Traversing a list} \index{statement!for} The most common way to traverse the elements of a list is -with a {\tt for} loop. The syntax is the same as for strings: +with a {\texttt for} loop. The syntax is the same as for strings: -\begin{verbatim} +\begin{lstlisting} for cheese in cheeses: print(cheese) -\end{verbatim} +\end{lstlisting} % This works well if you only need to read the elements of the list. But if you want to write or update the elements, you need the indices. A common way to do that is to combine -the built-in functions {\tt range} and {\tt len}: +the built-in functions {\texttt range} and {\texttt len}: \index{looping!with indices} \index{index!looping with} -\begin{verbatim} +\begin{lstlisting} for i in range(len(numbers)): numbers[i] = numbers[i] * 2 -\end{verbatim} +\end{lstlisting} % -This loop traverses the list and updates each element. {\tt len} -returns the number of elements in the list. {\tt range} returns +This loop traverses the list and updates each element. {\texttt len} +returns the number of elements in the list. {\texttt range} returns a list of indices from 0 to $n-1$, where $n$ is the length of -the list. Each time through the loop {\tt i} gets the index +the list. Each time through the loop {\texttt i} gets the index of the next element. The assignment statement in the body uses -{\tt i} to read the old value of the element and to assign the +{\texttt i} to read the old value of the element and to assign the new value. \index{item update} \index{update!item} -A {\tt for} loop over an empty list never runs the body: +A {\texttt for} loop over an empty list never runs the body: -\begin{verbatim} +\begin{lstlisting} for x in []: print('This never happens.') -\end{verbatim} +\end{lstlisting} % Although a list can contain another list, the nested list still counts as a single element. The length of this list is @@ -7529,40 +7670,40 @@ \section{Traversing a list} \index{nested list} \index{list!nested} -\begin{verbatim} +\begin{lstlisting} ['spam', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]] -\end{verbatim} +\end{lstlisting} \section{List operations} \index{list!operation} -The {\tt +} operator concatenates lists: +The {\texttt +} operator concatenates lists: \index{concatenation!list} \index{list!concatenation} -\begin{verbatim} +\begin{lstlisting} >>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> c = a + b >>> c [1, 2, 3, 4, 5, 6] -\end{verbatim} +\end{lstlisting} % -The {\tt *} operator repeats a list a given number of times: +The {\texttt *} operator repeats a list a given number of times: \index{repetition!list} \index{list!repetition} -\begin{verbatim} +\begin{lstlisting} >>> [0] * 4 [0, 0, 0, 0] >>> [1, 2, 3] * 3 [1, 2, 3, 1, 2, 3, 1, 2, 3] -\end{verbatim} +\end{lstlisting} % -The first example repeats {\tt [0]} four times. The second example -repeats the list {\tt [1, 2, 3]} three times. +The first example repeats {\texttt [0]} four times. The second example +repeats the list {\texttt [1, 2, 3]} three times. \section{List slices} @@ -7574,7 +7715,7 @@ \section{List slices} The slice operator also works on lists: -\begin{verbatim} +\begin{lstlisting} >>> t = ['a', 'b', 'c', 'd', 'e', 'f'] >>> t[1:3] ['b', 'c'] @@ -7582,7 +7723,7 @@ \section{List slices} ['a', 'b', 'c', 'd'] >>> t[3:] ['d', 'e', 'f'] -\end{verbatim} +\end{lstlisting} % If you omit the first index, the slice starts at the beginning. If you omit the second, the slice goes to the end. So if you @@ -7591,10 +7732,10 @@ \section{List slices} \index{slice!copy} \index{copy!slice} -\begin{verbatim} +\begin{lstlisting} >>> t[:] ['a', 'b', 'c', 'd', 'e', 'f'] -\end{verbatim} +\end{lstlisting} % Since lists are mutable, it is often useful to make a copy before performing operations that modify lists. @@ -7605,34 +7746,34 @@ \section{List slices} \index{slice!update} \index{update!slice} -\begin{verbatim} +\begin{lstlisting} >>> t = ['a', 'b', 'c', 'd', 'e', 'f'] >>> t[1:3] = ['x', 'y'] >>> t ['a', 'x', 'y', 'd', 'e', 'f'] -\end{verbatim} +\end{lstlisting} % % You can add elements to a list by squeezing them into an empty % slice: -% % \begin{verbatim} +% % \begin{lstlisting} % >>> t = ['a', 'd', 'e', 'f'] % >>> t[1:1] = ['b', 'c'] % >>> print t % ['a', 'b', 'c', 'd', 'e', 'f'] -% \end{verbatim} +% \end{lstlisting} % \afterverb % % And you can remove elements from a list by assigning the empty list to % them: -% % \begin{verbatim} +% % \begin{lstlisting} % >>> t = ['a', 'b', 'c', 'd', 'e', 'f'] % >>> t[1:3] = [] % >>> print t % ['a', 'd', 'e', 'f'] -% \end{verbatim} +% \end{lstlisting} % \afterverb % % But both of those operations can be expressed more clearly @@ -7644,45 +7785,45 @@ \section{List methods} \index{method, list} Python provides methods that operate on lists. For example, -{\tt append} adds a new element to the end of a list: +{\texttt append} adds a new element to the end of a list: \index{append method} \index{method!append} -\begin{verbatim} +\begin{lstlisting} >>> t = ['a', 'b', 'c'] >>> t.append('d') >>> t ['a', 'b', 'c', 'd'] -\end{verbatim} +\end{lstlisting} % -{\tt extend} takes a list as an argument and appends all of +{\texttt extend} takes a list as an argument and appends all of the elements: \index{extend method} \index{method!extend} -\begin{verbatim} +\begin{lstlisting} >>> t1 = ['a', 'b', 'c'] >>> t2 = ['d', 'e'] >>> t1.extend(t2) >>> t1 ['a', 'b', 'c', 'd', 'e'] -\end{verbatim} +\end{lstlisting} % -This example leaves {\tt t2} unmodified. +This example leaves {\texttt t2} unmodified. -{\tt sort} arranges the elements of the list from low to high: +{\texttt sort} arranges the elements of the list from low to high: \index{sort method} \index{method!sort} -\begin{verbatim} +\begin{lstlisting} >>> t = ['d', 'c', 'e', 'b', 'a'] >>> t.sort() >>> t ['a', 'b', 'c', 'd', 'e'] -\end{verbatim} +\end{lstlisting} % -Most list methods are void; they modify the list and return {\tt None}. -If you accidentally write {\tt t = t.sort()}, you will be disappointed +Most list methods are void; they modify the list and return {\texttt None}. +If you accidentally write {\texttt t = t.sort()}, you will be disappointed with the result. \index{void method} \index{method!void} @@ -7697,49 +7838,49 @@ \section{Map, filter and reduce} % see add.py -\begin{verbatim} +\begin{lstlisting} def add_all(t): total = 0 for x in t: total += x return total -\end{verbatim} +\end{lstlisting} % -{\tt total} is initialized to 0. Each time through the loop, -{\tt x} gets one element from the list. The {\tt +=} operator +{\texttt total} is initialized to 0. Each time through the loop, +{\texttt x} gets one element from the list. The {\texttt +=} operator provides a short way to update a variable. This -{\bf augmented assignment statement}, +{\textbf augmented assignment statement}, \index{update operator} \index{operator!update} \index{assignment!augmented} \index{augmented assignment} -\begin{verbatim} +\begin{lstlisting} total += x -\end{verbatim} +\end{lstlisting} % is equivalent to -\begin{verbatim} +\begin{lstlisting} total = total + x -\end{verbatim} +\end{lstlisting} % -As the loop runs, {\tt total} accumulates the sum of the +As the loop runs, {\texttt total} accumulates the sum of the elements; a variable used this way is sometimes called an -{\bf accumulator}. +{\textbf accumulator}. \index{accumulator!sum} Adding up the elements of a list is such a common operation -that Python provides it as a built-in function, {\tt sum}: +that Python provides it as a built-in function, {\texttt sum}: -\begin{verbatim} +\begin{lstlisting} >>> t = [1, 2, 3] >>> sum(t) 6 -\end{verbatim} +\end{lstlisting} % An operation like this that combines a sequence of elements into -a single value is sometimes called {\bf reduce}. +a single value is sometimes called {\textbf reduce}. \index{reduce pattern} \index{pattern!reduce} \index{traversal} @@ -7748,21 +7889,21 @@ \section{Map, filter and reduce} another. For example, the following function takes a list of strings and returns a new list that contains capitalized strings: -\begin{verbatim} +\begin{lstlisting} def capitalize_all(t): res = [] for s in t: res.append(s.capitalize()) return res -\end{verbatim} +\end{lstlisting} % -{\tt res} is initialized with an empty list; each time through -the loop, we append the next element. So {\tt res} is another +{\texttt res} is initialized with an empty list; each time through +the loop, we append the next element. So {\texttt res} is another kind of accumulator. \index{accumulator!list} -An operation like \verb"capitalize_all" is sometimes called a {\bf -map} because it ``maps'' a function (in this case the method {\tt +An operation like \verb"capitalize_all" is sometimes called a {\textbf +map} because it ``maps'' a function (in this case the method {\texttt capitalize}) onto each of the elements in a sequence. \index{map pattern} \index{pattern!map} @@ -7774,19 +7915,19 @@ \section{Map, filter and reduce} function takes a list of strings and returns a list that contains only the uppercase strings: -\begin{verbatim} +\begin{lstlisting} def only_upper(t): res = [] for s in t: if s.isupper(): res.append(s) return res -\end{verbatim} +\end{lstlisting} % -{\tt isupper} is a string method that returns {\tt True} if +{\texttt isupper} is a string method that returns {\texttt True} if the string contains only upper case letters. -An operation like \verb"only_upper" is called a {\bf filter} because +An operation like \verb"only_upper" is called a {\textbf filter} because it selects some of the elements and filters out the others. Most common list operations can be expressed as a combination @@ -7799,60 +7940,60 @@ \section{Deleting elements} There are several ways to delete elements from a list. If you know the index of the element you want, you can use -{\tt pop}: +{\texttt pop}: \index{pop method} \index{method!pop} -\begin{verbatim} +\begin{lstlisting} >>> t = ['a', 'b', 'c'] >>> x = t.pop(1) >>> t ['a', 'c'] >>> x 'b' -\end{verbatim} +\end{lstlisting} % -{\tt pop} modifies the list and returns the element that was removed. +{\texttt pop} modifies the list and returns the element that was removed. If you don't provide an index, it deletes and returns the last element. -If you don't need the removed value, you can use the {\tt del} +If you don't need the removed value, you can use the {\texttt del} operator: \index{del operator} \index{operator!del} -\begin{verbatim} +\begin{lstlisting} >>> t = ['a', 'b', 'c'] >>> del t[1] >>> t ['a', 'c'] -\end{verbatim} +\end{lstlisting} % If you know the element you want to remove (but not the index), you -can use {\tt remove}: +can use {\texttt remove}: \index{remove method} \index{method!remove} -\begin{verbatim} +\begin{lstlisting} >>> t = ['a', 'b', 'c'] >>> t.remove('b') >>> t ['a', 'c'] -\end{verbatim} +\end{lstlisting} % -The return value from {\tt remove} is {\tt None}. +The return value from {\texttt remove} is {\texttt None}. \index{None special value} \index{special value!None} -To remove more than one element, you can use {\tt del} with +To remove more than one element, you can use {\texttt del} with a slice index: -\begin{verbatim} +\begin{lstlisting} >>> t = ['a', 'b', 'c', 'd', 'e', 'f'] >>> del t[1:5] >>> t ['a', 'f'] -\end{verbatim} +\end{lstlisting} % As usual, the slice selects all the elements up to but not including the second index. @@ -7867,35 +8008,35 @@ \section{Lists and strings} A string is a sequence of characters and a list is a sequence of values, but a list of characters is not the same as a string. To convert from a string to a list of characters, -you can use {\tt list}: +you can use {\texttt list}: \index{list!function} \index{function!list} -\begin{verbatim} +\begin{lstlisting} >>> s = 'spam' >>> t = list(s) >>> t ['s', 'p', 'a', 'm'] -\end{verbatim} +\end{lstlisting} % -Because {\tt list} is the name of a built-in function, you should -avoid using it as a variable name. I also avoid {\tt l} because -it looks too much like {\tt 1}. So that's why I use {\tt t}. +Because {\texttt list} is the name of a built-in function, you should +avoid using it as a variable name. I also avoid {\texttt l} because +it looks too much like {\texttt 1}. So that's why I use {\texttt t}. -The {\tt list} function breaks a string into individual letters. If -you want to break a string into words, you can use the {\tt split} +The {\texttt list} function breaks a string into individual letters. If +you want to break a string into words, you can use the {\texttt split} method: \index{split method} \index{method!split} -\begin{verbatim} +\begin{lstlisting} >>> s = 'pining for the fjords' >>> t = s.split() >>> t ['pining', 'for', 'the', 'fjords'] -\end{verbatim} +\end{lstlisting} % -An optional argument called a {\bf delimiter} specifies which +An optional argument called a {\textbf delimiter} specifies which characters to use as word boundaries. The following example uses a hyphen as a delimiter: @@ -7903,33 +8044,33 @@ \section{Lists and strings} \index{argument!optional} \index{delimiter} -\begin{verbatim} +\begin{lstlisting} >>> s = 'spam-spam-spam' >>> delimiter = '-' >>> t = s.split(delimiter) >>> t ['spam', 'spam', 'spam'] -\end{verbatim} +\end{lstlisting} % -{\tt join} is the inverse of {\tt split}. It +{\texttt join} is the inverse of {\texttt split}. It takes a list of strings and -concatenates the elements. {\tt join} is a string method, +concatenates the elements. {\texttt join} is a string method, so you have to invoke it on the delimiter and pass the list as a parameter: \index{join method} \index{method!join} \index{concatenation} -\begin{verbatim} +\begin{lstlisting} >>> t = ['pining', 'for', 'the', 'fjords'] >>> delimiter = ' ' >>> s = delimiter.join(t) >>> s 'pining for the fjords' -\end{verbatim} +\end{lstlisting} % In this case the delimiter is a space character, so -{\tt join} puts a space between words. To concatenate +{\texttt join} puts a space between words. To concatenate strings without spaces, you can use the empty string, \verb"''", as a delimiter. \index{empty string} @@ -7943,12 +8084,12 @@ \section{Objects and values} If we run these assignment statements: -\begin{verbatim} +\begin{lstlisting} a = 'banana' b = 'banana' -\end{verbatim} +\end{lstlisting} % -We know that {\tt a} and {\tt b} both refer to a +We know that {\texttt a} and {\texttt b} both refer to a string, but we don't know whether they refer to the {\em same} string. There are two possible states, shown in Figure~\ref{fig.list1}. @@ -7961,32 +8102,32 @@ \section{Objects and values} \label{fig.list1} \end{figure} -In one case, {\tt a} and {\tt b} refer to two different objects that +In one case, {\texttt a} and {\texttt b} refer to two different objects that have the same value. In the second case, they refer to the same object. \index{is operator} \index{operator!is} To check whether two variables refer to the same object, you can -use the {\tt is} operator. +use the {\texttt is} operator. -\begin{verbatim} +\begin{lstlisting} >>> a = 'banana' >>> b = 'banana' >>> a is b True -\end{verbatim} +\end{lstlisting} % -In this example, Python only created one string object, and both {\tt - a} and {\tt b} refer to it. But when you create two lists, you get +In this example, Python only created one string object, and both {\texttt + a} and {\texttt b} refer to it. But when you create two lists, you get two objects: -\begin{verbatim} +\begin{lstlisting} >>> a = [1, 2, 3] >>> b = [1, 2, 3] >>> a is b False -\end{verbatim} +\end{lstlisting} % So the state diagram looks like Figure~\ref{fig.list2}. \index{state diagram} @@ -7999,8 +8140,8 @@ \section{Objects and values} \label{fig.list2} \end{figure} -In this case we would say that the two lists are {\bf equivalent}, -because they have the same elements, but not {\bf identical}, because +In this case we would say that the two lists are {\textbf equivalent}, +because they have the same elements, but not {\textbf identical}, because they are not the same object. If two objects are identical, they are also equivalent, but if they are equivalent, they are not necessarily identical. @@ -8009,7 +8150,7 @@ \section{Objects and values} Until now, we have been using ``object'' and ``value'' interchangeably, but it is more precise to say that an object has a -value. If you evaluate {\tt [1, 2, 3]}, you get a list +value. If you evaluate {\texttt [1, 2, 3]}, you get a list object whose value is a sequence of integers. If another list has the same elements, we say it has the same value, but it is not the same object. @@ -8021,15 +8162,15 @@ \section{Aliasing} \index{aliasing} \index{reference!aliasing} -If {\tt a} refers to an object and you assign {\tt b = a}, +If {\texttt a} refers to an object and you assign {\texttt b = a}, then both variables refer to the same object: -\begin{verbatim} +\begin{lstlisting} >>> a = [1, 2, 3] >>> b = a >>> b is a True -\end{verbatim} +\end{lstlisting} % The state diagram looks like Figure~\ref{fig.list3}. \index{state diagram} @@ -8042,23 +8183,23 @@ \section{Aliasing} \label{fig.list3} \end{figure} -The association of a variable with an object is called a {\bf +The association of a variable with an object is called a {\textbf reference}. In this example, there are two references to the same object. \index{reference} An object with more than one reference has more -than one name, so we say that the object is {\bf aliased}. +than one name, so we say that the object is {\textbf aliased}. \index{mutability} If the aliased object is mutable, changes made with one alias affect the other: -\begin{verbatim} +\begin{lstlisting} >>> b[0] = 42 >>> a [42, 2, 3] -\end{verbatim} +\end{lstlisting} % Although this behavior can be useful, it is error-prone. In general, it is safer to avoid aliasing when you are working with mutable @@ -8068,12 +8209,12 @@ \section{Aliasing} For immutable objects like strings, aliasing is not as much of a problem. In this example: -\begin{verbatim} +\begin{lstlisting} a = 'banana' b = 'banana' -\end{verbatim} +\end{lstlisting} % -It almost never makes a difference whether {\tt a} and {\tt b} refer +It almost never makes a difference whether {\texttt a} and {\texttt b} refer to the same string or not. @@ -8090,21 +8231,21 @@ \section{List arguments} the change. For example, \verb"delete_head" removes the first element from a list: -\begin{verbatim} +\begin{lstlisting} def delete_head(t): del t[0] -\end{verbatim} +\end{lstlisting} % Here's how it is used: -\begin{verbatim} +\begin{lstlisting} >>> letters = ['a', 'b', 'c'] >>> delete_head(letters) >>> letters ['b', 'c'] -\end{verbatim} +\end{lstlisting} % -The parameter {\tt t} and the variable {\tt letters} are +The parameter {\texttt t} and the variable {\texttt letters} are aliases for the same object. The stack diagram looks like Figure~\ref{fig.stack5}. \index{stack diagram} @@ -8122,35 +8263,35 @@ \section{List arguments} It is important to distinguish between operations that modify lists and operations that create new lists. For -example, the {\tt append} method modifies a list, but the -{\tt +} operator creates a new list. +example, the {\texttt append} method modifies a list, but the +{\texttt +} operator creates a new list. \index{append method} \index{method!append} \index{list!concatenation} \index{concatenation!list} -Here's an example using {\tt append}: +Here's an example using {\texttt append}: % -\begin{verbatim} +\begin{lstlisting} >>> t1 = [1, 2] >>> t2 = t1.append(3) >>> t1 [1, 2, 3] >>> t2 None -\end{verbatim} +\end{lstlisting} % -The return value from {\tt append} is {\tt None}. +The return value from {\texttt append} is {\texttt None}. -Here's an example using the {\tt +} operator: +Here's an example using the {\texttt +} operator: % -\begin{verbatim} +\begin{lstlisting} >>> t3 = t1 + [4] >>> t1 [1, 2, 3] >>> t3 [1, 2, 3, 4] -\end{verbatim} +\end{lstlisting} % The result of the operator is a new list, and the original list is unchanged. @@ -8159,46 +8300,46 @@ \section{List arguments} are supposed to modify lists. For example, this function {\em does not} delete the head of a list: % -\begin{verbatim} +\begin{lstlisting} def bad_delete_head(t): t = t[1:] # WRONG! -\end{verbatim} +\end{lstlisting} % The slice operator creates a new list and the assignment -makes {\tt t} refer to it, but that doesn't affect the caller. +makes {\texttt t} refer to it, but that doesn't affect the caller. \index{slice operator} \index{operator!slice} % -\begin{verbatim} +\begin{lstlisting} >>> t4 = [1, 2, 3] >>> bad_delete_head(t4) >>> t4 [1, 2, 3] -\end{verbatim} +\end{lstlisting} % -At the beginning of \verb"bad_delete_head", {\tt t} and {\tt t4} -refer to the same list. At the end, {\tt t} refers to a new list, -but {\tt t4} still refers to the original, unmodified list. +At the beginning of \verb"bad_delete_head", {\texttt t} and {\texttt t4} +refer to the same list. At the end, {\texttt t} refers to a new list, +but {\texttt t4} still refers to the original, unmodified list. An alternative is to write a function that creates and returns a new list. For -example, {\tt tail} returns all but the first +example, {\texttt tail} returns all but the first element of a list: -\begin{verbatim} +\begin{lstlisting} def tail(t): return t[1:] -\end{verbatim} +\end{lstlisting} % This function leaves the original list unmodified. Here's how it is used: -\begin{verbatim} +\begin{lstlisting} >>> letters = ['a', 'b', 'c'] >>> rest = tail(letters) >>> rest ['b', 'c'] -\end{verbatim} +\end{lstlisting} @@ -8212,25 +8353,25 @@ \section{Debugging} \begin{enumerate} \item Most list methods modify the argument and - return {\tt None}. This is the opposite of the string methods, + return {\texttt None}. This is the opposite of the string methods, which return a new string and leave the original alone. If you are used to writing string code like this: -\begin{verbatim} +\begin{lstlisting} word = word.strip() -\end{verbatim} +\end{lstlisting} It is tempting to write list code like this: -\begin{verbatim} +\begin{lstlisting} t = t.sort() # WRONG! -\end{verbatim} +\end{lstlisting} \index{sort method} \index{method!sort} -Because {\tt sort} returns {\tt None}, the -next operation you perform with {\tt t} is likely to fail. +Because {\texttt sort} returns {\texttt None}, the +next operation you perform with {\texttt t} is likely to fail. Before using list methods and operators, you should read the documentation carefully and then test them in interactive mode. @@ -8239,27 +8380,27 @@ \section{Debugging} Part of the problem with lists is that there are too many ways to do things. For example, to remove an element from -a list, you can use {\tt pop}, {\tt remove}, {\tt del}, +a list, you can use {\texttt pop}, {\texttt remove}, {\texttt del}, or even a slice assignment. -To add an element, you can use the {\tt append} method or -the {\tt +} operator. Assuming that {\tt t} is a list and -{\tt x} is a list element, these are correct: +To add an element, you can use the {\texttt append} method or +the {\texttt +} operator. Assuming that {\texttt t} is a list and +{\texttt x} is a list element, these are correct: -\begin{verbatim} +\begin{lstlisting} t.append(x) t = t + [x] t += [x] -\end{verbatim} +\end{lstlisting} And these are wrong: -\begin{verbatim} +\begin{lstlisting} t.append([x]) # WRONG! t = t.append(x) # WRONG! t + [x] # WRONG! t = t + x # WRONG! -\end{verbatim} +\end{lstlisting} Try out each of these examples in interactive mode to make sure you understand what they do. Notice that only the last @@ -8271,11 +8412,11 @@ \section{Debugging} \index{aliasing!copying to avoid} \index{copy!to avoid aliasing} -If you want to use a method like {\tt sort} that modifies +If you want to use a method like {\texttt sort} that modifies the argument, but you need to keep the original list as well, you can make a copy. -\begin{verbatim} +\begin{lstlisting} >>> t = [3, 1, 2] >>> t2 = t[:] >>> t2.sort() @@ -8283,20 +8424,20 @@ \section{Debugging} [3, 1, 2] >>> t2 [1, 2, 3] -\end{verbatim} +\end{lstlisting} -In this example you could also use the built-in function {\tt sorted}, +In this example you could also use the built-in function {\texttt sorted}, which returns a new, sorted list and leaves the original alone. \index{sorted!function} \index{function!sorted} -\begin{verbatim} +\begin{lstlisting} >>> t2 = sorted(t) >>> t [3, 1, 2] >>> t2 [1, 2, 3] -\end{verbatim} +\end{lstlisting} \end{enumerate} @@ -8371,99 +8512,106 @@ \section{Exercises} \url{http://thinkpython2.com/code/list_exercises.py}. \begin{exercise} +\normalfont Write a function called \verb"nested_sum" that takes a list of lists of integers and adds up the elements from all of the nested lists. For example: -\begin{verbatim} +\begin{lstlisting} >>> t = [[1, 2], [3], [4, 5, 6]] >>> nested_sum(t) 21 -\end{verbatim} +\end{lstlisting} \end{exercise} \begin{exercise} +\normalfont \label{cumulative} \index{cumulative sum} -Write a function called {\tt cumsum} that takes a list of numbers and +Write a function called {\texttt cumsum} that takes a list of numbers and returns the cumulative sum; that is, a new list where the $i$th element is the sum of the first $i+1$ elements from the original list. For example: -\begin{verbatim} +\begin{lstlisting} >>> t = [1, 2, 3] >>> cumsum(t) [1, 3, 6] -\end{verbatim} +\end{lstlisting} \end{exercise} \begin{exercise} +\normalfont Write a function called \verb"middle" that takes a list and returns a new list that contains all but the first and last elements. For example: -\begin{verbatim} +\begin{lstlisting} >>> t = [1, 2, 3, 4] >>> middle(t) [2, 3] -\end{verbatim} +\end{lstlisting} \end{exercise} \begin{exercise} +\normalfont Write a function called \verb"chop" that takes a list, modifies it -by removing the first and last elements, and returns {\tt None}. +by removing the first and last elements, and returns {\texttt None}. For example: -\begin{verbatim} +\begin{lstlisting} >>> t = [1, 2, 3, 4] >>> chop(t) >>> t [2, 3] -\end{verbatim} +\end{lstlisting} \end{exercise} \begin{exercise} +\normalfont Write a function called \verb"is_sorted" that takes a list as a -parameter and returns {\tt True} if the list is sorted in ascending -order and {\tt False} otherwise. For example: +parameter and returns {\texttt True} if the list is sorted in ascending +order and {\texttt False} otherwise. For example: -\begin{verbatim} +\begin{lstlisting} >>> is_sorted([1, 2, 2]) True >>> is_sorted(['b', 'a']) False -\end{verbatim} +\end{lstlisting} \end{exercise} \begin{exercise} +\normalfont \label{anagram} \index{anagram} Two words are anagrams if you can rearrange the letters from one to spell the other. Write a function called \verb"is_anagram" -that takes two strings and returns {\tt True} if they are anagrams. +that takes two strings and returns {\texttt True} if they are anagrams. \end{exercise} \begin{exercise} +\normalfont \label{duplicate} \index{duplicate} \index{uniqueness} Write a function called \verb"has_duplicates" that takes -a list and returns {\tt True} if there is any element that +a list and returns {\texttt True} if there is any element that appears more than once. It should not modify the original list. @@ -8471,6 +8619,7 @@ \section{Exercises} \begin{exercise} +\normalfont This exercise pertains to the so-called Birthday Paradox, which you can read about at \url{http://en.wikipedia.org/wiki/Birthday_paradox}. @@ -8480,7 +8629,7 @@ \section{Exercises} that two of you have the same birthday? You can estimate this probability by generating random samples of 23 birthdays and checking for matches. Hint: you can generate random birthdays -with the {\tt randint} function in the {\tt random} module. +with the {\texttt randint} function in the {\texttt random} module. \index{random module} \index{module!random} \index{randint function} @@ -8494,15 +8643,16 @@ \section{Exercises} \begin{exercise} +\normalfont \index{append method} \index{method append} \index{list!concatenation} \index{concatenation!list} -Write a function that reads the file {\tt words.txt} and builds +Write a function that reads the file {\texttt words.txt} and builds a list with one element per word. Write two versions of -this function, one using the {\tt append} method and the -other using the idiom {\tt t = t + [x]}. Which one takes +this function, one using the {\texttt append} method and the +other using the idiom {\texttt t = t + [x]}. Which one takes longer to run? Why? Solution: \url{http://thinkpython2.com/code/wordlist.py}. @@ -8513,6 +8663,7 @@ \section{Exercises} \begin{exercise} +\normalfont \label{wordlist1} \label{bisection} \index{membership!bisection search} @@ -8523,7 +8674,7 @@ \section{Exercises} \index{search, binary} To check whether a word is in the word list, you could use -the {\tt in} operator, but it would be slow because it searches +the {\texttt in} operator, but it would be slow because it searches through the words in order. Because the words are in alphabetical order, we can speed things up @@ -8540,16 +8691,17 @@ \section{Exercises} Write a function called \verb"in_bisect" that takes a sorted list and a target value and returns the index of the value -in the list if it's there, or {\tt None} if it's not. +in the list if it's there, or {\texttt None} if it's not. \index{bisect module} \index{module!bisect} -Or you could read the documentation of the {\tt bisect} module +Or you could read the documentation of the {\texttt bisect} module and use that! Solution: \url{http://thinkpython2.com/code/inlist.py}. \end{exercise} \begin{exercise} +\normalfont \index{reverse word pair} Two words are a ``reverse pair'' if each is the reverse of the @@ -8559,6 +8711,7 @@ \section{Exercises} \end{exercise} \begin{exercise} +\normalfont \index{interlocking words} Two words ``interlock'' if taking alternating letters from each forms @@ -8595,65 +8748,65 @@ \section{A dictionary is a mapping} \index{key} \index{key-value pair} \index{index} -A {\bf dictionary} is like a list, but more general. In a list, +A {\textbf dictionary} is like a list, but more general. In a list, the indices have to be integers; in a dictionary they can be (almost) any type. -A dictionary contains a collection of indices, which are called {\bf +A dictionary contains a collection of indices, which are called {\textbf keys}, and a collection of values. Each key is associated with a -single value. The association of a key and a value is called a {\bf - key-value pair} or sometimes an {\bf item}. \index{item} +single value. The association of a key and a value is called a {\textbf + key-value pair} or sometimes an {\textbf item}. \index{item} -In mathematical language, a dictionary represents a {\bf mapping} +In mathematical language, a dictionary represents a {\textbf mapping} from keys to values, so you can also say that each key ``maps to'' a value. As an example, we'll build a dictionary that maps from English to Spanish words, so the keys and the values are all strings. -The function {\tt dict} creates a new dictionary with no items. -Because {\tt dict} is the name of a built-in function, you +The function {\texttt dict} creates a new dictionary with no items. +Because {\texttt dict} is the name of a built-in function, you should avoid using it as a variable name. \index{dict function} \index{function!dict} -\begin{verbatim} +\begin{lstlisting} >>> eng2sp = dict() >>> eng2sp {} -\end{verbatim} +\end{lstlisting} The squiggly-brackets, \verb"{}", represent an empty dictionary. To add items to the dictionary, you can use square brackets: \index{squiggly bracket} \index{bracket!squiggly} -\begin{verbatim} +\begin{lstlisting} >>> eng2sp['one'] = 'uno' -\end{verbatim} +\end{lstlisting} % This line creates an item that maps from the key \verb"'one'" to the value \verb"'uno'". If we print the dictionary again, we see a key-value pair with a colon between the key and value: -\begin{verbatim} +\begin{lstlisting} >>> eng2sp {'one': 'uno'} -\end{verbatim} +\end{lstlisting} % This output format is also an input format. For example, you can create a new dictionary with three items: -\begin{verbatim} +\begin{lstlisting} >>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'} -\end{verbatim} +\end{lstlisting} % -But if you print {\tt eng2sp}, you might be surprised: +But if you print {\texttt eng2sp}, you might be surprised: -\begin{verbatim} +\begin{lstlisting} >>> eng2sp {'one': 'uno', 'three': 'tres', 'two': 'dos'} -\end{verbatim} +\end{lstlisting} % The order of the key-value pairs might not be the same. If you type the same example on your computer, you might get a @@ -8664,10 +8817,10 @@ \section{A dictionary is a mapping} the elements of a dictionary are never indexed with integer indices. Instead, you use the keys to look up the corresponding values: -\begin{verbatim} +\begin{lstlisting} >>> eng2sp['two'] 'dos' -\end{verbatim} +\end{lstlisting} % The key \verb"'two'" always maps to the value \verb"'dos'" so the order of the items doesn't matter. @@ -8676,55 +8829,55 @@ \section{A dictionary is a mapping} \index{exception!KeyError} \index{KeyError} -\begin{verbatim} +\begin{lstlisting} >>> eng2sp['four'] KeyError: 'four' -\end{verbatim} +\end{lstlisting} % -The {\tt len} function works on dictionaries; it returns the +The {\texttt len} function works on dictionaries; it returns the number of key-value pairs: \index{len function} \index{function!len} -\begin{verbatim} +\begin{lstlisting} >>> len(eng2sp) 3 -\end{verbatim} +\end{lstlisting} % -The {\tt in} operator works on dictionaries, too; it tells you whether +The {\texttt in} operator works on dictionaries, too; it tells you whether something appears as a {\em key} in the dictionary (appearing as a value is not good enough). \index{membership!dictionary} \index{in operator} \index{operator!in} -\begin{verbatim} +\begin{lstlisting} >>> 'one' in eng2sp True >>> 'uno' in eng2sp False -\end{verbatim} +\end{lstlisting} % To see whether something appears as a value in a dictionary, you -can use the method {\tt values}, which returns a collection of -values, and then use the {\tt in} operator: +can use the method {\texttt values}, which returns a collection of +values, and then use the {\texttt in} operator: \index{values method} \index{method!values} -\begin{verbatim} +\begin{lstlisting} >>> vals = eng2sp.values() >>> 'uno' in vals True -\end{verbatim} +\end{lstlisting} % -The {\tt in} operator uses different algorithms for lists and +The {\texttt in} operator uses different algorithms for lists and dictionaries. For lists, it searches the elements of the list in order, as in Section~\ref{find}. As the list gets longer, the search time gets longer in direct proportion. For dictionaries, Python uses an -algorithm called a {\bf hashtable} that has a remarkable property: the -{\tt in} operator takes about the same amount of time no matter how +algorithm called a {\textbf hashtable} that has a remarkable property: the +{\texttt in} operator takes about the same amount of time no matter how many items are in the dictionary. I explain how that's possible in Section~\ref{hashtable}, but the explanation might not make sense until you've read a few more chapters. @@ -8746,7 +8899,7 @@ \section{Dictionary as a collection of counters} \item You could create a list with 26 elements. Then you could convert each character to a number (using the built-in function -{\tt ord}), use the number as an index into the list, and increment +{\texttt ord}), use the number as an index into the list, and increment the appropriate counter. \item You could create a dictionary with characters as keys @@ -8760,7 +8913,7 @@ \section{Dictionary as a collection of counters} of them implements that computation in a different way. \index{implementation} -An {\bf implementation} is a way of performing a computation; +An {\textbf implementation} is a way of performing a computation; some implementations are better than others. For example, an advantage of the dictionary implementation is that we don't have to know ahead of time which letters appear in the string @@ -8768,7 +8921,7 @@ \section{Dictionary as a collection of counters} Here is what the code might look like: -\begin{verbatim} +\begin{lstlisting} def histogram(s): d = dict() for c in s: @@ -8777,29 +8930,29 @@ \section{Dictionary as a collection of counters} else: d[c] += 1 return d -\end{verbatim} +\end{lstlisting} % -The name of the function is {\tt histogram}, which is a statistical +The name of the function is {\texttt histogram}, which is a statistical term for a collection of counters (or frequencies). \index{histogram} \index{frequency} \index{traversal} The first line of the -function creates an empty dictionary. The {\tt for} loop traverses -the string. Each time through the loop, if the character {\tt c} is -not in the dictionary, we create a new item with key {\tt c} and the -initial value 1 (since we have seen this letter once). If {\tt c} is -already in the dictionary we increment {\tt d[c]}. +function creates an empty dictionary. The {\texttt for} loop traverses +the string. Each time through the loop, if the character {\texttt c} is +not in the dictionary, we create a new item with key {\texttt c} and the +initial value 1 (since we have seen this letter once). If {\texttt c} is +already in the dictionary we increment {\texttt d[c]}. \index{histogram} Here's how it works: -\begin{verbatim} +\begin{lstlisting} >>> h = histogram('brontosaurus') >>> h {'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 2, 't': 1} -\end{verbatim} +\end{lstlisting} % The histogram indicates that the letters \verb"'a'" and \verb"'b'" appear once; \verb"'o'" appears twice, and so on. @@ -8807,12 +8960,12 @@ \section{Dictionary as a collection of counters} \index{get method} \index{method!get} -Dictionaries have a method called {\tt get} that takes a key +Dictionaries have a method called {\texttt get} that takes a key and a default value. If the key appears in the dictionary, -{\tt get} returns the corresponding value; otherwise it returns +{\texttt get} returns the corresponding value; otherwise it returns the default value. For example: -\begin{verbatim} +\begin{lstlisting} >>> h = histogram('a') >>> h {'a': 1} @@ -8820,10 +8973,10 @@ \section{Dictionary as a collection of counters} 1 >>> h.get('b', 0) 0 -\end{verbatim} +\end{lstlisting} % -As an exercise, use {\tt get} to write {\tt histogram} more concisely. You -should be able to eliminate the {\tt if} statement. +As an exercise, use {\texttt get} to write {\texttt histogram} more concisely. You +should be able to eliminate the {\texttt if} statement. \section{Looping and dictionaries} @@ -8831,19 +8984,19 @@ \section{Looping and dictionaries} \index{looping!with dictionaries} \index{traversal} -If you use a dictionary in a {\tt for} statement, it traverses +If you use a dictionary in a {\texttt for} statement, it traverses the keys of the dictionary. For example, \verb"print_hist" prints each key and the corresponding value: -\begin{verbatim} +\begin{lstlisting} def print_hist(h): for c in h: print(c, h[c]) -\end{verbatim} +\end{lstlisting} % Here's what the output looks like: -\begin{verbatim} +\begin{lstlisting} >>> h = histogram('parrot') >>> print_hist(h) a 1 @@ -8851,14 +9004,14 @@ \section{Looping and dictionaries} r 2 t 1 o 1 -\end{verbatim} +\end{lstlisting} % Again, the keys are in no particular order. To traverse the keys -in sorted order, you can use the built-in function {\tt sorted}: +in sorted order, you can use the built-in function {\texttt sorted}: \index{sorted!function} \index{function!sorted} -\begin{verbatim} +\begin{lstlisting} >>> for key in sorted(h): ... print(key, h[key]) a 1 @@ -8866,7 +9019,7 @@ \section{Looping and dictionaries} p 1 r 2 t 1 -\end{verbatim} +\end{lstlisting} %TODO: get this on Atlas @@ -8878,59 +9031,59 @@ \section{Reverse lookup} \index{lookup, dictionary} \index{reverse lookup, dictionary} -Given a dictionary {\tt d} and a key {\tt k}, it is easy to -find the corresponding value {\tt v = d[k]}. This operation -is called a {\bf lookup}. +Given a dictionary {\texttt d} and a key {\texttt k}, it is easy to +find the corresponding value {\texttt v = d[k]}. This operation +is called a {\textbf lookup}. -But what if you have {\tt v} and you want to find {\tt k}? +But what if you have {\texttt v} and you want to find {\texttt k}? You have two problems: first, there might be more than one -key that maps to the value {\tt v}. Depending on the application, +key that maps to the value {\texttt v}. Depending on the application, you might be able to pick one, or you might have to make a list that contains all of them. Second, there is no -simple syntax to do a {\bf reverse lookup}; you have to search. +simple syntax to do a {\textbf reverse lookup}; you have to search. Here is a function that takes a value and returns the first key that maps to that value: -\begin{verbatim} +\begin{lstlisting} def reverse_lookup(d, v): for k in d: if d[k] == v: return k raise LookupError() -\end{verbatim} +\end{lstlisting} % This function is yet another example of the search pattern, but it -uses a feature we haven't seen before, {\tt raise}. The -{\bf raise statement} causes an exception; in this case it causes a -{\tt LookupError}, which is a built-in exception used to indicate +uses a feature we haven't seen before, {\texttt raise}. The +{\textbf raise statement} causes an exception; in this case it causes a +{\texttt LookupError}, which is a built-in exception used to indicate that a lookup operation failed. \index{search} \index{pattern!search} \index{raise statement} \index{statement!raise} \index{exception!LookupError} \index{LookupError} -If we get to the end of the loop, that means {\tt v} +If we get to the end of the loop, that means {\texttt v} doesn't appear in the dictionary as a value, so we raise an exception. Here is an example of a successful reverse lookup: -\begin{verbatim} +\begin{lstlisting} >>> h = histogram('parrot') >>> key = reverse_lookup(h, 2) >>> key 'r' -\end{verbatim} +\end{lstlisting} % And an unsuccessful one: -\begin{verbatim} +\begin{lstlisting} >>> key = reverse_lookup(h, 3) Traceback (most recent call last): File "", line 1, in File "", line 5, in reverse_lookup LookupError -\end{verbatim} +\end{lstlisting} % The effect when you raise an exception is the same as when Python raises one: it prints a traceback and an error message. @@ -8938,15 +9091,15 @@ \section{Reverse lookup} \index{optional argument} \index{argument!optional} -The {\tt raise} statement can take a detailed error message as an +The {\texttt raise} statement can take a detailed error message as an optional argument. For example: -\begin{verbatim} +\begin{lstlisting} >>> raise LookupError('value does not appear in the dictionary') Traceback (most recent call last): File "", line 1, in ? LookupError: value does not appear in the dictionary -\end{verbatim} +\end{lstlisting} % A reverse lookup is much slower than a forward lookup; if you have to do it often, or if the dictionary gets big, the performance @@ -8967,7 +9120,7 @@ \section{Dictionaries and lists} Here is a function that inverts a dictionary: -\begin{verbatim} +\begin{lstlisting} def invert_dict(d): inverse = dict() for key in d: @@ -8977,25 +9130,25 @@ \section{Dictionaries and lists} else: inverse[val].append(key) return inverse -\end{verbatim} +\end{lstlisting} % -Each time through the loop, {\tt key} gets a key from {\tt d} and -{\tt val} gets the corresponding value. If {\tt val} is not in {\tt +Each time through the loop, {\texttt key} gets a key from {\texttt d} and +{\texttt val} gets the corresponding value. If {\texttt val} is not in {\texttt inverse}, that means we haven't seen it before, so we create a new -item and initialize it with a {\bf singleton} (a list that contains a +item and initialize it with a {\textbf singleton} (a list that contains a single element). Otherwise we have seen this value before, so we append the corresponding key to the list. \index{singleton} Here is an example: -\begin{verbatim} +\begin{lstlisting} >>> hist = histogram('parrot') >>> hist {'a': 1, 'p': 1, 'r': 2, 't': 1, 'o': 1} >>> inverse = invert_dict(hist) >>> inverse {1: ['a', 'p', 't', 'o'], 2: ['r']} -\end{verbatim} +\end{lstlisting} \begin{figure} \centerline @@ -9004,8 +9157,8 @@ \section{Dictionaries and lists} \label{fig.dict1} \end{figure} -Figure~\ref{fig.dict1} is a state diagram showing {\tt hist} and {\tt inverse}. -A dictionary is represented as a box with the type {\tt dict} above it +Figure~\ref{fig.dict1} is a state diagram showing {\texttt hist} and {\texttt inverse}. +A dictionary is represented as a box with the type {\texttt dict} above it and the key-value pairs inside. If the values are integers, floats or strings, I draw them inside the box, but I usually draw lists outside the box, just to keep the diagram simple. @@ -9018,21 +9171,21 @@ \section{Dictionaries and lists} \index{exception!TypeError} -\begin{verbatim} +\begin{lstlisting} >>> t = [1, 2, 3] >>> d = dict() >>> d[t] = 'oops' Traceback (most recent call last): File "", line 1, in ? TypeError: list objects are unhashable -\end{verbatim} +\end{lstlisting} % I mentioned earlier that a dictionary is implemented using -a hashtable and that means that the keys have to be {\bf hashable}. +a hashtable and that means that the keys have to be {\textbf hashable}. \index{hash function} \index{hashable} -A {\bf hash} is a function that takes a value (of any kind) +A {\textbf hash} is a function that takes a value (of any kind) and returns an integer. Dictionaries use these integers, called hash values, to store and look up key-value pairs. \index{immutability} @@ -9057,7 +9210,7 @@ \section{Dictionaries and lists} \section{Memos} \label{memoize} -If you played with the {\tt fibonacci} function from +If you played with the {\texttt fibonacci} function from Section~\ref{one.more.example}, you might have noticed that the bigger the argument you provide, the longer the function takes to run. Furthermore, the run time increases quickly. @@ -9065,7 +9218,7 @@ \section{Memos} \index{function!fibonacci} To understand why, consider Figure~\ref{fig.fibonacci}, which shows -the {\bf call graph} for {\tt fibonacci} with {\tt n=4}: +the {\textbf call graph} for {\texttt fibonacci} with {\texttt n=4}: \begin{figure} \centerline @@ -9076,24 +9229,24 @@ \section{Memos} A call graph shows a set of function frames, with lines connecting each frame to the frames of the functions it calls. At the top of the -graph, {\tt fibonacci} with {\tt n=4} calls {\tt fibonacci} with {\tt -n=3} and {\tt n=2}. In turn, {\tt fibonacci} with {\tt n=3} calls -{\tt fibonacci} with {\tt n=2} and {\tt n=1}. And so on. +graph, {\texttt fibonacci} with {\texttt n=4} calls {\texttt fibonacci} with {\texttt +n=3} and {\texttt n=2}. In turn, {\texttt fibonacci} with {\texttt n=3} calls +{\texttt fibonacci} with {\texttt n=2} and {\texttt n=1}. And so on. \index{function frame} \index{frame} \index{call graph} -Count how many times {\tt fibonacci(0)} and {\tt fibonacci(1)} are +Count how many times {\texttt fibonacci(0)} and {\texttt fibonacci(1)} are called. This is an inefficient solution to the problem, and it gets worse as the argument gets bigger. \index{memo} One solution is to keep track of values that have already been computed by storing them in a dictionary. A previously computed value -that is stored for later use is called a {\bf memo}. Here is a -``memoized'' version of {\tt fibonacci}: +that is stored for later use is called a {\textbf memo}. Here is a +``memoized'' version of {\texttt fibonacci}: -\begin{verbatim} +\begin{lstlisting} known = {0:0, 1:1} def fibonacci(n): @@ -9103,18 +9256,18 @@ \section{Memos} res = fibonacci(n-1) + fibonacci(n-2) known[n] = res return res -\end{verbatim} +\end{lstlisting} % -{\tt known} is a dictionary that keeps track of the Fibonacci +{\texttt known} is a dictionary that keeps track of the Fibonacci numbers we already know. It starts with two items: 0 maps to 0 and 1 maps to 1. -Whenever {\tt fibonacci} is called, it checks {\tt known}. +Whenever {\texttt fibonacci} is called, it checks {\texttt known}. If the result is already there, it can return immediately. Otherwise it has to compute the new value, add it to the dictionary, and return it. -If you run this version of {\tt fibonacci} and compare it with +If you run this version of {\texttt fibonacci} and compare it with the original, you will find that it is much faster. @@ -9123,43 +9276,43 @@ \section{Global variables} \index{global variable} \index{variable!global} -In the previous example, {\tt known} is created outside the function, +In the previous example, {\texttt known} is created outside the function, so it belongs to the special frame called \verb"__main__". -Variables in \verb"__main__" are sometimes called {\bf global} +Variables in \verb"__main__" are sometimes called {\textbf global} because they can be accessed from any function. Unlike local variables, which disappear when their function ends, global variables persist from one function call to the next. \index{flag} \index{main} -It is common to use global variables for {\bf flags}; that is, +It is common to use global variables for {\textbf flags}; that is, boolean variables that indicate (``flag'') whether a condition is true. For example, some programs use -a flag named {\tt verbose} to control the level of detail in the +a flag named {\texttt verbose} to control the level of detail in the output: -\begin{verbatim} +\begin{lstlisting} verbose = True def example1(): if verbose: print('Running example1') -\end{verbatim} +\end{lstlisting} % If you try to reassign a global variable, you might be surprised. The following example is supposed to keep track of whether the function has been called: \index{reassignment} -\begin{verbatim} +\begin{lstlisting} been_called = False def example2(): been_called = True # WRONG -\end{verbatim} +\end{lstlisting} % But if you run it you will see that the value of \verb"been_called" -doesn't change. The problem is that {\tt example2} creates a new local +doesn't change. The problem is that {\texttt example2} creates a new local variable named \verb"been_called". The local variable goes away when the function ends, and has no effect on the global variable. \index{global statement} @@ -9167,17 +9320,17 @@ \section{Global variables} \index{declaration} To reassign a global variable inside a function you have to -{\bf declare} the global variable before you use it: +{\textbf declare} the global variable before you use it: -\begin{verbatim} +\begin{lstlisting} been_called = False def example2(): global been_called been_called = True -\end{verbatim} +\end{lstlisting} % -The {\bf global statement} tells the interpreter +The {\textbf global statement} tells the interpreter something like, ``In this function, when I say \verb"been_called", I mean the global variable; don't create a local one.'' \index{update!global variable} @@ -9185,52 +9338,52 @@ \section{Global variables} Here's an example that tries to update a global variable: -\begin{verbatim} +\begin{lstlisting} count = 0 def example3(): count = count + 1 # WRONG -\end{verbatim} +\end{lstlisting} % If you run it you get: \index{UnboundLocalError} \index{exception!UnboundLocalError} -\begin{verbatim} +\begin{lstlisting} UnboundLocalError: local variable 'count' referenced before assignment -\end{verbatim} +\end{lstlisting} % -Python assumes that {\tt count} is local, and under that assumption +Python assumes that {\texttt count} is local, and under that assumption you are reading it before writing it. The solution, again, -is to declare {\tt count} global. +is to declare {\texttt count} global. \index{counter} -\begin{verbatim} +\begin{lstlisting} def example3(): global count count += 1 -\end{verbatim} +\end{lstlisting} % If a global variable refers to a mutable value, you can modify the value without declaring the variable: \index{mutability} -\begin{verbatim} +\begin{lstlisting} known = {0:0, 1:1} def example4(): known[2] = 1 -\end{verbatim} +\end{lstlisting} % So you can add, remove and replace elements of a global list or dictionary, but if you want to reassign the variable, you have to declare it: -\begin{verbatim} +\begin{lstlisting} def example5(): global known known = dict() -\end{verbatim} +\end{lstlisting} % Global variables can be useful, but if you have a lot of them, and you modify them frequently, they can make programs @@ -9250,9 +9403,9 @@ \section{Debugging} dataset. For example if the program reads a text file, start with just the first 10 lines, or with the smallest example you can find. You can either edit the files themselves, or (better) modify the -program so it reads only the first {\tt n} lines. +program so it reads only the first {\texttt n} lines. -If there is an error, you can reduce {\tt n} to the smallest +If there is an error, you can reduce {\texttt n} to the smallest value that manifests the error, and then increase it gradually as you find and correct errors. @@ -9279,9 +9432,9 @@ \section{Debugging} \item[Format the output:] Formatting debugging output can make it easier to spot an error. We saw an example in -Section~\ref{factdebug}. Another tool you might find useful is the {\tt pprint} module, which provides -a {\tt pprint} function that displays built-in types in -a more human-readable format ({\tt pprint} stands for +Section~\ref{factdebug}. Another tool you might find useful is the {\texttt pprint} module, which provides +a {\texttt pprint} function that displays built-in types in +a more human-readable format ({\texttt pprint} stands for ``pretty print''). \index{pretty print} \index{pprint module} @@ -9378,7 +9531,7 @@ \section{Glossary} is true. \index{flag} -\item[declaration:] A statement like {\tt global} that tells the +\item[declaration:] A statement like {\texttt global} that tells the interpreter something about a variable. \index{declaration} @@ -9388,27 +9541,29 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont \label{wordlist2} \index{set membership} \index{membership!set} -Write a function that reads the words in {\tt words.txt} and +Write a function that reads the words in {\texttt words.txt} and stores them as keys in a dictionary. It doesn't matter what the -values are. Then you can use the {\tt in} operator +values are. Then you can use the {\texttt in} operator as a fast way to check whether a string is in the dictionary. If you did Exercise~\ref{wordlist1}, you can compare the speed -of this implementation with the list {\tt in} operator and the +of this implementation with the list {\texttt in} operator and the bisection search. \end{exercise} \begin{exercise} +\normalfont \label{setdefault} -Read the documentation of the dictionary method {\tt setdefault} +Read the documentation of the dictionary method {\texttt setdefault} and use it to write a more concise version of \verb"invert_dict". Solution: \url{http://thinkpython2.com/code/invert_dict.py}. \index{setdefault method} @@ -9418,6 +9573,7 @@ \section{Exercises} \begin{exercise} +\normalfont Memoize the Ackermann function from Exercise~\ref{ackermann} and see if memoization makes it possible to evaluate the function with bigger arguments. Hint: no. @@ -9430,11 +9586,12 @@ \section{Exercises} \begin{exercise} +\normalfont \index{duplicate} If you did Exercise~\ref{duplicate}, you already have a function named \verb"has_duplicates" that takes a list -as a parameter and returns {\tt True} if there is any object +as a parameter and returns {\texttt True} if there is any object that appears more than once in the list. Use a dictionary to write a faster, simpler version of @@ -9445,6 +9602,7 @@ \section{Exercises} \begin{exercise} +\normalfont \label{exrotatepairs} \index{letter rotation} \index{rotation!letters} @@ -9459,6 +9617,7 @@ \section{Exercises} \begin{exercise} +\normalfont \index{Car Talk} \index{Puzzler} @@ -9538,58 +9697,58 @@ \section{Tuples are immutable} Syntactically, a tuple is a comma-separated list of values: -\begin{verbatim} +\begin{lstlisting} >>> t = 'a', 'b', 'c', 'd', 'e' -\end{verbatim} +\end{lstlisting} % Although it is not necessary, it is common to enclose tuples in parentheses: \index{parentheses!tuples in} -\begin{verbatim} +\begin{lstlisting} >>> t = ('a', 'b', 'c', 'd', 'e') -\end{verbatim} +\end{lstlisting} % To create a tuple with a single element, you have to include a final comma: \index{singleton} \index{tuple!singleton} -\begin{verbatim} +\begin{lstlisting} >>> t1 = 'a', >>> type(t1) -\end{verbatim} +\end{lstlisting} % A value in parentheses is not a tuple: -\begin{verbatim} +\begin{lstlisting} >>> t2 = ('a') >>> type(t2) -\end{verbatim} +\end{lstlisting} % -Another way to create a tuple is the built-in function {\tt tuple}. +Another way to create a tuple is the built-in function {\texttt tuple}. With no argument, it creates an empty tuple: \index{tuple function} \index{function!tuple} -\begin{verbatim} +\begin{lstlisting} >>> t = tuple() >>> t () -\end{verbatim} +\end{lstlisting} % If the argument is a sequence (string, list or tuple), the result is a tuple with the elements of the sequence: -\begin{verbatim} +\begin{lstlisting} >>> t = tuple('lupins') >>> t ('l', 'u', 'p', 'i', 'n', 's') -\end{verbatim} +\end{lstlisting} % -Because {\tt tuple} is the name of a built-in function, you should +Because {\texttt tuple} is the name of a built-in function, you should avoid using it as a variable name. Most list operators also work on tuples. The bracket operator @@ -9597,11 +9756,11 @@ \section{Tuples are immutable} \index{bracket operator} \index{operator!bracket} -\begin{verbatim} +\begin{lstlisting} >>> t = ('a', 'b', 'c', 'd', 'e') >>> t[0] 'a' -\end{verbatim} +\end{lstlisting} % And the slice operator selects a range of elements. \index{slice operator} @@ -9609,10 +9768,10 @@ \section{Tuples are immutable} \index{tuple!slice} \index{slice!tuple} -\begin{verbatim} +\begin{lstlisting} >>> t[1:3] ('b', 'c') -\end{verbatim} +\end{lstlisting} % But if you try to modify one of the elements of the tuple, you get an error: @@ -9621,21 +9780,21 @@ \section{Tuples are immutable} \index{item assignment} \index{assignment!item} -\begin{verbatim} +\begin{lstlisting} >>> t[0] = 'A' TypeError: object doesn't support item assignment -\end{verbatim} +\end{lstlisting} % Because tuples are immutable, you can't modify the elements. But you can replace one tuple with another: -\begin{verbatim} +\begin{lstlisting} >>> t = ('A',) + t[1:] >>> t ('A', 'b', 'c', 'd', 'e') -\end{verbatim} +\end{lstlisting} % -This statement makes a new tuple and then makes {\tt t} refer to it. +This statement makes a new tuple and then makes {\texttt t} refer to it. The relational operators work with tuples and other sequences; Python starts by comparing the first element from each @@ -9645,12 +9804,12 @@ \section{Tuples are immutable} \index{comparison!tuple} \index{tuple!comparison} -\begin{verbatim} +\begin{lstlisting} >>> (0, 1, 2) < (0, 3, 4) True >>> (0, 1, 2000000) < (0, 3, 4) True -\end{verbatim} +\end{lstlisting} @@ -9663,19 +9822,19 @@ \section{Tuple assignment} It is often useful to swap the values of two variables. With conventional assignments, you have to use a temporary -variable. For example, to swap {\tt a} and {\tt b}: +variable. For example, to swap {\texttt a} and {\texttt b}: -\begin{verbatim} +\begin{lstlisting} >>> temp = a >>> a = b >>> b = temp -\end{verbatim} +\end{lstlisting} % -This solution is cumbersome; {\bf tuple assignment} is more elegant: +This solution is cumbersome; {\textbf tuple assignment} is more elegant: -\begin{verbatim} +\begin{lstlisting} >>> a, b = b, a -\end{verbatim} +\end{lstlisting} % The left side is a tuple of variables; the right side is a tuple of expressions. Each value is assigned to its respective variable. @@ -9687,10 +9846,10 @@ \section{Tuple assignment} \index{exception!ValueError} \index{ValueError} -\begin{verbatim} +\begin{lstlisting} >>> a, b = 1, 2, 3 ValueError: too many values to unpack -\end{verbatim} +\end{lstlisting} % More generally, the right side can be any kind of sequence (string, list or tuple). For example, to split an email address @@ -9699,21 +9858,21 @@ \section{Tuple assignment} \index{method!split} \index{email address} -\begin{verbatim} +\begin{lstlisting} >>> addr = 'monty@python.org' >>> uname, domain = addr.split('@') -\end{verbatim} +\end{lstlisting} % -The return value from {\tt split} is a list with two elements; -the first element is assigned to {\tt uname}, the second to -{\tt domain}. +The return value from {\texttt split} is a list with two elements; +the first element is assigned to {\texttt uname}, the second to +{\texttt domain}. -\begin{verbatim} +\begin{lstlisting} >>> uname 'monty' >>> domain 'python.org' -\end{verbatim} +\end{lstlisting} % \section{Tuples as return values} @@ -9726,40 +9885,40 @@ \section{Tuples as return values} if the value is a tuple, the effect is the same as returning multiple values. For example, if you want to divide two integers and compute the quotient and remainder, it is inefficient to -compute {\tt x/y} and then {\tt x\%y}. It is better to compute +compute {\texttt x/y} and then {\texttt x\%y}. It is better to compute them both at the same time. \index{divmod} -The built-in function {\tt divmod} takes two arguments and +The built-in function {\texttt divmod} takes two arguments and returns a tuple of two values, the quotient and remainder. You can store the result as a tuple: -\begin{verbatim} +\begin{lstlisting} >>> t = divmod(7, 3) >>> t (2, 1) -\end{verbatim} +\end{lstlisting} % Or use tuple assignment to store the elements separately: \index{tuple assignment} \index{assignment!tuple} -\begin{verbatim} +\begin{lstlisting} >>> quot, rem = divmod(7, 3) >>> quot 2 >>> rem 1 -\end{verbatim} +\end{lstlisting} % Here is an example of a function that returns a tuple: -\begin{verbatim} +\begin{lstlisting} def min_max(t): return min(t), max(t) -\end{verbatim} +\end{lstlisting} % -{\tt max} and {\tt min} are built-in functions that find +{\texttt max} and {\texttt min} are built-in functions that find the largest and smallest elements of a sequence. \verb"min_max" computes both and returns a tuple of two values. \index{max function} @@ -9777,69 +9936,69 @@ \section{Variable-length argument tuples} \index{argument!gather} Functions can take a variable number of arguments. A parameter -name that begins with {\tt *} {\bf gathers} arguments into -a tuple. For example, {\tt printall} +name that begins with {\texttt *} {\textbf gathers} arguments into +a tuple. For example, {\texttt printall} takes any number of arguments and prints them: -\begin{verbatim} +\begin{lstlisting} def printall(*args): print(args) -\end{verbatim} +\end{lstlisting} % -The gather parameter can have any name you like, but {\tt args} is +The gather parameter can have any name you like, but {\texttt args} is conventional. Here's how the function works: -\begin{verbatim} +\begin{lstlisting} >>> printall(1, 2.0, '3') (1, 2.0, '3') -\end{verbatim} +\end{lstlisting} % -The complement of gather is {\bf scatter}. If you have a +The complement of gather is {\textbf scatter}. If you have a sequence of values and you want to pass it to a function -as multiple arguments, you can use the {\tt *} operator. -For example, {\tt divmod} takes exactly two arguments; it +as multiple arguments, you can use the {\texttt *} operator. +For example, {\texttt divmod} takes exactly two arguments; it doesn't work with a tuple: \index{scatter} \index{argument scatter} \index{TypeError} \index{exception!TypeError} -\begin{verbatim} +\begin{lstlisting} >>> t = (7, 3) >>> divmod(t) TypeError: divmod expected 2 arguments, got 1 -\end{verbatim} +\end{lstlisting} % But if you scatter the tuple, it works: -\begin{verbatim} +\begin{lstlisting} >>> divmod(*t) (2, 1) -\end{verbatim} +\end{lstlisting} % Many of the built-in functions use -variable-length argument tuples. For example, {\tt max} -and {\tt min} can take any number of arguments: +variable-length argument tuples. For example, {\texttt max} +and {\texttt min} can take any number of arguments: \index{max function} \index{function!max} \index{min function} \index{function!min} -\begin{verbatim} +\begin{lstlisting} >>> max(1, 2, 3) 3 -\end{verbatim} +\end{lstlisting} % -But {\tt sum} does not. +But {\texttt sum} does not. \index{sum function} \index{function!sum} -\begin{verbatim} +\begin{lstlisting} >>> sum(1, 2, 3) TypeError: sum expected at most 2 arguments, got 3 -\end{verbatim} +\end{lstlisting} % -As an exercise, write a function called {\tt sumall} that takes any number +As an exercise, write a function called {\texttt sumall} that takes any number of arguments and returns their sum. @@ -9847,33 +10006,33 @@ \section{Lists and tuples} \index{zip function} \index{function!zip} -{\tt zip} is a built-in function that takes two or more sequences and +{\texttt zip} is a built-in function that takes two or more sequences and returns a list of tuples where each tuple contains one element from each sequence. The name of the function refers to a zipper, which joins and interleaves two rows of teeth. This example zips a string and a list: -\begin{verbatim} +\begin{lstlisting} >>> s = 'abc' >>> t = [0, 1, 2] >>> zip(s, t) -\end{verbatim} +\end{lstlisting} % -The result is a {\bf zip object} that knows how to iterate through -the pairs. The most common use of {\tt zip} is in a {\tt for} loop: +The result is a {\textbf zip object} that knows how to iterate through +the pairs. The most common use of {\texttt zip} is in a {\texttt for} loop: -\begin{verbatim} +\begin{lstlisting} >>> for pair in zip(s, t): ... print(pair) ... ('a', 0) ('b', 1) ('c', 2) -\end{verbatim} +\end{lstlisting} % -A zip object is a kind of {\bf iterator}, which is any object +A zip object is a kind of {\textbf iterator}, which is any object that iterates through a sequence. Iterators are similar to lists in some ways, but unlike lists, you can't use an index to select an element from an iterator. @@ -9882,10 +10041,10 @@ \section{Lists and tuples} If you want to use list operators and methods, you can use a zip object to make a list: -\begin{verbatim} +\begin{lstlisting} >>> list(zip(s, t)) [('a', 0), ('b', 1), ('c', 2)] -\end{verbatim} +\end{lstlisting} % The result is a list of tuples; in this example, each tuple contains a character from the string and the corresponding element from @@ -9895,70 +10054,70 @@ \section{Lists and tuples} If the sequences are not the same length, the result has the length of the shorter one. -\begin{verbatim} +\begin{lstlisting} >>> list(zip('Anne', 'Elk')) [('A', 'E'), ('n', 'l'), ('n', 'k')] -\end{verbatim} +\end{lstlisting} % -You can use tuple assignment in a {\tt for} loop to traverse a list of +You can use tuple assignment in a {\texttt for} loop to traverse a list of tuples: \index{traversal} \index{tuple assignment} \index{assignment!tuple} -\begin{verbatim} +\begin{lstlisting} t = [('a', 0), ('b', 1), ('c', 2)] for letter, number in t: print(number, letter) -\end{verbatim} +\end{lstlisting} % Each time through the loop, Python selects the next tuple in -the list and assigns the elements to {\tt letter} and -{\tt number}. The output of this loop is: +the list and assigns the elements to {\texttt letter} and +{\texttt number}. The output of this loop is: \index{loop} -\begin{verbatim} +\begin{lstlisting} 0 a 1 b 2 c -\end{verbatim} +\end{lstlisting} % -If you combine {\tt zip}, {\tt for} and tuple assignment, you get a +If you combine {\texttt zip}, {\texttt for} and tuple assignment, you get a useful idiom for traversing two (or more) sequences at the same -time. For example, \verb"has_match" takes two sequences, {\tt t1} and -{\tt t2}, and returns {\tt True} if there is an index {\tt i} -such that {\tt t1[i] == t2[i]}: +time. For example, \verb"has_match" takes two sequences, {\texttt t1} and +{\texttt t2}, and returns {\texttt True} if there is an index {\texttt i} +such that {\texttt t1[i] == t2[i]}: \index{for loop} -\begin{verbatim} +\begin{lstlisting} def has_match(t1, t2): for x, y in zip(t1, t2): if x == y: return True return False -\end{verbatim} +\end{lstlisting} % If you need to traverse the elements of a sequence and their -indices, you can use the built-in function {\tt enumerate}: +indices, you can use the built-in function {\texttt enumerate}: \index{traversal} \index{enumerate function} \index{function!enumerate} -\begin{verbatim} +\begin{lstlisting} for index, element in enumerate('abc'): print(index, element) -\end{verbatim} +\end{lstlisting} % -The result from {\tt enumerate} is an enumerate object, which +The result from {\texttt enumerate} is an enumerate object, which iterates a sequence of pairs; each pair contains an index (starting from 0) and an element from the given sequence. In this example, the output is -\begin{verbatim} +\begin{lstlisting} 0 a 1 b 2 c -\end{verbatim} +\end{lstlisting} % Again. \index{iterator} @@ -9973,29 +10132,29 @@ \section{Dictionaries and tuples} \index{method!items} \index{key-value pair} -Dictionaries have a method called {\tt items} that returns a sequence of +Dictionaries have a method called {\texttt items} that returns a sequence of tuples, where each tuple is a key-value pair. -\begin{verbatim} +\begin{lstlisting} >>> d = {'a':0, 'b':1, 'c':2} >>> t = d.items() >>> t dict_items([('c', 2), ('a', 0), ('b', 1)]) -\end{verbatim} +\end{lstlisting} % The result is a \verb"dict_items" object, which is an iterator that -iterates the key-value pairs. You can use it in a {\tt for} loop +iterates the key-value pairs. You can use it in a {\texttt for} loop like this: \index{iterator} -\begin{verbatim} +\begin{lstlisting} >>> for key, value in d.items(): ... print(key, value) ... c 2 a 0 b 1 -\end{verbatim} +\end{lstlisting} % As you should expect from a dictionary, the items are in no particular order. @@ -10003,24 +10162,24 @@ \section{Dictionaries and tuples} Going in the other direction, you can use a list of tuples to initialize a new dictionary: \index{dictionary!initialize} -\begin{verbatim} +\begin{lstlisting} >>> t = [('a', 0), ('c', 2), ('b', 1)] >>> d = dict(t) >>> d {'a': 0, 'c': 2, 'b': 1} -\end{verbatim} +\end{lstlisting} -Combining {\tt dict} with {\tt zip} yields a concise way +Combining {\texttt dict} with {\texttt zip} yields a concise way to create a dictionary: \index{zip function!use with dict} -\begin{verbatim} +\begin{lstlisting} >>> d = dict(zip('abc', range(3))) >>> d {'a': 0, 'c': 2, 'b': 1} -\end{verbatim} +\end{lstlisting} % -The dictionary method {\tt update} also takes a list of tuples +The dictionary method {\texttt update} also takes a list of tuples and adds them, as key-value pairs, to an existing dictionary. \index{update method} \index{method!update} @@ -10030,26 +10189,26 @@ \section{Dictionaries and tuples} It is common to use tuples as keys in dictionaries (primarily because you can't use lists). For example, a telephone directory might map from last-name, first-name pairs to telephone numbers. Assuming -that we have defined {\tt last}, {\tt first} and {\tt number}, we +that we have defined {\texttt last}, {\texttt first} and {\texttt number}, we could write: \index{tuple!as key in dictionary} \index{hashable} -\begin{verbatim} +\begin{lstlisting} directory[last, first] = number -\end{verbatim} +\end{lstlisting} % The expression in brackets is a tuple. We could use tuple assignment to traverse this dictionary. \index{tuple!in brackets} -\begin{verbatim} +\begin{lstlisting} for last, first in directory: print(first, last, directory[last,first]) -\end{verbatim} +\end{lstlisting} % -This loop traverses the keys in {\tt directory}, which are tuples. It -assigns the elements of each tuple to {\tt last} and {\tt first}, then +This loop traverses the keys in {\texttt directory}, which are tuples. It +assigns the elements of each tuple to {\texttt last} and {\texttt first}, then prints the name and corresponding telephone number. There are two ways to represent tuples in a state diagram. The more @@ -10110,7 +10269,7 @@ \section{Sequences of sequences} \begin{enumerate} -\item In some contexts, like a {\tt return} statement, it is +\item In some contexts, like a {\texttt return} statement, it is syntactically simpler to create a tuple than a list. \item If you want to use a sequence as a dictionary key, you @@ -10122,11 +10281,11 @@ \section{Sequences of sequences} \end{enumerate} -Because tuples are immutable, they don't provide methods like {\tt - sort} and {\tt reverse}, which modify existing lists. But Python -provides the built-in function {\tt sorted}, which takes any sequence +Because tuples are immutable, they don't provide methods like {\texttt + sort} and {\texttt reverse}, which modify existing lists. But Python +provides the built-in function {\texttt sorted}, which takes any sequence and returns a new list with the same elements in sorted order, and -{\tt reversed}, which takes a sequence and returns an iterator that +{\texttt reversed}, which takes a sequence and returns an iterator that traverses the list in reverse order. \index{sorted function} \index{function!sorted} \index{reversed function} @@ -10140,11 +10299,11 @@ \section{Debugging} \index{shape error} \index{error!shape} -Lists, dictionaries and tuples are examples of {\bf data +Lists, dictionaries and tuples are examples of {\textbf data structures}; in this chapter we are starting to see compound data structures, like lists of tuples, or dictionaries that contain tuples as keys and lists as values. Compound data structures are useful, but -they are prone to what I call {\bf shape errors}; that is, errors +they are prone to what I call {\textbf shape errors}; that is, errors caused when a data structure has the wrong type, size, or structure. For example, if you are expecting a list with one integer and I give you a plain old integer (not in a list), it won't work. @@ -10152,57 +10311,57 @@ \section{Debugging} \index{module!structshape} To help debug these kinds of errors, I have written a module -called {\tt structshape} that provides a function, also called -{\tt structshape}, that takes any kind of data structure as +called {\texttt structshape} that provides a function, also called +{\texttt structshape}, that takes any kind of data structure as an argument and returns a string that summarizes its shape. You can download it from \url{http://thinkpython2.com/code/structshape.py} Here's the result for a simple list: -\begin{verbatim} +\begin{lstlisting} >>> from structshape import structshape >>> t = [1, 2, 3] >>> structshape(t) 'list of 3 int' -\end{verbatim} +\end{lstlisting} % A fancier program might write ``list of 3 int{\em s}'', but it was easier not to deal with plurals. Here's a list of lists: -\begin{verbatim} +\begin{lstlisting} >>> t2 = [[1,2], [3,4], [5,6]] >>> structshape(t2) 'list of 3 list of 2 int' -\end{verbatim} +\end{lstlisting} % If the elements of the list are not the same type, -{\tt structshape} groups them, in order, by type: +{\texttt structshape} groups them, in order, by type: -\begin{verbatim} +\begin{lstlisting} >>> t3 = [1, 2, 3, 4.0, '5', '6', [7], [8], 9] >>> structshape(t3) 'list of (3 int, float, 2 str, 2 list of int, int)' -\end{verbatim} +\end{lstlisting} % Here's a list of tuples: -\begin{verbatim} +\begin{lstlisting} >>> s = 'abc' >>> lt = list(zip(t, s)) >>> structshape(lt) 'list of 3 tuple of (int, str)' -\end{verbatim} +\end{lstlisting} % And here's a dictionary with 3 items that map integers to strings. -\begin{verbatim} +\begin{lstlisting} >>> d = dict(lt) >>> structshape(d) 'dict of 3 int->str' -\end{verbatim} +\end{lstlisting} % If you are having trouble keeping track of your data structures, -{\tt structshape} can help. +{\texttt structshape} can help. \section{Glossary} @@ -10227,7 +10386,7 @@ \section{Glossary} arguments. \index{scatter} -\item[zip object:] The result of calling a built-in function {\tt zip}; +\item[zip object:] The result of calling a built-in function {\texttt zip}; an object that iterates through a sequence of tuples. \index{zip object} \index{object!zip} @@ -10250,6 +10409,7 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont Write a function called \verb"most_frequent" that takes a string and prints the letters in decreasing order of frequency. Find text @@ -10263,6 +10423,7 @@ \section{Exercises} \begin{exercise} +\normalfont \label{anagrams} \index{anagram set} \index{set!anagram} @@ -10277,12 +10438,12 @@ \section{Exercises} Here is an example of what the output might look like: -\begin{verbatim} +\begin{lstlisting} ['deltas', 'desalt', 'lasted', 'salted', 'slated', 'staled'] ['retainers', 'ternaries'] ['generating', 'greatening'] ['resmelts', 'smelters', 'termless'] -\end{verbatim} +\end{lstlisting} % Hint: you might want to build a dictionary that maps from a collection of letters to a list of words that can be spelled with those @@ -10308,6 +10469,7 @@ \section{Exercises} \end{exercise} \begin{exercise} +\normalfont \index{metathesis} Two words form a ``metathesis pair'' if you can transform one into the @@ -10322,6 +10484,7 @@ \section{Exercises} \begin{exercise} +\normalfont \index{Car Talk} \index{Puzzler} @@ -10367,7 +10530,7 @@ \section{Exercises} are reducible. As a base case, you can consider the empty string reducible. -\item The wordlist I provided, {\tt words.txt}, doesn't +\item The wordlist I provided, {\texttt words.txt}, doesn't contain single letter words. So you might want to add ``I'', ``a'', and the empty string. @@ -10384,6 +10547,7 @@ \section{Exercises} %\begin{exercise} +\normalfont %\url{http://en.wikipedia.org/wiki/Word_Ladder} %\end{exercise} @@ -10410,6 +10574,7 @@ \section{Word frequency analysis} before you read my solutions. \begin{exercise} +\normalfont Write a program that reads a file, breaks each line into words, strips whitespace and punctuation from the words, and @@ -10417,19 +10582,19 @@ \section{Word frequency analysis} \index{string module} \index{module!string} -Hint: The {\tt string} module provides a string named {\tt whitespace}, -which contains space, tab, newline, etc., and {\tt +Hint: The {\texttt string} module provides a string named {\texttt whitespace}, +which contains space, tab, newline, etc., and {\texttt punctuation} which contains the punctuation characters. Let's see if we can make Python swear: -\begin{verbatim} +\begin{lstlisting} >>> import string >>> string.punctuation '!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~' -\end{verbatim} +\end{lstlisting} % -Also, you might consider using the string methods {\tt strip}, -{\tt replace} and {\tt translate}. +Also, you might consider using the string methods {\texttt strip}, +{\texttt replace} and {\texttt translate}. \index{strip method} \index{method!strip} \index{replace method} @@ -10441,6 +10606,7 @@ \section{Word frequency analysis} \begin{exercise} +\normalfont \index{Project Gutenberg} Go to Project Gutenberg (\url{http://gutenberg.org}) and download @@ -10464,6 +10630,7 @@ \section{Word frequency analysis} \begin{exercise} +\normalfont Modify the program from the previous exercise to print the 20 most frequently used words in the book. @@ -10472,6 +10639,7 @@ \section{Word frequency analysis} \begin{exercise} +\normalfont Modify the previous program to read a word list (see Section~\ref{wordlist}) and then print all the words in the book that @@ -10489,7 +10657,7 @@ \section{Random numbers} \index{pseudorandom} Given the same inputs, most computer programs generate the same -outputs every time, so they are said to be {\bf deterministic}. +outputs every time, so they are said to be {\textbf deterministic}. Determinism is usually a good thing, since we expect the same calculation to yield the same result. For some applications, though, we want the computer to be unpredictable. Games are an obvious @@ -10497,63 +10665,64 @@ \section{Random numbers} Making a program truly nondeterministic turns out to be difficult, but there are ways to make it at least seem nondeterministic. One of -them is to use algorithms that generate {\bf pseudorandom} numbers. +them is to use algorithms that generate {\textbf pseudorandom} numbers. Pseudorandom numbers are not truly random because they are generated by a deterministic computation, but just by looking at the numbers it is all but impossible to distinguish them from random. \index{random module} \index{module!random} -The {\tt random} module provides functions that generate +The {\texttt random} module provides functions that generate pseudorandom numbers (which I will simply call ``random'' from here on). \index{random function} \index{function!random} -The function {\tt random} returns a random float +The function {\texttt random} returns a random float between 0.0 and 1.0 (including 0.0 but not 1.0). Each time you -call {\tt random}, you get the next number in a long series. To see a +call {\texttt random}, you get the next number in a long series. To see a sample, run this loop: -\begin{verbatim} +\begin{lstlisting} import random for i in range(10): x = random.random() print(x) -\end{verbatim} +\end{lstlisting} % -The function {\tt randint} takes parameters {\tt low} and -{\tt high} and returns an integer between {\tt low} and -{\tt high} (including both). +The function {\texttt randint} takes parameters {\texttt low} and +{\texttt high} and returns an integer between {\texttt low} and +{\texttt high} (including both). \index{randint function} \index{function!randint} -\begin{verbatim} +\begin{lstlisting} >>> random.randint(5, 10) 5 >>> random.randint(5, 10) 9 -\end{verbatim} +\end{lstlisting} % To choose an element from a sequence at random, you can use -{\tt choice}: +{\texttt choice}: \index{choice function} \index{function!choice} -\begin{verbatim} +\begin{lstlisting} >>> t = [1, 2, 3] >>> random.choice(t) 2 >>> random.choice(t) 3 -\end{verbatim} +\end{lstlisting} % -The {\tt random} module also provides functions to generate +The {\texttt random} module also provides functions to generate random values from continuous distributions including Gaussian, exponential, gamma, and a few more. \begin{exercise} +\normalfont \index{histogram!random choice} Write a function named \verb"choose_from_hist" that takes @@ -10561,12 +10730,12 @@ \section{Random numbers} random value from the histogram, chosen with probability in proportion to frequency. For example, for this histogram: -\begin{verbatim} +\begin{lstlisting} >>> t = ['a', 'a', 'b'] >>> hist = histogram(t) >>> hist {'a': 2, 'b': 1} -\end{verbatim} +\end{lstlisting} % your function should return \verb"'a'" with probability $2/3$ and \verb"'b'" with probability $1/3$. @@ -10584,7 +10753,7 @@ \section{Word histogram} words in the file: \index{histogram!word frequencies} -\begin{verbatim} +\begin{lstlisting} import string def process_file(filename): @@ -10603,24 +10772,24 @@ \section{Word histogram} hist[word] = hist.get(word, 0) + 1 hist = process_file('emma.txt') -\end{verbatim} +\end{lstlisting} % -This program reads {\tt emma.txt}, which contains the text of {\em +This program reads {\texttt emma.txt}, which contains the text of {\em Emma} by Jane Austen. \index{Austin, Jane} \verb"process_file" loops through the lines of the file, passing them one at a time to \verb"process_line". The histogram -{\tt hist} is being used as an accumulator. +{\texttt hist} is being used as an accumulator. \index{accumulator!histogram} \index{traversal} -\verb"process_line" uses the string method {\tt replace} to replace -hyphens with spaces before using {\tt split} to break the line into a -list of strings. It traverses the list of words and uses {\tt strip} -and {\tt lower} to remove punctuation and convert to lower case. (It +\verb"process_line" uses the string method {\texttt replace} to replace +hyphens with spaces before using {\texttt split} to break the line into a +list of strings. It traverses the list of words and uses {\texttt strip} +and {\texttt lower} to remove punctuation and convert to lower case. (It is a shorthand to say that strings are ``converted''; remember that -strings are immutable, so methods like {\tt strip} and {\tt lower} +strings are immutable, so methods like {\texttt strip} and {\texttt lower} return new strings.) Finally, \verb"process_line" updates the histogram by creating a new @@ -10630,32 +10799,32 @@ \section{Word histogram} To count the total number of words in the file, we can add up the frequencies in the histogram: -\begin{verbatim} +\begin{lstlisting} def total_words(hist): return sum(hist.values()) -\end{verbatim} +\end{lstlisting} % The number of different words is just the number of items in the dictionary: -\begin{verbatim} +\begin{lstlisting} def different_words(hist): return len(hist) -\end{verbatim} +\end{lstlisting} % Here is some code to print the results: -\begin{verbatim} +\begin{lstlisting} print('Total number of words:', total_words(hist)) print('Number of different words:', different_words(hist)) -\end{verbatim} +\end{lstlisting} % And the results: -\begin{verbatim} +\begin{lstlisting} Total number of words: 161080 Number of different words: 7214 -\end{verbatim} +\end{lstlisting} % \section{Most common words} @@ -10667,7 +10836,7 @@ \section{Most common words} The following function takes a histogram and returns a list of word-frequency tuples: -\begin{verbatim} +\begin{lstlisting} def most_common(hist): t = [] for key, value in hist.items(): @@ -10675,24 +10844,24 @@ \section{Most common words} t.sort(reverse=True) return t -\end{verbatim} +\end{lstlisting} In each tuple, the frequency appears first, so the resulting list is sorted by frequency. Here is a loop that prints the ten most common words: -\begin{verbatim} +\begin{lstlisting} t = most_common(hist) print('The most common words are:') for freq, word in t[:10]: print(word, freq, sep='\t') -\end{verbatim} +\end{lstlisting} % -I use the keyword argument {\tt sep} to tell {\tt print} to use a tab +I use the keyword argument {\texttt sep} to tell {\texttt print} to use a tab character as a ``separator'', rather than a space, so the second column is lined up. Here are the results from {\em Emma}: -\begin{verbatim} +\begin{lstlisting} The most common words are: to 5242 the 5205 @@ -10704,10 +10873,10 @@ \section{Most common words} her 2483 was 2400 she 2364 -\end{verbatim} +\end{lstlisting} % -This code can be simplified using the {\tt key} parameter of -the {\tt sort} function. If you are curious, you can read about it +This code can be simplified using the {\texttt key} parameter of +the {\texttt sort} function. If you are curious, you can read about it at \url{https://wiki.python.org/moin/HowTo/Sorting}. @@ -10722,33 +10891,33 @@ \section{Optional parameters} \index{programmer-defined function} \index{function!programmer defined} -\begin{verbatim} +\begin{lstlisting} def print_most_common(hist, num=10): t = most_common(hist) print('The most common words are:') for freq, word in t[:num]: print(word, freq, sep='\t') -\end{verbatim} +\end{lstlisting} The first parameter is required; the second is optional. -The {\bf default value} of {\tt num} is 10. +The {\textbf default value} of {\texttt num} is 10. \index{default value} \index{value!default} If you only provide one argument: -\begin{verbatim} +\begin{lstlisting} print_most_common(hist) -\end{verbatim} +\end{lstlisting} -{\tt num} gets the default value. If you provide two arguments: +{\texttt num} gets the default value. If you provide two arguments: -\begin{verbatim} +\begin{lstlisting} print_most_common(hist, 20) -\end{verbatim} +\end{lstlisting} -{\tt num} gets the value of the argument instead. In other -words, the optional argument {\bf overrides} the default value. +{\texttt num} gets the value of the argument instead. In other +words, the optional argument {\textbf overrides} the default value. \index{override} If a function has both required and optional parameters, all @@ -10762,55 +10931,56 @@ \section{Dictionary subtraction} \index{subtraction!dictionary} Finding the words from the book that are not in the word list -from {\tt words.txt} is a problem you might recognize as set +from {\texttt words.txt} is a problem you might recognize as set subtraction; that is, we want to find all the words from one set (the words in the book) that are not in the other (the words in the list). -{\tt subtract} takes dictionaries {\tt d1} and {\tt d2} and returns a -new dictionary that contains all the keys from {\tt d1} that are not -in {\tt d2}. Since we don't really care about the values, we +{\texttt subtract} takes dictionaries {\texttt d1} and {\texttt d2} and returns a +new dictionary that contains all the keys from {\texttt d1} that are not +in {\texttt d2}. Since we don't really care about the values, we set them all to None. -\begin{verbatim} +\begin{lstlisting} def subtract(d1, d2): res = dict() for key in d1: if key not in d2: res[key] = None return res -\end{verbatim} +\end{lstlisting} % -To find the words in the book that are not in {\tt words.txt}, +To find the words in the book that are not in {\texttt words.txt}, we can use \verb"process_file" to build a histogram for -{\tt words.txt}, and then subtract: +{\texttt words.txt}, and then subtract: -\begin{verbatim} +\begin{lstlisting} words = process_file('words.txt') diff = subtract(hist, words) print("Words in the book that aren't in the word list:") for word in diff: print(word, end=' ') -\end{verbatim} +\end{lstlisting} % Here are some of the results from {\em Emma}: -\begin{verbatim} +\begin{lstlisting} Words in the book that aren't in the word list: rencontre jane's blanche woodhouses disingenuousness friend's venice apartment ... -\end{verbatim} +\end{lstlisting} % Some of these words are names and possessives. Others, like ``rencontre'', are no longer in common use. But a few are common words that should really be in the list! \begin{exercise} +\normalfont \index{set} \index{type!set} -Python provides a data structure called {\tt set} that provides many +Python provides a data structure called {\texttt set} that provides many common set operations. You can read about them in Section~\ref{sets}, or read the documentation at \url{http://docs.python.org/3/library/stdtypes.html#types-set}. @@ -10830,18 +11000,18 @@ \section{Random words} is to build a list with multiple copies of each word, according to the observed frequency, and then choose from the list: -\begin{verbatim} +\begin{lstlisting} def random_word(h): t = [] for word, freq in h.items(): t.extend([word] * freq) return random.choice(t) -\end{verbatim} +\end{lstlisting} % -The expression {\tt [word] * freq} creates a list with {\tt freq} -copies of the string {\tt word}. The {\tt extend} -method is similar to {\tt append} except that the argument is +The expression {\texttt [word] * freq} creates a list with {\texttt freq} +copies of the string {\texttt word}. The {\texttt extend} +method is similar to {\texttt append} except that the argument is a sequence. This algorithm works, but it is not very efficient; each time you @@ -10853,7 +11023,7 @@ \section{Random words} \begin{enumerate} -\item Use {\tt keys} to get a list of the words in the book. +\item Use {\texttt keys} to get a list of the words in the book. \item Build a list that contains the cumulative sum of the word frequencies (see Exercise~\ref{cumulative}). The last item @@ -10868,6 +11038,7 @@ \section{Random words} \end{enumerate} \begin{exercise} +\normalfont \label{randhist} \index{algorithm} @@ -10886,9 +11057,9 @@ \section{Markov analysis} If you choose words from the book at random, you can get a sense of the vocabulary, but you probably won't get a sentence: -\begin{verbatim} +\begin{lstlisting} this the small regard harriet which knightley's it most things -\end{verbatim} +\end{lstlisting} % A series of random words seldom makes sense because there is no relationship between successive words. For example, in @@ -10942,6 +11113,7 @@ \section{Markov analysis} you can do Markov analysis with any prefix length. \begin{exercise} +\normalfont Markov analysis: @@ -11033,14 +11205,14 @@ \section{Data structures} With tuples, you can't append or remove, but you can use the addition operator to form a new tuple: -\begin{verbatim} +\begin{lstlisting} def shift(prefix, word): return prefix[1:] + (word,) -\end{verbatim} +\end{lstlisting} % -{\tt shift} takes a tuple of words, {\tt prefix}, and a string, -{\tt word}, and forms a new tuple that has all the words -in {\tt prefix} except the first, and {\tt word} added to +{\texttt shift} takes a tuple of words, {\texttt prefix}, and a string, +{\texttt word}, and forms a new tuple that has all the words +in {\texttt prefix} except the first, and {\texttt word} added to the end. For the collection of suffixes, the operations we need to @@ -11056,16 +11228,16 @@ \section{Data structures} but there are other factors to consider in choosing data structures. One is run time. Sometimes there is a theoretical reason to expect one data structure to be faster than other; for example, I mentioned -that the {\tt in} operator is faster for dictionaries than for lists, +that the {\texttt in} operator is faster for dictionaries than for lists, at least when the number of elements is large. But often you don't know ahead of time which implementation will be faster. One option is to implement both of them and see which -is better. This approach is called {\bf benchmarking}. A practical +is better. This approach is called {\textbf benchmarking}. A practical alternative is to choose the data structure that is easiest to implement, and then see if it is fast enough for the intended application. If so, there is no need to go on. If not, -there are tools, like the {\tt profile} module, that can identify +there are tools, like the {\texttt profile} module, that can identify the places in a program that take the most time. \index{benchmarking} \index{profile module} @@ -11112,7 +11284,7 @@ \section{Debugging} \item[Rubberducking:] If you explain the problem to someone else, you sometimes find the answer before you finish asking the question. Often you don't need the other person; you could just talk to a rubber - duck. And that's the origin of the well-known strategy called {\bf + duck. And that's the origin of the well-known strategy called {\textbf rubber duck debugging}. I am not making this up; see \url{https://en.wikipedia.org/wiki/Rubber_duck_debugging}. @@ -11202,6 +11374,7 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont \index{word frequency} \index{frequency!word} \index{Zipf's law} @@ -11235,8 +11408,8 @@ \section{Exercises} a straight line. Can you estimate the value of $s$? Solution: \url{http://thinkpython2.com/code/zipf.py}. -To run my solution, you need the plotting module {\tt matplotlib}. -If you installed Anaconda, you already have {\tt matplotlib}; +To run my solution, you need the plotting module {\texttt matplotlib}. +If you installed Anaconda, you already have {\texttt matplotlib}; otherwise you might have to install it. \index{matplotlib} @@ -11261,7 +11434,7 @@ \section{Persistence} but when they end, their data disappears. If you run the program again, it starts with a clean slate. -Other programs are {\bf persistent}: they run for a long time +Other programs are {\textbf persistent}: they run for a long time (or all the time); they keep at least some of their data in permanent storage (a hard drive, for example); and if they shut down and restart, they pick up where they left off. @@ -11278,7 +11451,7 @@ \section{Persistence} An alternative is to store the state of the program in a database. In this chapter I will present a simple database and a module, -{\tt pickle}, that makes it easy to store program data. +{\texttt pickle}, that makes it easy to store program data. \index{pickle module} \index{module!pickle} @@ -11295,40 +11468,40 @@ \section{Reading and writing} To write a file, you have to open it with mode \verb"'w'" as a second parameter: -\begin{verbatim} +\begin{lstlisting} >>> fout = open('output.txt', 'w') -\end{verbatim} +\end{lstlisting} % If the file already exists, opening it in write mode clears out the old data and starts fresh, so be careful! If the file doesn't exist, a new one is created. -{\tt open} returns a file object that provides methods for working +{\texttt open} returns a file object that provides methods for working with the file. -The {\tt write} method puts data into the file. +The {\texttt write} method puts data into the file. -\begin{verbatim} +\begin{lstlisting} >>> line1 = "This here's the wattle,\n" >>> fout.write(line1) 24 -\end{verbatim} +\end{lstlisting} % The return value is the number of characters that were written. The file object keeps track of where it is, so if -you call {\tt write} again, it adds the new data to the end of +you call {\texttt write} again, it adds the new data to the end of the file. -\begin{verbatim} +\begin{lstlisting} >>> line2 = "the emblem of our land.\n" >>> fout.write(line2) 24 -\end{verbatim} +\end{lstlisting} % When you are done writing, you should close the file. -\begin{verbatim} +\begin{lstlisting} >>> fout.close() -\end{verbatim} +\end{lstlisting} % \index{close method} \index{method!close} @@ -11341,22 +11514,22 @@ \section{Format operator} \index{format operator} \index{operator!format} -The argument of {\tt write} has to be a string, so if we want +The argument of {\texttt write} has to be a string, so if we want to put other values in a file, we have to convert them to -strings. The easiest way to do that is with {\tt str}: +strings. The easiest way to do that is with {\texttt str}: -\begin{verbatim} +\begin{lstlisting} >>> x = 52 >>> fout.write(str(x)) -\end{verbatim} +\end{lstlisting} % -An alternative is to use the {\bf format operator}, {\tt \%}. When -applied to integers, {\tt \%} is the modulus operator. But -when the first operand is a string, {\tt \%} is the format operator. +An alternative is to use the {\textbf format operator}, {\texttt \%}. When +applied to integers, {\texttt \%} is the modulus operator. But +when the first operand is a string, {\texttt \%} is the format operator. \index{format string} -The first operand is the {\bf format string}, which contains -one or more {\bf format sequences}, which +The first operand is the {\textbf format string}, which contains +one or more {\textbf format sequences}, which specify how the second operand is formatted. The result is a string. \index{format sequence} @@ -11365,22 +11538,22 @@ \section{Format operator} the second operand should be formatted as a decimal integer: -\begin{verbatim} +\begin{lstlisting} >>> camels = 42 >>> '%d' % camels '42' -\end{verbatim} +\end{lstlisting} % The result is the string \verb"'42'", which is not to be confused -with the integer value {\tt 42}. +with the integer value {\texttt 42}. A format sequence can appear anywhere in the string, so you can embed a value in a sentence: -\begin{verbatim} +\begin{lstlisting} >>> 'I have spotted %d camels.' % camels 'I have spotted 42 camels.' -\end{verbatim} +\end{lstlisting} % If there is more than one format sequence in the string, the second argument has to be a tuple. Each format sequence is @@ -11390,10 +11563,10 @@ \section{Format operator} \verb"'%g'" to format a floating-point number, and \verb"'%s'" to format a string: -\begin{verbatim} +\begin{lstlisting} >>> 'In %d years I have spotted %g %s.' % (3, 0.1, 'camels') 'In 3 years I have spotted 0.1 camels.' -\end{verbatim} +\end{lstlisting} % The number of elements in the tuple has to match the number of format sequences in the string. Also, the types of the @@ -11401,12 +11574,12 @@ \section{Format operator} \index{exception!TypeError} \index{TypeError} -\begin{verbatim} +\begin{lstlisting} >>> '%d %d %d' % (1, 2) TypeError: not enough arguments for format string >>> '%d' % 'dollars' TypeError: %d format: a number is required, not str -\end{verbatim} +\end{lstlisting} % In the first example, there aren't enough elements; in the second, the element is the wrong type. @@ -11421,10 +11594,10 @@ \section{Format operator} % formats a floating-point number to be 8 characters long, with % 2 digits after the decimal point: -% % \begin{verbatim} +% % \begin{lstlisting} % >>> '%8.2f' % 3.14159 % ' 3.14' -% \end{verbatim} +% \end{lstlisting} % \afterverb % % % The result takes up eight spaces with two @@ -11438,7 +11611,7 @@ \section{Filenames and paths} \index{directory} \index{folder} -Files are organized into {\bf directories} (also called ``folders''). +Files are organized into {\textbf directories} (also called ``folders''). Every running program has a ``current directory'', which is the default directory for most operations. For example, when you open a file for reading, Python looks for it in the @@ -11446,74 +11619,74 @@ \section{Filenames and paths} \index{os module} \index{module!os} -The {\tt os} module provides functions for working with files and -directories (``os'' stands for ``operating system''). {\tt os.getcwd} +The {\texttt os} module provides functions for working with files and +directories (``os'' stands for ``operating system''). {\texttt os.getcwd} returns the name of the current directory: \index{getcwd function} \index{function!getcwd} -\begin{verbatim} +\begin{lstlisting} >>> import os >>> cwd = os.getcwd() >>> cwd '/home/dinsdale' -\end{verbatim} +\end{lstlisting} % -{\tt cwd} stands for ``current working directory''. The result in -this example is {\tt /home/dinsdale}, which is the home directory of a -user named {\tt dinsdale}. +{\texttt cwd} stands for ``current working directory''. The result in +this example is {\texttt /home/dinsdale}, which is the home directory of a +user named {\texttt dinsdale}. \index{working directory} \index{directory!working} A string like \verb"'/home/dinsdale'" that identifies a file or -directory is called a {\bf path}. +directory is called a {\textbf path}. -A simple filename, like {\tt memo.txt} is also considered a path, -but it is a {\bf relative path} because it relates to the current -directory. If the current directory is {\tt /home/dinsdale}, the -filename {\tt memo.txt} would refer to {\tt /home/dinsdale/memo.txt}. +A simple filename, like {\texttt memo.txt} is also considered a path, +but it is a {\textbf relative path} because it relates to the current +directory. If the current directory is {\texttt /home/dinsdale}, the +filename {\texttt memo.txt} would refer to {\texttt /home/dinsdale/memo.txt}. \index{relative path} \index{path!relative} \index{absolute path} \index{path!absolute} -A path that begins with {\tt /} does not depend on the current -directory; it is called an {\bf absolute path}. To find the absolute -path to a file, you can use {\tt os.path.abspath}: +A path that begins with {\texttt /} does not depend on the current +directory; it is called an {\textbf absolute path}. To find the absolute +path to a file, you can use {\texttt os.path.abspath}: -\begin{verbatim} +\begin{lstlisting} >>> os.path.abspath('memo.txt') '/home/dinsdale/memo.txt' -\end{verbatim} +\end{lstlisting} % -{\tt os.path} provides other functions for working with filenames +{\texttt os.path} provides other functions for working with filenames and paths. For example, -{\tt os.path.exists} checks +{\texttt os.path.exists} checks whether a file or directory exists: \index{exists function} \index{function!exists} -\begin{verbatim} +\begin{lstlisting} >>> os.path.exists('memo.txt') True -\end{verbatim} +\end{lstlisting} % -If it exists, {\tt os.path.isdir} checks whether it's a directory: +If it exists, {\texttt os.path.isdir} checks whether it's a directory: -\begin{verbatim} +\begin{lstlisting} >>> os.path.isdir('memo.txt') False >>> os.path.isdir('/home/dinsdale') True -\end{verbatim} +\end{lstlisting} % -Similarly, {\tt os.path.isfile} checks whether it's a file. +Similarly, {\texttt os.path.isfile} checks whether it's a file. -{\tt os.listdir} returns a list of the files (and other directories) +{\texttt os.listdir} returns a list of the files (and other directories) in the given directory: -\begin{verbatim} +\begin{lstlisting} >>> os.listdir(cwd) ['music', 'photos', 'memo.txt'] -\end{verbatim} +\end{lstlisting} % To demonstrate these functions, the following example ``walks'' through a directory, prints @@ -11522,7 +11695,7 @@ \section{Filenames and paths} \index{walk, directory} \index{directory!walk} -\begin{verbatim} +\begin{lstlisting} def walk(dirname): for name in os.listdir(dirname): path = os.path.join(dirname, name) @@ -11531,12 +11704,12 @@ \section{Filenames and paths} print(path) else: walk(path) -\end{verbatim} +\end{lstlisting} % -{\tt os.path.join} takes a directory and a file name and joins +{\texttt os.path.join} takes a directory and a file name and joins them into a complete path. -The {\tt os} module provides a function called {\tt walk} that is +The {\texttt os} module provides a function called {\texttt walk} that is similar to this one but more versatile. As an exercise, read the documentation and use it to print the names of the files in a given directory and its subdirectories. You can download my solution from @@ -11548,59 +11721,59 @@ \section{Catching exceptions} A lot of things can go wrong when you try to read and write files. If you try to open a file that doesn't exist, you get an -{\tt IOError}: +{\texttt IOError}: \index{open function} \index{function!open} \index{exception!IOError} \index{IOError} -\begin{verbatim} +\begin{lstlisting} >>> fin = open('bad_file') IOError: [Errno 2] No such file or directory: 'bad_file' -\end{verbatim} +\end{lstlisting} % If you don't have permission to access a file: \index{file!permission} \index{permission, file} -\begin{verbatim} +\begin{lstlisting} >>> fout = open('/etc/passwd', 'w') PermissionError: [Errno 13] Permission denied: '/etc/passwd' -\end{verbatim} +\end{lstlisting} % And if you try to open a directory for reading, you get -\begin{verbatim} +\begin{lstlisting} >>> fin = open('/home') IsADirectoryError: [Errno 21] Is a directory: '/home' -\end{verbatim} +\end{lstlisting} % -To avoid these errors, you could use functions like {\tt os.path.exists} -and {\tt os.path.isfile}, but it would take a lot of time and code -to check all the possibilities (if ``{\tt Errno 21}'' is any +To avoid these errors, you could use functions like {\texttt os.path.exists} +and {\texttt os.path.isfile}, but it would take a lot of time and code +to check all the possibilities (if ``{\texttt Errno 21}'' is any indication, there are at least 21 things that can go wrong). \index{exception, catching} \index{try statement} \index{statement!try} It is better to go ahead and try---and deal with problems if they -happen---which is exactly what the {\tt try} statement does. The -syntax is similar to an {\tt if...else} statement: +happen---which is exactly what the {\texttt try} statement does. The +syntax is similar to an {\texttt if...else} statement: -\begin{verbatim} +\begin{lstlisting} try: fin = open('bad_file') except: print('Something went wrong.') -\end{verbatim} +\end{lstlisting} % -Python starts by executing the {\tt try} clause. If all goes -well, it skips the {\tt except} clause and proceeds. If an -exception occurs, it jumps out of the {\tt try} clause and -runs the {\tt except} clause. +Python starts by executing the {\texttt try} clause. If all goes +well, it skips the {\texttt except} clause and proceeds. If an +exception occurs, it jumps out of the {\texttt try} clause and +runs the {\texttt except} clause. -Handling an exception with a {\tt try} statement is called {\bf -catching} an exception. In this example, the {\tt except} clause +Handling an exception with a {\texttt try} statement is called {\textbf +catching} an exception. In this example, the {\texttt except} clause prints an error message that is not very helpful. In general, catching an exception gives you a chance to fix the problem, or try again, or at least end the program gracefully. @@ -11609,14 +11782,14 @@ \section{Catching exceptions} \section{Databases} \index{database} -A {\bf database} is a file that is organized for storing data. Many +A {\textbf database} is a file that is organized for storing data. Many databases are organized like a dictionary in the sense that they map from keys to values. The biggest difference between a database and a dictionary is that the database is on disk (or other permanent storage), so it persists after the program ends. \index{dbm module} \index{module!dbm} -The module {\tt dbm} provides an interface for creating +The module {\texttt dbm} provides an interface for creating and updating database files. As an example, I'll create a database that contains captions for image files. @@ -11625,10 +11798,10 @@ \section{Databases} Opening a database is similar to opening other files: -\begin{verbatim} +\begin{lstlisting} >>> import dbm >>> db = dbm.open('captions', 'c') -\end{verbatim} +\end{lstlisting} % The mode \verb"'c'" means that the database should be created if it doesn't already exist. The result is a database object @@ -11636,53 +11809,53 @@ \section{Databases} \index{database object} \index{object!database} -When you create a new item, {\tt dbm} updates the database file. +When you create a new item, {\texttt dbm} updates the database file. \index{update!database} -\begin{verbatim} +\begin{lstlisting} >>> db['cleese.png'] = 'Photo of John Cleese.' -\end{verbatim} +\end{lstlisting} % -When you access one of the items, {\tt dbm} reads the file: +When you access one of the items, {\texttt dbm} reads the file: -\begin{verbatim} +\begin{lstlisting} >>> db['cleese.png'] b'Photo of John Cleese.' -\end{verbatim} +\end{lstlisting} % -The result is a {\bf bytes object}, which is why it begins with {\tt +The result is a {\textbf bytes object}, which is why it begins with {\texttt b}. A bytes object is similar to a string in many ways. When you get farther into Python, the difference becomes important, but for now we can ignore it. \index{bytes object} \index{object!bytes} -If you make another assignment to an existing key, {\tt dbm} replaces +If you make another assignment to an existing key, {\texttt dbm} replaces the old value: -\begin{verbatim} +\begin{lstlisting} >>> db['cleese.png'] = 'Photo of John Cleese doing a silly walk.' >>> db['cleese.png'] b'Photo of John Cleese doing a silly walk.' -\end{verbatim} +\end{lstlisting} % -Some dictionary methods, like {\tt keys} and {\tt items}, don't -work with database objects. But iteration with a {\tt for} +Some dictionary methods, like {\texttt keys} and {\texttt items}, don't +work with database objects. But iteration with a {\texttt for} loop works: \index{dictionary methods!dbm module} -\begin{verbatim} +\begin{lstlisting} for key in db: print(key, db[key]) -\end{verbatim} +\end{lstlisting} % As with other files, you should close the database when you are done: -\begin{verbatim} +\begin{lstlisting} >>> db.close() -\end{verbatim} +\end{lstlisting} % \index{close method} \index{method!close} @@ -11691,52 +11864,52 @@ \section{Databases} \section{Pickling} \index{pickling} -A limitation of {\tt dbm} is that the keys and values have to be +A limitation of {\texttt dbm} is that the keys and values have to be strings or bytes. If you try to use any other type, you get an error. \index{pickle module} \index{module!pickle} -The {\tt pickle} module can help. It translates +The {\texttt pickle} module can help. It translates almost any type of object into a string suitable for storage in a database, and then translates strings back into objects. -{\tt pickle.dumps} takes an object as a parameter and returns -a string representation ({\tt dumps} is short for ``dump string''): +{\texttt pickle.dumps} takes an object as a parameter and returns +a string representation ({\texttt dumps} is short for ``dump string''): -\begin{verbatim} +\begin{lstlisting} >>> import pickle >>> t = [1, 2, 3] >>> pickle.dumps(t) b'\x80\x03]q\x00(K\x01K\x02K\x03e.' -\end{verbatim} +\end{lstlisting} % The format isn't obvious to human readers; it is meant to be -easy for {\tt pickle} to interpret. {\tt pickle.loads} +easy for {\texttt pickle} to interpret. {\texttt pickle.loads} (``load string'') reconstitutes the object: -\begin{verbatim} +\begin{lstlisting} >>> t1 = [1, 2, 3] >>> s = pickle.dumps(t1) >>> t2 = pickle.loads(s) >>> t2 [1, 2, 3] -\end{verbatim} +\end{lstlisting} % Although the new object has the same value as the old, it is not (in general) the same object: -\begin{verbatim} +\begin{lstlisting} >>> t1 == t2 True >>> t1 is t2 False -\end{verbatim} +\end{lstlisting} % In other words, pickling and then unpickling has the same effect as copying the object. -You can use {\tt pickle} to store non-strings in a database. +You can use {\texttt pickle} to store non-strings in a database. In fact, this combination is so common that it has been -encapsulated in a module called {\tt shelve}. +encapsulated in a module called {\texttt shelve}. \index{shelve module} \index{module!shelve} @@ -11746,61 +11919,61 @@ \section{Pipes} \index{pipe} Most operating systems provide a command-line interface, -also known as a {\bf shell}. Shells usually provide commands +also known as a {\textbf shell}. Shells usually provide commands to navigate the file system and launch applications. For -example, in Unix you can change directories with {\tt cd}, -display the contents of a directory with {\tt ls}, and launch -a web browser by typing (for example) {\tt firefox}. +example, in Unix you can change directories with {\texttt cd}, +display the contents of a directory with {\texttt ls}, and launch +a web browser by typing (for example) {\texttt firefox}. \index{ls (Unix command)} \index{Unix command!ls} Any program that you can launch from the shell can also be -launched from Python using a {\bf pipe object}, which +launched from Python using a {\textbf pipe object}, which represents a running program. -For example, the Unix command {\tt ls -l} normally displays the +For example, the Unix command {\texttt ls -l} normally displays the contents of the current directory in long format. You can -launch {\tt ls} with {\tt os.popen}\footnote{{\tt popen} is deprecated +launch {\texttt ls} with {\texttt os.popen}\footnote{{\texttt popen} is deprecated now, which means we are supposed to stop using it and start using -the {\tt subprocess} module. But for simple cases, I find -{\tt subprocess} more complicated than necessary. So I am going -to keep using {\tt popen} until they take it away.}: +the {\texttt subprocess} module. But for simple cases, I find +{\texttt subprocess} more complicated than necessary. So I am going +to keep using {\texttt popen} until they take it away.}: \index{popen function} \index{function!popen} -\begin{verbatim} +\begin{lstlisting} >>> cmd = 'ls -l' >>> fp = os.popen(cmd) -\end{verbatim} +\end{lstlisting} % The argument is a string that contains a shell command. The return value is an object that behaves like an open -file. You can read the output from the {\tt ls} process one -line at a time with {\tt readline} or get the whole thing at -once with {\tt read}: +file. You can read the output from the {\texttt ls} process one +line at a time with {\texttt readline} or get the whole thing at +once with {\texttt read}: \index{readline method} \index{method!readline} \index{read method} \index{method!read} -\begin{verbatim} +\begin{lstlisting} >>> res = fp.read() -\end{verbatim} +\end{lstlisting} % When you are done, you close the pipe like a file: \index{close method} \index{method!close} -\begin{verbatim} +\begin{lstlisting} >>> stat = fp.close() >>> print(stat) None -\end{verbatim} +\end{lstlisting} % -The return value is the final status of the {\tt ls} process; -{\tt None} means that it ended normally (with no errors). +The return value is the final status of the {\texttt ls} process; +{\texttt None} means that it ended normally (with no errors). -For example, most Unix systems provide a command called {\tt md5sum} +For example, most Unix systems provide a command called {\texttt md5sum} that reads the contents of a file and computes a ``checksum''. You can read about MD5 at \url{http://en.wikipedia.org/wiki/Md5}. This command provides an efficient way to check whether two files @@ -11810,9 +11983,9 @@ \section{Pipes} \index{md5} \index{checksum} -You can use a pipe to run {\tt md5sum} from Python and get the result: +You can use a pipe to run {\texttt md5sum} from Python and get the result: -\begin{verbatim} +\begin{lstlisting} >>> filename = 'book.tex' >>> cmd = 'md5sum ' + filename >>> fp = os.popen(cmd) @@ -11822,7 +11995,7 @@ \section{Pipes} 1e0033f0ed0656636de0d75144ba32e0 book.tex >>> print(stat) None -\end{verbatim} +\end{lstlisting} @@ -11832,10 +12005,10 @@ \section{Writing modules} \index{word count} Any file that contains Python code can be imported as a module. -For example, suppose you have a file named {\tt wc.py} with the following +For example, suppose you have a file named {\texttt wc.py} with the following code: -\begin{verbatim} +\begin{lstlisting} def linecount(filename): count = 0 for line in open(filename): @@ -11843,32 +12016,32 @@ \section{Writing modules} return count print(linecount('wc.py')) -\end{verbatim} +\end{lstlisting} % If you run this program, it reads itself and prints the number of lines in the file, which is 7. You can also import it like this: -\begin{verbatim} +\begin{lstlisting} >>> import wc 7 -\end{verbatim} +\end{lstlisting} % -Now you have a module object {\tt wc}: +Now you have a module object {\texttt wc}: \index{module object} \index{object!module} -\begin{verbatim} +\begin{lstlisting} >>> wc -\end{verbatim} +\end{lstlisting} % The module object provides \verb"linecount": -\begin{verbatim} +\begin{lstlisting} >>> wc.linecount('wc.py') 7 -\end{verbatim} +\end{lstlisting} % So that's how you write modules in Python. @@ -11882,10 +12055,10 @@ \section{Writing modules} Programs that will be imported as modules often use the following idiom: -\begin{verbatim} +\begin{lstlisting} if __name__ == '__main__': print(linecount('wc.py')) -\end{verbatim} +\end{lstlisting} % \verb"__name__" is a built-in variable that is set when the program starts. If the program is running as a script, @@ -11896,9 +12069,9 @@ \section{Writing modules} \index{name built-in variable} \index{main} -As an exercise, type this example into a file named {\tt wc.py} and run +As an exercise, type this example into a file named {\texttt wc.py} and run it as a script. Then run the Python interpreter and -{\tt import wc}. What is the value of \verb"__name__" +{\texttt import wc}. What is the value of \verb"__name__" when the module is being imported? Warning: If you import a module that has already been imported, @@ -11909,7 +12082,7 @@ \section{Writing modules} \index{function!reload} If you want to reload a module, you can use the built-in function -{\tt reload}, but it can be tricky, so the safest thing to do is +{\texttt reload}, but it can be tricky, so the safest thing to do is restart the interpreter and then import the module again. @@ -11921,25 +12094,25 @@ \section{Debugging} with whitespace. These errors can be hard to debug because spaces, tabs and newlines are normally invisible: -\begin{verbatim} +\begin{lstlisting} >>> s = '1 2\t 3\n 4' >>> print(s) 1 2 3 4 -\end{verbatim} +\end{lstlisting} \index{repr function} \index{function!repr} \index{string representation} -The built-in function {\tt repr} can help. It takes any object as an +The built-in function {\texttt repr} can help. It takes any object as an argument and returns a string representation of the object. For strings, it represents whitespace characters with backslash sequences: -\begin{verbatim} +\begin{lstlisting} >>> print(repr(s)) '1 2\t 3\n 4' -\end{verbatim} +\end{lstlisting} This can be helpful for debugging. @@ -11965,7 +12138,7 @@ \section{Glossary} and keeps at least some of its data in permanent storage. \index{persistence} -\item[format operator:] An operator, {\tt \%}, that takes a format +\item[format operator:] An operator, {\texttt \%}, that takes a format string and a tuple and generates a string that includes the elements of the tuple formatted as specified by the format string. \index{format operator} @@ -11976,7 +12149,7 @@ \section{Glossary} \index{format string} \item[format sequence:] A sequence of characters in a format string, -like {\tt \%d}, that specifies how a value should be formatted. +like {\texttt \%d}, that specifies how a value should be formatted. \index{format sequence} \item[text file:] A sequence of characters stored in permanent @@ -11997,8 +12170,8 @@ \section{Glossary} \index{absolute path} \item[catch:] To prevent an exception from terminating -a program using the {\tt try} -and {\tt except} statements. +a program using the {\texttt try} +and {\texttt except} statements. \index{catch} \item[database:] A file whose contents are organized like a dictionary @@ -12024,8 +12197,9 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont -Write a function called {\tt sed} that takes as arguments a pattern string, +Write a function called {\texttt sed} that takes as arguments a pattern string, a replacement string, and two filenames; it should read the first file and write the contents into the second file (creating it if necessary). If the pattern string appears anywhere in the file, it @@ -12039,6 +12213,7 @@ \section{Exercises} \begin{exercise} +\normalfont \index{anagram set} \index{set!anagram} @@ -12059,6 +12234,7 @@ \section{Exercises} \begin{exercise} +\normalfont \label{checksum} \index{MP3} @@ -12071,20 +12247,20 @@ \section{Exercises} \item Write a program that searches a directory and all of its subdirectories, recursively, and returns a list of complete paths -for all files with a given suffix (like {\tt .mp3}). -Hint: {\tt os.path} provides several useful functions for +for all files with a given suffix (like {\texttt .mp3}). +Hint: {\texttt os.path} provides several useful functions for manipulating file and path names. \index{duplicate} \index{MD5 algorithm} \index{algorithm!MD5} \index{checksum} -\item To recognize duplicates, you can use {\tt md5sum} +\item To recognize duplicates, you can use {\texttt md5sum} to compute a ``checksum'' for each files. If two files have the same checksum, they probably have the same contents. \index{md5sum} -\item To double-check, you can use the Unix command {\tt diff}. +\item To double-check, you can use the Unix command {\texttt diff}. \index{diff} \end{enumerate} @@ -12119,7 +12295,7 @@ \section{Programmer-defined types} We have used many of Python's built-in types; now we are going to define a new type. As an example, we will create a type -called {\tt Point} that represents a point in two-dimensional +called {\texttt Point} that represents a point in two-dimensional space. \index{point, mathematical} @@ -12133,7 +12309,7 @@ \section{Programmer-defined types} \begin{itemize} \item We could store the coordinates separately in two -variables, {\tt x} and {\tt y}. +variables, {\texttt x} and {\texttt y}. \item We could store the coordinates as elements in a list or tuple. @@ -12148,19 +12324,19 @@ \section{Programmer-defined types} is more complicated than the other options, but it has advantages that will be apparent soon. -A programmer-defined type is also called a {\bf class}. +A programmer-defined type is also called a {\textbf class}. A class definition looks like this: \index{class} \index{object!class} \index{class definition} \index{definition!class} -\begin{verbatim} +\begin{lstlisting} class Point: """Represents a point in 2-D space.""" -\end{verbatim} +\end{lstlisting} % -The header indicates that the new class is called {\tt Point}. +The header indicates that the new class is called {\texttt Point}. The body is a docstring that explains what the class is for. You can define variables and methods inside a class definition, but we will get back to that later. @@ -12168,39 +12344,39 @@ \section{Programmer-defined types} \index{class!Point} \index{docstring} -Defining a class named {\tt Point} creates a {\bf class object}. +Defining a class named {\texttt Point} creates a {\textbf class object}. -\begin{verbatim} +\begin{lstlisting} >>> Point -\end{verbatim} +\end{lstlisting} % -Because {\tt Point} is defined at the top level, its ``full +Because {\texttt Point} is defined at the top level, its ``full name'' is \verb"__main__.Point". \index{object!class} \index{class object} The class object is like a factory for creating objects. To create a -Point, you call {\tt Point} as if it were a function. +Point, you call {\texttt Point} as if it were a function. -\begin{verbatim} +\begin{lstlisting} >>> blank = Point() >>> blank <__main__.Point object at 0xb7e9d3ac> -\end{verbatim} +\end{lstlisting} % The return value is a reference to a Point object, which we -assign to {\tt blank}. +assign to {\texttt blank}. Creating a new object is called -{\bf instantiation}, and the object is an {\bf instance} of +{\textbf instantiation}, and the object is an {\textbf instance} of the class. \index{instance} \index{instantiation} When you print an instance, Python tells you what class it belongs to and where it is stored in memory (the prefix -{\tt 0x} means that the following number is in hexadecimal). +{\texttt 0x} means that the following number is in hexadecimal). \index{hexadecimal} Every object is an instance of some class, so ``object'' and @@ -12217,22 +12393,22 @@ \section{Attributes} You can assign values to an instance using dot notation: -\begin{verbatim} +\begin{lstlisting} >>> blank.x = 3.0 >>> blank.y = 4.0 -\end{verbatim} +\end{lstlisting} % This syntax is similar to the syntax for selecting a variable from a -module, such as {\tt math.pi} or {\tt string.whitespace}. In this case, +module, such as {\texttt math.pi} or {\texttt string.whitespace}. In this case, though, we are assigning values to named elements of an object. -These elements are called {\bf attributes}. +These elements are called {\textbf attributes}. As a noun, ``AT-trib-ute'' is pronounced with emphasis on the first syllable, as opposed to ``a-TRIB-ute'', which is a verb. The following diagram shows the result of these assignments. A state diagram that shows an object and its attributes is -called an {\bf object diagram}; see Figure~\ref{fig.point}. +called an {\textbf object diagram}; see Figure~\ref{fig.point}. \index{state diagram} \index{diagram!state} \index{object diagram} @@ -12245,55 +12421,55 @@ \section{Attributes} \label{fig.point} \end{figure} -The variable {\tt blank} refers to a Point object, which +The variable {\texttt blank} refers to a Point object, which contains two attributes. Each attribute refers to a floating-point number. You can read the value of an attribute using the same syntax: -\begin{verbatim} +\begin{lstlisting} >>> blank.y 4.0 >>> x = blank.x >>> x 3.0 -\end{verbatim} +\end{lstlisting} % -The expression {\tt blank.x} means, ``Go to the object {\tt blank} -refers to and get the value of {\tt x}.'' In the example, we assign that -value to a variable named {\tt x}. There is no conflict between -the variable {\tt x} and the attribute {\tt x}. +The expression {\texttt blank.x} means, ``Go to the object {\texttt blank} +refers to and get the value of {\texttt x}.'' In the example, we assign that +value to a variable named {\texttt x}. There is no conflict between +the variable {\texttt x} and the attribute {\texttt x}. You can use dot notation as part of any expression. For example: -\begin{verbatim} +\begin{lstlisting} >>> '(%g, %g)' % (blank.x, blank.y) '(3.0, 4.0)' >>> distance = math.sqrt(blank.x**2 + blank.y**2) >>> distance 5.0 -\end{verbatim} +\end{lstlisting} % You can pass an instance as an argument in the usual way. For example: \index{instance!as argument} -\begin{verbatim} +\begin{lstlisting} def print_point(p): print('(%g, %g)' % (p.x, p.y)) -\end{verbatim} +\end{lstlisting} % \verb"print_point" takes a point as an argument and displays it in -mathematical notation. To invoke it, you can pass {\tt blank} as +mathematical notation. To invoke it, you can pass {\texttt blank} as an argument: -\begin{verbatim} +\begin{lstlisting} >>> print_point(blank) (3.0, 4.0) -\end{verbatim} +\end{lstlisting} % -Inside the function, {\tt p} is an alias for {\tt blank}, so if -the function modifies {\tt p}, {\tt blank} changes. +Inside the function, {\texttt p} is an alias for {\texttt blank}, so if +the function modifies {\texttt p}, {\texttt blank} changes. \index{aliasing} As an exercise, write a function called \verb"distance_between_points" @@ -12330,34 +12506,34 @@ \section{Rectangles} Here is the class definition: -\begin{verbatim} +\begin{lstlisting} class Rectangle: """Represents a rectangle. attributes: width, height, corner. """ -\end{verbatim} +\end{lstlisting} % -The docstring lists the attributes: {\tt width} and -{\tt height} are numbers; {\tt corner} is a Point object that +The docstring lists the attributes: {\texttt width} and +{\texttt height} are numbers; {\texttt corner} is a Point object that specifies the lower-left corner. To represent a rectangle, you have to instantiate a Rectangle object and assign values to the attributes: -\begin{verbatim} +\begin{lstlisting} box = Rectangle() box.width = 100.0 box.height = 200.0 box.corner = Point() box.corner.x = 0.0 box.corner.y = 0.0 -\end{verbatim} +\end{lstlisting} % -The expression {\tt box.corner.x} means, -``Go to the object {\tt box} refers to and select the attribute named -{\tt corner}; then go to that object and select the attribute named -{\tt x}.'' +The expression {\texttt box.corner.x} means, +``Go to the object {\texttt box} refers to and select the attribute named +{\texttt corner}; then go to that object and select the attribute named +{\texttt x}.'' \begin{figure} \centerline @@ -12368,7 +12544,7 @@ \section{Rectangles} Figure~\ref{fig.rectangle} shows the state of this object. -An object that is an attribute of another object is {\bf embedded}. +An object that is an attribute of another object is {\textbf embedded}. \index{state diagram} \index{diagram!state} \index{object diagram} @@ -12382,25 +12558,25 @@ \section{Instances as return values} \index{return value} Functions can return instances. For example, \verb"find_center" -takes a {\tt Rectangle} as an argument and returns a {\tt Point} -that contains the coordinates of the center of the {\tt Rectangle}: +takes a {\texttt Rectangle} as an argument and returns a {\texttt Point} +that contains the coordinates of the center of the {\texttt Rectangle}: -\begin{verbatim} +\begin{lstlisting} def find_center(rect): p = Point() p.x = rect.corner.x + rect.width/2 p.y = rect.corner.y + rect.height/2 return p -\end{verbatim} +\end{lstlisting} % -Here is an example that passes {\tt box} as an argument and assigns -the resulting Point to {\tt center}: +Here is an example that passes {\texttt box} as an argument and assigns +the resulting Point to {\texttt center}: -\begin{verbatim} +\begin{lstlisting} >>> center = find_center(box) >>> print_point(center) (50, 100) -\end{verbatim} +\end{lstlisting} % \section{Objects are mutable} @@ -12409,44 +12585,44 @@ \section{Objects are mutable} You can change the state of an object by making an assignment to one of its attributes. For example, to change the size of a rectangle -without changing its position, you can modify the values of {\tt -width} and {\tt height}: +without changing its position, you can modify the values of {\texttt +width} and {\texttt height}: -\begin{verbatim} +\begin{lstlisting} box.width = box.width + 50 box.height = box.height + 100 -\end{verbatim} +\end{lstlisting} % You can also write functions that modify objects. For example, \verb"grow_rectangle" takes a Rectangle object and two numbers, -{\tt dwidth} and {\tt dheight}, and adds the numbers to the +{\texttt dwidth} and {\texttt dheight}, and adds the numbers to the width and height of the rectangle: -\begin{verbatim} +\begin{lstlisting} def grow_rectangle(rect, dwidth, dheight): rect.width += dwidth rect.height += dheight -\end{verbatim} +\end{lstlisting} % Here is an example that demonstrates the effect: -\begin{verbatim} +\begin{lstlisting} >>> box.width, box.height (150.0, 300.0) >>> grow_rectangle(box, 50, 100) >>> box.width, box.height (200.0, 400.0) -\end{verbatim} +\end{lstlisting} % -Inside the function, {\tt rect} is an -alias for {\tt box}, so when the function modifies {\tt rect}, -{\tt box} changes. +Inside the function, {\texttt rect} is an +alias for {\texttt box}, so when the function modifies {\texttt rect}, +{\texttt box} changes. As an exercise, write a function named \verb"move_rectangle" that takes -a Rectangle and two numbers named {\tt dx} and {\tt dy}. It -should change the location of the rectangle by adding {\tt dx} -to the {\tt x} coordinate of {\tt corner} and adding {\tt dy} -to the {\tt y} coordinate of {\tt corner}. +a Rectangle and two numbers named {\texttt dx} and {\texttt dy}. It +should change the location of the rectangle by adding {\texttt dx} +to the {\texttt x} coordinate of {\texttt corner} and adding {\texttt dy} +to the {\texttt y} coordinate of {\texttt corner}. \section{Copying} @@ -12463,22 +12639,22 @@ \section{Copying} \index{module!copy} Copying an object is often an alternative to aliasing. -The {\tt copy} module contains a function called {\tt copy} that +The {\texttt copy} module contains a function called {\texttt copy} that can duplicate any object: -\begin{verbatim} +\begin{lstlisting} >>> p1 = Point() >>> p1.x = 3.0 >>> p1.y = 4.0 >>> import copy >>> p2 = copy.copy(p1) -\end{verbatim} +\end{lstlisting} % -{\tt p1} and {\tt p2} contain the same data, but they are +{\texttt p1} and {\texttt p2} contain the same data, but they are not the same Point. -\begin{verbatim} +\begin{lstlisting} >>> print_point(p1) (3, 4) >>> print_point(p2) @@ -12487,14 +12663,14 @@ \section{Copying} False >>> p1 == p2 False -\end{verbatim} +\end{lstlisting} % -The {\tt is} operator indicates that {\tt p1} and {\tt p2} are not the +The {\texttt is} operator indicates that {\texttt p1} and {\texttt p2} are not the same object, which is what we expected. But you might have expected -{\tt ==} to yield {\tt True} because these points contain the same +{\texttt ==} to yield {\texttt True} because these points contain the same data. In that case, you will be disappointed to learn that for -instances, the default behavior of the {\tt ==} operator is the same -as the {\tt is} operator; it checks object identity, not object +instances, the default behavior of the {\texttt ==} operator is the same +as the {\texttt is} operator; it checks object identity, not object equivalence. That's because for programmer-defined types, Python doesn't know what should be considered equivalent. At least, not yet. \index{is operator} @@ -12502,17 +12678,17 @@ \section{Copying} \index{identity} \index{equivalence} -If you use {\tt copy.copy} to duplicate a Rectangle, you will find +If you use {\texttt copy.copy} to duplicate a Rectangle, you will find that it copies the Rectangle object but not the embedded Point. \index{embedded object!copying} -\begin{verbatim} +\begin{lstlisting} >>> box2 = copy.copy(box) >>> box2 is box False >>> box2.corner is box.corner True -\end{verbatim} +\end{lstlisting} \begin{figure} \centerline @@ -12526,7 +12702,7 @@ \section{Copying} \index{diagram!state} \index{object diagram} \index{diagram!object} -This operation is called a {\bf shallow copy} because it copies the +This operation is called a {\textbf shallow copy} because it copies the object and any references it contains, but not the embedded objects. \index{shallow copy} \index{copy!shallow} @@ -12538,24 +12714,24 @@ \section{Copying} \index{deep copy} \index{copy!deep} -Fortunately, the {\tt copy} module provides a method named {\tt +Fortunately, the {\texttt copy} module provides a method named {\texttt deepcopy} that copies not only the object but also the objects it refers to, and the objects {\em they} refer to, and so on. You will not be surprised to learn that this operation is -called a {\bf deep copy}. +called a {\textbf deep copy}. \index{deepcopy function} \index{function!deepcopy} -\begin{verbatim} +\begin{lstlisting} >>> box3 = copy.deepcopy(box) >>> box3 is box False >>> box3.corner is box.corner False -\end{verbatim} +\end{lstlisting} % -{\tt box3} and {\tt box} are completely separate objects. +{\texttt box3} and {\texttt box} are completely separate objects. As an exercise, write a version of \verb"move_rectangle" that creates and returns a new Rectangle instead of modifying the old one. @@ -12567,64 +12743,64 @@ \section{Debugging} When you start working with objects, you are likely to encounter some new exceptions. If you try to access an attribute -that doesn't exist, you get an {\tt AttributeError}: +that doesn't exist, you get an {\texttt AttributeError}: \index{exception!AttributeError} \index{AttributeError} -\begin{verbatim} +\begin{lstlisting} >>> p = Point() >>> p.x = 3 >>> p.y = 4 >>> p.z AttributeError: Point instance has no attribute 'z' -\end{verbatim} +\end{lstlisting} % If you are not sure what type an object is, you can ask: \index{type function} \index{function!type} -\begin{verbatim} +\begin{lstlisting} >>> type(p) -\end{verbatim} +\end{lstlisting} % -You can also use {\tt isinstance} to check whether an object +You can also use {\texttt isinstance} to check whether an object is an instance of a class: \index{isinstance function} \index{function!isinstance} -\begin{verbatim} +\begin{lstlisting} >>> isinstance(p, Point) True -\end{verbatim} +\end{lstlisting} % If you are not sure whether an object has a particular attribute, -you can use the built-in function {\tt hasattr}: +you can use the built-in function {\texttt hasattr}: \index{hasattr function} \index{function!hasattr} -\begin{verbatim} +\begin{lstlisting} >>> hasattr(p, 'x') True >>> hasattr(p, 'z') False -\end{verbatim} +\end{lstlisting} % The first argument can be any object; the second argument is a {\em string} that contains the name of the attribute. \index{attribute} -You can also use a {\tt try} statement to see if the object has the +You can also use a {\texttt try} statement to see if the object has the attributes you need: \index{try statement} \index{statement!try} -\begin{verbatim} +\begin{lstlisting} try: x = p.x except AttributeError: x = 0 -\end{verbatim} +\end{lstlisting} This approach can make it easier to write functions that work with different types; more on that topic is @@ -12664,12 +12840,12 @@ \section{Glossary} \item[shallow copy:] To copy the contents of an object, including any references to embedded objects; -implemented by the {\tt copy} function in the {\tt copy} module. +implemented by the {\texttt copy} function in the {\texttt copy} module. \index{shallow copy} \item[deep copy:] To copy the contents of an object as well as any embedded objects, and any objects embedded in them, and so on; -implemented by the {\tt deepcopy} function in the {\tt copy} module. +implemented by the {\texttt deepcopy} function in the {\texttt copy} module. \index{deep copy} \item[object diagram:] A diagram that shows objects, their @@ -12683,9 +12859,10 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont -Write a definition for a class named {\tt Circle} with attributes -{\tt center} and {\tt radius}, where {\tt center} is a Point object +Write a definition for a class named {\texttt Circle} with attributes +{\texttt center} and {\texttt radius}, where {\texttt center} is a Point object and radius is a number. Instantiate a Circle object that represents a circle with its center @@ -12710,6 +12887,7 @@ \section{Exercises} \begin{exercise} +\normalfont Write a function called \verb"draw_rect" that takes a Turtle object and a Rectangle and uses the Turtle to draw the Rectangle. See @@ -12743,43 +12921,43 @@ \section{Time} \label{isafter} As another example of a programmer-defined type, we'll define a class -called {\tt Time} that records the time of day. The class definition +called {\texttt Time} that records the time of day. The class definition looks like this: \index{programmer-defined type} \index{type!programmer-defined} \index{Time class} \index{class!Time} -\begin{verbatim} +\begin{lstlisting} class Time: """Represents the time of day. attributes: hour, minute, second """ -\end{verbatim} +\end{lstlisting} % -We can create a new {\tt Time} object and assign +We can create a new {\texttt Time} object and assign attributes for hours, minutes, and seconds: -\begin{verbatim} +\begin{lstlisting} time = Time() time.hour = 11 time.minute = 59 time.second = 30 -\end{verbatim} +\end{lstlisting} % -The state diagram for the {\tt Time} object looks like Figure~\ref{fig.time}. +The state diagram for the {\texttt Time} object looks like Figure~\ref{fig.time}. \index{state diagram} \index{diagram!state} \index{object diagram} \index{diagram!object} As an exercise, write a function called \verb"print_time" that takes a -Time object and prints it in the form {\tt hour:minute:second}. +Time object and prints it in the form {\texttt hour:minute:second}. Hint: the format sequence \verb"'%.2d'" prints an integer using at least two digits, including a leading zero if necessary. Write a boolean function called \verb"is_after" that -takes two Time objects, {\tt t1} and {\tt t2}, and -returns {\tt True} if {\tt t1} follows {\tt t2} chronologically and -{\tt False} otherwise. Challenge: don't use an {\tt if} statement. +takes two Time objects, {\texttt t1} and {\texttt t2}, and +returns {\texttt True} if {\texttt t1} follows {\texttt t2} chronologically and +{\texttt False} otherwise. Challenge: don't use an {\texttt if} statement. \begin{figure} \centerline @@ -12795,40 +12973,40 @@ \section{Pure functions} In the next few sections, we'll write two functions that add time values. They demonstrate two kinds of functions: pure functions and -modifiers. They also demonstrate a development plan I'll call {\bf +modifiers. They also demonstrate a development plan I'll call {\textbf prototype and patch}, which is a way of tackling a complex problem by starting with a simple prototype and incrementally dealing with the complications. Here is a simple prototype of \verb"add_time": -\begin{verbatim} +\begin{lstlisting} def add_time(t1, t2): sum = Time() sum.hour = t1.hour + t2.hour sum.minute = t1.minute + t2.minute sum.second = t1.second + t2.second return sum -\end{verbatim} +\end{lstlisting} % -The function creates a new {\tt Time} object, initializes its +The function creates a new {\texttt Time} object, initializes its attributes, and returns a reference to the new object. This is called -a {\bf pure function} because it does not modify any of the objects +a {\textbf pure function} because it does not modify any of the objects passed to it as arguments and it has no effect, like displaying a value or getting user input, other than returning a value. \index{pure function} \index{function type!pure} -To test this function, I'll create two Time objects: {\tt start} +To test this function, I'll create two Time objects: {\texttt start} contains the start time of a movie, like {\em Monty Python and the -Holy Grail}, and {\tt duration} contains the run time of the movie, +Holy Grail}, and {\texttt duration} contains the run time of the movie, which is one hour 35 minutes. \index{Monty Python and the Holy Grail} \verb"add_time" figures out when the movie will be done. -\begin{verbatim} +\begin{lstlisting} >>> start = Time() >>> start.hour = 9 >>> start.minute = 45 @@ -12842,9 +13020,9 @@ \section{Pure functions} >>> done = add_time(start, duration) >>> print_time(done) 10:80:00 -\end{verbatim} +\end{lstlisting} % -The result, {\tt 10:80:00} might not be what you were hoping +The result, {\texttt 10:80:00} might not be what you were hoping for. The problem is that this function does not deal with cases where the number of seconds or minutes adds up to more than sixty. When that happens, we have to ``carry'' the extra seconds into the minute column @@ -12853,7 +13031,7 @@ \section{Pure functions} Here's an improved version: -\begin{verbatim} +\begin{lstlisting} def add_time(t1, t2): sum = Time() sum.hour = t1.hour + t2.hour @@ -12869,7 +13047,7 @@ \section{Pure functions} sum.hour += 1 return sum -\end{verbatim} +\end{lstlisting} % Although this function is correct, it is starting to get big. We will see a shorter alternative later. @@ -12882,14 +13060,14 @@ \section{Modifiers} Sometimes it is useful for a function to modify the objects it gets as parameters. In that case, the changes are visible to the caller. -Functions that work this way are called {\bf modifiers}. +Functions that work this way are called {\textbf modifiers}. \index{increment} -{\tt increment}, which adds a given number of seconds to a {\tt Time} +{\texttt increment}, which adds a given number of seconds to a {\texttt Time} object, can be written naturally as a modifier. Here is a rough draft: -\begin{verbatim} +\begin{lstlisting} def increment(time, seconds): time.second += seconds @@ -12900,20 +13078,20 @@ \section{Modifiers} if time.minute >= 60: time.minute -= 60 time.hour += 1 -\end{verbatim} +\end{lstlisting} % The first line performs the basic operation; the remainder deals with the special cases we saw before. \index{special case} -Is this function correct? What happens if {\tt seconds} +Is this function correct? What happens if {\texttt seconds} is much greater than sixty? In that case, it is not enough to carry once; we have to keep doing it -until {\tt time.second} is less than sixty. One solution is to -replace the {\tt if} statements with {\tt while} statements. That +until {\texttt time.second} is less than sixty. One solution is to +replace the {\texttt if} statements with {\texttt while} statements. That would make the function correct, but not very efficient. As an -exercise, write a correct version of {\tt increment} that doesn't +exercise, write a correct version of {\texttt increment} that doesn't contain any loops. Anything that can be done with modifiers can also be done with pure @@ -12925,11 +13103,11 @@ \section{Modifiers} In general, I recommend that you write pure functions whenever it is reasonable and resort to modifiers only if there is a compelling -advantage. This approach might be called a {\bf functional +advantage. This approach might be called a {\textbf functional programming style}. \index{functional programming style} -As an exercise, write a ``pure'' version of {\tt increment} that +As an exercise, write a ``pure'' version of {\texttt increment} that creates and returns a new Time object rather than modifying the parameter. @@ -12952,16 +13130,16 @@ \section{Prototyping versus planning} many special cases---and unreliable---since it is hard to know if you have found all the errors. -An alternative is {\bf designed development}, in which high-level +An alternative is {\textbf designed development}, in which high-level insight into the problem can make the programming much easier. In this case, the insight is that a Time object is really a three-digit number in base 60 (see \url{http://en.wikipedia.org/wiki/Sexagesimal}.)! The -{\tt second} attribute is the ``ones column'', the {\tt minute} -attribute is the ``sixties column'', and the {\tt hour} attribute is +{\texttt second} attribute is the ``ones column'', the {\texttt minute} +attribute is the ``sixties column'', and the {\texttt hour} attribute is the ``thirty-six hundreds column''. \index{sexagesimal} -When we wrote \verb"add_time" and {\tt increment}, we were effectively +When we wrote \verb"add_time" and {\texttt increment}, we were effectively doing addition in base 60, which is why we had to carry from one column to the next. \index{carrying, addition with} @@ -12972,43 +13150,43 @@ \section{Prototyping versus planning} Here is a function that converts Times to integers: -\begin{verbatim} +\begin{lstlisting} def time_to_int(time): minutes = time.hour * 60 + time.minute seconds = minutes * 60 + time.second return seconds -\end{verbatim} +\end{lstlisting} % And here is a function that converts an integer to a Time -(recall that {\tt divmod} divides the first argument by the second +(recall that {\texttt divmod} divides the first argument by the second and returns the quotient and remainder as a tuple). \index{divmod} -\begin{verbatim} +\begin{lstlisting} def int_to_time(seconds): time = Time() minutes, time.second = divmod(seconds, 60) time.hour, time.minute = divmod(minutes, 60) return time -\end{verbatim} +\end{lstlisting} % You might have to think a bit, and run some tests, to convince yourself that these functions are correct. One way to test them is to check that \verb"time_to_int(int_to_time(x)) == x" for many values of -{\tt x}. This is an example of a consistency check. +{\texttt x}. This is an example of a consistency check. \index{consistency check} Once you are convinced they are correct, you can use them to rewrite \verb"add_time": -\begin{verbatim} +\begin{lstlisting} def add_time(t1, t2): seconds = time_to_int(t1) + time_to_int(t2) return int_to_time(seconds) -\end{verbatim} +\end{lstlisting} % This version is shorter than the original, and easier to verify. As -an exercise, rewrite {\tt increment} using \verb"time_to_int" and +an exercise, rewrite {\texttt increment} using \verb"time_to_int" and \verb"int_to_time". In some ways, converting from base 60 to base 10 and back is harder @@ -13037,57 +13215,57 @@ \section{Prototyping versus planning} \section{Debugging} \index{debugging} -A Time object is well-formed if the values of {\tt minute} and {\tt +A Time object is well-formed if the values of {\texttt minute} and {\texttt second} are between 0 and 60 (including 0 but not 60) and if -{\tt hour} is positive. {\tt hour} and {\tt minute} should be -integral values, but we might allow {\tt second} to have a +{\texttt hour} is positive. {\texttt hour} and {\texttt minute} should be +integral values, but we might allow {\texttt second} to have a fraction part. \index{invariant} -Requirements like these are called {\bf invariants} because +Requirements like these are called {\textbf invariants} because they should always be true. To put it a different way, if they are not true, something has gone wrong. Writing code to check invariants can help detect errors and find their causes. For example, you might have a function like \verb"valid_time" that takes a Time object and returns -{\tt False} if it violates an invariant: +{\texttt False} if it violates an invariant: -\begin{verbatim} +\begin{lstlisting} def valid_time(time): if time.hour < 0 or time.minute < 0 or time.second < 0: return False if time.minute >= 60 or time.second >= 60: return False return True -\end{verbatim} +\end{lstlisting} % At the beginning of each function you could check the arguments to make sure they are valid: \index{raise statement} \index{statement!raise} -\begin{verbatim} +\begin{lstlisting} def add_time(t1, t2): if not valid_time(t1) or not valid_time(t2): raise ValueError('invalid Time object in add_time') seconds = time_to_int(t1) + time_to_int(t2) return int_to_time(seconds) -\end{verbatim} +\end{lstlisting} % -Or you could use an {\bf assert statement}, which checks a given invariant +Or you could use an {\textbf assert statement}, which checks a given invariant and raises an exception if it fails: \index{assert statement} \index{statement!assert} -\begin{verbatim} +\begin{lstlisting} def add_time(t1, t2): assert valid_time(t1) and valid_time(t2) seconds = time_to_int(t1) + time_to_int(t2) return int_to_time(seconds) -\end{verbatim} +\end{lstlisting} % -{\tt assert} statements are useful because they distinguish +{\texttt assert} statements are useful because they distinguish code that deals with normal conditions from code that checks for errors. @@ -13112,7 +13290,7 @@ \section{Glossary} \item[modifier:] A function that changes one or more of the objects it receives as arguments. Most modifiers are void; that is, they - return {\tt None}. \index{modifier} + return {\texttt None}. \index{modifier} \item[functional programming style:] A style of program design in which the majority of functions are pure. @@ -13137,6 +13315,7 @@ \section{Exercises} exercises are available from \url{http://thinkpython2.com/code/Time1_soln.py}. \begin{exercise} +\normalfont Write a function called \verb"mul_time" that takes a Time object and a number and returns a new Time object that contains @@ -13152,17 +13331,18 @@ \section{Exercises} \begin{exercise} +\normalfont \index{datetime module} \index{module!datetime} -The {\tt datetime} module provides {\tt time} objects +The {\texttt datetime} module provides {\texttt time} objects that are similar to the Time objects in this chapter, but they provide a rich set of methods and operators. Read the documentation at \url{http://docs.python.org/3/library/datetime.html}. \begin{enumerate} -\item Use the {\tt datetime} module to write a program that gets the +\item Use the {\texttt datetime} module to write a program that gets the current date and prints the day of the week. \item Write a program that takes a birthday as input and prints the @@ -13202,7 +13382,7 @@ \chapter{Classes and methods} \section{Object-oriented features} \index{object-oriented programming} -Python is an {\bf object-oriented programming language}, which means +Python is an {\textbf object-oriented programming language}, which means that it provides features that support object-oriented programming, which has these defining characteristics: @@ -13219,10 +13399,10 @@ \section{Object-oriented features} \end{itemize} -For example, the {\tt Time} class defined in Chapter~\ref{time} +For example, the {\texttt Time} class defined in Chapter~\ref{time} corresponds to the way people record the time of day, and the functions we defined correspond to the kinds of things people do with -times. Similarly, the {\tt Point} and {\tt Rectangle} classes +times. Similarly, the {\texttt Point} and {\texttt Rectangle} classes in Chapter~\ref{clobjects} correspond to the mathematical concepts of a point and a rectangle. @@ -13233,14 +13413,14 @@ \section{Object-oriented features} the alternative is more concise and more accurately conveys the structure of the program. -For example, in {\tt Time1.py} there is no obvious +For example, in {\texttt Time1.py} there is no obvious connection between the class definition and the function definitions that follow. With some examination, it is apparent that every function -takes at least one {\tt Time} object as an argument. +takes at least one {\texttt Time} object as an argument. \index{method} \index{function} -This observation is the motivation for {\bf methods}; a method is +This observation is the motivation for {\textbf methods}; a method is a function that is associated with a particular class. We have seen methods for strings, lists, dictionaries and tuples. In this chapter, we will define methods for programmer-defined types. @@ -13273,84 +13453,84 @@ \section{Printing objects} \index{object!printing} In Chapter~\ref{time}, we defined a class named -{\tt Time} and in Section~\ref{isafter}, you +{\texttt Time} and in Section~\ref{isafter}, you wrote a function named \verb"print_time": -\begin{verbatim} +\begin{lstlisting} class Time: """Represents the time of day.""" def print_time(time): print('%.2d:%.2d:%.2d' % (time.hour, time.minute, time.second)) -\end{verbatim} +\end{lstlisting} % -To call this function, you have to pass a {\tt Time} object as an +To call this function, you have to pass a {\texttt Time} object as an argument: -\begin{verbatim} +\begin{lstlisting} >>> start = Time() >>> start.hour = 9 >>> start.minute = 45 >>> start.second = 00 >>> print_time(start) 09:45:00 -\end{verbatim} +\end{lstlisting} % To make \verb"print_time" a method, all we have to do is move the function definition inside the class definition. Notice the change in indentation. \index{indentation} -\begin{verbatim} +\begin{lstlisting} class Time: def print_time(time): print('%.2d:%.2d:%.2d' % (time.hour, time.minute, time.second)) -\end{verbatim} +\end{lstlisting} % Now there are two ways to call \verb"print_time". The first (and less common) way is to use function syntax: \index{function syntax} \index{dot notation} -\begin{verbatim} +\begin{lstlisting} >>> Time.print_time(start) 09:45:00 -\end{verbatim} +\end{lstlisting} % -In this use of dot notation, {\tt Time} is the name of the class, -and \verb"print_time" is the name of the method. {\tt start} is +In this use of dot notation, {\texttt Time} is the name of the class, +and \verb"print_time" is the name of the method. {\texttt start} is passed as a parameter. The second (and more concise) way is to use method syntax: \index{method syntax} -\begin{verbatim} +\begin{lstlisting} >>> start.print_time() 09:45:00 -\end{verbatim} +\end{lstlisting} % In this use of dot notation, \verb"print_time" is the name of the -method (again), and {\tt start} is the object the method is -invoked on, which is called the {\bf subject}. Just as the +method (again), and {\texttt start} is the object the method is +invoked on, which is called the {\textbf subject}. Just as the subject of a sentence is what the sentence is about, the subject of a method invocation is what the method is about. \index{subject} Inside the method, the subject is assigned to the first -parameter, so in this case {\tt start} is assigned -to {\tt time}. +parameter, so in this case {\texttt start} is assigned +to {\texttt time}. \index{self (parameter name)} \index{parameter!self} By convention, the first parameter of a method is -called {\tt self}, so it would be more common to write +called {\texttt self}, so it would be more common to write \verb"print_time" like this: -\begin{verbatim} +\begin{lstlisting} class Time: def print_time(self): print('%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)) -\end{verbatim} +\end{lstlisting} % The reason for this convention is an implicit metaphor: \index{metaphor, method invocation} @@ -13363,7 +13543,7 @@ \section{Printing objects} \item In object-oriented programming, the objects are the active agents. A method invocation like \verb"start.print_time()" says - ``Hey {\tt start}! Please print yourself.'' + ``Hey {\texttt start}! Please print yourself.'' \end{itemize} @@ -13383,61 +13563,61 @@ \section{Printing objects} \section{Another example} \index{increment} -Here's a version of {\tt increment} (from Section~\ref{increment}) +Here's a version of {\texttt increment} (from Section~\ref{increment}) rewritten as a method: -\begin{verbatim} +\begin{lstlisting} # inside class Time: def increment(self, seconds): seconds += self.time_to_int() return int_to_time(seconds) -\end{verbatim} +\end{lstlisting} % This version assumes that \verb"time_to_int" is written as a method. Also, note that it is a pure function, not a modifier. -Here's how you would invoke {\tt increment}: +Here's how you would invoke {\texttt increment}: -\begin{verbatim} +\begin{lstlisting} >>> start.print_time() 09:45:00 >>> end = start.increment(1337) >>> end.print_time() 10:07:17 -\end{verbatim} +\end{lstlisting} % -The subject, {\tt start}, gets assigned to the first parameter, -{\tt self}. The argument, {\tt 1337}, gets assigned to the -second parameter, {\tt seconds}. +The subject, {\texttt start}, gets assigned to the first parameter, +{\texttt self}. The argument, {\texttt 1337}, gets assigned to the +second parameter, {\texttt seconds}. This mechanism can be confusing, especially if you make an error. -For example, if you invoke {\tt increment} with two arguments, you +For example, if you invoke {\texttt increment} with two arguments, you get: \index{exception!TypeError} \index{TypeError} -\begin{verbatim} +\begin{lstlisting} >>> end = start.increment(1337, 460) TypeError: increment() takes 2 positional arguments but 3 were given -\end{verbatim} +\end{lstlisting} % The error message is initially confusing, because there are only two arguments in parentheses. But the subject is also considered an argument, so all together that's three. -By the way, a {\bf positional argument} is an argument that +By the way, a {\textbf positional argument} is an argument that doesn't have a parameter name; that is, it is not a keyword argument. In this function call: \index{positional argument} \index{argument!positional} -\begin{verbatim} +\begin{lstlisting} sketch(parrot, cage, dead=True) -\end{verbatim} +\end{lstlisting} -{\tt parrot} and {\tt cage} are positional, and {\tt dead} is +{\texttt parrot} and {\texttt cage} are positional, and {\texttt dead} is a keyword argument. @@ -13445,24 +13625,24 @@ \section{A more complicated example} Rewriting \verb"is_after" (from Section~\ref{isafter}) is slightly more complicated because it takes two Time objects as parameters. In -this case it is conventional to name the first parameter {\tt self} -and the second parameter {\tt other}: \index{other (parameter name)} +this case it is conventional to name the first parameter {\texttt self} +and the second parameter {\texttt other}: \index{other (parameter name)} \index{parameter!other} -\begin{verbatim} +\begin{lstlisting} # inside class Time: def is_after(self, other): return self.time_to_int() > other.time_to_int() -\end{verbatim} +\end{lstlisting} % To use this method, you have to invoke it on one object and pass the other as an argument: -\begin{verbatim} +\begin{lstlisting} >>> end.is_after(start) True -\end{verbatim} +\end{lstlisting} % One nice thing about this syntax is that it almost reads like English: ``end is after start?'' @@ -13475,69 +13655,69 @@ \section{The init method} The init method (short for ``initialization'') is a special method that gets invoked when an object is instantiated. Its full name is \verb"__init__" (two underscore characters, -followed by {\tt init}, and then two more underscores). An -init method for the {\tt Time} class might look like this: +followed by {\texttt init}, and then two more underscores). An +init method for the {\texttt Time} class might look like this: -\begin{verbatim} +\begin{lstlisting} # inside class Time: def __init__(self, hour=0, minute=0, second=0): self.hour = hour self.minute = minute self.second = second -\end{verbatim} +\end{lstlisting} % It is common for the parameters of \verb"__init__" to have the same names as the attributes. The statement -\begin{verbatim} +\begin{lstlisting} self.hour = hour -\end{verbatim} +\end{lstlisting} % -stores the value of the parameter {\tt hour} as an attribute -of {\tt self}. +stores the value of the parameter {\texttt hour} as an attribute +of {\texttt self}. \index{optional parameter} \index{parameter!optional} \index{default value} \index{override} -The parameters are optional, so if you call {\tt Time} with +The parameters are optional, so if you call {\texttt Time} with no arguments, you get the default values. -\begin{verbatim} +\begin{lstlisting} >>> time = Time() >>> time.print_time() 00:00:00 -\end{verbatim} +\end{lstlisting} % -If you provide one argument, it overrides {\tt hour}: +If you provide one argument, it overrides {\texttt hour}: -\begin{verbatim} +\begin{lstlisting} >>> time = Time (9) >>> time.print_time() 09:00:00 -\end{verbatim} +\end{lstlisting} % -If you provide two arguments, they override {\tt hour} and -{\tt minute}. +If you provide two arguments, they override {\texttt hour} and +{\texttt minute}. -\begin{verbatim} +\begin{lstlisting} >>> time = Time(9, 45) >>> time.print_time() 09:45:00 -\end{verbatim} +\end{lstlisting} % And if you provide three arguments, they override all three default values. -As an exercise, write an init method for the {\tt Point} class that takes -{\tt x} and {\tt y} as optional parameters and assigns +As an exercise, write an init method for the {\texttt Point} class that takes +{\texttt x} and {\texttt y} as optional parameters and assigns them to the corresponding attributes. \index{Point class} \index{class!Point} -\section{The {\tt \_\_str\_\_} method} +\section{The {\texttt \_\_str\_\_} method} \index{str method@\_\_str\_\_ method} \index{method!\_\_str\_\_} @@ -13545,30 +13725,30 @@ \section{The {\tt \_\_str\_\_} method} that is supposed to return a string representation of an object. \index{string representation} -For example, here is a {\tt str} method for Time objects: +For example, here is a {\texttt str} method for Time objects: -\begin{verbatim} +\begin{lstlisting} # inside class Time: def __str__(self): return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second) -\end{verbatim} +\end{lstlisting} % -When you {\tt print} an object, Python invokes the {\tt str} method: +When you {\texttt print} an object, Python invokes the {\texttt str} method: \index{print statement} \index{statement!print} -\begin{verbatim} +\begin{lstlisting} >>> time = Time(9, 45) >>> print(time) 09:45:00 -\end{verbatim} +\end{lstlisting} % When I write a new class, I almost always start by writing \verb"__init__", which makes it easier to instantiate objects, and \verb"__str__", which is useful for debugging. -As an exercise, write a {\tt str} method for the {\tt Point} class. +As an exercise, write a {\texttt str} method for the {\texttt Point} class. Create a Point object and print it. @@ -13577,8 +13757,8 @@ \section{Operator overloading} By defining other special methods, you can specify the behavior of operators on programmer-defined types. For example, if you define -a method named \verb"__add__" for the {\tt Time} class, you can use the -{\tt +} operator on Time objects. +a method named \verb"__add__" for the {\texttt Time} class, you can use the +{\texttt +} operator on Time objects. \index{programmer-defined type} \index{type!programmer-defined} @@ -13586,35 +13766,35 @@ \section{Operator overloading} \index{add method} \index{method!add} -\begin{verbatim} +\begin{lstlisting} # inside class Time: def __add__(self, other): seconds = self.time_to_int() + other.time_to_int() return int_to_time(seconds) -\end{verbatim} +\end{lstlisting} % And here is how you could use it: -\begin{verbatim} +\begin{lstlisting} >>> start = Time(9, 45) >>> duration = Time(1, 35) >>> print(start + duration) 11:20:00 -\end{verbatim} +\end{lstlisting} % -When you apply the {\tt +} operator to Time objects, Python invokes +When you apply the {\texttt +} operator to Time objects, Python invokes \verb"__add__". When you print the result, Python invokes \verb"__str__". So there is a lot happening behind the scenes! \index{operator overloading} Changing the behavior of an operator so that it works with -programmer-defined types is called {\bf operator overloading}. For every +programmer-defined types is called {\textbf operator overloading}. For every operator in Python there is a corresponding special method, like \verb"__add__". For more details, see \url{http://docs.python.org/3/reference/datamodel.html#specialnames}. -As an exercise, write an {\tt add} method for the Point class. +As an exercise, write an {\texttt add} method for the Point class. \section{Type-based dispatch} @@ -13622,10 +13802,10 @@ \section{Type-based dispatch} In the previous section we added two Time objects, but you also might want to add an integer to a Time object. The following is a version of \verb"__add__" -that checks the type of {\tt other} and invokes either -\verb"add_time" or {\tt increment}: +that checks the type of {\texttt other} and invokes either +\verb"add_time" or {\texttt increment}: -\begin{verbatim} +\begin{lstlisting} # inside class Time: def __add__(self, other): @@ -13641,69 +13821,69 @@ \section{Type-based dispatch} def increment(self, seconds): seconds += self.time_to_int() return int_to_time(seconds) -\end{verbatim} +\end{lstlisting} % -The built-in function {\tt isinstance} takes a value and a -class object, and returns {\tt True} if the value is an instance +The built-in function {\texttt isinstance} takes a value and a +class object, and returns {\texttt True} if the value is an instance of the class. \index{isinstance function} \index{function!isinstance} -If {\tt other} is a Time object, \verb"__add__" invokes +If {\texttt other} is a Time object, \verb"__add__" invokes \verb"add_time". Otherwise it assumes that the parameter -is a number and invokes {\tt increment}. This operation is -called a {\bf type-based dispatch} because it dispatches the +is a number and invokes {\texttt increment}. This operation is +called a {\textbf type-based dispatch} because it dispatches the computation to different methods based on the type of the arguments. \index{type-based dispatch} \index{dispatch, type-based} -Here are examples that use the {\tt +} operator with different +Here are examples that use the {\texttt +} operator with different types: -\begin{verbatim} +\begin{lstlisting} >>> start = Time(9, 45) >>> duration = Time(1, 35) >>> print(start + duration) 11:20:00 >>> print(start + 1337) 10:07:17 -\end{verbatim} +\end{lstlisting} % Unfortunately, this implementation of addition is not commutative. If the integer is the first operand, you get \index{commutativity} -\begin{verbatim} +\begin{lstlisting} >>> print(1337 + start) TypeError: unsupported operand type(s) for +: 'int' and 'instance' -\end{verbatim} +\end{lstlisting} % The problem is, instead of asking the Time object to add an integer, Python is asking an integer to add a Time object, and it doesn't know how. But there is a clever solution for this problem: the special method \verb"__radd__", which stands for ``right-side add''. This method is invoked when a Time object appears on the right side of -the {\tt +} operator. Here's the definition: +the {\texttt +} operator. Here's the definition: \index{radd method} \index{method!radd} -\begin{verbatim} +\begin{lstlisting} # inside class Time: def __radd__(self, other): return self.__add__(other) -\end{verbatim} +\end{lstlisting} % And here's how it's used: -\begin{verbatim} +\begin{lstlisting} >>> print(1337 + start) 10:07:17 -\end{verbatim} +\end{lstlisting} % -As an exercise, write an {\tt add} method for Points that works with +As an exercise, write an {\texttt add} method for Points that works with either a Point object or a tuple: \begin{itemize} @@ -13733,10 +13913,10 @@ \section{Polymorphism} Many of the functions we wrote for strings also work for other sequence types. For example, in Section~\ref{histogram} -we used {\tt histogram} to count the number of times each letter +we used {\texttt histogram} to count the number of times each letter appears in a word. -\begin{verbatim} +\begin{lstlisting} def histogram(s): d = dict() for c in s: @@ -13745,35 +13925,35 @@ \section{Polymorphism} else: d[c] = d[c]+1 return d -\end{verbatim} +\end{lstlisting} % This function also works for lists, tuples, and even dictionaries, -as long as the elements of {\tt s} are hashable, so they can be used -as keys in {\tt d}. +as long as the elements of {\texttt s} are hashable, so they can be used +as keys in {\texttt d}. -\begin{verbatim} +\begin{lstlisting} >>> t = ['spam', 'egg', 'spam', 'spam', 'bacon', 'spam'] >>> histogram(t) {'bacon': 1, 'egg': 1, 'spam': 4} -\end{verbatim} +\end{lstlisting} % -Functions that work with several types are called {\bf polymorphic}. +Functions that work with several types are called {\textbf polymorphic}. Polymorphism can facilitate code reuse. For example, the built-in -function {\tt sum}, which adds the elements of a sequence, works +function {\texttt sum}, which adds the elements of a sequence, works as long as the elements of the sequence support addition. \index{polymorphism} -Since Time objects provide an {\tt add} method, they work -with {\tt sum}: +Since Time objects provide an {\texttt add} method, they work +with {\texttt sum}: -\begin{verbatim} +\begin{lstlisting} >>> t1 = Time(7, 43) >>> t2 = Time(7, 41) >>> t3 = Time(7, 37) >>> total = sum([t1, t2, t3]) >>> print(total) 23:01:00 -\end{verbatim} +\end{lstlisting} % In general, if all of the operations inside a function work with a given type, the function works with that type. @@ -13795,37 +13975,37 @@ \section{Debugging} \index{attribute!initializing} If you are not sure whether an object has a particular attribute, you -can use the built-in function {\tt hasattr} (see Section~\ref{hasattr}). +can use the built-in function {\texttt hasattr} (see Section~\ref{hasattr}). \index{hasattr function} \index{function!hasattr} \index{dict attribute@\_\_dict\_\_ attribute} \index{attribute!\_\_dict\_\_} -Another way to access attributes is the built-in function {\tt vars}, +Another way to access attributes is the built-in function {\texttt vars}, which takes an object and returns a dictionary that maps from attribute names (as strings) to their values: -\begin{verbatim} +\begin{lstlisting} >>> p = Point(3, 4) >>> vars(p) {'y': 4, 'x': 3} -\end{verbatim} +\end{lstlisting} % For purposes of debugging, you might find it useful to keep this function handy: -\begin{verbatim} +\begin{lstlisting} def print_attributes(obj): for attr in vars(obj): print(attr, getattr(obj, attr)) -\end{verbatim} +\end{lstlisting} % \verb"print_attributes" traverses the dictionary and prints each attribute name and its corresponding value. \index{traversal!dictionary} \index{dictionary!traversal} -The built-in function {\tt getattr} takes an object and an attribute +The built-in function {\texttt getattr} takes an object and an attribute name (as a string) and returns the attribute's value. \index{getattr function} \index{function!getattr} @@ -13854,8 +14034,8 @@ \section{Interface and implementation} We could implement those methods in several ways. The details of the implementation depend on how we represent time. In this chapter, the -attributes of a {\tt Time} object are {\tt hour}, {\tt minute}, and -{\tt second}. +attributes of a {\texttt Time} object are {\texttt hour}, {\texttt minute}, and +{\texttt second}. As an alternative, we could replace these attributes with a single integer representing the number of seconds @@ -13900,7 +14080,7 @@ \section{Glossary} \index{argument!positional} \item[operator overloading:] Changing the behavior of an operator like -{\tt +} so it works with a programmer-defined type. +{\texttt +} so it works with a programmer-defined type. \index{overloading} \index{operator!overloading} @@ -13923,13 +14103,14 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont Download the code from this chapter from \url{http://thinkpython2.com/code/Time2.py}. Change the attributes of - {\tt Time} to be a single integer representing seconds since + {\texttt Time} to be a single integer representing seconds since midnight. Then modify the methods (and the function \verb"int_to_time") to work with the new implementation. You - should not have to modify the test code in {\tt main}. When you + should not have to modify the test code in {\texttt main}. When you are done, the output should be the same as before. Solution: \url{http://thinkpython2.com/code/Time2_soln.py}. @@ -13937,6 +14118,7 @@ \section{Exercises} \begin{exercise} +\normalfont \label{kangaroo} \index{default value!avoiding mutable} \index{mutable object, as default value} @@ -13947,7 +14129,7 @@ \section{Exercises} This exercise is a cautionary tale about one of the most common, and difficult to find, errors in Python. -Write a definition for a class named {\tt Kangaroo} with the following +Write a definition for a class named {\texttt Kangaroo} with the following methods: \begin{enumerate} @@ -13964,9 +14146,9 @@ \section{Exercises} \end{enumerate} % Test your code -by creating two {\tt Kangaroo} objects, assigning them to variables -named {\tt kanga} and {\tt roo}, and then adding {\tt roo} to the -contents of {\tt kanga}'s pouch. +by creating two {\texttt Kangaroo} objects, assigning them to variables +named {\texttt kanga} and {\texttt roo}, and then adding {\texttt roo} to the +contents of {\texttt kanga}'s pouch. Download \url{http://thinkpython2.com/code/BadKangaroo.py}. It contains a solution to the previous problem with one big, nasty bug. @@ -13986,7 +14168,7 @@ \section{Exercises} \chapter{Inheritance} The language feature most often associated with object-oriented -programming is {\bf inheritance}. Inheritance is the ability to +programming is {\textbf inheritance}. Inheritance is the ability to define a new class that is a modified version of an existing class. In this chapter I demonstrate inheritance using classes that represent playing cards, decks of cards, and poker hands. @@ -14016,8 +14198,8 @@ \section{Card objects} \index{suit} If we want to define a new object to represent a playing card, it is -obvious what the attributes should be: {\tt rank} and -{\tt suit}. It is not as obvious what type the attributes +obvious what the attributes should be: {\texttt rank} and +{\texttt suit}. It is not as obvious what type the attributes should be. One possibility is to use strings containing words like \verb"'Spade'" for suits and \verb"'Queen'" for ranks. One problem with this implementation is that it would not be easy to compare cards to @@ -14027,7 +14209,7 @@ \section{Card objects} \index{map to} \index{representation} -An alternative is to use integers to {\bf encode} the ranks and suits. +An alternative is to use integers to {\textbf encode} the ranks and suits. In this context, ``encode'' means that we are going to define a mapping between numbers and suits, or between numbers and ranks. This kind of encoding is not meant to be a secret (that @@ -14063,16 +14245,16 @@ \section{Card objects} \index{Card class} \index{class!Card} -The class definition for {\tt Card} looks like this: +The class definition for {\texttt Card} looks like this: -\begin{verbatim} +\begin{lstlisting} class Card: """Represents a standard playing card.""" def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank -\end{verbatim} +\end{lstlisting} % As usual, the init method takes an optional parameter for each attribute. The default card is @@ -14080,12 +14262,12 @@ \section{Card objects} \index{init method} \index{method!init} -To create a Card, you call {\tt Card} with the +To create a Card, you call {\texttt Card} with the suit and rank of the card you want. -\begin{verbatim} +\begin{lstlisting} queen_of_diamonds = Card(1, 12) -\end{verbatim} +\end{lstlisting} % @@ -14097,10 +14279,10 @@ \section{Class attributes} In order to print Card objects in a way that people can easily read, we need a mapping from the integer codes to the corresponding ranks and suits. A natural way to -do that is with lists of strings. We assign these lists to {\bf class +do that is with lists of strings. We assign these lists to {\textbf class attributes}: -\begin{verbatim} +\begin{lstlisting} # inside class Card: suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] @@ -14110,47 +14292,47 @@ \section{Class attributes} def __str__(self): return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit]) -\end{verbatim} +\end{lstlisting} % Variables like \verb"suit_names" and \verb"rank_names", which are defined inside a class but outside of any method, are called class attributes because they are associated with the class object -{\tt Card}. +{\texttt Card}. \index{instance attribute} \index{attribute!instance} -This term distinguishes them from variables like {\tt suit} and {\tt - rank}, which are called {\bf instance attributes} because they are +This term distinguishes them from variables like {\texttt suit} and {\texttt + rank}, which are called {\textbf instance attributes} because they are associated with a particular instance. \index{dot notation} Both kinds of attribute are accessed using dot notation. For -example, in \verb"__str__", {\tt self} is a Card object, -and {\tt self.rank} is its rank. Similarly, {\tt Card} +example, in \verb"__str__", {\texttt self} is a Card object, +and {\texttt self.rank} is its rank. Similarly, {\texttt Card} is a class object, and \verb"Card.rank_names" is a list of strings associated with the class. -Every card has its own {\tt suit} and {\tt rank}, but there +Every card has its own {\texttt suit} and {\texttt rank}, but there is only one copy of \verb"suit_names" and \verb"rank_names". Putting it all together, the expression -\verb"Card.rank_names[self.rank]" means ``use the attribute {\tt rank} -from the object {\tt self} as an index into the list \verb"rank_names" -from the class {\tt Card}, and select the appropriate string.'' +\verb"Card.rank_names[self.rank]" means ``use the attribute {\texttt rank} +from the object {\texttt self} as an index into the list \verb"rank_names" +from the class {\texttt Card}, and select the appropriate string.'' -The first element of \verb"rank_names" is {\tt None} because there -is no card with rank zero. By including {\tt None} as a place-keeper, +The first element of \verb"rank_names" is {\texttt None} because there +is no card with rank zero. By including {\texttt None} as a place-keeper, we get a mapping with the nice property that the index 2 maps to the string \verb"'2'", and so on. To avoid this tweak, we could have used a dictionary instead of a list. With the methods we have so far, we can create and print cards: -\begin{verbatim} +\begin{lstlisting} >>> card1 = Card(2, 11) >>> print(card1) Jack of Hearts -\end{verbatim} +\end{lstlisting} \begin{figure} \centerline @@ -14159,10 +14341,10 @@ \section{Class attributes} \label{fig.card1} \end{figure} -Figure~\ref{fig.card1} is a diagram of the {\tt Card} class object and -one Card instance. {\tt Card} is a class object; its type is {\tt - type}. {\tt card1} is an instance of {\tt Card}, so its type is -{\tt Card}. To save space, I didn't draw the contents of +Figure~\ref{fig.card1} is a diagram of the {\texttt Card} class object and +one Card instance. {\texttt Card} is a class object; its type is {\texttt + type}. {\texttt card1} is an instance of {\texttt Card}, so its type is +{\texttt Card}. To save space, I didn't draw the contents of \verb"suit_names" and \verb"rank_names". \index{state diagram} \index{diagram!state} \index{object diagram} \index{diagram!object} @@ -14173,7 +14355,7 @@ \section{Comparing cards} \index{relational operator} For built-in types, there are relational operators -({\tt <}, {\tt >}, {\tt ==}, etc.) +({\texttt <}, {\texttt >}, {\texttt ==}, etc.) that compare values and determine when one is greater than, less than, or equal to another. For programmer-defined types, we can override the behavior of @@ -14182,8 +14364,8 @@ \section{Comparing cards} \index{programmer-defined type} \index{type!programmer-defined} -\verb"__lt__" takes two parameters, {\tt self} and {\tt other}, -and returns {\tt True} if {\tt self} is strictly less than {\tt other}. +\verb"__lt__" takes two parameters, {\texttt self} and {\texttt other}, +and returns {\texttt True} if {\texttt self} is strictly less than {\texttt other}. \index{override} \index{operator overloading} @@ -14202,7 +14384,7 @@ \section{Comparing cards} With that decided, we can write \verb"__lt__": -\begin{verbatim} +\begin{lstlisting} # inside class Card: def __lt__(self, other): @@ -14212,20 +14394,20 @@ \section{Comparing cards} # suits are the same... check ranks return self.rank < other.rank -\end{verbatim} +\end{lstlisting} % You can write this more concisely using tuple comparison: \index{tuple!comparison} \index{comparison!tuple} -\begin{verbatim} +\begin{lstlisting} # inside class Card: def __lt__(self, other): t1 = self.suit, self.rank t2 = other.suit, other.rank return t1 < t2 -\end{verbatim} +\end{lstlisting} % As an exercise, write an \verb"__lt__" method for Time objects. You can use tuple comparison, but you also might consider @@ -14242,15 +14424,15 @@ \section{Decks} \index{init method} \index{method!init} -The following is a class definition for {\tt Deck}. The -init method creates the attribute {\tt cards} and generates +The following is a class definition for {\texttt Deck}. The +init method creates the attribute {\texttt cards} and generates the standard set of fifty-two cards: \index{composition} \index{loop!nested} \index{Deck class} \index{class!Deck} -\begin{verbatim} +\begin{lstlisting} class Deck: def __init__(self): @@ -14259,13 +14441,13 @@ \section{Decks} for rank in range(1, 14): card = Card(suit, rank) self.cards.append(card) -\end{verbatim} +\end{lstlisting} % The easiest way to populate the deck is with a nested loop. The outer loop enumerates the suits from 0 to 3. The inner loop enumerates the ranks from 1 to 13. Each iteration creates a new Card with the current suit and rank, -and appends it to {\tt self.cards}. +and appends it to {\texttt self.cards}. \index{append method} \index{method!append} @@ -14275,9 +14457,9 @@ \section{Printing the deck} \index{str method@\_\_str\_\_ method} \index{method!\_\_str\_\_} -Here is a \verb"__str__" method for {\tt Deck}: +Here is a \verb"__str__" method for {\texttt Deck}: -\begin{verbatim} +\begin{lstlisting} #inside class Deck: def __str__(self): @@ -14285,19 +14467,19 @@ \section{Printing the deck} for card in self.cards: res.append(str(card)) return '\n'.join(res) -\end{verbatim} +\end{lstlisting} % This method demonstrates an efficient way to accumulate a large string: building a list of strings and then using the string method -{\tt join}. The built-in function {\tt str} invokes the +{\texttt join}. The built-in function {\texttt str} invokes the \verb"__str__" method on each card and returns the string representation. \index{accumulator!string} \index{string!accumulator} \index{join method} \index{method!join} \index{newline} -Since we invoke {\tt join} on a newline character, the cards +Since we invoke {\texttt join} on a newline character, the cards are separated by newlines. Here's what the result looks like: -\begin{verbatim} +\begin{lstlisting} >>> deck = Deck() >>> print(deck) Ace of Clubs @@ -14308,7 +14490,7 @@ \section{Printing the deck} Jack of Spades Queen of Spades King of Spades -\end{verbatim} +\end{lstlisting} % Even though the result appears on 52 lines, it is one long string that contains newlines. @@ -14318,33 +14500,33 @@ \section{Add, remove, shuffle and sort} To deal cards, we would like a method that removes a card from the deck and returns it. -The list method {\tt pop} provides a convenient way to do that: +The list method {\texttt pop} provides a convenient way to do that: \index{pop method} \index{method!pop} -\begin{verbatim} +\begin{lstlisting} #inside class Deck: def pop_card(self): return self.cards.pop() -\end{verbatim} +\end{lstlisting} % -Since {\tt pop} removes the {\em last} card in the list, we are +Since {\texttt pop} removes the {\em last} card in the list, we are dealing from the bottom of the deck. \index{append method} \index{method!append} -To add a card, we can use the list method {\tt append}: +To add a card, we can use the list method {\texttt append}: -\begin{verbatim} +\begin{lstlisting} #inside class Deck: def add_card(self, card): self.cards.append(card) -\end{verbatim} +\end{lstlisting} % A method like this that uses another method without doing -much work is sometimes called a {\bf veneer}. The metaphor +much work is sometimes called a {\textbf veneer}. The metaphor comes from woodworking, where a veneer is a thin layer of good quality wood glued to the surface of a cheaper piece of wood to improve the appearance. @@ -14355,24 +14537,24 @@ \section{Add, remove, shuffle and sort} improves the appearance, or interface, of the implementation. -As another example, we can write a Deck method named {\tt shuffle} -using the function {\tt shuffle} from the {\tt random} module: +As another example, we can write a Deck method named {\texttt shuffle} +using the function {\texttt shuffle} from the {\texttt random} module: \index{random module} \index{module!random} \index{shuffle function} \index{function!shuffle} -\begin{verbatim} +\begin{lstlisting} # inside class Deck: def shuffle(self): random.shuffle(self.cards) -\end{verbatim} +\end{lstlisting} % -Don't forget to import {\tt random}. +Don't forget to import {\texttt random}. -As an exercise, write a Deck method named {\tt sort} that uses the -list method {\tt sort} to sort the cards in a {\tt Deck}. {\tt sort} +As an exercise, write a Deck method named {\texttt sort} that uses the +list method {\texttt sort} to sort the cards in a {\texttt Deck}. {\texttt sort} uses the \verb"__lt__" method we defined to determine the order. \index{sort method} \index{method!sort} @@ -14403,81 +14585,81 @@ \section{Inheritance} \index{Hand class} \index{class!Hand} -\begin{verbatim} +\begin{lstlisting} class Hand(Deck): """Represents a hand of playing cards.""" -\end{verbatim} +\end{lstlisting} % -This definition indicates that {\tt Hand} inherits from {\tt Deck}; +This definition indicates that {\texttt Hand} inherits from {\texttt Deck}; that means we can use methods like \verb"pop_card" and \verb"add_card" for Hands as well as Decks. When a new class inherits from an existing one, the existing -one is called the {\bf parent} and the new class is -called the {\bf child}. +one is called the {\textbf parent} and the new class is +called the {\textbf child}. \index{parent class} \index{child class} \index{class!child} -In this example, {\tt Hand} inherits \verb"__init__" from {\tt Deck}, +In this example, {\texttt Hand} inherits \verb"__init__" from {\texttt Deck}, but it doesn't really do what we want: instead of populating the hand -with 52 new cards, the init method for Hands should initialize {\tt +with 52 new cards, the init method for Hands should initialize {\texttt cards} with an empty list. \index{override} \index{init method} \index{method!init} -If we provide an init method in the {\tt Hand} class, it overrides the -one in the {\tt Deck} class: +If we provide an init method in the {\texttt Hand} class, it overrides the +one in the {\texttt Deck} class: -\begin{verbatim} +\begin{lstlisting} # inside class Hand: def __init__(self, label=''): self.cards = [] self.label = label -\end{verbatim} +\end{lstlisting} % When you create a Hand, Python invokes this init method, not the -one in {\tt Deck}. +one in {\texttt Deck}. -\begin{verbatim} +\begin{lstlisting} >>> hand = Hand('new hand') >>> hand.cards [] >>> hand.label 'new hand' -\end{verbatim} +\end{lstlisting} % -The other methods are inherited from {\tt Deck}, so we can use +The other methods are inherited from {\texttt Deck}, so we can use \verb"pop_card" and \verb"add_card" to deal a card: -\begin{verbatim} +\begin{lstlisting} >>> deck = Deck() >>> card = deck.pop_card() >>> hand.add_card(card) >>> print(hand) King of Spades -\end{verbatim} +\end{lstlisting} % A natural next step is to encapsulate this code in a method called \verb"move_cards": \index{encapsulation} -\begin{verbatim} +\begin{lstlisting} #inside class Deck: def move_cards(self, hand, num): for i in range(num): hand.add_card(self.pop_card()) -\end{verbatim} +\end{lstlisting} % \verb"move_cards" takes two arguments, a Hand object and the number of -cards to deal. It modifies both {\tt self} and {\tt hand}, and -returns {\tt None}. +cards to deal. It modifies both {\texttt self} and {\texttt hand}, and +returns {\texttt None}. In some games, cards are moved from one hand to another, or from a hand back to the deck. You can use \verb"move_cards" -for any of these operations: {\tt self} can be either a Deck -or a Hand, and {\tt hand}, despite the name, can also be a {\tt Deck}. +for any of these operations: {\texttt self} can be either a Deck +or a Hand, and {\texttt hand}, despite the name, can also be a {\texttt Deck}. Inheritance is a useful feature. Some programs that would be repetitive without inheritance can be written more elegantly @@ -14515,16 +14697,16 @@ \section{Class diagrams} \item Objects in one class might contain references to objects in another class. For example, each Rectangle contains a reference to a Point, and each Deck contains references to many Cards. -This kind of relationship is called {\bf HAS-A}, as in, ``a Rectangle +This kind of relationship is called {\textbf HAS-A}, as in, ``a Rectangle has a Point.'' \item One class might inherit from another. This relationship -is called {\bf IS-A}, as in, ``a Hand is a kind of a Deck.'' +is called {\textbf IS-A}, as in, ``a Hand is a kind of a Deck.'' \item One class might depend on another in the sense that objects in one class take objects in the second class as parameters, or use objects in the second class as part of a computation. This -kind of relationship is called a {\bf dependency}. +kind of relationship is called a {\textbf dependency}. \end{itemize} \index{IS-A relationship} @@ -14532,9 +14714,9 @@ \section{Class diagrams} \index{class diagram} \index{diagram!class} -A {\bf class diagram} is a graphical representation of these +A {\textbf class diagram} is a graphical representation of these relationships. For example, Figure~\ref{fig.class1} shows the -relationships between {\tt Card}, {\tt Deck} and {\tt Hand}. +relationships between {\texttt Card}, {\texttt Deck} and {\texttt Hand}. \begin{figure} \centerline @@ -14552,10 +14734,10 @@ \section{Class diagrams} objects. \index{multiplicity (in class diagram)} -The star ({\tt *}) near the arrow head is a -{\bf multiplicity}; it indicates how many Cards a Deck has. -A multiplicity can be a simple number, like {\tt 52}, a range, -like {\tt 5..7} or a star, which indicates that a Deck can +The star ({\texttt *}) near the arrow head is a +{\textbf multiplicity}; it indicates how many Cards a Deck has. +A multiplicity can be a simple number, like {\texttt 52}, a range, +like {\texttt 5..7} or a star, which indicates that a Deck can have any number of Cards. There are no dependencies in this diagram. They would normally @@ -14578,15 +14760,15 @@ \section{Debugging} Suppose you are writing a function that works with Hand objects. You would like it to work with all kinds of Hands, like PokerHands, BridgeHands, etc. If you invoke a method like -{\tt shuffle}, you might get the one defined in {\tt Deck}, +{\texttt shuffle}, you might get the one defined in {\texttt Deck}, but if any of the subclasses override this method, you'll get that version instead. This behavior is usually a good thing, but it can be confusing. Any time you are unsure about the flow of execution through your program, the simplest solution is to add print statements at the -beginning of the relevant methods. If {\tt Deck.shuffle} prints a -message that says something like {\tt Running Deck.shuffle}, then as +beginning of the relevant methods. If {\texttt Deck.shuffle} prints a +message that says something like {\texttt Running Deck.shuffle}, then as the program runs it traces the flow of execution. \index{flow of execution} @@ -14594,27 +14776,27 @@ \section{Debugging} object and a method name (as a string) and returns the class that provides the definition of the method: -\begin{verbatim} +\begin{lstlisting} def find_defining_class(obj, meth_name): for ty in type(obj).mro(): if meth_name in ty.__dict__: return ty -\end{verbatim} +\end{lstlisting} % Here's an example: -\begin{verbatim} +\begin{lstlisting} >>> hand = Hand() >>> find_defining_class(hand, 'shuffle') -\end{verbatim} +\end{lstlisting} % -So the {\tt shuffle} method for this Hand is the one in {\tt Deck}. +So the {\texttt shuffle} method for this Hand is the one in {\texttt Deck}. \index{mro method} \index{method!mro} \index{method resolution order} -\verb"find_defining_class" uses the {\tt mro} method to get the list +\verb"find_defining_class" uses the {\texttt mro} method to get the list of class objects (types) that will be searched for methods. ``MRO'' stands for ``method resolution order'', which is the sequence of classes Python searches to ``resolve'' a method name. @@ -14640,7 +14822,7 @@ \section{Data encapsulation} The previous chapters demonstrate a development plan we might call ``object-oriented design''. We identified objects we needed---like -{\tt Point}, {\tt Rectangle} and {\tt Time}---and defined classes to +{\texttt Point}, {\texttt Rectangle} and {\texttt Time}---and defined classes to represent them. In each case there is an obvious correspondence between the object and some entity in the real world (or at least a mathematical world). @@ -14650,7 +14832,7 @@ \section{Data encapsulation} and how they should interact. In that case you need a different development plan. In the same way that we discovered function interfaces by encapsulation and generalization, we can discover -class interfaces by {\bf data encapsulation}. +class interfaces by {\textbf data encapsulation}. \index{data encapsulation} Markov analysis, from Section~\ref{markov}, provides a good example. @@ -14658,10 +14840,10 @@ \section{Data encapsulation} you'll see that it uses two global variables---\verb"suffix_map" and \verb"prefix"---that are read and written from several functions. -\begin{verbatim} +\begin{lstlisting} suffix_map = {} prefix = () -\end{verbatim} +\end{lstlisting} Because these variables are global, we can only run one analysis at a time. If we read two texts, their prefixes and suffixes would be @@ -14672,18 +14854,18 @@ \section{Data encapsulation} the state of each analysis in an object. Here's what that looks like: -\begin{verbatim} +\begin{lstlisting} class Markov: def __init__(self): self.suffix_map = {} self.prefix = () -\end{verbatim} +\end{lstlisting} Next, we transform the functions into methods. For example, here's \verb"process_word": -\begin{verbatim} +\begin{lstlisting} def process_word(self, word, order=2): if len(self.prefix) < order: self.prefix += (word,) @@ -14696,7 +14878,7 @@ \section{Data encapsulation} self.suffix_map[self.prefix] = [word] self.prefix = shift(self.prefix, word) -\end{verbatim} +\end{lstlisting} Transforming a program like this---changing the design without changing the behavior---is another example of refactoring @@ -14724,7 +14906,7 @@ \section{Data encapsulation} As an exercise, download my Markov code from \url{http://thinkpython2.com/code/markov.py}, and follow the steps described above to encapsulate the global variables as attributes of a -new class called {\tt Markov}. Solution: +new class called {\texttt Markov}. Solution: \url{http://thinkpython2.com/code/Markov.py} (note the capital M). @@ -14799,10 +14981,11 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont For the following program, draw a UML class diagram that shows these classes and the relationships among them. -\begin{verbatim} +\begin{lstlisting} class PingPongParent: pass @@ -14824,7 +15007,7 @@ \section{Exercises} pong = Pong() ping = Ping(pong) pong.add_ping(ping) -\end{verbatim} +\end{lstlisting} \end{exercise} @@ -14832,6 +15015,7 @@ \section{Exercises} \begin{exercise} +\normalfont Write a Deck method called \verb"deal_hands" that takes two parameters, the number of hands and the number of cards per hand. It should create the appropriate number of Hand objects, deal @@ -14840,6 +15024,7 @@ \section{Exercises} \begin{exercise} +\normalfont \label{poker} The following are the possible hands in poker, in increasing order @@ -14858,8 +15043,8 @@ \section{Exercises} \vspace{-0.05in} \item[straight:] five cards with ranks in sequence (aces can -be high or low, so {\tt Ace-2-3-4-5} is a straight and so is {\tt -10-Jack-Queen-King-Ace}, but {\tt Queen-King-Ace-2-3} is not.) +be high or low, so {\texttt Ace-2-3-4-5} is a straight and so is {\texttt +10-Jack-Queen-King-Ace}, but {\texttt Queen-King-Ace-2-3} is not.) \vspace{-0.05in} \item[flush:] five cards with the same suit @@ -14886,32 +15071,32 @@ \section{Exercises} \begin{description} -\item[{\tt Card.py}]: A complete version of the {\tt Card}, -{\tt Deck} and {\tt Hand} classes in this chapter. +\item[{\texttt Card.py}]: A complete version of the {\texttt Card}, +{\texttt Deck} and {\texttt Hand} classes in this chapter. -\item[{\tt PokerHand.py}]: An incomplete implementation of a class +\item[{\texttt PokerHand.py}]: An incomplete implementation of a class that represents a poker hand, and some code that tests it. \end{description} % -\item If you run {\tt PokerHand.py}, it deals seven 7-card poker hands +\item If you run {\texttt PokerHand.py}, it deals seven 7-card poker hands and checks to see if any of them contains a flush. Read this code carefully before you go on. -\item Add methods to {\tt PokerHand.py} named \verb"has_pair", +\item Add methods to {\texttt PokerHand.py} named \verb"has_pair", \verb"has_twopair", etc. that return True or False according to whether or not the hand meets the relevant criteria. Your code should work correctly for ``hands'' that contain any number of cards (although 5 and 7 are the most common sizes). -\item Write a method named {\tt classify} that figures out +\item Write a method named {\texttt classify} that figures out the highest-value classification for a hand and sets the -{\tt label} attribute accordingly. For example, a 7-card hand +{\texttt label} attribute accordingly. For example, a 7-card hand might contain a flush and a pair; it should be labeled ``flush''. \item When you are convinced that your classification methods are working, the next step is to estimate the probabilities of the various -hands. Write a function in {\tt PokerHand.py} that shuffles a deck of +hands. Write a function in {\texttt PokerHand.py} that shuffles a deck of cards, divides it into hands, classifies the hands, and counts the number of times various classifications appear. @@ -14949,71 +15134,71 @@ \section{Conditional expressions} \index{conditional expression} \index{expression!conditional} -\begin{verbatim} +\begin{lstlisting} if x > 0: y = math.log(x) else: y = float('nan') -\end{verbatim} +\end{lstlisting} -This statement checks whether {\tt x} is positive. If so, it computes -{\tt math.log}. If not, {\tt math.log} would raise a ValueError. To +This statement checks whether {\texttt x} is positive. If so, it computes +{\texttt math.log}. If not, {\texttt math.log} would raise a ValueError. To avoid stopping the program, we generate a ``NaN'', which is a special floating-point value that represents ``Not a Number''. \index{NaN} \index{floating-point} -We can write this statement more concisely using a {\bf conditional +We can write this statement more concisely using a {\textbf conditional expression}: -\begin{verbatim} +\begin{lstlisting} y = math.log(x) if x > 0 else float('nan') -\end{verbatim} +\end{lstlisting} -You can almost read this line like English: ``{\tt y} gets log-{\tt x} -if {\tt x} is greater than 0; otherwise it gets NaN''. +You can almost read this line like English: ``{\texttt y} gets log-{\texttt x} +if {\texttt x} is greater than 0; otherwise it gets NaN''. Recursive functions can sometimes be rewritten using conditional -expressions. For example, here is a recursive version of {\tt factorial}: +expressions. For example, here is a recursive version of {\texttt factorial}: \index{factorial} \index{function!factorial} -\begin{verbatim} +\begin{lstlisting} def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) -\end{verbatim} +\end{lstlisting} We can rewrite it like this: -\begin{verbatim} +\begin{lstlisting} def factorial(n): return 1 if n == 0 else n * factorial(n-1) -\end{verbatim} +\end{lstlisting} Another use of conditional expressions is handling optional arguments. For example, here is the init method from -{\tt GoodKangaroo} (see Exercise~\ref{kangaroo}): +{\texttt GoodKangaroo} (see Exercise~\ref{kangaroo}): \index{optional argument} \index{argument!optional} -\begin{verbatim} +\begin{lstlisting} def __init__(self, name, contents=None): self.name = name if contents == None: contents = [] self.pouch_contents = contents -\end{verbatim} +\end{lstlisting} We can rewrite this one like this: -\begin{verbatim} +\begin{lstlisting} def __init__(self, name, contents=None): self.name = name self.pouch_contents = [] if contents == None else contents -\end{verbatim} +\end{lstlisting} In general, you can replace a conditional statement with a conditional expression if both branches contain simple expressions that are @@ -15027,57 +15212,57 @@ \section{List comprehensions} In Section~\ref{filter} we saw the map and filter patterns. For example, this function takes a list of strings, maps the string method -{\tt capitalize} to the elements, and returns a new list of strings: +{\texttt capitalize} to the elements, and returns a new list of strings: -\begin{verbatim} +\begin{lstlisting} def capitalize_all(t): res = [] for s in t: res.append(s.capitalize()) return res -\end{verbatim} +\end{lstlisting} -We can write this more concisely using a {\bf list comprehension}: +We can write this more concisely using a {\textbf list comprehension}: \index{list comprehension} -\begin{verbatim} +\begin{lstlisting} def capitalize_all(t): return [s.capitalize() for s in t] -\end{verbatim} +\end{lstlisting} The bracket operators indicate that we are constructing a new list. The expression inside the brackets specifies the elements -of the list, and the {\tt for} clause indicates what sequence +of the list, and the {\texttt for} clause indicates what sequence we are traversing. \index{list} \index{for loop} The syntax of a list comprehension is a little awkward because -the loop variable, {\tt s} in this example, appears in the expression +the loop variable, {\texttt s} in this example, appears in the expression before we get to the definition. \index{loop variable} List comprehensions can also be used for filtering. For example, -this function selects only the elements of {\tt t} that are +this function selects only the elements of {\texttt t} that are upper case, and returns a new list: \index{filter pattern} \index{pattern!filter} -\begin{verbatim} +\begin{lstlisting} def only_upper(t): res = [] for s in t: if s.isupper(): res.append(s) return res -\end{verbatim} +\end{lstlisting} We can rewrite it using a list comprehension -\begin{verbatim} +\begin{lstlisting} def only_upper(t): return [s for s in t if s.isupper()] -\end{verbatim} +\end{lstlisting} List comprehensions are concise and easy to read, at least for simple expressions. And they are usually faster than the equivalent for @@ -15094,109 +15279,109 @@ \section{List comprehensions} \section{Generator expressions} -{\bf Generator expressions} are similar to list comprehensions, but +{\textbf Generator expressions} are similar to list comprehensions, but with parentheses instead of square brackets: \index{generator expression} \index{expression!generator} -\begin{verbatim} +\begin{lstlisting} >>> g = (x**2 for x in range(5)) >>> g at 0x7f4c45a786c0> -\end{verbatim} +\end{lstlisting} % The result is a generator object that knows how to iterate through a sequence of values. But unlike a list comprehension, it does not compute the values all at once; it waits to be asked. The built-in -function {\tt next} gets the next value from the generator: +function {\texttt next} gets the next value from the generator: \index{generator object} \index{object!generator} -\begin{verbatim} +\begin{lstlisting} >>> next(g) 0 >>> next(g) 1 -\end{verbatim} +\end{lstlisting} % -When you get to the end of the sequence, {\tt next} raises a -StopIteration exception. You can also use a {\tt for} loop to iterate +When you get to the end of the sequence, {\texttt next} raises a +StopIteration exception. You can also use a {\texttt for} loop to iterate through the values: \index{StopIteration} \index{exception!StopIteration} -\begin{verbatim} +\begin{lstlisting} >>> for val in g: ... print(val) 4 9 16 -\end{verbatim} +\end{lstlisting} % The generator object keeps track of where it is in the sequence, -so the {\tt for} loop picks up where {\tt next} left off. Once the -generator is exhausted, it continues to raise {\tt StopException}: +so the {\texttt for} loop picks up where {\texttt next} left off. Once the +generator is exhausted, it continues to raise {\texttt StopException}: -\begin{verbatim} +\begin{lstlisting} >>> next(g) StopIteration -\end{verbatim} +\end{lstlisting} -Generator expressions are often used with functions like {\tt sum}, -{\tt max}, and {\tt min}: +Generator expressions are often used with functions like {\texttt sum}, +{\texttt max}, and {\texttt min}: \index{sum} \index{function!sum} -\begin{verbatim} +\begin{lstlisting} >>> sum(x**2 for x in range(5)) 30 -\end{verbatim} +\end{lstlisting} -\section{{\tt any} and {\tt all}} +\section{{\texttt any} and {\texttt all}} -Python provides a built-in function, {\tt any}, that takes a sequence -of boolean values and returns {\tt True} if any of the values are {\tt +Python provides a built-in function, {\texttt any}, that takes a sequence +of boolean values and returns {\texttt True} if any of the values are {\texttt True}. It works on lists: \index{any} \index{built-in function!any} -\begin{verbatim} +\begin{lstlisting} >>> any([False, False, True]) True -\end{verbatim} +\end{lstlisting} % But it is often used with generator expressions: \index{generator expression} \index{expression!generator} -\begin{verbatim} +\begin{lstlisting} >>> any(letter == 't' for letter in 'monty') True -\end{verbatim} +\end{lstlisting} % That example isn't very useful because it does the same thing -as the {\tt in} operator. But we could use {\tt any} to rewrite +as the {\texttt in} operator. But we could use {\texttt any} to rewrite some of the search functions we wrote in Section~\ref{search}. For -example, we could write {\tt avoids} like this: +example, we could write {\texttt avoids} like this: \index{search pattern} \index{pattern!search} -\begin{verbatim} +\begin{lstlisting} def avoids(word, forbidden): return not any(letter in forbidden for letter in word) -\end{verbatim} +\end{lstlisting} % -The function almost reads like English, ``{\tt word} avoids -{\tt forbidden} if there are not any forbidden letters in {\tt word}.'' +The function almost reads like English, ``{\texttt word} avoids +{\texttt forbidden} if there are not any forbidden letters in {\texttt word}.'' -Using {\tt any} with a generator expression is efficient because -it stops immediately if it finds a {\tt True} value, +Using {\texttt any} with a generator expression is efficient because +it stops immediately if it finds a {\texttt True} value, so it doesn't have to evaluate the whole sequence. -Python provides another built-in function, {\tt all}, that returns -{\tt True} if every element of the sequence is {\tt True}. As -an exercise, use {\tt all} to re-write \verb"uses_all" from +Python provides another built-in function, {\texttt all}, that returns +{\texttt True} if every element of the sequence is {\texttt True}. As +an exercise, use {\texttt all} to re-write \verb"uses_all" from Section~\ref{search}. \index{all} \index{built-in function!any} @@ -15207,25 +15392,25 @@ \section{Sets} In Section~\ref{dictsub} I use dictionaries to find the words that appear in a document but not in a word list. The function -I wrote takes {\tt d1}, which contains the words from the document -as keys, and {\tt d2}, which contains the list of words. It -returns a dictionary that contains the keys from {\tt d1} that -are not in {\tt d2}. +I wrote takes {\texttt d1}, which contains the words from the document +as keys, and {\texttt d2}, which contains the list of words. It +returns a dictionary that contains the keys from {\texttt d1} that +are not in {\texttt d2}. -\begin{verbatim} +\begin{lstlisting} def subtract(d1, d2): res = dict() for key in d1: if key not in d2: res[key] = None return res -\end{verbatim} +\end{lstlisting} % -In all of these dictionaries, the values are {\tt None} because +In all of these dictionaries, the values are {\texttt None} because we never use them. As a result, we waste some storage space. \index{dictionary subtraction} -Python provides another built-in type, called a {\tt set}, that +Python provides another built-in type, called a {\texttt set}, that behaves like a collection of dictionary keys with no values. Adding elements to a set is fast; so is checking membership. And sets provide methods and operators to compute common set operations. @@ -15233,14 +15418,14 @@ \section{Sets} \index{object!set} For example, set subtraction is available as a method called -{\tt difference} or as an operator, {\tt -}. So we can rewrite -{\tt subtract} like this: +{\texttt difference} or as an operator, {\texttt -}. So we can rewrite +{\texttt subtract} like this: \index{set subtraction} -\begin{verbatim} +\begin{lstlisting} def subtract(d1, d2): return set(d1) - set(d2) -\end{verbatim} +\end{lstlisting} % The result is a set instead of a dictionary, but for operations like iteration, the behavior is the same. @@ -15250,7 +15435,7 @@ \section{Sets} \verb"has_duplicates", from Exercise~\ref{duplicate}, that uses a dictionary: -\begin{verbatim} +\begin{lstlisting} def has_duplicates(t): d = {} for x in t: @@ -15258,47 +15443,47 @@ \section{Sets} return True d[x] = True return False -\end{verbatim} +\end{lstlisting} When an element appears for the first time, it is added to the dictionary. If the same element appears again, the function returns -{\tt True}. +{\texttt True}. Using sets, we can write the same function like this: -\begin{verbatim} +\begin{lstlisting} def has_duplicates(t): return len(set(t)) < len(t) -\end{verbatim} +\end{lstlisting} % -An element can only appear in a set once, so if an element in {\tt t} -appears more than once, the set will be smaller than {\tt t}. If there -are no duplicates, the set will be the same size as {\tt t}. +An element can only appear in a set once, so if an element in {\texttt t} +appears more than once, the set will be smaller than {\texttt t}. If there +are no duplicates, the set will be the same size as {\texttt t}. \index{duplicate} We can also use sets to do some of the exercises in Chapter~\ref{wordplay}. For example, here's a version of \verb"uses_only" with a loop: -\begin{verbatim} +\begin{lstlisting} def uses_only(word, available): for letter in word: if letter not in available: return False return True -\end{verbatim} +\end{lstlisting} % -\verb"uses_only" checks whether all letters in {\tt word} are -in {\tt available}. We can rewrite it like this: +\verb"uses_only" checks whether all letters in {\texttt word} are +in {\texttt available}. We can rewrite it like this: -\begin{verbatim} +\begin{lstlisting} def uses_only(word, available): return set(word) <= set(available) -\end{verbatim} +\end{lstlisting} % The \verb"<=" operator checks whether one set is a subset or another, including the possibility that they are equal, which is true if all -the letters in {\tt word} appear in {\tt available}. +the letters in {\texttt word} appear in {\texttt available}. \index{subset} As an exercise, rewrite \verb"avoids" using sets. @@ -15308,24 +15493,24 @@ \section{Counters} A Counter is like a set, except that if an element appears more than once, the Counter keeps track of how many times it appears. -If you are familiar with the mathematical idea of a {\bf multiset}, +If you are familiar with the mathematical idea of a {\textbf multiset}, a Counter is a natural way to represent a multiset. \index{Counter} \index{object!Counter} \index{multiset} -Counter is defined in a standard module called {\tt collections}, +Counter is defined in a standard module called {\texttt collections}, so you have to import it. You can initialize a Counter with a string, list, or anything else that supports iteration: \index{collections} \index{module!collections} -\begin{verbatim} +\begin{lstlisting} >>> from collections import Counter >>> count = Counter('parrot') >>> count Counter({'r': 2, 't': 1, 'o': 1, 'p': 1, 'a': 1}) -\end{verbatim} +\end{lstlisting} Counters behave like dictionaries in many ways; they map from each key to the number of times it appears. As in dictionaries, @@ -15334,18 +15519,18 @@ \section{Counters} Unlike dictionaries, Counters don't raise an exception if you access an element that doesn't appear. Instead, they return 0: -\begin{verbatim} +\begin{lstlisting} >>> count['d'] 0 -\end{verbatim} +\end{lstlisting} We can use Counters to rewrite \verb"is_anagram" from Exercise~\ref{anagram}: -\begin{verbatim} +\begin{lstlisting} def is_anagram(word1, word2): return Counter(word1) == Counter(word2) -\end{verbatim} +\end{lstlisting} If two words are anagrams, they contain the same letters with the same counts, so their Counters are equivalent. @@ -15356,19 +15541,19 @@ \section{Counters} returns a list of value-frequency pairs, sorted from most common to least: -\begin{verbatim} +\begin{lstlisting} >>> count = Counter('parrot') >>> for val, freq in count.most_common(3): ... print(val, freq) r 2 p 1 a 1 -\end{verbatim} +\end{lstlisting} \section{defaultdict} -The {\tt collections} module also provides {\tt defaultdict}, which is +The {\texttt collections} module also provides {\texttt defaultdict}, which is like a dictionary except that if you access a key that doesn't exist, it can generate a new value on the fly. \index{defaultdict} @@ -15378,46 +15563,46 @@ \section{defaultdict} When you create a defaultdict, you provide a function that's used to create new values. A function used to create objects is sometimes -called a {\bf factory}. The built-in functions that create lists, sets, +called a {\textbf factory}. The built-in functions that create lists, sets, and other types can be used as factories: \index{factory function} -\begin{verbatim} +\begin{lstlisting} >>> from collections import defaultdict >>> d = defaultdict(list) -\end{verbatim} +\end{lstlisting} -Notice that the argument is {\tt list}, which is a class object, -not {\tt list()}, which is a new list. The function you provide +Notice that the argument is {\texttt list}, which is a class object, +not {\texttt list()}, which is a new list. The function you provide doesn't get called unless you access a key that doesn't exist. -\begin{verbatim} +\begin{lstlisting} >>> t = d['new key'] >>> t [] -\end{verbatim} +\end{lstlisting} -The new list, which we're calling {\tt t}, is also added to the -dictionary. So if we modify {\tt t}, the change appears in {\tt d}: +The new list, which we're calling {\texttt t}, is also added to the +dictionary. So if we modify {\texttt t}, the change appears in {\texttt d}: -\begin{verbatim} +\begin{lstlisting} >>> t.append('new value') >>> d defaultdict(, {'new key': ['new value']}) -\end{verbatim} +\end{lstlisting} If you are making a dictionary of lists, you can often write simpler -code using {\tt defaultdict}. In my solution to +code using {\texttt defaultdict}. In my solution to Exercise~\ref{anagrams}, which you can get from \url{http://thinkpython2.com/code/anagram_sets.py}, I make a dictionary that maps from a sorted string of letters to the list of -words that can be spelled with those letters. For example, {\tt - 'opst'} maps to the list {\tt ['opts', 'post', 'pots', 'spot', +words that can be spelled with those letters. For example, {\texttt + 'opst'} maps to the list {\texttt ['opts', 'post', 'pots', 'spot', 'stop', 'tops']}. Here's the original code: -\begin{verbatim} +\begin{lstlisting} def all_anagrams(filename): d = {} for line in open(filename): @@ -15428,13 +15613,13 @@ \section{defaultdict} else: d[t].append(word) return d -\end{verbatim} +\end{lstlisting} -This can be simplified using {\tt setdefault}, which you might +This can be simplified using {\texttt setdefault}, which you might have used in Exercise~\ref{setdefault}: \index{setdefault} -\begin{verbatim} +\begin{lstlisting} def all_anagrams(filename): d = {} for line in open(filename): @@ -15442,7 +15627,7 @@ \section{defaultdict} t = signature(word) d.setdefault(t, []).append(word) return d -\end{verbatim} +\end{lstlisting} This solution has the drawback that it makes a new list every time, regardless of whether it is needed. For lists, @@ -15451,9 +15636,9 @@ \section{defaultdict} \index{factory function} We can avoid this problem and -simplify the code using a {\tt defaultdict}: +simplify the code using a {\texttt defaultdict}: -\begin{verbatim} +\begin{lstlisting} def all_anagrams(filename): d = defaultdict(list) for line in open(filename): @@ -15461,13 +15646,13 @@ \section{defaultdict} t = signature(word) d[t].append(word) return d -\end{verbatim} +\end{lstlisting} My solution to Exercise~\ref{poker}, which you can download from \url{http://thinkpython2.com/code/PokerHandSoln.py}, -uses {\tt setdefault} in the function +uses {\texttt setdefault} in the function \verb"has_straightflush". This solution has the drawback -of creating a {\tt Hand} object every time through the loop, whether +of creating a {\texttt Hand} object every time through the loop, whether it is needed or not. As an exercise, rewrite it using a defaultdict. @@ -15476,10 +15661,10 @@ \section{Named tuples} Many simple objects are basically collections of related values. For example, the Point object defined in Chapter~\ref{clobjects} contains -two numbers, {\tt x} and {\tt y}. When you define a class like +two numbers, {\texttt x} and {\texttt y}. When you define a class like this, you usually start with an init method and a str method: -\begin{verbatim} +\begin{lstlisting} class Point: def __init__(self, x=0, y=0): @@ -15488,41 +15673,41 @@ \section{Named tuples} def __str__(self): return '(%g, %g)' % (self.x, self.y) -\end{verbatim} +\end{lstlisting} This is a lot of code to convey a small amount of information. Python provides a more concise way to say the same thing: -\begin{verbatim} +\begin{lstlisting} from collections import namedtuple Point = namedtuple('Point', ['x', 'y']) -\end{verbatim} +\end{lstlisting} The first argument is the name of the class you want to create. The second is a list of the attributes Point objects should have, -as strings. The return value from {\tt namedtuple} is a class object: +as strings. The return value from {\texttt namedtuple} is a class object: \index{namedtuple} \index{object!namedtuple} \index{collections} \index{module!collections} -\begin{verbatim} +\begin{lstlisting} >>> Point -\end{verbatim} +\end{lstlisting} -{\tt Point} automatically provides methods like \verb"__init__" and +{\texttt Point} automatically provides methods like \verb"__init__" and \verb"__str__" so you don't have to write them. \index{class object} \index{object!class} To create a Point object, you use the Point class as a function: -\begin{verbatim} +\begin{lstlisting} >>> p = Point(1, 2) >>> p Point(x=1, y=2) -\end{verbatim} +\end{lstlisting} The init method assigns the arguments to attributes using the names you provided. The str method prints a representation of the Point @@ -15530,21 +15715,21 @@ \section{Named tuples} You can access the elements of the named tuple by name: -\begin{verbatim} +\begin{lstlisting} >>> p.x, p.y (1, 2) -\end{verbatim} +\end{lstlisting} But you can also treat a named tuple as a tuple: -\begin{verbatim} +\begin{lstlisting} >>> p[0], p[1] (1, 2) >>> x, y = p >>> x, y (1, 2) -\end{verbatim} +\end{lstlisting} Named tuples provide a quick way to define simple classes. The drawback is that simple classes don't always stay simple. @@ -15553,10 +15738,10 @@ \section{Named tuples} the named tuple: \index{inheritance} -\begin{verbatim} +\begin{lstlisting} class Pointier(Point): # add more methods here -\end{verbatim} +\end{lstlisting} Or you could switch to a conventional class definition. @@ -15567,67 +15752,67 @@ \section{Gathering keyword args} gathers its arguments into a tuple: \index{gather} -\begin{verbatim} +\begin{lstlisting} def printall(*args): print(args) -\end{verbatim} +\end{lstlisting} % You can call this function with any number of positional arguments (that is, arguments that don't have keywords): \index{positional argument} \index{argument!positional} -\begin{verbatim} +\begin{lstlisting} >>> printall(1, 2.0, '3') (1, 2.0, '3') -\end{verbatim} +\end{lstlisting} % -But the {\tt *} operator doesn't gather keyword arguments: +But the {\texttt *} operator doesn't gather keyword arguments: \index{keyword argument} \index{argument!keyword} -\begin{verbatim} +\begin{lstlisting} >>> printall(1, 2.0, third='3') TypeError: printall() got an unexpected keyword argument 'third' -\end{verbatim} +\end{lstlisting} % -To gather keyword arguments, you can use the {\tt **} operator: +To gather keyword arguments, you can use the {\texttt **} operator: -\begin{verbatim} +\begin{lstlisting} def printall(*args, **kwargs): print(args, kwargs) -\end{verbatim} +\end{lstlisting} % You can call the keyword gathering parameter anything you want, but -{\tt kwargs} is a common choice. The result is a dictionary that maps +{\texttt kwargs} is a common choice. The result is a dictionary that maps keywords to values: -\begin{verbatim} +\begin{lstlisting} >>> printall(1, 2.0, third='3') (1, 2.0) {'third': '3'} -\end{verbatim} +\end{lstlisting} % If you have a dictionary of keywords and values, you can use the -scatter operator, {\tt **} to call a function: +scatter operator, {\texttt **} to call a function: \index{scatter} -\begin{verbatim} +\begin{lstlisting} >>> d = dict(x=1, y=2) >>> Point(**d) Point(x=1, y=2) -\end{verbatim} +\end{lstlisting} % -Without the scatter operator, the function would treat {\tt d} as -a single positional argument, so it would assign {\tt d} to -{\tt x} and complain because there's nothing to assign to {\tt y}: +Without the scatter operator, the function would treat {\texttt d} as +a single positional argument, so it would assign {\texttt d} to +{\texttt x} and complain because there's nothing to assign to {\texttt y}: -\begin{verbatim} +\begin{lstlisting} >>> d = dict(x=1, y=2) >>> Point(d) Traceback (most recent call last): File "", line 1, in TypeError: __new__() missing 1 required positional argument: 'y' -\end{verbatim} +\end{lstlisting} % When you are working with functions that have a large number of parameters, it is often useful to create and pass around dictionaries @@ -15643,11 +15828,11 @@ \section{Glossary} \index{conditional expression} \index{expression!conditional} -\item[list comprehension:] An expression with a {\tt for} loop in square +\item[list comprehension:] An expression with a {\texttt for} loop in square brackets that yields a new list. \index{list comprehension} -\item[generator expression:] An expression with a {\tt for} loop in parentheses +\item[generator expression:] An expression with a {\texttt for} loop in parentheses that yields a generator object. \index{generator expression} \index{expression!generator} @@ -15667,11 +15852,12 @@ \section{Glossary} \section{Exercises} \begin{exercise} +\normalfont The following is a function computes the binomial coefficient recursively. -\begin{verbatim} +\begin{lstlisting} def binomial_coeff(n, k): """Compute the binomial coefficient "n choose k". @@ -15687,7 +15873,7 @@ \section{Exercises} res = binomial_coeff(n-1, k) + binomial_coeff(n-1, k-1) return res -\end{verbatim} +\end{lstlisting} Rewrite the body of the function using nested conditional expressions. @@ -15714,8 +15900,8 @@ \chapter{Debugging} \item Syntax errors are discovered by the interpreter when it is translating the source code into byte code. They indicate that there is something wrong with the structure of the program. - Example: Omitting the colon at the end of a {\tt def} statement - generates the somewhat redundant message {\tt SyntaxError: invalid + Example: Omitting the colon at the end of a {\texttt def} statement + generates the somewhat redundant message {\texttt SyntaxError: invalid syntax}. \index{syntax error} \index{error!syntax} @@ -15749,8 +15935,8 @@ \section{Syntax errors} Syntax errors are usually easy to fix once you figure out what they are. Unfortunately, the error messages are often not helpful. -The most common messages are {\tt SyntaxError: invalid syntax} and -{\tt SyntaxError: invalid token}, neither of which is very informative. +The most common messages are {\texttt SyntaxError: invalid syntax} and +{\texttt SyntaxError: invalid token}, neither of which is very informative. On the other hand, the message does tell you where in the program the problem occurred. Actually, it tells you where Python @@ -15778,8 +15964,8 @@ \section{Syntax errors} \index{keyword} \item Check that you have a colon at the end of the header of every -compound statement, including {\tt for}, {\tt while}, -{\tt if}, and {\tt def} statements. +compound statement, including {\texttt for}, {\texttt while}, +{\texttt if}, and {\texttt def} statements. \index{header} \index{colon} @@ -15790,7 +15976,7 @@ \section{Syntax errors} \item If you have multiline strings with triple quotes (single or double), make sure you have terminated the string properly. An unterminated string -may cause an {\tt invalid token} error at the end of your program, +may cause an {\texttt invalid token} error at the end of your program, or it may treat the following part of the program as a string until it comes to the next string. In the second case, it might not produce an error message at all! @@ -15802,7 +15988,7 @@ \section{Syntax errors} current statement. Generally, an error occurs almost immediately in the next line. -\item Check for the classic {\tt =} instead of {\tt ==} inside +\item Check for the classic {\texttt =} instead of {\texttt ==} inside a conditional. \index{conditional} @@ -15850,12 +16036,12 @@ \subsection{I keep making changes and it makes no difference.} \item Something in your development environment is configured incorrectly. -\item If you are writing a module and using {\tt import}, +\item If you are writing a module and using {\texttt import}, make sure you don't give your module the same name as one of the standard Python modules. -\item If you are using {\tt import} to read a module, remember -that you have to restart the interpreter or use {\tt reload} +\item If you are using {\texttt import} to read a module, remember +that you have to restart the interpreter or use {\texttt reload} to read a modified file. If you import the module again, it doesn't do anything. \index{module!reload} @@ -15901,7 +16087,7 @@ \subsection{My program hangs.} \begin{itemize} \item If there is a particular loop that you suspect is the -problem, add a {\tt print} statement immediately before the loop that says +problem, add a {\texttt print} statement immediately before the loop that says ``entering the loop'' and another immediately after that says ``exiting the loop''. @@ -15935,13 +16121,13 @@ \subsubsection{Infinite Loop} \index{loop!condition} If you think you have an infinite loop and you think you know -what loop is causing the problem, add a {\tt print} statement at +what loop is causing the problem, add a {\texttt print} statement at the end of the loop that prints the values of the variables in the condition and the value of the condition. For example: -\begin{verbatim} +\begin{lstlisting} while x > 0 and y < 0 : # do something to x # do something to y @@ -15949,12 +16135,12 @@ \subsubsection{Infinite Loop} print('x: ', x) print('y: ', y) print("condition: ", (x > 0 and y < 0)) -\end{verbatim} +\end{lstlisting} % Now when you run the program, you will see three lines of output for each time through the loop. The last time through the -loop, the condition should be {\tt False}. If the loop keeps -going, you will be able to see the values of {\tt x} and {\tt y}, +loop, the condition should be {\texttt False}. If the loop keeps +going, you will be able to see the values of {\texttt x} and {\texttt y}, and you might figure out why they are not being updated correctly. @@ -15963,7 +16149,7 @@ \subsubsection{Infinite Recursion} \index{recursion!infinite} Most of the time, infinite recursion causes the program to run -for a while and then produce a {\tt Maximum recursion depth exceeded} +for a while and then produce a {\texttt Maximum recursion depth exceeded} error. If you suspect that a function is causing an infinite @@ -15974,7 +16160,7 @@ \subsubsection{Infinite Recursion} case. If there is a base case but the program doesn't seem to be reaching -it, add a {\tt print} statement at the beginning of the function +it, add a {\texttt print} statement at the beginning of the function that prints the parameters. Now when you run the program, you will see a few lines of output every time the function is invoked, and you will see the parameter values. If the parameters are not moving @@ -15985,9 +16171,9 @@ \subsubsection{Flow of Execution} \index{flow of execution} If you are not sure how the flow of execution is moving through -your program, add {\tt print} statements to the beginning of each -function with a message like ``entering function {\tt foo}'', where -{\tt foo} is the name of the function. +your program, add {\texttt print} statements to the beginning of each +function with a message like ``entering function {\texttt foo}'', where +{\texttt foo} is the name of the function. Now when you run the program, it will print a trace of each function as it is invoked. @@ -16041,7 +16227,7 @@ \subsection{When I run the program I get an exception.} \item You are passing the wrong number of arguments to a function. For methods, look at the method definition and -check that the first parameter is {\tt self}. Then look at the +check that the first parameter is {\texttt self}. Then look at the method invocation; make sure you are invoking the method on an object with the right type and providing the other arguments correctly. @@ -16057,26 +16243,26 @@ \subsection{When I run the program I get an exception.} \item[AttributeError:] You are trying to access an attribute or method that does not exist. Check the spelling! You can use the built-in - function {\tt vars} to list the attributes that do exist. + function {\texttt vars} to list the attributes that do exist. \index{dir function} \index{function!dir} -If an AttributeError indicates that an object has {\tt NoneType}, -that means that it is {\tt None}. So the problem is not the +If an AttributeError indicates that an object has {\texttt NoneType}, +that means that it is {\texttt None}. So the problem is not the attribute name, but the object. The reason the object is none might be that you forgot to return a value from a function; if you get to the end of -a function without hitting a {\tt return} statement, it returns -{\tt None}. Another common cause is using the result from -a list method, like {\tt sort}, that returns {\tt None}. +a function without hitting a {\texttt return} statement, it returns +{\texttt None}. Another common cause is using the result from +a list method, like {\texttt sort}, that returns {\texttt None}. \index{AttributeError} \index{exception!AttributeError} \item[IndexError:] The index you are using to access a list, string, or tuple is greater than its length minus one. Immediately before the site of the error, -add a {\tt print} statement to display +add a {\texttt print} statement to display the value of the index and the length of the array. Is the array the right size? Is the index the right value? \index{IndexError} @@ -16084,24 +16270,24 @@ \subsection{When I run the program I get an exception.} \end{description} -The Python debugger ({\tt pdb}) is useful for tracking down +The Python debugger ({\texttt pdb}) is useful for tracking down exceptions because it allows you to examine the state of the program immediately before the error. You can read -about {\tt pdb} at \url{https://docs.python.org/3/library/pdb.html}. +about {\texttt pdb} at \url{https://docs.python.org/3/library/pdb.html}. \index{debugger (pdb)} \index{pdb (Python debugger)} -\subsection{I added so many {\tt print} statements I get inundated with +\subsection{I added so many {\texttt print} statements I get inundated with output.} \index{print statement} \index{statement!print} -One of the problems with using {\tt print} statements for debugging +One of the problems with using {\texttt print} statements for debugging is that you can end up buried in output. There are two ways to proceed: simplify the output or simplify the program. -To simplify the output, you can remove or comment out {\tt print} +To simplify the output, you can remove or comment out {\texttt print} statements that aren't helping, or combine them, or format the output so it is easier to understand. @@ -16146,7 +16332,7 @@ \section{Semantic errors} You will often wish that you could slow the program down to human speed, and with some debuggers you can. But the time it takes to -insert a few well-placed {\tt print} statements is often short compared to +insert a few well-placed {\texttt print} statements is often short compared to setting up the debugger, inserting and removing breakpoints, and ``stepping'' the program to where the error is occurring. @@ -16204,17 +16390,17 @@ \subsection{I've got a big hairy expression and it doesn't For example: -\begin{verbatim} +\begin{lstlisting} self.hands[i].addCard(self.hands[self.findNeighbor(i)].popCard()) -\end{verbatim} +\end{lstlisting} % This can be rewritten as: -\begin{verbatim} +\begin{lstlisting} neighbor = self.findNeighbor(i) pickedCard = self.hands[neighbor].popCard() self.hands[i].addCard(pickedCard) -\end{verbatim} +\end{lstlisting} % The explicit version is easier to read because the variable names provide additional documentation, and it is easier to debug @@ -16228,9 +16414,9 @@ \subsection{I've got a big hairy expression and it doesn't For example, if you are translating the expression $\frac{x}{2 \pi}$ into Python, you might write: -\begin{verbatim} +\begin{lstlisting} y = x / 2 * math.pi -\end{verbatim} +\end{lstlisting} % That is not correct because multiplication and division have the same precedence and are evaluated from left to right. @@ -16241,9 +16427,9 @@ \subsection{I've got a big hairy expression and it doesn't A good way to debug expressions is to add parentheses to make the order of evaluation explicit: -\begin{verbatim} +\begin{lstlisting} y = x / (2 * math.pi) -\end{verbatim} +\end{lstlisting} % Whenever you are not sure of the order of evaluation, use parentheses. Not only will the program be correct (in the sense @@ -16256,24 +16442,24 @@ \subsection{I've got a function that doesn't return what I \index{return statement} \index{statement!return} -If you have a {\tt return} statement with a complex expression, +If you have a {\texttt return} statement with a complex expression, you don't have a chance to print the result before returning. Again, you can use a temporary variable. For example, instead of: -\begin{verbatim} +\begin{lstlisting} return self.hands[i].removeMatches() -\end{verbatim} +\end{lstlisting} % you could write: -\begin{verbatim} +\begin{lstlisting} count = self.hands[i].removeMatches() return count -\end{verbatim} +\end{lstlisting} % Now you have the opportunity to display the value of -{\tt count} before returning. +{\texttt count} before returning. \subsection{I'm really, really stuck and I need help.} @@ -16325,7 +16511,7 @@ \subsection{No, I really need help.} Before you bring someone else in, make sure you are prepared. Your program should be as simple as possible, and you should be working on the smallest input -that causes the error. You should have {\tt print} statements in the +that causes the error. You should have {\texttt print} statements in the appropriate places (and the output they produce should be comprehensible). You should understand the problem well enough to describe it concisely. @@ -16358,12 +16544,12 @@ \chapter{Analysis of Algorithms} \label{algorithms} \begin{quote} -This appendix is an edited excerpt from {\it Think Complexity}, by +This appendix is an edited excerpt from {\textit Think Complexity}, by Allen B. Downey, also published by O'Reilly Media (2012). When you are done with this book, you might want to move on to that one. \end{quote} -{\bf Analysis of algorithms} is a branch of computer science that +{\textbf Analysis of algorithms} is a branch of computer science that studies the performance of algorithms, especially their run time and space requirements. See \url{http://en.wikipedia.org/wiki/Analysis_of_algorithms}. @@ -16407,7 +16593,7 @@ \chapter{Analysis of Algorithms} depend on characteristics of the hardware, so one algorithm might be faster on Machine A, another on Machine B. The general solution to this problem is to specify a -{\bf machine model} and analyze the number of steps, or +{\textbf machine model} and analyze the number of steps, or operations, an algorithm requires under a given model. \index{machine model} @@ -16416,7 +16602,7 @@ \chapter{Analysis of Algorithms} algorithms run faster if the data are already partially sorted; other algorithms run slower in this case. A common way to avoid this problem is to analyze the -{\bf worst case} scenario. It is sometimes useful to +{\textbf worst case} scenario. It is sometimes useful to analyze average case performance, but that's usually harder, and it might not be obvious what set of cases to average over. \index{worst case} @@ -16472,7 +16658,7 @@ \section{Order of growth} The fundamental reason is that for large values of $n$, any function that contains an $n^2$ term will grow faster than a function whose -leading term is $n$. The {\bf leading term} is the term with the +leading term is $n$. The {\textbf leading term} is the term with the highest exponent. \index{leading term} \index{exponent} @@ -16489,7 +16675,7 @@ \section{Order of growth} In general, we expect an algorithm with a smaller leading term to be a better algorithm for large problems, but for smaller problems, there -may be a {\bf crossover point} where another algorithm is better. The +may be a {\textbf crossover point} where another algorithm is better. The location of the crossover point depends on the details of the algorithms, the inputs, and the hardware, so it is usually ignored for purposes of algorithmic analysis. But that doesn't mean you can forget @@ -16501,16 +16687,16 @@ \section{Order of growth} algorithmic analysis, functions with the same leading term are considered equivalent, even if they have different coefficients. -An {\bf order of growth} is a set of functions whose growth +An {\textbf order of growth} is a set of functions whose growth behavior is considered equivalent. For example, $2n$, $100n$ and $n+1$ belong to the same order of growth, which is written $O(n)$ in -{\bf Big-Oh notation} and often called {\bf linear} because every function +{\textbf Big-Oh notation} and often called {\textbf linear} because every function in the set grows linearly with $n$. \index{big-oh notation} \index{linear growth} All functions with the leading term $n^2$ belong to $O(n^2)$; they are -called {\bf quadratic}. +called {\textbf quadratic}. \index{quadratic growth} The following table shows some of the orders of growth that @@ -16545,6 +16731,7 @@ \section{Order of growth} \begin{exercise} +\normalfont Read the Wikipedia page on Big-Oh notation at \url{http://en.wikipedia.org/wiki/Big_O_notation} and @@ -16602,18 +16789,18 @@ \section{Analysis of basic Python operations} of the data structure. \index{indexing} -A {\tt for} loop that traverses a sequence or dictionary is +A {\texttt for} loop that traverses a sequence or dictionary is usually linear, as long as all of the operations in the body of the loop are constant time. For example, adding up the elements of a list is linear: -\begin{verbatim} +\begin{lstlisting} total = 0 for x in t: total += x -\end{verbatim} +\end{lstlisting} -The built-in function {\tt sum} is also linear because it does +The built-in function {\texttt sum} is also linear because it does the same thing, but it tends to be faster because it is a more efficient implementation; in the language of algorithmic analysis, it has a smaller leading coefficient. @@ -16628,9 +16815,9 @@ \section{Analysis of basic Python operations} does dividing. So if the body of a loop is in $O(n^a)$ and it runs $n/k$ times, the loop is in $O(n^{a+1})$, even for large $k$. -Most string and tuple operations are linear, except indexing and {\tt - len}, which are constant time. The built-in functions {\tt min} and -{\tt max} are linear. The run-time of a slice operation is +Most string and tuple operations are linear, except indexing and {\texttt + len}, which are constant time. The built-in functions {\texttt min} and +{\texttt max} are linear. The run-time of a slice operation is proportional to the length of the output, but independent of the size of the input. \index{string methods} @@ -16643,9 +16830,9 @@ \section{Analysis of basic Python operations} All string methods are linear, but if the lengths of the strings are bounded by a constant---for example, operations on single characters---they are considered constant time. -The string method {\tt join} is linear; the run time depends on +The string method {\texttt join} is linear; the run time depends on the total length of the strings. -\index{join@{\tt join}} +\index{join@{\texttt join}} Most list methods are linear, but there are some exceptions: \index{list methods} @@ -16671,11 +16858,11 @@ \section{Analysis of basic Python operations} \begin{itemize} -\item The run time of {\tt update} is +\item The run time of {\texttt update} is proportional to the size of the dictionary passed as a parameter, not the dictionary being updated. -\item {\tt keys}, {\tt values} and {\tt items} are constant time because +\item {\texttt keys}, {\texttt values} and {\texttt items} are constant time because they return iterators. But if you loop through the iterators, the loop will be linear. \index{iterator} @@ -16688,6 +16875,7 @@ \section{Analysis of basic Python operations} \begin{exercise} +\normalfont Read the Wikipedia page on sorting algorithms at \url{http://en.wikipedia.org/wiki/Sorting_algorithm} and answer @@ -16726,7 +16914,7 @@ \section{Analysis of basic Python operations} \section{Analysis of search algorithms} -A {\bf search} is an algorithm that takes a collection and a target +A {\textbf search} is an algorithm that takes a collection and a target item and determines whether the target is in the collection, often returning the index of the target. \index{search} @@ -16737,11 +16925,11 @@ \section{Analysis of search algorithms} time is linear. \index{linear search} -The {\tt in} operator for sequences uses a linear search; so do string -methods like {\tt find} and {\tt count}. -\index{in@{\tt in} operator} +The {\texttt in} operator for sequences uses a linear search; so do string +methods like {\texttt find} and {\texttt count}. +\index{in@{\texttt in} operator} -If the elements of the sequence are in order, you can use a {\bf +If the elements of the sequence are in order, you can use a {\textbf bisection search}, which is $O(\log n)$. Bisection search is similar to the algorithm you might use to look a word up in a dictionary (a paper dictionary, not the data structure). Instead of @@ -16760,11 +16948,11 @@ \section{Analysis of search algorithms} it requires the sequence to be in order, which might require extra work. -There is another data structure, called a {\bf hashtable} that +There is another data structure, called a {\textbf hashtable} that is even faster---it can do a search in constant time---and it doesn't require the items to be sorted. Python dictionaries are implemented using hashtables, which is why most dictionary -operations, including the {\tt in} operator, are constant time. +operations, including the {\texttt in} operator, are constant time. \section{Hashtables} @@ -16784,22 +16972,22 @@ \section{Hashtables} \begin{description} -\item[{\tt add(k, v)}:] Add a new item that maps from key {\tt k} -to value {\tt v}. With a Python dictionary, {\tt d}, this operation -is written {\tt d[k] = v}. +\item[{\texttt add(k, v)}:] Add a new item that maps from key {\texttt k} +to value {\texttt v}. With a Python dictionary, {\texttt d}, this operation +is written {\texttt d[k] = v}. -\item[{\tt get(k)}:] Look up and return the value that corresponds -to key {\tt k}. With a Python dictionary, {\tt d}, this operation -is written {\tt d[k]} or {\tt d.get(k)}. +\item[{\texttt get(k)}:] Look up and return the value that corresponds +to key {\texttt k}. With a Python dictionary, {\texttt d}, this operation +is written {\texttt d[k]} or {\texttt d.get(k)}. \end{description} For now, I assume that each key only appears once. The simplest implementation of this interface uses a list of tuples, where each tuple is a key-value pair. -\index{LinearMap@{\tt LinearMap}} +\index{LinearMap@{\texttt LinearMap}} -\begin{verbatim} +\begin{lstlisting} class LinearMap: def __init__(self): @@ -16813,33 +17001,33 @@ \section{Hashtables} if key == k: return val raise KeyError -\end{verbatim} +\end{lstlisting} -{\tt add} appends a key-value tuple to the list of items, which +{\texttt add} appends a key-value tuple to the list of items, which takes constant time. -{\tt get} uses a {\tt for} loop to search the list: +{\texttt get} uses a {\texttt for} loop to search the list: if it finds the target key it returns the corresponding value; -otherwise it raises a {\tt KeyError}. -So {\tt get} is linear. -\index{KeyError@{\tt KeyError}} +otherwise it raises a {\texttt KeyError}. +So {\texttt get} is linear. +\index{KeyError@{\texttt KeyError}} -An alternative is to keep the list sorted by key. Then {\tt get} +An alternative is to keep the list sorted by key. Then {\texttt get} could use a bisection search, which is $O(\log n)$. But inserting a new item in the middle of a list is linear, so this might not be the -best option. There are other data structures that can implement {\tt - add} and {\tt get} in log time, but that's still not as good as +best option. There are other data structures that can implement {\texttt + add} and {\texttt get} in log time, but that's still not as good as constant time, so let's move on. \index{red-black tree} -One way to improve {\tt LinearMap} is to break the list of key-value +One way to improve {\texttt LinearMap} is to break the list of key-value pairs into smaller lists. Here's an implementation called -{\tt BetterMap}, which is a list of 100 LinearMaps. As we'll see -in a second, the order of growth for {\tt get} is still linear, -but {\tt BetterMap} is a step on the path toward hashtables: -\index{BetterMap@{\tt BetterMap}} +{\texttt BetterMap}, which is a list of 100 LinearMaps. As we'll see +in a second, the order of growth for {\texttt get} is still linear, +but {\texttt BetterMap} is a step on the path toward hashtables: +\index{BetterMap@{\texttt BetterMap}} -\begin{verbatim} +\begin{lstlisting} class BetterMap: def __init__(self, n=100): @@ -16858,16 +17046,16 @@ \section{Hashtables} def get(self, k): m = self.find_map(k) return m.get(k) -\end{verbatim} +\end{lstlisting} -\verb"__init__" makes a list of {\tt n} {\tt LinearMap}s. +\verb"__init__" makes a list of {\texttt n} {\texttt LinearMap}s. \verb"find_map" is used by -{\tt add} and {\tt get} +{\texttt add} and {\texttt get} to figure out which map to put the new item in, or which map to search. -\verb"find_map" uses the built-in function {\tt hash}, which takes +\verb"find_map" uses the built-in function {\texttt hash}, which takes almost any Python object and returns an integer. A limitation of this implementation is that it only works with hashable keys. Mutable types like lists and dictionaries are unhashable. @@ -16878,20 +17066,20 @@ \section{Hashtables} different values can return the same hash value. \verb"find_map" uses the modulus operator to wrap the hash values -into the range from 0 to {\tt len(self.maps)}, so the result is a legal +into the range from 0 to {\texttt len(self.maps)}, so the result is a legal index into the list. Of course, this means that many different hash values will wrap onto the same index. But if the hash function spreads things out pretty evenly (which is what hash functions are designed to do), then we expect $n/100$ items per LinearMap. -Since the run time of {\tt LinearMap.get} is proportional to the +Since the run time of {\texttt LinearMap.get} is proportional to the number of items, we expect BetterMap to be about 100 times faster than LinearMap. The order of growth is still linear, but the leading coefficient is smaller. That's nice, but still not as good as a hashtable. Here (finally) is the crucial idea that makes hashtables fast: if you -can keep the maximum length of the LinearMaps bounded, {\tt +can keep the maximum length of the LinearMaps bounded, {\texttt LinearMap.get} is constant time. All you have to do is keep track of the number of items and when the number of items per LinearMap exceeds a threshold, resize the hashtable by @@ -16901,7 +17089,7 @@ \section{Hashtables} Here is an implementation of a hashtable: \index{HashMap} -\begin{verbatim} +\begin{lstlisting} class HashMap: def __init__(self): @@ -16926,18 +17114,18 @@ \section{Hashtables} new_maps.add(k, v) self.maps = new_maps -\end{verbatim} +\end{lstlisting} -Each {\tt HashMap} contains a {\tt BetterMap}; \verb"__init__" starts -with just 2 LinearMaps and initializes {\tt num}, which keeps track of +each {\texttt HashMap} contains a {\texttt BetterMap}; \verb"__init__" starts +with just 2 LinearMaps and initializes {\texttt num}, which keeps track of the number of items. -{\tt get} just dispatches to {\tt BetterMap}. The real work happens -in {\tt add}, which checks the number of items and the size of the -{\tt BetterMap}: if they are equal, the average number of items per -LinearMap is 1, so it calls {\tt resize}. +{\texttt get} just dispatches to {\texttt BetterMap}. The real work happens +in {\texttt add}, which checks the number of items and the size of the +{\texttt BetterMap}: if they are equal, the average number of items per +LinearMap is 1, so it calls {\texttt resize}. -{\tt resize} make a new {\tt BetterMap}, twice as big as the previous +{\texttt resize} make a new {\texttt BetterMap}, twice as big as the previous one, and then ``rehashes'' the items from the old map to the new. Rehashing is necessary because changing the number of LinearMaps @@ -16948,12 +17136,12 @@ \section{Hashtables} \index{rehashing} Rehashing is linear, so -{\tt resize} is linear, which might seem bad, since I promised -that {\tt add} would be constant time. But remember that -we don't have to resize every time, so {\tt add} is usually +{\texttt resize} is linear, which might seem bad, since I promised +that {\texttt add} would be constant time. But remember that +we don't have to resize every time, so {\texttt add} is usually constant time and only occasionally linear. The total amount -of work to run {\tt add} $n$ times is proportional to $n$, -so the average time of each {\tt add} is constant time! +of work to run {\texttt add} $n$ times is proportional to $n$, +so the average time of each {\texttt add} is constant time! \index{constant time} To see how this works, think about starting with an empty @@ -16966,11 +17154,11 @@ \section{Hashtables} costs 1 unit, so the total so far is 6 units of work for 4 items. -The next {\tt add} costs 5 units, but the next three +The next {\texttt add} costs 5 units, but the next three are only one unit each, so the total is 14 units for the first 8 adds. -The next {\tt add} costs 9 units, but then we can add 7 more +The next {\texttt add} costs 9 units, but then we can add 7 more before the next resize, so the total is 30 units for the first 16 adds. @@ -16986,7 +17174,7 @@ \section{Hashtables} Figure~\ref{fig.hash} shows how this works graphically. Each block represents a unit of work. The columns show the total work for each add in order from left to right: the first two -{\tt adds} cost 1 units, the third costs 3 units, etc. +{\texttt adds} cost 1 units, the third costs 3 units, etc. \begin{figure} \centerline{\includegraphics[width=5.5in]{figs/towers.pdf}} @@ -17003,7 +17191,7 @@ \section{Hashtables} HashTable it grows geometrically; that is, we multiply the size by a constant. If you increase the size arithmetically---adding a fixed number each time---the average time -per {\tt add} is linear. +per {\texttt add} is linear. \index{geometric resizing} You can download my implementation of HashMap from diff --git a/book/latexonly b/book/latexonly index 9f6786f..41018a9 100644 --- a/book/latexonly +++ b/book/latexonly @@ -27,19 +27,19 @@ \makeatletter -\renewcommand{\section}{\@startsection - {section} {1} {0mm}% - {-3.5ex \@plus -1ex \@minus -.2ex}% - {0.7ex \@plus.2ex}% - {\normalfont\Large\bfseries}} -\renewcommand\subsection{\@startsection {subsection}{2}{0mm}% - {-3.25ex\@plus -1ex \@minus -.2ex}% - {0.3ex \@plus .2ex}% - {\normalfont\large\bfseries}} -\renewcommand\subsubsection{\@startsection {subsubsection}{3}{0mm}% - {-3.25ex\@plus -1ex \@minus -.2ex}% - {0.3ex \@plus .2ex}% - {\normalfont\normalsize\bfseries}} +% \renewcommand{\section}{\@startsection +% {section} {1} {0mm}% +% {-3.5ex \@plus -1ex \@minus -.2ex}% +% {0.7ex \@plus.2ex}% +% {\normalfont\Large\bfseries}} +% \renewcommand\subsection{\@startsection {subsection}{2}{0mm}% +% {-3.25ex\@plus -1ex \@minus -.2ex}% +% {0.3ex \@plus .2ex}% +% {\normalfont\large\bfseries}} +% \renewcommand\subsubsection{\@startsection {subsubsection}{3}{0mm}% +% {-3.25ex\@plus -1ex \@minus -.2ex}% +% {0.3ex \@plus .2ex}% +% {\normalfont\normalsize\bfseries}} % The following line adds a little extra space to the column % in which the Section numbers appear in the table of contents diff --git a/book/thinkpython2.pdf b/book/thinkpython2.pdf new file mode 100644 index 0000000..9bb7381 Binary files /dev/null and b/book/thinkpython2.pdf differ