├── .gitignore ├── Dockerfile ├── Makefile ├── README.md ├── book ├── Makefile ├── back.png ├── book_cn.tex ├── chpt_00_preface.tex ├── chpt_01_way_of_program.tex ├── chpt_02_variables.tex ├── chpt_03_functions.tex ├── chpt_04_interface_design.tex ├── chpt_05_conditionals_and_recursion.tex ├── chpt_06_fruitful_functions.tex ├── chpt_07_iteration.tex ├── chpt_08_strings.tex ├── chpt_09_word_play.tex ├── chpt_10_lists.tex ├── chpt_11_dictionaries.tex ├── chpt_12_tuples.tex ├── chpt_13_cs_data_structure_selection.tex ├── chpt_14_files.tex ├── chpt_15_classes_and_objects.tex ├── chpt_16_classes_and_functions.tex ├── chpt_17_classes_and_methods.tex ├── chpt_18_inheritance.tex ├── chpt_19_the_goodies.tex ├── chpt_20_debugging.tex ├── chpt_21_analysis_of_algorithms.tex ├── footer.html ├── header.html ├── hevea.sty ├── htmlonly ├── latexonly ├── localdef.py ├── next.png └── up.png ├── code ├── BadKangaroo.py ├── Card.py ├── Circle.py ├── GoodKangaroo.py ├── Map.py ├── Point1.py ├── Point1_soln.py ├── Point2_soln.py ├── PokerHand.py ├── PokerHandSoln.py ├── Time1.py ├── Time1_soln.py ├── Time2.py ├── Time2_soln.py ├── ackermann.py ├── ackermann_memo.py ├── anagram_db.py ├── anagram_sets.py ├── analyze_book1.py ├── analyze_book2.py ├── analyze_book3.py ├── birthday.py ├── c06d ├── cartalk1.py ├── cartalk2.py ├── cartalk3.py ├── do_four.py ├── double.py ├── draw.py ├── emma.txt ├── find_duplicates.py ├── find_duplicates_copy.py ├── flower.py ├── grid.py ├── has_duplicates.py ├── homophone.py ├── inlist.py ├── interlock.py ├── invert_dict.py ├── koch.py ├── letters.py ├── list_exercises.py ├── markov.py ├── metathesis.py ├── most_frequent.py ├── pace_calc.py ├── palindrome_soln.py ├── pi.py ├── pie.py ├── polygon.py ├── pronounce.py ├── reducible.py ├── reverse_pair.py ├── rotate.py ├── rotate_pairs.py ├── run_all_solutions.py ├── sed.py ├── spiral.py ├── structshape.py ├── typewriter.py ├── unstable_sort.py ├── walk.py ├── wordlist.py ├── words.txt └── zipf.py ├── make.bat ├── references ├── ThinkPython-CN.rst └── ThinkPython2-EN.rst └── source ├── 00-preface.rst ├── 01-the-way-of-the-program.rst ├── 02-variables-expressions-and-statements.rst ├── 03-functions.rst ├── 04-case-study-interface-design.rst ├── 05-conditionals-and-recursion.rst ├── 06-fruitful-functions.rst ├── 07-iteration.rst ├── 08-strings.rst ├── 09-case-study-word-play.rst ├── 10-lists.rst ├── 11-dictionaries.rst ├── 12-tuples.rst ├── 13-case-study-data-structure-selection.rst ├── 14-files.rst ├── 15-classes-and-objects.rst ├── 16-classes-and-functions.rst ├── 17-classes-and-methods.rst ├── 18-inheritance.rst ├── 19-the-goodies.rst ├── 20-debugging.rst ├── 21-analysis-of-algorithms.rst ├── _static └── cover.jpg ├── _templates └── epub_cover.html ├── _theme └── sphinx_rtd_theme │ ├── __init__.py │ ├── breadcrumbs.html │ ├── epub-layout.html │ ├── footer.html │ ├── layout.html │ ├── layout_old.html │ ├── search.html │ ├── searchbox.html │ ├── static │ ├── css │ │ ├── badge_only.css │ │ └── theme.css │ ├── fonts │ │ ├── Inconsolata-Bold.ttf │ │ ├── Inconsolata-Regular.ttf │ │ ├── Lato-Bold.ttf │ │ ├── Lato-Regular.ttf │ │ ├── RobotoSlab-Bold.ttf │ │ ├── RobotoSlab-Regular.ttf │ │ ├── fontawesome-webfont.eot │ │ ├── fontawesome-webfont.svg │ │ ├── fontawesome-webfont.ttf │ │ └── fontawesome-webfont.woff │ └── js │ │ ├── modernizr.min.js │ │ └── theme.js │ ├── theme.conf │ └── versions.html ├── conf.py ├── favicon.ico ├── figs ├── assign2.eps ├── assign2.fig ├── assign2.fig.bak ├── assign2.pdf ├── assign2.png ├── back.png ├── banana.eps ├── banana.fig ├── banana.pdf ├── banana.png ├── card1.eps ├── card1.fig ├── card1.pdf ├── card1.png ├── class1.eps ├── class1.fig ├── class1.pdf ├── class1.png ├── compile.eps ├── compile.fig ├── compile.pdf ├── dict1.eps ├── dict1.fig ├── dict1.pdf ├── dict1.png ├── dict2.eps ├── dict2.fig ├── dict2.pdf ├── dict2.png ├── fibonacci.eps ├── fibonacci.fig ├── fibonacci.pdf ├── fibonacci.png ├── flower.test.pdf ├── flowers.eps ├── flowers.pdf ├── flowers.png ├── interpret.eps ├── interpret.fig ├── interpret.pdf ├── koch.eps ├── koch.pdf ├── koch.png ├── list1.eps ├── list1.fig ├── list1.pdf ├── list1.png ├── list2.eps ├── list2.fig ├── list2.pdf ├── list2.png ├── list3.eps ├── list3.fig ├── list3.pdf ├── list3.png ├── liststate.eps ├── liststate.fig ├── liststate.fig.bak ├── liststate.pdf ├── liststate.png ├── listsum1.eps ├── listsum1.pdf ├── listsum2.eps ├── listsum2.pdf ├── loop.py ├── next.png ├── pies.eps ├── pies.pdf ├── pies.png ├── point.eps ├── point.fig ├── point.pdf ├── point.png ├── rectangle.eps ├── rectangle.fig ├── rectangle.pdf ├── rectangle.png ├── rectangle2.eps ├── rectangle2.fig ├── rectangle2.pdf ├── rectangle2.png ├── stack.eps ├── stack.fig ├── stack.pdf ├── stack.png ├── stack2.eps ├── stack2.fig ├── stack2.pdf ├── stack2.png ├── stack3.eps ├── stack3.fig ├── stack3.pdf ├── stack3.png ├── stack4.eps ├── stack4.fig ├── stack4.pdf ├── stack5.eps ├── stack5.fig ├── stack5.pdf ├── stack5.png ├── state.eps ├── state.fig ├── state.pdf ├── state2.eps ├── state2.fig ├── state2.pdf ├── state2.png ├── state3.eps ├── state3.fig ├── state3.pdf ├── state4.eps ├── state4.fig ├── state4.pdf ├── state4.png ├── state5.eps ├── state5.fig ├── state5.pdf ├── time.eps ├── time.fig ├── time.pdf ├── time.png ├── towers.eps ├── towers.fig ├── towers.pdf ├── towers.png ├── tuple1.eps ├── tuple1.fig ├── tuple1.pdf ├── tuple1.png └── up.png ├── foreword.rst ├── hevea.sty ├── index.rst └── latexonly.tex /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | 12 | tmp/ 13 | 14 | 15 | # tex related files 16 | *.fdb_latexmk 17 | *.fls 18 | *.idx 19 | *.ilg 20 | *.ind 21 | *.log 22 | *.out 23 | *.toc 24 | *.aux 25 | *.synctex.gz 26 | 27 | build/ 28 | 29 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.3 2 | ENV PYTHONUNBUFFERED 1 3 | RUN mkdir /doc 4 | ADD . /doc 5 | WORKDIR /doc 6 | 7 | RUN apt-get update 8 | RUN apt-get install -y texlive 9 | RUN apt-get install -y texlive-xetex 10 | RUN apt-get install -y texlive-lang-cjk 11 | RUN apt-get install -y texlive-latex-recommended 12 | RUN apt-get install -y texlive-latex-extra 13 | RUN apt-get install -y texlive-fonts-recommended 14 | RUN apt-get install -y texlive-base 15 | RUN apt-get install -y fonts-wqy-zenhei 16 | RUN apt-get remove -y \ 17 | texlive-fonts-recommended-doc \ 18 | texlive-latex-base-doc \ 19 | texlive-latex-extra-doc \ 20 | texlive-latex-recommended-doc \ 21 | texlive-pictures-doc \ 22 | texlive-pstricks-doc \ 23 | && apt-get clean 24 | 25 | RUN pip install \ 26 | sphinx \ 27 | sphinx_rtd_theme \ 28 | sphinx_bootstrap_theme \ 29 | alabaster 30 | 31 | ENTRYPOINT ["/bin/bash"] 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 《Think Python 2ed》最新版中文翻译 2 | 3 | 本书中文翻译已经完结,感谢各位贡献者的参与和大家的关注及支持。 4 | 5 | 贡献者名单如下: 6 | 7 | - [@bingjin](https://github.com/bingjin) 8 | 全书校对,完成第1-4、17-21章的翻译。 9 | - [@ipyher](https://github.com/iphyer) 10 | 完成第5、9、13、15章的翻译。 11 | - [@theJian](https://github.com/thejian) 12 | 完成第6、11章的翻译。 13 | - [@lroolle](https://github.com/lroolle) 14 | 完成第7章的翻译。 15 | - [@xpgeng](https://github.com/xpgeng) 16 | 完成第8章的翻译。 17 | - [@obserthinker](https://github.com/obserthinker) 18 | 完成第10、14章的翻译。 19 | - [@SeikaScarlet](https://github.com/SeikaScarlet) 20 | 完成第12章的翻译,独自完成全书TEX版的制作和校对。 21 | - [@cxyfreedom](https://github.com/cxyfreedom) 22 | 完成第16章的翻译。 23 | 24 | 本书目前托管在[编程派网站](https://codingpy.com/books/thinkpython2/)。 25 | 26 | 使用 Sphinx 制作的 PDF、EPUB 等电子版的分享链接为:http://pan.baidu.com/s/1eRZOrHC。 提取码: n24y。 27 | 28 | TEX 精校版 PDF 的分享链接为:https://pan.baidu.com/s/1pLiwSAn。 29 | 30 | 如果你觉得这本书对你有帮助,可以考虑扫描下面的二维码,打赏我一杯咖啡。 31 | 32 | ![赞助编程派的翻译计划](http://ww1.sinaimg.cn/mw690/006faQNTgw1f1g1gf903aj308w0ai74q.jpg) 33 | 34 | 同时也欢迎大家关注我的微信公众号:编程派(ID:codingpy),现在基本是每天定期更新,分享 Python 精彩教程和资源。 35 | 36 | *** 37 | 38 | 该中译版以知识共享协议([Creative Commons Attribution-NonCommercial 3.0 Unported License](https://creativecommons.org/licenses/by-nc/3.0/us/))发布,你可以自由分享、修改、复制该中译版,但请不要用于商业用途。 39 | 40 | 本书原著版权归[Allen Downey](http://greenteapress.com/wp/think-python-2e/)所有,原作链接如下:[https://github.com/AllenDowney/ThinkPython2](https://github.com/AllenDowney/ThinkPython2)。 41 | 42 | 该中译版在翻译过程中,参考了车万翔老师组织翻译的 Python 2 版:[https://github.com/carfly/thinkpython-cn](https://github.com/carfly/thinkpython-cn),在此表示感谢。 43 | 44 | 本书中如仍存在细微错误,会不定期更新,也希望大家指正。 45 | -------------------------------------------------------------------------------- /book/Makefile: -------------------------------------------------------------------------------- 1 | LATEX = latex 2 | 3 | DVIPS = dvips 4 | 5 | PDFFLAGS = -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress \ 6 | -dCompressPages=true -dUseFlateCompression=true \ 7 | -dEmbedAllFonts=true -dSubsetFonts=true -dMaxSubsetPct=100 8 | 9 | 10 | %.dvi: %.tex 11 | $(LATEX) $< 12 | 13 | %.ps: %.dvi 14 | $(DVIPS) -o $@ $< 15 | 16 | %.pdf: %.ps 17 | ps2pdf $(PDFFLAGS) $< 18 | 19 | all: book.tex 20 | makeindex book.idx 21 | pdflatex book 22 | mv book.pdf thinkpython2.pdf 23 | evince thinkpython2.pdf 24 | 25 | hevea: book.tex header.html footer.html 26 | # replace the pdfs with eps 27 | sed s/.pdf/.eps/g book.tex > thinkpython2.tex 28 | latex thinkpython2 29 | rm -rf html 30 | mkdir html 31 | hevea -fix -O -e latexonly htmlonly thinkpython2 32 | # the following greps are a kludge to prevent imagen from seeing 33 | # the definitions in latexonly, and to avoid headers on the images 34 | grep -v latexonly thinkpython2.image.tex > a; mv a thinkpython2.image.tex 35 | grep -v fancyhdr thinkpython2.image.tex > a; mv a thinkpython2.image.tex 36 | imagen -png thinkpython2 37 | hacha thinkpython2.html 38 | cp up.png next.png back.png html 39 | mv index.html thinkpython2.css thinkpython2*.html thinkpython2*.png *motif.gif html 40 | 41 | DEST = /home/downey/public_html/greent/thinkpython2 42 | 43 | epub: 44 | cd html; ebook-convert index.html thinkpython2.epub 45 | 46 | distrib: 47 | rm -rf dist 48 | mkdir dist dist/tex dist/tex/figs 49 | rsync -a thinkpython2.pdf html dist 50 | rsync -a Makefile book.tex latexonly htmlonly dist/tex 51 | rsync -a figs/*.fig figs/*.pdf dist/tex/figs 52 | cd dist; zip -r thinkpython2.tex.zip tex 53 | cd dist; zip -r thinkpython2.html.zip html 54 | rsync -a dist/* $(DEST) 55 | chmod -R o+r $(DEST)/* 56 | cd $(DEST)/..; sh back 57 | 58 | # UPDATE THE PATHS BELOW BEFORE RUNNING PLASTEX 59 | 60 | plastex: 61 | # Before running plastex, we need the current directory in PYTHONPATH 62 | # export PYTHONPATH=$PYTHONPATH:. 63 | python Filist.py book.tex > book.plastex 64 | rm -rf /home/downey/thinkpython2/trunk/book 65 | plastex --renderer=DocBook --theme=book --image-resolution=300 --filename=book.xml book.plastex 66 | rm -rf /home/downey/thinkpython2/trunk/book/.svn 67 | 68 | plastest: 69 | # Before running plastex, we need the current directory in PYTHONPATH 70 | # export PYTHONPATH=$PYTHONPATH:. 71 | python Filist.py test.tex > test.plastex 72 | rm -rf /home/downey/thinkpython2/trunk/test 73 | plastex --renderer=DocBook --theme=test --filename=test.xml test.plastex 74 | rm -rf /home/downey/thinkpython2/trunk/test/.svn 75 | 76 | xxe: 77 | xmlcopyeditor ~/ThinkDSP/book/book/book.xml & 78 | 79 | lint: 80 | xmllint -noout book/book.xml 81 | 82 | OREILLY = atlas 83 | 84 | oreilly: 85 | rsync -a book.tex $(OREILLY) 86 | rsync -a book/ $(OREILLY) 87 | rsync -a figs/* $(OREILLY)/figs 88 | cd $(OREILLY); git add . 89 | cd $(OREILLY); git commit -m "Automated check in." 90 | cd $(OREILLY); git push 91 | 92 | clean: 93 | rm -f *~ *.aux *.log *.dvi *.idx *.ilg *.ind *.toc 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /book/back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/book/back.png -------------------------------------------------------------------------------- /book/footer.html: -------------------------------------------------------------------------------- 1 |

2 | Buy this book at Amazon.com 3 | 4 | 5 | 6 | 7 | 8 |

Contribute

9 | If you would like to make a contribution to support my books, 10 | you can use the button below. Thank you! 11 | 12 |
13 | 14 | 15 | 16 | 23 |
Pay what you want:
24 | 25 | 26 | 27 |
28 | 29 |

30 |

Are you using one of our books in a class?

We'd like to know 31 | about it. Please consider filling out this short survey. 32 | 33 |

34 |
35 | 36 |

37 | 38 | 39 |

40 | 41 | 42 |

43 | 44 | 45 |

46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /book/header.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 |
6 | 9 | 10 |

11 | Buy this book at Amazon.com 12 | 13 | -------------------------------------------------------------------------------- /book/hevea.sty: -------------------------------------------------------------------------------- 1 | % hevea : hevea.sty 2 | % This is a very basic style file for latex document to be processed 3 | % with hevea. It contains definitions of LaTeX environment which are 4 | % processed in a special way by the translator. 5 | % Mostly : 6 | % - latexonly, not processed by hevea, processed by latex. 7 | % - htmlonly , the reverse. 8 | % - rawhtml, to include raw HTML in hevea output. 9 | % - toimage, to send text to the image file. 10 | % The package also provides hevea logos, html related commands (ahref 11 | % etc.), void cutting and image commands. 12 | \NeedsTeXFormat{LaTeX2e} 13 | \ProvidesPackage{hevea}[2002/01/11] 14 | \RequirePackage{comment} 15 | \newif\ifhevea\heveafalse 16 | \@ifundefined{ifimagen}{\newif\ifimagen\imagenfalse} 17 | \makeatletter% 18 | \newcommand{\heveasmup}[2]{% 19 | \raise #1\hbox{$\m@th$% 20 | \csname S@\f@size\endcsname 21 | \fontsize\sf@size 0% 22 | \math@fontsfalse\selectfont 23 | #2% 24 | }}% 25 | \DeclareRobustCommand{\hevea}{H\kern-.15em\heveasmup{.2ex}{E}\kern-.15emV\kern-.15em\heveasmup{.2ex}{E}\kern-.15emA}% 26 | \DeclareRobustCommand{\hacha}{H\kern-.15em\heveasmup{.2ex}{A}\kern-.15emC\kern-.1em\heveasmup{.2ex}{H}\kern-.15emA}% 27 | \DeclareRobustCommand{\html}{\protect\heveasmup{0.ex}{HTML}} 28 | %%%%%%%%% Hyperlinks hevea style 29 | \newcommand{\ahref}[2]{{#2}} 30 | \newcommand{\ahrefloc}[2]{{#2}} 31 | \newcommand{\aname}[2]{{#2}} 32 | \newcommand{\ahrefurl}[1]{\texttt{#1}} 33 | \newcommand{\footahref}[2]{#2\footnote{\texttt{#1}}} 34 | \newcommand{\mailto}[1]{\texttt{#1}} 35 | \newcommand{\imgsrc}[2][]{} 36 | \newcommand{\home}[1]{\protect\raisebox{-.75ex}{\char126}#1} 37 | \AtBeginDocument 38 | {\@ifundefined{url} 39 | {%url package is not loaded 40 | \let\url\ahref\let\oneurl\ahrefurl\let\footurl\footahref} 41 | {}} 42 | %% Void cutting instructions 43 | \newcounter{cuttingdepth} 44 | \newcommand{\tocnumber}{} 45 | \newcommand{\notocnumber}{} 46 | \newcommand{\cuttingunit}{} 47 | \newcommand{\cutdef}[2][]{} 48 | \newcommand{\cuthere}[2]{} 49 | \newcommand{\cutend}{} 50 | \newcommand{\htmlhead}[1]{} 51 | \newcommand{\htmlfoot}[1]{} 52 | \newcommand{\htmlprefix}[1]{} 53 | \newenvironment{cutflow}[1]{}{} 54 | \newcommand{\cutname}[1]{} 55 | \newcommand{\toplinks}[3]{} 56 | \newcommand{\setlinkstext}[3]{} 57 | \newcommand{\flushdef}[1]{} 58 | \newcommand{\footnoteflush}[1]{} 59 | %%%% Html only 60 | \excludecomment{rawhtml} 61 | \newcommand{\rawhtmlinput}[1]{} 62 | \excludecomment{htmlonly} 63 | %%%% Latex only 64 | \newenvironment{latexonly}{}{} 65 | \newenvironment{verblatex}{}{} 66 | %%%% Image file stuff 67 | \def\toimage{\endgroup} 68 | \def\endtoimage{\begingroup\def\@currenvir{toimage}} 69 | \def\verbimage{\endgroup} 70 | \def\endverbimage{\begingroup\def\@currenvir{verbimage}} 71 | \newcommand{\imageflush}[1][]{} 72 | %%% Bgcolor definition 73 | \newsavebox{\@bgcolorbin} 74 | \newenvironment{bgcolor}[2][] 75 | {\newcommand{\@mycolor}{#2}\begin{lrbox}{\@bgcolorbin}\vbox\bgroup} 76 | {\egroup\end{lrbox}% 77 | \begin{flushleft}% 78 | \colorbox{\@mycolor}{\usebox{\@bgcolorbin}}% 79 | \end{flushleft}} 80 | %%% Style sheets macros, defined as no-ops 81 | \newcommand{\newstyle}[2]{} 82 | \newcommand{\addstyle}[1]{} 83 | \newcommand{\setenvclass}[2]{} 84 | \newcommand{\getenvclass}[1]{} 85 | \newcommand{\loadcssfile}[1]{} 86 | \newenvironment{divstyle}[1]{}{} 87 | \newenvironment{cellstyle}[2]{}{} 88 | \newif\ifexternalcss 89 | %%% Postlude 90 | \makeatother 91 | -------------------------------------------------------------------------------- /book/htmlonly: -------------------------------------------------------------------------------- 1 | % put commands here that should be used for the HTML 2 | % version of the book but not Postscript or PDF 3 | 4 | \newcommand{\beforefig}{} 5 | \newcommand{\afterfig}{} 6 | 7 | \newcommand{\beforeverb}{\blue \large} 8 | \newcommand{\afterverb}{\black \normalsize} 9 | 10 | \newcommand{\adjustpage}[1]{} 11 | 12 | \newcommand{\clearemptydoublepage}{} 13 | \newcommand{\blankpage}{} 14 | 15 | \newcommand{\spacing}{} 16 | \newcommand{\endspacing}{} 17 | 18 | \newcommand{\frontmatter}{} 19 | \newcommand{\mainmatter}{} 20 | 21 | \newcommand{\theoremstyle}[1]{} 22 | \newcommand{\newtheoremstyle}[1]{} 23 | 24 | \newcommand{\vfill}{} 25 | 26 | \htmlhead{\rawhtmlinput{header.html}} 27 | 28 | \htmlfoot{\rawhtmlinput{footer.html}} -------------------------------------------------------------------------------- /book/latexonly: -------------------------------------------------------------------------------- 1 | \sloppy 2 | %\setlength{\topmargin}{-0.375in} 3 | %\setlength{\oddsidemargin}{0.0in} 4 | %\setlength{\evensidemargin}{0.0in} 5 | 6 | % Uncomment these to center on 8.5 x 11 7 | %\setlength{\topmargin}{0.625in} 8 | %\setlength{\oddsidemargin}{0.875in} 9 | %\setlength{\evensidemargin}{0.875in} 10 | 11 | %\setlength{\textheight}{7.2in} 12 | 13 | \setlength{\headsep}{3ex} 14 | \setlength{\parindent}{0.0in} 15 | \setlength{\parskip}{1.7ex plus 0.5ex minus 0.5ex} 16 | \renewcommand{\baselinestretch}{1.02} 17 | 18 | % see LaTeX Companion page 62 19 | \setlength{\topsep}{-0.0\parskip} 20 | \setlength{\partopsep}{-0.5\parskip} 21 | \setlength{\itemindent}{0.0in} 22 | \setlength{\listparindent}{0.0in} 23 | 24 | % see LaTeX Companion page 26 25 | % these are copied from /usr/local/teTeX/share/texmf/tex/latex/base/book.cls 26 | % all I changed is afterskip 27 | 28 | \makeatletter 29 | 30 | \renewcommand{\section}{\@startsection 31 | {section} {1} {0mm}% 32 | {-3.5ex \@plus -1ex \@minus -.2ex}% 33 | {0.7ex \@plus.2ex}% 34 | {\normalfont\Large\bfseries}} 35 | \renewcommand\subsection{\@startsection {subsection}{2}{0mm}% 36 | {-3.25ex\@plus -1ex \@minus -.2ex}% 37 | {0.3ex \@plus .2ex}% 38 | {\normalfont\large\bfseries}} 39 | \renewcommand\subsubsection{\@startsection {subsubsection}{3}{0mm}% 40 | {-3.25ex\@plus -1ex \@minus -.2ex}% 41 | {0.3ex \@plus .2ex}% 42 | {\normalfont\normalsize\bfseries}} 43 | 44 | % The following line adds a little extra space to the column 45 | % in which the Section numbers appear in the table of contents 46 | \renewcommand{\l@section}{\@dottedtocline{1}{1.5em}{3.0em}} 47 | \setcounter{tocdepth}{1} 48 | 49 | \makeatother 50 | 51 | \newcommand{\beforefig}{\vspace{1.3\parskip}} 52 | \newcommand{\afterfig}{\vspace{-0.2\parskip}} 53 | 54 | \newcommand{\beforeverb}{\vspace{0.6\parskip}} 55 | \newcommand{\afterverb}{\vspace{0.6\parskip}} 56 | 57 | \newcommand{\adjustpage}[1]{\enlargethispage{#1\baselineskip}} 58 | 59 | 60 | % Note: the following command seems to cause problems for Acroreader 61 | % on Windows, so for now I am overriding it. 62 | %\newcommand{\clearemptydoublepage}{ 63 | % \newpage{\pagestyle{empty}\cleardoublepage}} 64 | \newcommand{\clearemptydoublepage}{\cleardoublepage} 65 | 66 | %\newcommand{\blankpage}{\pagestyle{empty}\vspace*{1in}\newpage} 67 | \newcommand{\blankpage}{\vspace*{1in}\newpage} 68 | 69 | % HEADERS 70 | 71 | \renewcommand{\chaptermark}[1]{\markboth{#1}{}} 72 | \renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}{}} 73 | 74 | \lhead[\fancyplain{}{\bfseries\thepage}]% 75 | {\fancyplain{}{\bfseries\rightmark}} 76 | \rhead[\fancyplain{}{\bfseries\leftmark}]% 77 | {\fancyplain{}{\bfseries\thepage}} 78 | \cfoot{} 79 | 80 | \pagestyle{fancyplain} 81 | 82 | 83 | % turn off the rule under the header 84 | %\setlength{\headrulewidth}{0pt} 85 | 86 | % the following is a brute-force way to prevent the headers 87 | % from getting transformed into all-caps 88 | \renewcommand\MakeUppercase{} 89 | 90 | % Exercise environment 91 | \newtheoremstyle{myex}% name 92 | {9pt}% Space above 93 | {9pt}% Space below 94 | {}% Body font 95 | {}% Indent amount (empty = no indent, \parindent = para indent) 96 | {\bfseries}% Thm head font 97 | {}% Punctuation after thm head 98 | {0.5em}% Space after thm head: " " = normal interword space; 99 | % \newline = linebreak 100 | {}% Thm head spec (can be left empty, meaning `normal') 101 | 102 | \theoremstyle{myex} 103 | -------------------------------------------------------------------------------- /book/localdef.py: -------------------------------------------------------------------------------- 1 | import plasTeX.Base as Base 2 | 3 | def idgen(): 4 | """ Generate a unique ID """ 5 | i = 1 6 | while 1: 7 | yield 'a%.10d' % i 8 | i += 1 9 | 10 | idgen = idgen() 11 | 12 | class Eqn(Base.Command): 13 | args = 'self' 14 | 15 | class Anchor(Base.Command): 16 | args = 'label:str' 17 | def invoke(self, tex): 18 | Base.Command.invoke(self, tex) 19 | self.ownerDocument.context.label(self.attributes['label'], self) 20 | 21 | class exercise(Base.Environment): 22 | counter = 'exercise' 23 | 24 | class index(Base.Command): 25 | args = 'termstring' 26 | 27 | def setEntry(self, s, seetype=0): 28 | # TYPE_NORMAL = 0 29 | # TYPE_SEE = 1 30 | # TYPE_SEEALSO = 2 31 | if type(s) != type(''): 32 | s = s.textContent 33 | if s.count('!'): 34 | priterm, secterm = s.split('!') 35 | if priterm.count('@'): 36 | prisort, primary = priterm.split('@') 37 | else: 38 | prisort, primary = None, priterm 39 | if secterm.count('@'): 40 | secsort, secondary = secterm.split('@') 41 | else: 42 | secsort, secondary = None, secterm 43 | elif s.count('@'): 44 | prisort, primary = s.split('@') 45 | secsort, secondary = None, None 46 | else: 47 | prisort, primary = None, s 48 | secsort, secondary = None, None 49 | 50 | # if secondary: 51 | # self.ownerDocument.userdata.setdefault('index', []).append(\ 52 | # Base.IndexEntry([primary, secondary], self, [prisort, secsort], None, type=seetype)) 53 | # else: 54 | # self.ownerDocument.userdata.setdefault('index', []).append(\ 55 | # Base.IndexEntry([primary], self, [prisort], None, type=seetype)) 56 | return prisort, primary, secsort, secondary 57 | 58 | def invoke(self, tex): 59 | Base.Command.invoke(self, tex) 60 | self.ownerDocument.context.label(idgen.next(), self) 61 | p0,p1,s0,s1 = self.setEntry(self.attributes['termstring']) 62 | if p0: 63 | self.prisort = '%s' % p0 64 | if p1: 65 | self.primary = '%s' % p1 66 | if s0: 67 | self.secsort = '%s' % s0 68 | if s1: 69 | self.secondary = '%s' % s1 70 | 71 | class scriptN(Base.Command): 72 | unicode = u'\U0001D4A9' 73 | 74 | class uxbar(Base.Command): pass 75 | class uybar(Base.Command): pass 76 | class unhat(Base.Command): pass 77 | class ule(Base.Command): pass 78 | class minus(Base.Command): pass 79 | class lowast(Base.Command): pass 80 | class Erdos(Base.Command): pass 81 | 82 | -------------------------------------------------------------------------------- /book/next.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/book/next.png -------------------------------------------------------------------------------- /book/up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/book/up.png -------------------------------------------------------------------------------- /code/BadKangaroo.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | """ 15 | 16 | WARNING: this program contains a NASTY bug. I put 17 | it there on purpose as a debugging exercise, but 18 | you DO NOT want to emulate this example! 19 | 20 | """ 21 | 22 | class Kangaroo: 23 | """A Kangaroo is a marsupial.""" 24 | 25 | def __init__(self, name, contents=[]): 26 | """Initialize the pouch contents. 27 | 28 | name: string 29 | contents: initial pouch contents. 30 | """ 31 | self.name = name 32 | self.pouch_contents = contents 33 | 34 | def __str__(self): 35 | """Return a string representaion of this Kangaroo. 36 | """ 37 | t = [ self.name + ' has pouch contents:' ] 38 | for obj in self.pouch_contents: 39 | s = ' ' + object.__str__(obj) 40 | t.append(s) 41 | return '\n'.join(t) 42 | 43 | def put_in_pouch(self, item): 44 | """Adds a new item to the pouch contents. 45 | 46 | item: object to be added 47 | """ 48 | self.pouch_contents.append(item) 49 | 50 | 51 | kanga = Kangaroo('Kanga') 52 | roo = Kangaroo('Roo') 53 | kanga.put_in_pouch('wallet') 54 | kanga.put_in_pouch('car keys') 55 | kanga.put_in_pouch(roo) 56 | 57 | print(kanga) 58 | 59 | # If you run this program as is, it seems to work. 60 | # To see the problem, trying printing roo. 61 | 62 | # Hint: to find the problem try running pylint. 63 | -------------------------------------------------------------------------------- /code/Circle.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import copy 15 | 16 | from Point1 import Point, Rectangle, print_point 17 | from Point1_soln import distance_between_points 18 | 19 | 20 | class Circle: 21 | """Represents a circle. 22 | 23 | Attributes: center, radius 24 | """ 25 | 26 | 27 | def point_in_circle(point, circle): 28 | """Checks whether a point lies inside a circle (or on the boundary). 29 | 30 | point: Point object 31 | circle: Circle object 32 | """ 33 | d = distance_between_points(point, circle.center) 34 | print(d) 35 | return d <= circle.radius 36 | 37 | 38 | def rect_in_circle(rect, circle): 39 | """Checks whether the corners of a rect fall in/on a circle. 40 | 41 | rect: Rectangle object 42 | circle: Circle object 43 | """ 44 | p = copy.copy(rect.corner) 45 | print_point(p) 46 | if not point_in_circle(p, circle): 47 | return False 48 | 49 | p.x += rect.width 50 | print_point(p) 51 | if not point_in_circle(p, circle): 52 | return False 53 | 54 | p.y -= rect.height 55 | print_point(p) 56 | if not point_in_circle(p, circle): 57 | return False 58 | 59 | p.x -= rect.width 60 | print_point(p) 61 | if not point_in_circle(p, circle): 62 | return False 63 | 64 | return True 65 | 66 | 67 | def rect_circle_overlap(rect, circle): 68 | """Checks whether any corners of a rect fall in/on a circle. 69 | 70 | rect: Rectangle object 71 | circle: Circle object 72 | """ 73 | p = copy.copy(rect.corner) 74 | print_point(p) 75 | if point_in_circle(p, circle): 76 | return True 77 | 78 | p.x += rect.width 79 | print_point(p) 80 | if point_in_circle(p, circle): 81 | return True 82 | 83 | p.y -= rect.height 84 | print_point(p) 85 | if point_in_circle(p, circle): 86 | return True 87 | 88 | p.x -= rect.width 89 | print_point(p) 90 | if point_in_circle(p, circle): 91 | return True 92 | 93 | return False 94 | 95 | 96 | def main(): 97 | box = Rectangle() 98 | box.width = 100.0 99 | box.height = 200.0 100 | box.corner = Point() 101 | box.corner.x = 50.0 102 | box.corner.y = 50.0 103 | 104 | print(box.corner.x) 105 | print(box.corner.y) 106 | 107 | circle = Circle 108 | circle.center = Point() 109 | circle.center.x = 150.0 110 | circle.center.y = 100.0 111 | circle.radius = 75.0 112 | 113 | print(circle.center.x) 114 | print(circle.center.y) 115 | print(circle.radius) 116 | 117 | print(point_in_circle(box.corner, circle)) 118 | print(rect_in_circle(box, circle)) 119 | print(rect_circle_overlap(box, circle)) 120 | 121 | 122 | if __name__ == '__main__': 123 | main() 124 | 125 | -------------------------------------------------------------------------------- /code/GoodKangaroo.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | """ 15 | 16 | WARNING: this program contains a NASTY bug. I put 17 | it there on purpose as a debugging exercise, but 18 | you DO NOT want to emulate this example! 19 | 20 | """ 21 | 22 | class Kangaroo: 23 | """A Kangaroo is a marsupial.""" 24 | 25 | def __init__(self, name, contents=[]): 26 | """Initialize the pouch contents. 27 | 28 | name: string 29 | contents: initial pouch contents. 30 | """ 31 | # The problem is the default value for contents. 32 | # Default values get evaluated ONCE, when the function 33 | # is defined; they don't get evaluated again when the 34 | # function is called. 35 | 36 | # In this case that means that when __init__ is defined, 37 | # [] gets evaluated and contents gets a reference to 38 | # an empty list. 39 | 40 | # After that, every Kangaroo that gets the default 41 | # value gets a reference to THE SAME list. If any 42 | # Kangaroo modifies this shared list, they all see 43 | # the change. 44 | 45 | # The next version of __init__ shows an idiomatic way 46 | # to avoid this problem. 47 | self.name = name 48 | self.pouch_contents = contents 49 | 50 | def __init__(self, name, contents=None): 51 | """Initialize the pouch contents. 52 | 53 | name: string 54 | contents: initial pouch contents. 55 | """ 56 | # In this version, the default value is None. When 57 | # __init__ runs, it checks the value of contents and, 58 | # if necessary, creates a new empty list. That way, 59 | # every Kangaroo that gets the default value gets a 60 | # reference to a different list. 61 | 62 | # As a general rule, you should avoid using a mutable 63 | # object as a default value, unless you really know 64 | # what you are doing. 65 | self.name = name 66 | if contents == None: 67 | contents = [] 68 | self.pouch_contents = contents 69 | 70 | def __str__(self): 71 | """Return a string representaion of this Kangaroo. 72 | """ 73 | t = [ self.name + ' has pouch contents:' ] 74 | for obj in self.pouch_contents: 75 | s = ' ' + object.__str__(obj) 76 | t.append(s) 77 | return '\n'.join(t) 78 | 79 | def put_in_pouch(self, item): 80 | """Adds a new item to the pouch contents. 81 | 82 | item: object to be added 83 | """ 84 | self.pouch_contents.append(item) 85 | 86 | 87 | kanga = Kangaroo('Kanga') 88 | roo = Kangaroo('Roo') 89 | kanga.put_in_pouch('wallet') 90 | kanga.put_in_pouch('car keys') 91 | kanga.put_in_pouch(roo) 92 | 93 | print(kanga) 94 | print(roo) 95 | 96 | # If you run this program as is, it seems to work. 97 | # To see the problem, trying printing roo. 98 | -------------------------------------------------------------------------------- /code/Map.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | class LinearMap: 16 | """A simple implementation of a map using a list of tuples 17 | where each tuple is a key-value pair.""" 18 | 19 | def __init__(self): 20 | self.items = [] 21 | 22 | def add(self, k, v): 23 | """Adds a new item that maps from key (k) to value (v). 24 | Assumes that they keys are unique.""" 25 | self.items.append((k, v)) 26 | 27 | def get(self, k): 28 | """Looks up the key (k) and returns the corresponding value, 29 | or raises KeyError if the key is not found.""" 30 | for key, val in self.items: 31 | if key == k: 32 | return val 33 | raise KeyError 34 | 35 | 36 | class BetterMap: 37 | """A faster implementation of a map using a list of LinearMaps 38 | and the built-in function hash() to determine which LinearMap 39 | to put each key into.""" 40 | 41 | def __init__(self, n=100): 42 | """Appends (n) LinearMaps onto (self).""" 43 | self.maps = [] 44 | for i in range(n): 45 | self.maps.append(LinearMap()) 46 | 47 | def find_map(self, k): 48 | """Finds the right LinearMap for key (k).""" 49 | index = hash(k) % len(self.maps) 50 | return self.maps[index] 51 | 52 | def add(self, k, v): 53 | """Adds a new item to the appropriate LinearMap for key (k).""" 54 | m = self.find_map(k) 55 | m.add(k, v) 56 | 57 | def get(self, k): 58 | """Finds the right LinearMap for key (k) and looks up (k) in it.""" 59 | m = self.find_map(k) 60 | return m.get(k) 61 | 62 | 63 | class HashMap: 64 | """An implementation of a hashtable using a BetterMap 65 | that grows so that the number of items never exceeds the number 66 | of LinearMaps. 67 | 68 | The amortized cost of add should be O(1) provided that the 69 | implementation of sum in resize is linear.""" 70 | 71 | def __init__(self): 72 | """Starts with 2 LinearMaps and 0 items.""" 73 | self.maps = BetterMap(2) 74 | self.num = 0 75 | 76 | def get(self, k): 77 | """Looks up the key (k) and returns the corresponding value, 78 | or raises KeyError if the key is not found.""" 79 | return self.maps.get(k) 80 | 81 | def add(self, k, v): 82 | """Resize the map if necessary and adds the new item.""" 83 | if self.num == len(self.maps.maps): 84 | self.resize() 85 | 86 | self.maps.add(k, v) 87 | self.num += 1 88 | 89 | def resize(self): 90 | """Makes a new map, twice as big, and rehashes the items.""" 91 | new_map = BetterMap(self.num * 2) 92 | 93 | for m in self.maps.maps: 94 | for k, v in m.items: 95 | new_map.add(k, v) 96 | 97 | self.maps = new_map 98 | 99 | 100 | def main(): 101 | import string 102 | 103 | m = HashMap() 104 | s = string.ascii_lowercase 105 | 106 | for k, v in enumerate(s): 107 | m.add(k, v) 108 | 109 | for k in range(len(s)): 110 | print(k, m.get(k)) 111 | 112 | 113 | if __name__ == '__main__': 114 | main() 115 | -------------------------------------------------------------------------------- /code/Point1.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | class Point: 16 | """Represents a point in 2-D space. 17 | 18 | attributes: x, y 19 | """ 20 | 21 | 22 | def print_point(p): 23 | """Print a Point object in human-readable format.""" 24 | print('(%g, %g)' % (p.x, p.y)) 25 | 26 | 27 | class Rectangle: 28 | """Represents a rectangle. 29 | 30 | attributes: width, height, corner. 31 | """ 32 | 33 | 34 | def find_center(rect): 35 | """Returns a Point at the center of a Rectangle. 36 | 37 | rect: Rectangle 38 | 39 | returns: new Point 40 | """ 41 | p = Point() 42 | p.x = rect.corner.x + rect.width/2.0 43 | p.y = rect.corner.y + rect.height/2.0 44 | return p 45 | 46 | 47 | def grow_rectangle(rect, dwidth, dheight): 48 | """Modifies the Rectangle by adding to its width and height. 49 | 50 | rect: Rectangle object. 51 | dwidth: change in width (can be negative). 52 | dheight: change in height (can be negative). 53 | """ 54 | rect.width += dwidth 55 | rect.height += dheight 56 | 57 | 58 | def main(): 59 | blank = Point() 60 | blank.x = 3 61 | blank.y = 4 62 | print('blank', end=' ') 63 | print_point(blank) 64 | 65 | box = Rectangle() 66 | box.width = 100.0 67 | box.height = 200.0 68 | box.corner = Point() 69 | box.corner.x = 0.0 70 | box.corner.y = 0.0 71 | 72 | center = find_center(box) 73 | print('center', end=' ') 74 | print_point(center) 75 | 76 | print(box.width) 77 | print(box.height) 78 | print('grow') 79 | grow_rectangle(box, 50, 100) 80 | print(box.width) 81 | print(box.height) 82 | 83 | 84 | if __name__ == '__main__': 85 | main() 86 | 87 | -------------------------------------------------------------------------------- /code/Point1_soln.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import copy 15 | import math 16 | 17 | from Point1 import Point, Rectangle 18 | 19 | 20 | def distance_between_points(p1, p2): 21 | """Computes the distance between two Point objects. 22 | 23 | p1: Point 24 | p2: Point 25 | 26 | returns: float 27 | """ 28 | dx = p1.x - p2.x 29 | dy = p1.y - p2.y 30 | dist = math.sqrt(dx**2 + dy**2) 31 | return dist 32 | 33 | 34 | def move_rectangle(rect, dx, dy): 35 | """Move the Rectangle by modifying its corner object. 36 | 37 | rect: Rectangle object. 38 | dx: change in x coordinate (can be negative). 39 | dy: change in y coordinate (can be negative). 40 | """ 41 | rect.corner.x += dx 42 | rect.corner.y += dy 43 | 44 | 45 | def move_rectangle_copy(rect, dx, dy): 46 | """Move the Rectangle and return a new Rectangle object. 47 | 48 | rect: Rectangle object. 49 | dx: change in x coordinate (can be negative). 50 | dy: change in y coordinate (can be negative). 51 | 52 | returns: new Rectangle 53 | """ 54 | new = copy.deepcopy(rect) 55 | move_rectangle(new, dx, dy) 56 | return new 57 | 58 | 59 | def main(): 60 | blank = Point() 61 | blank.x = 0 62 | blank.y = 0 63 | 64 | grosse = Point() 65 | grosse.x = 3 66 | grosse.y = 4 67 | 68 | print('distance', end=' ') 69 | print(distance_between_points(grosse, blank)) 70 | 71 | box = Rectangle() 72 | box.width = 100.0 73 | box.height = 200.0 74 | box.corner = Point() 75 | box.corner.x = 50.0 76 | box.corner.y = 50.0 77 | 78 | print(box.corner.x) 79 | print(box.corner.y) 80 | print('move') 81 | move_rectangle(box, 50, 100) 82 | print(box.corner.x) 83 | print(box.corner.y) 84 | 85 | new_box = move_rectangle_copy(box, 50, 100) 86 | print(new_box.corner.x) 87 | print(new_box.corner.y) 88 | 89 | 90 | if __name__ == '__main__': 91 | main() 92 | 93 | -------------------------------------------------------------------------------- /code/Point2_soln.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | class Point: 16 | """Represents a point in 2-D space. 17 | 18 | attributes: x, y 19 | """ 20 | def __init__(self, x=0, y=0): 21 | self.x = x 22 | self.y = y 23 | 24 | def __str__(self): 25 | return '(%g, %g)' % (self.x, self.y) 26 | 27 | def __add__(self, other): 28 | """Adds a Point or tuple.""" 29 | if isinstance(other, Point): 30 | return self.add_point(other) 31 | elif isinstance(other, tuple): 32 | return self.add_tuple(other) 33 | else: 34 | msg = "Point doesn't know how to add type " + type(other) 35 | raise TypeError(msg) 36 | 37 | def add_point(self, other): 38 | """Adds a point.""" 39 | return Point(self.x + other.x, self.y + other.y) 40 | 41 | def add_tuple(self, other): 42 | """Adds a tuple.""" 43 | return Point(self.x + other[0], self.y + other[1]) 44 | 45 | 46 | 47 | def main(): 48 | p1 = Point(1, 2) 49 | p2 = Point(3, 4) 50 | print(p1) 51 | print(p2) 52 | print(p1 + p2) 53 | print(p1 + (3, 4)) 54 | 55 | if __name__ == '__main__': 56 | main() 57 | 58 | -------------------------------------------------------------------------------- /code/PokerHand.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | from Card import Hand, Deck 15 | 16 | 17 | class PokerHand(Hand): 18 | """Represents a poker hand.""" 19 | 20 | def suit_hist(self): 21 | """Builds a histogram of the suits that appear in the hand. 22 | 23 | Stores the result in attribute suits. 24 | """ 25 | self.suits = {} 26 | for card in self.cards: 27 | self.suits[card.suit] = self.suits.get(card.suit, 0) + 1 28 | 29 | def has_flush(self): 30 | """Returns True if the hand has a flush, False otherwise. 31 | 32 | Note that this works correctly for hands with more than 5 cards. 33 | """ 34 | self.suit_hist() 35 | for val in self.suits.values(): 36 | if val >= 5: 37 | return True 38 | return False 39 | 40 | 41 | if __name__ == '__main__': 42 | # make a deck 43 | deck = Deck() 44 | deck.shuffle() 45 | 46 | # deal the cards and classify the hands 47 | for i in range(7): 48 | hand = PokerHand() 49 | deck.move_cards(hand, 7) 50 | hand.sort() 51 | print(hand) 52 | print(hand.has_flush()) 53 | print('') 54 | 55 | -------------------------------------------------------------------------------- /code/Time1.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | class Time: 16 | """Represents the time of day. 17 | 18 | attributes: hour, minute, second 19 | """ 20 | 21 | 22 | def print_time(t): 23 | """Prints a string representation of the time. 24 | 25 | t: Time object 26 | """ 27 | print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second)) 28 | 29 | 30 | def int_to_time(seconds): 31 | """Makes a new Time object. 32 | 33 | seconds: int seconds since midnight. 34 | """ 35 | time = Time() 36 | minutes, time.second = divmod(seconds, 60) 37 | time.hour, time.minute = divmod(minutes, 60) 38 | return time 39 | 40 | 41 | def time_to_int(time): 42 | """Computes the number of seconds since midnight. 43 | 44 | time: Time object. 45 | """ 46 | minutes = time.hour * 60 + time.minute 47 | seconds = minutes * 60 + time.second 48 | return seconds 49 | 50 | 51 | def add_times(t1, t2): 52 | """Adds two time objects. 53 | 54 | t1, t2: Time 55 | 56 | returns: Time 57 | """ 58 | assert valid_time(t1) and valid_time(t2) 59 | seconds = time_to_int(t1) + time_to_int(t2) 60 | return int_to_time(seconds) 61 | 62 | 63 | def valid_time(time): 64 | """Checks whether a Time object satisfies the invariants. 65 | 66 | time: Time 67 | 68 | returns: boolean 69 | """ 70 | if time.hour < 0 or time.minute < 0 or time.second < 0: 71 | return False 72 | if time.minute >= 60 or time.second >= 60: 73 | return False 74 | return True 75 | 76 | 77 | def main(): 78 | # if a movie starts at noon... 79 | noon_time = Time() 80 | noon_time.hour = 12 81 | noon_time.minute = 0 82 | noon_time.second = 0 83 | 84 | print('Starts at', end=' ') 85 | print_time(noon_time) 86 | 87 | # and the run time of the movie is 109 minutes... 88 | movie_minutes = 109 89 | run_time = int_to_time(movie_minutes * 60) 90 | print('Run time', end=' ') 91 | print_time(run_time) 92 | 93 | # what time does the movie end? 94 | end_time = add_times(noon_time, run_time) 95 | print('Ends at', end=' ') 96 | print_time(end_time) 97 | 98 | 99 | if __name__ == '__main__': 100 | main() 101 | -------------------------------------------------------------------------------- /code/Time2.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | class Time: 16 | """Represents the time of day. 17 | 18 | attributes: hour, minute, second 19 | """ 20 | def __init__(self, hour=0, minute=0, second=0): 21 | """Initializes a time object. 22 | 23 | hour: int 24 | minute: int 25 | second: int or float 26 | """ 27 | self.hour = hour 28 | self.minute = minute 29 | self.second = second 30 | 31 | def __str__(self): 32 | """Returns a string representation of the time.""" 33 | return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second) 34 | 35 | def print_time(self): 36 | """Prints a string representation of the time.""" 37 | print(str(self)) 38 | 39 | def time_to_int(self): 40 | """Computes the number of seconds since midnight.""" 41 | minutes = self.hour * 60 + self.minute 42 | seconds = minutes * 60 + self.second 43 | return seconds 44 | 45 | def is_after(self, other): 46 | """Returns True if t1 is after t2; false otherwise.""" 47 | return self.time_to_int() > other.time_to_int() 48 | 49 | def __add__(self, other): 50 | """Adds two Time objects or a Time object and a number. 51 | 52 | other: Time object or number of seconds 53 | """ 54 | if isinstance(other, Time): 55 | return self.add_time(other) 56 | else: 57 | return self.increment(other) 58 | 59 | def __radd__(self, other): 60 | """Adds two Time objects or a Time object and a number.""" 61 | return self.__add__(other) 62 | 63 | def add_time(self, other): 64 | """Adds two time objects.""" 65 | assert self.is_valid() and other.is_valid() 66 | seconds = self.time_to_int() + other.time_to_int() 67 | return int_to_time(seconds) 68 | 69 | def increment(self, seconds): 70 | """Returns a new Time that is the sum of this time and seconds.""" 71 | seconds += self.time_to_int() 72 | return int_to_time(seconds) 73 | 74 | def is_valid(self): 75 | """Checks whether a Time object satisfies the invariants.""" 76 | if self.hour < 0 or self.minute < 0 or self.second < 0: 77 | return False 78 | if self.minute >= 60 or self.second >= 60: 79 | return False 80 | return True 81 | 82 | 83 | def int_to_time(seconds): 84 | """Makes a new Time object. 85 | 86 | seconds: int seconds since midnight. 87 | """ 88 | minutes, second = divmod(seconds, 60) 89 | hour, minute = divmod(minutes, 60) 90 | time = Time(hour, minute, second) 91 | return time 92 | 93 | 94 | def main(): 95 | start = Time(9, 45, 00) 96 | start.print_time() 97 | 98 | end = start.increment(1337) 99 | #end = start.increment(1337, 460) 100 | end.print_time() 101 | 102 | print('Is end after start?') 103 | print(end.is_after(start)) 104 | 105 | print('Using __str__') 106 | print(start, end) 107 | 108 | start = Time(9, 45) 109 | duration = Time(1, 35) 110 | print(start + duration) 111 | print(start + 1337) 112 | print(1337 + start) 113 | 114 | print('Example of polymorphism') 115 | t1 = Time(7, 43) 116 | t2 = Time(7, 41) 117 | t3 = Time(7, 37) 118 | total = sum([t1, t2, t3]) 119 | print(total) 120 | 121 | 122 | if __name__ == '__main__': 123 | main() 124 | -------------------------------------------------------------------------------- /code/Time2_soln.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | class Time: 16 | """Represents the time of day. 17 | 18 | attributes: hour, minute, second 19 | """ 20 | def __init__(self, hour=0, minute=0, second=0): 21 | """Initializes a time object. 22 | 23 | hour: int 24 | minute: int 25 | second: int or float 26 | """ 27 | minutes = hour * 60 + minute 28 | self.seconds = minutes * 60 + second 29 | 30 | def __str__(self): 31 | """Returns a string representation of the time.""" 32 | minutes, second = divmod(self.seconds, 60) 33 | hour, minute = divmod(minutes, 60) 34 | return '%.2d:%.2d:%.2d' % (hour, minute, second) 35 | 36 | def print_time(self): 37 | """Prints a string representation of the time.""" 38 | print(str(self)) 39 | 40 | def time_to_int(self): 41 | """Computes the number of seconds since midnight.""" 42 | return self.seconds 43 | 44 | def is_after(self, other): 45 | """Returns True if t1 is after t2; false otherwise.""" 46 | return self.seconds > other.seconds 47 | 48 | def __add__(self, other): 49 | """Adds two Time objects or a Time object and a number. 50 | 51 | other: Time object or number of seconds 52 | """ 53 | if isinstance(other, Time): 54 | return self.add_time(other) 55 | else: 56 | return self.increment(other) 57 | 58 | def __radd__(self, other): 59 | """Adds two Time objects or a Time object and a number.""" 60 | return self.__add__(other) 61 | 62 | def add_time(self, other): 63 | """Adds two time objects.""" 64 | assert self.is_valid() and other.is_valid() 65 | seconds = self.seconds + other.seconds 66 | return int_to_time(seconds) 67 | 68 | def increment(self, seconds): 69 | """Returns a new Time that is the sum of this time and seconds.""" 70 | seconds += self.seconds 71 | return int_to_time(seconds) 72 | 73 | def is_valid(self): 74 | """Checks whether a Time object satisfies the invariants.""" 75 | return self.seconds >= 0 and self.seconds < 24*60*60 76 | 77 | 78 | def int_to_time(seconds): 79 | """Makes a new Time object. 80 | 81 | seconds: int seconds since midnight. 82 | """ 83 | return Time(0, 0, seconds) 84 | 85 | 86 | def main(): 87 | start = Time(9, 45, 00) 88 | start.print_time() 89 | 90 | end = start.increment(1337) 91 | end.print_time() 92 | 93 | print('Is end after start?') 94 | print(end.is_after(start)) 95 | 96 | print('Using __str__') 97 | print(start, end) 98 | 99 | start = Time(9, 45) 100 | duration = Time(1, 35) 101 | print(start + duration) 102 | print(start + 1337) 103 | print(1337 + start) 104 | 105 | print('Example of polymorphism') 106 | t1 = Time(7, 43) 107 | t2 = Time(7, 41) 108 | t3 = Time(7, 37) 109 | total = sum([t1, t2, t3]) 110 | print(total) 111 | 112 | 113 | if __name__ == '__main__': 114 | main() 115 | -------------------------------------------------------------------------------- /code/ackermann.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def ackermann(m, n): 16 | """Computes the Ackermann function A(m, n) 17 | 18 | See http://en.wikipedia.org/wiki/Ackermann_function 19 | 20 | n, m: non-negative integers 21 | """ 22 | if m == 0: 23 | return n+1 24 | if n == 0: 25 | return ackermann(m-1, 1) 26 | return ackermann(m-1, ackermann(m, n-1)) 27 | 28 | 29 | print(ackermann(3, 4)) 30 | -------------------------------------------------------------------------------- /code/ackermann_memo.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | cache = {} 15 | 16 | def ackermann(m, n): 17 | """Computes the Ackermann function A(m, n) 18 | 19 | See http://en.wikipedia.org/wiki/Ackermann_function 20 | 21 | n, m: non-negative integers 22 | """ 23 | if m == 0: 24 | return n+1 25 | if n == 0: 26 | return ackermann(m-1, 1) 27 | try: 28 | return cache[m, n] 29 | except KeyError: 30 | cache[m, n] = ackermann(m-1, ackermann(m, n-1)) 31 | return cache[m, n] 32 | 33 | 34 | print(ackermann(3, 4)) 35 | print(ackermann(3, 6)) 36 | -------------------------------------------------------------------------------- /code/anagram_db.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import shelve 15 | import sys 16 | 17 | from anagram_sets import all_anagrams, signature 18 | 19 | 20 | def store_anagrams(filename, anagram_map): 21 | """Stores the anagrams from a dictionary in a shelf. 22 | 23 | filename: string file name of shelf 24 | anagram_map: dictionary that maps strings to list of anagrams 25 | """ 26 | shelf = shelve.open(filename, 'c') 27 | 28 | for word, word_list in anagram_map.items(): 29 | shelf[word] = word_list 30 | 31 | shelf.close() 32 | 33 | 34 | def read_anagrams(filename, word): 35 | """Looks up a word in a shelf and returns a list of its anagrams. 36 | 37 | filename: string file name of shelf 38 | word: word to look up 39 | """ 40 | shelf = shelve.open(filename) 41 | sig = signature(word) 42 | try: 43 | return shelf[sig] 44 | except KeyError: 45 | return [] 46 | 47 | 48 | def main(script, command='make_db'): 49 | if command == 'make_db': 50 | anagram_map = all_anagrams('words.txt') 51 | store_anagrams('anagrams.db', anagram_map) 52 | else: 53 | print(read_anagrams('anagrams.db', command)) 54 | 55 | 56 | if __name__ == '__main__': 57 | main(*sys.argv) 58 | 59 | -------------------------------------------------------------------------------- /code/anagram_sets.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def signature(s): 16 | """Returns the signature of this string. 17 | 18 | Signature is a string that contains all of the letters in order. 19 | 20 | s: string 21 | """ 22 | # TODO: rewrite using sorted() 23 | t = list(s) 24 | t.sort() 25 | t = ''.join(t) 26 | return t 27 | 28 | 29 | def all_anagrams(filename): 30 | """Finds all anagrams in a list of words. 31 | 32 | filename: string filename of the word list 33 | 34 | Returns: a map from each word to a list of its anagrams. 35 | """ 36 | d = {} 37 | for line in open(filename): 38 | word = line.strip().lower() 39 | t = signature(word) 40 | 41 | # TODO: rewrite using defaultdict 42 | if t not in d: 43 | d[t] = [word] 44 | else: 45 | d[t].append(word) 46 | return d 47 | 48 | 49 | def print_anagram_sets(d): 50 | """Prints the anagram sets in d. 51 | 52 | d: map from words to list of their anagrams 53 | """ 54 | for v in d.values(): 55 | if len(v) > 1: 56 | print(len(v), v) 57 | 58 | 59 | def print_anagram_sets_in_order(d): 60 | """Prints the anagram sets in d in decreasing order of size. 61 | 62 | d: map from words to list of their anagrams 63 | """ 64 | # make a list of (length, word pairs) 65 | t = [] 66 | for v in d.values(): 67 | if len(v) > 1: 68 | t.append((len(v), v)) 69 | 70 | # sort in ascending order of length 71 | t.sort() 72 | 73 | # print the sorted list 74 | for x in t: 75 | print(x) 76 | 77 | 78 | def filter_length(d, n): 79 | """Select only the words in d that have n letters. 80 | 81 | d: map from word to list of anagrams 82 | n: integer number of letters 83 | 84 | returns: new map from word to list of anagrams 85 | """ 86 | res = {} 87 | for word, anagrams in d.items(): 88 | if len(word) == n: 89 | res[word] = anagrams 90 | return res 91 | 92 | 93 | if __name__ == '__main__': 94 | anagram_map = all_anagrams('words.txt') 95 | print_anagram_sets_in_order(anagram_map) 96 | 97 | eight_letters = filter_length(anagram_map, 8) 98 | print_anagram_sets_in_order(eight_letters) 99 | 100 | -------------------------------------------------------------------------------- /code/analyze_book2.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | from analyze_book1 import process_file 15 | 16 | 17 | def subtract(d1, d2): 18 | """Returns a set of all keys that appear in d1 but not d2. 19 | 20 | d1, d2: dictionaries 21 | """ 22 | return set(d1) - set(d2) 23 | 24 | 25 | def main(): 26 | hist = process_file('emma.txt', skip_header=True) 27 | words = process_file('words.txt', skip_header=False) 28 | 29 | diff = subtract(hist, words) 30 | print("The words in the book that aren't in the word list are:") 31 | for word in diff: 32 | print(word, end=' ') 33 | 34 | 35 | if __name__ == '__main__': 36 | main() 37 | 38 | -------------------------------------------------------------------------------- /code/analyze_book3.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import random 15 | 16 | from bisect import bisect 17 | 18 | from analyze_book1 import process_file 19 | 20 | 21 | def random_word(hist): 22 | """Chooses a random word from a histogram. 23 | 24 | The probability of each word is proportional to its frequency. 25 | 26 | hist: map from word to frequency 27 | """ 28 | # TODO: This could be made faster by computing the cumulative 29 | # frequencies once and reusing them. 30 | 31 | words = [] 32 | freqs = [] 33 | total_freq = 0 34 | 35 | # make a list of words and a list of cumulative frequencies 36 | for word, freq in hist.items(): 37 | total_freq += freq 38 | words.append(word) 39 | freqs.append(total_freq) 40 | 41 | # choose a random value and find its location in the cumulative list 42 | x = random.randint(0, total_freq-1) 43 | index = bisect(freqs, x) 44 | return words[index] 45 | 46 | 47 | def main(): 48 | hist = process_file('emma.txt', skip_header=True) 49 | 50 | print("\n\nHere are some random words from the book") 51 | for i in range(100): 52 | print(random_word(hist), end=' ') 53 | 54 | 55 | if __name__ == '__main__': 56 | main() 57 | 58 | -------------------------------------------------------------------------------- /code/birthday.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import random 15 | 16 | 17 | def has_duplicates(t): 18 | """Returns True if any element appears more than once in a sequence. 19 | 20 | t: list 21 | 22 | returns: bool 23 | """ 24 | # make a copy of t to avoid modifying the parameter 25 | s = t[:] 26 | s.sort() 27 | 28 | # check for adjacent elements that are equal 29 | for i in range(len(s)-1): 30 | if s[i] == s[i+1]: 31 | return True 32 | return False 33 | 34 | 35 | def random_bdays(n): 36 | """Returns a list of integers between 1 and 365, with length n. 37 | 38 | n: int 39 | 40 | returns: list of int 41 | """ 42 | t = [] 43 | for i in range(n): 44 | bday = random.randint(1, 365) 45 | t.append(bday) 46 | return t 47 | 48 | 49 | def count_matches(num_students, num_simulations): 50 | """Generates a sample of birthdays and counts duplicates. 51 | 52 | num_students: how many students in the group 53 | num_samples: how many groups to simulate 54 | 55 | returns: int 56 | """ 57 | count = 0 58 | for i in range(num_simulations): 59 | t = random_bdays(num_students) 60 | if has_duplicates(t): 61 | count += 1 62 | return count 63 | 64 | 65 | def main(): 66 | """Runs the birthday simulation and prints the number of matches.""" 67 | num_students = 23 68 | num_simulations = 1000 69 | count = count_matches(num_students, num_simulations) 70 | 71 | print('After %d simulations' % num_simulations) 72 | print('with %d students' % num_students) 73 | print('there were %d simulations with at least one match' % count) 74 | 75 | 76 | if __name__ == '__main__': 77 | main() 78 | -------------------------------------------------------------------------------- /code/cartalk1.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def is_triple_double(word): 16 | """Tests if a word contains three consecutive double letters. 17 | 18 | word: string 19 | 20 | returns: bool 21 | """ 22 | i = 0 23 | count = 0 24 | while i < len(word)-1: 25 | if word[i] == word[i+1]: 26 | count = count + 1 27 | if count == 3: 28 | return True 29 | i = i + 2 30 | else: 31 | count = 0 32 | i = i + 1 33 | return False 34 | 35 | 36 | def find_triple_double(): 37 | """Reads a word list and prints words with triple double letters.""" 38 | fin = open('words.txt') 39 | for line in fin: 40 | word = line.strip() 41 | if is_triple_double(word): 42 | print(word) 43 | 44 | 45 | print('Here are all the words in the list that have') 46 | print('three consecutive double letters.') 47 | find_triple_double() 48 | print('') 49 | 50 | 51 | -------------------------------------------------------------------------------- /code/cartalk2.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def has_palindrome(i, start, length): 16 | """Checks if the string representation of i has a palindrome. 17 | 18 | i: integer 19 | start: where in the string to start 20 | length: length of the palindrome to check for 21 | """ 22 | s = str(i)[start:start+length] 23 | return s[::-1] == s 24 | 25 | 26 | def check(i): 27 | """Checks if the integer (i) has the desired properties. 28 | 29 | i: int 30 | """ 31 | return (has_palindrome(i, 2, 4) and 32 | has_palindrome(i+1, 1, 5) and 33 | has_palindrome(i+2, 1, 4) and 34 | has_palindrome(i+3, 0, 6)) 35 | 36 | 37 | def check_all(): 38 | """Enumerate the six-digit numbers and print any winners. 39 | """ 40 | i = 100000 41 | while i <= 999996: 42 | if check(i): 43 | print(i) 44 | i = i + 1 45 | 46 | 47 | print('The following are the possible odometer readings:') 48 | check_all() 49 | print() 50 | 51 | 52 | -------------------------------------------------------------------------------- /code/cartalk3.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def str_fill(i, n): 16 | """Returns i as a string with at least n digits. 17 | 18 | i: int 19 | n: int length 20 | 21 | returns: string 22 | """ 23 | return str(i).zfill(n) 24 | 25 | 26 | def are_reversed(i, j): 27 | """Checks if i and j are the reverse of each other. 28 | 29 | i: int 30 | j: int 31 | 32 | returns:bool 33 | """ 34 | return str_fill(i, 2) == str_fill(j, 2)[::-1] 35 | 36 | 37 | def num_instances(diff, flag=False): 38 | """Counts the number of palindromic ages. 39 | 40 | Returns the number of times the mother and daughter have 41 | palindromic ages in their lives, given the difference in age. 42 | 43 | diff: int difference in ages 44 | flag: bool, if True, prints the details 45 | """ 46 | daughter = 0 47 | count = 0 48 | while True: 49 | mother = daughter + diff 50 | if are_reversed(daughter, mother) or are_reversed(daughter, mother+1): 51 | count = count + 1 52 | if flag: 53 | print(daughter, mother) 54 | if mother > 120: 55 | break 56 | daughter = daughter + 1 57 | return count 58 | 59 | 60 | def check_diffs(): 61 | """Finds age differences that satisfy the problem. 62 | 63 | Enumerates the possible differences in age between mother 64 | and daughter, and for each difference, counts the number of times 65 | over their lives they will have ages that are the reverse of 66 | each other. 67 | """ 68 | diff = 10 69 | while diff < 70: 70 | n = num_instances(diff) 71 | if n > 0: 72 | print(diff, n) 73 | diff = diff + 1 74 | 75 | print('diff #instances') 76 | check_diffs() 77 | 78 | print() 79 | print('daughter mother') 80 | num_instances(18, True) 81 | -------------------------------------------------------------------------------- /code/do_four.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def do_twice(func, arg): 16 | """Runs a function twice. 17 | 18 | func: function object 19 | arg: argument passed to the function 20 | """ 21 | func(arg) 22 | func(arg) 23 | 24 | 25 | def print_twice(arg): 26 | """Prints the argument twice. 27 | 28 | arg: anything printable 29 | """ 30 | print(arg) 31 | print(arg) 32 | 33 | 34 | def do_four(func, arg): 35 | """Runs a function four times. 36 | 37 | func: function object 38 | arg: argument passed to the function 39 | """ 40 | do_twice(func, arg) 41 | do_twice(func, arg) 42 | 43 | 44 | do_twice(print_twice, 'spam') 45 | print('') 46 | 47 | do_four(print_twice, 'spam') 48 | -------------------------------------------------------------------------------- /code/double.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | from datetime import datetime 15 | 16 | 17 | def main(): 18 | print("Today's date and the day of the week:") 19 | today = datetime.today() 20 | print(today) 21 | print(today.strftime("%A")) 22 | 23 | print("Your next birthday and how far away it is:") 24 | #s = input('Enter your birthday in mm/dd/yyyy format: ') 25 | s = '5/11/1967' 26 | bday = datetime.strptime(s, '%m/%d/%Y') 27 | 28 | next_bday = bday.replace(year=today.year) 29 | if next_bday < today: 30 | next_bday = next_bday.replace(year=today.year+1) 31 | print(next_bday) 32 | 33 | until_next_bday = next_bday - today 34 | print(until_next_bday) 35 | 36 | print("Your current age:") 37 | last_bday = next_bday.replace(year=next_bday.year-1) 38 | age = last_bday.year - bday.year 39 | print(age) 40 | 41 | print("For people born on these dates:") 42 | bday1 = datetime(day=11, month=5, year=1967) 43 | bday2 = datetime(day=11, month=10, year=2003) 44 | print(bday1) 45 | print(bday2) 46 | 47 | print("Double Day is") 48 | d1 = min(bday1, bday2) 49 | d2 = max(bday1, bday2) 50 | dd = d2 + (d2 - d1) 51 | print(dd) 52 | 53 | 54 | if __name__ == '__main__': 55 | main() 56 | -------------------------------------------------------------------------------- /code/draw.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import turtle 15 | 16 | from Point1 import Point, Rectangle 17 | from Circle import Circle 18 | 19 | import polygon 20 | 21 | def draw_circle(t, circle): 22 | """Draws a circle. 23 | 24 | t: Turtle 25 | circle: Circle 26 | """ 27 | t.pu() 28 | t.goto(circle.center.x, circle.center.y) 29 | t.fd(circle.radius) 30 | t.lt(90) 31 | t.pd() 32 | polygon.circle(t, circle.radius) 33 | 34 | 35 | def draw_rect(t, rect): 36 | """Draws a rectangle. 37 | 38 | t: Turtle 39 | rect: Rectangle 40 | """ 41 | t.pu() 42 | t.goto(rect.corner.x, rect.corner.y) 43 | t.setheading(0) 44 | t.pd() 45 | 46 | for length in rect.width, rect.height, rect.width, rect.height: 47 | t.fd(length) 48 | t.rt(90) 49 | 50 | 51 | if __name__ == '__main__': 52 | bob = turtle.Turtle() 53 | 54 | # draw the axes 55 | length = 400 56 | bob.fd(length) 57 | bob.bk(length) 58 | bob.lt(90) 59 | bob.fd(length) 60 | bob.bk(length) 61 | 62 | # draw a rectangle 63 | box = Rectangle() 64 | box.width = 100.0 65 | box.height = 200.0 66 | box.corner = Point() 67 | box.corner.x = 50.0 68 | box.corner.y = 50.0 69 | 70 | draw_rect(bob, box) 71 | 72 | # draw a circle 73 | circle = Circle 74 | circle.center = Point() 75 | circle.center.x = 150.0 76 | circle.center.y = 100.0 77 | circle.radius = 75.0 78 | 79 | draw_circle(bob, circle) 80 | 81 | # wait for the user to close the window 82 | turtle.mainloop() 83 | -------------------------------------------------------------------------------- /code/find_duplicates.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import os 15 | 16 | 17 | def walk(dirname): 18 | """Finds the names of all files in dirname and its subdirectories. 19 | 20 | dirname: string name of directory 21 | """ 22 | names = [] 23 | if '__pycache__' in dirname: 24 | return names 25 | 26 | for name in os.listdir(dirname): 27 | path = os.path.join(dirname, name) 28 | 29 | if os.path.isfile(path): 30 | names.append(path) 31 | else: 32 | names.extend(walk(path)) 33 | return names 34 | 35 | 36 | def compute_checksum(filename): 37 | """Computes the MD5 checksum of the contents of a file. 38 | 39 | filename: string 40 | """ 41 | cmd = 'md5sum ' + filename 42 | return pipe(cmd) 43 | 44 | 45 | def check_diff(name1, name2): 46 | """Computes the difference between the contents of two files. 47 | 48 | name1, name2: string filenames 49 | """ 50 | cmd = 'diff %s %s' % (name1, name2) 51 | return pipe(cmd) 52 | 53 | 54 | def pipe(cmd): 55 | """Runs a command in a subprocess. 56 | 57 | cmd: string Unix command 58 | 59 | Returns (res, stat), the output of the subprocess and the exit status. 60 | """ 61 | # Note: os.popen is deprecated 62 | # now, which means we are supposed to stop using it and start using 63 | # the subprocess module. But for simple cases, I find 64 | # subprocess more complicated than necessary. So I am going 65 | # to keep using os.popen until they take it away. 66 | 67 | fp = os.popen(cmd) 68 | res = fp.read() 69 | stat = fp.close() 70 | assert stat is None 71 | return res, stat 72 | 73 | 74 | def compute_checksums(dirname, suffix): 75 | """Computes checksums for all files with the given suffix. 76 | 77 | dirname: string name of directory to search 78 | suffix: string suffix to match 79 | 80 | Returns: map from checksum to list of files with that checksum 81 | """ 82 | names = walk(dirname) 83 | 84 | d = {} 85 | for name in names: 86 | if name.endswith(suffix): 87 | res, stat = compute_checksum(name) 88 | checksum, _ = res.split() 89 | 90 | if checksum in d: 91 | d[checksum].append(name) 92 | else: 93 | d[checksum] = [name] 94 | 95 | return d 96 | 97 | 98 | def check_pairs(names): 99 | """Checks whether any in a list of files differs from the others. 100 | 101 | names: list of string filenames 102 | """ 103 | for name1 in names: 104 | for name2 in names: 105 | if name1 < name2: 106 | res, stat = check_diff(name1, name2) 107 | if res: 108 | return False 109 | return True 110 | 111 | 112 | def print_duplicates(d): 113 | """Checks for duplicate files. 114 | 115 | Reports any files with the same checksum and checks whether they 116 | are, in fact, identical. 117 | 118 | d: map from checksum to list of files with that checksum 119 | """ 120 | for key, names in d.items(): 121 | if len(names) > 1: 122 | print('The following files have the same checksum:') 123 | for name in names: 124 | print(name) 125 | 126 | if check_pairs(names): 127 | print('And they are identical.') 128 | 129 | 130 | if __name__ == '__main__': 131 | d = compute_checksums(dirname='.', suffix='.py') 132 | print_duplicates(d) 133 | -------------------------------------------------------------------------------- /code/flower.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import turtle 15 | 16 | from polygon import arc 17 | 18 | 19 | def petal(t, r, angle): 20 | """Draws a petal using two arcs. 21 | 22 | t: Turtle 23 | r: radius of the arcs 24 | angle: angle (degrees) that subtends the arcs 25 | """ 26 | for i in range(2): 27 | arc(t, r, angle) 28 | t.lt(180-angle) 29 | 30 | 31 | def flower(t, n, r, angle): 32 | """Draws a flower with n petals. 33 | 34 | t: Turtle 35 | n: number of petals 36 | r: radius of the arcs 37 | angle: angle (degrees) that subtends the arcs 38 | """ 39 | for i in range(n): 40 | petal(t, r, angle) 41 | t.lt(360.0/n) 42 | 43 | 44 | def move(t, length): 45 | """Move Turtle (t) forward (length) units without leaving a trail. 46 | Leaves the pen down. 47 | """ 48 | t.pu() 49 | t.fd(length) 50 | t.pd() 51 | 52 | 53 | bob = turtle.Turtle() 54 | 55 | # draw a sequence of three flowers, as shown in the book. 56 | move(bob, -100) 57 | flower(bob, 7, 60.0, 60.0) 58 | 59 | move(bob, 100) 60 | flower(bob, 10, 40.0, 80.0) 61 | 62 | move(bob, 100) 63 | flower(bob, 20, 140.0, 20.0) 64 | 65 | bob.hideturtle() 66 | turtle.mainloop() 67 | -------------------------------------------------------------------------------- /code/grid.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | # here is a mostly-straightforward solution to the 15 | # two-by-two version of the grid. 16 | 17 | def do_twice(f): 18 | f() 19 | f() 20 | 21 | def do_four(f): 22 | do_twice(f) 23 | do_twice(f) 24 | 25 | def print_beam(): 26 | print('+ - - - -', end=' ') 27 | 28 | def print_post(): 29 | print('| ', end=' ') 30 | 31 | def print_beams(): 32 | do_twice(print_beam) 33 | print('+') 34 | 35 | def print_posts(): 36 | do_twice(print_post) 37 | print('|') 38 | 39 | def print_row(): 40 | print_beams() 41 | do_four(print_posts) 42 | 43 | def print_grid(): 44 | do_twice(print_row) 45 | print_beams() 46 | 47 | print_grid() 48 | 49 | 50 | # here is a less-straightforward solution to the 51 | # four-by-four grid 52 | 53 | def one_four_one(f, g, h): 54 | f() 55 | do_four(g) 56 | h() 57 | 58 | def print_plus(): 59 | print('+', end=' ') 60 | 61 | def print_dash(): 62 | print('-', end=' ') 63 | 64 | def print_bar(): 65 | print('|', end=' ') 66 | 67 | def print_space(): 68 | print(' ', end=' ') 69 | 70 | def print_end(): 71 | print() 72 | 73 | def nothing(): 74 | "do nothing" 75 | 76 | def print1beam(): 77 | one_four_one(nothing, print_dash, print_plus) 78 | 79 | def print1post(): 80 | one_four_one(nothing, print_space, print_bar) 81 | 82 | def print4beams(): 83 | one_four_one(print_plus, print1beam, print_end) 84 | 85 | def print4posts(): 86 | one_four_one(print_bar, print1post, print_end) 87 | 88 | def print_row(): 89 | one_four_one(nothing, print4posts, print4beams) 90 | 91 | def print_grid(): 92 | one_four_one(print4beams, print_row, nothing) 93 | 94 | print_grid() 95 | 96 | comment = """ 97 | After writing a draft of the 4x4 grid, I noticed that many of the 98 | functions had the same structure: they would do something, do 99 | something else four times, and then do something else once. 100 | 101 | So I wrote one_four_one, which takes three functions as arguments; it 102 | calls the first one once, then uses do_four to call the second one 103 | four times, then calls the third. 104 | 105 | Then I rewrote print1beam, print1post, print4beams, print4posts, 106 | print_row and print_grid using one_four_one. 107 | 108 | Programming is an exploratory process. Writing a draft of a program 109 | often gives you insight into the problem, which might lead you to 110 | rewrite the code to reflect the structure of the solution. 111 | 112 | --- Allen 113 | """ 114 | 115 | print(comment) 116 | -------------------------------------------------------------------------------- /code/has_duplicates.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def has_duplicates(t): 16 | """Checks whether any element appears more than once in a sequence. 17 | 18 | Simple version using a for loop. 19 | 20 | t: sequence 21 | """ 22 | d = {} 23 | for x in t: 24 | if x in d: 25 | return True 26 | d[x] = True 27 | return False 28 | 29 | 30 | def has_duplicates2(t): 31 | """Checks whether any element appears more than once in a sequence. 32 | 33 | Faster version using a set. 34 | 35 | t: sequence 36 | """ 37 | return len(set(t)) < len(t) 38 | 39 | 40 | if __name__ == '__main__': 41 | t = [1, 2, 3] 42 | print(has_duplicates(t)) 43 | t.append(1) 44 | print(has_duplicates(t)) 45 | 46 | t = [1, 2, 3] 47 | print(has_duplicates2(t)) 48 | t.append(1) 49 | print(has_duplicates2(t)) 50 | 51 | -------------------------------------------------------------------------------- /code/homophone.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | from pronounce import read_dictionary 15 | 16 | 17 | def make_word_dict(): 18 | """Read. the words in words.txt and return a dictionary 19 | that contains the words as keys.""" 20 | d = dict() 21 | fin = open('words.txt') 22 | for line in fin: 23 | word = line.strip().lower() 24 | d[word] = word 25 | 26 | return d 27 | 28 | 29 | def homophones(a, b, phonetic): 30 | """Checks if words two can be pronounced the same way. 31 | 32 | If either word is not in the pronouncing dictionary, return False 33 | 34 | a, b: strings 35 | phonetic: map from words to pronunciation codes 36 | """ 37 | if a not in phonetic or b not in phonetic: 38 | return False 39 | 40 | return phonetic[a] == phonetic[b] 41 | 42 | 43 | def check_word(word, word_dict, phonetic): 44 | """Checks to see if the word has the following property: 45 | removing the first letter yields a word with the same 46 | pronunciation, and removing the second letter yields a word 47 | with the same pronunciation. 48 | 49 | word: string 50 | word_dict: dictionary with words as keys 51 | phonetic: map from words to pronunciation codes 52 | """ 53 | word1 = word[1:] 54 | if word1 not in word_dict: 55 | return False 56 | if not homophones(word, word1, phonetic): 57 | return False 58 | 59 | word2 = word[0] + word[2:] 60 | if word2 not in word_dict: 61 | return False 62 | if not homophones(word, word2, phonetic): 63 | return False 64 | 65 | return True 66 | 67 | 68 | if __name__ == '__main__': 69 | phonetic = read_dictionary() 70 | word_dict = make_word_dict() 71 | 72 | for word in word_dict: 73 | if check_word(word, word_dict, phonetic): 74 | print(word, word[1:], word[0] + word[2:]) 75 | -------------------------------------------------------------------------------- /code/inlist.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import bisect 15 | 16 | 17 | def make_word_list(): 18 | """Reads lines from a file and builds a list using append. 19 | 20 | returns: list of strings 21 | """ 22 | word_list = [] 23 | fin = open('words.txt') 24 | for line in fin: 25 | word = line.strip() 26 | word_list.append(word) 27 | return word_list 28 | 29 | 30 | def in_bisect(word_list, word): 31 | """Checks whether a word is in a list using bisection search. 32 | 33 | Precondition: the words in the list are sorted 34 | 35 | word_list: list of strings 36 | word: string 37 | """ 38 | if len(word_list) == 0: 39 | return False 40 | 41 | i = len(word_list) // 2 42 | if word_list[i] == word: 43 | return True 44 | 45 | if word_list[i] > word: 46 | # search the first half 47 | return in_bisect(word_list[:i], word) 48 | else: 49 | # search the second half 50 | return in_bisect(word_list[i+1:], word) 51 | 52 | 53 | def in_bisect_cheat(word_list, word): 54 | """Checks whether a word is in a list using bisection search. 55 | 56 | Precondition: the words in the list are sorted 57 | 58 | word_list: list of strings 59 | word: string 60 | """ 61 | i = bisect.bisect_left(word_list, word) 62 | if i == len(word_list): 63 | return False 64 | 65 | return word_list[i] == word 66 | 67 | 68 | if __name__ == '__main__': 69 | word_list = make_word_list() 70 | 71 | for word in ['aa', 'alien', 'allen', 'zymurgy']: 72 | print(word, 'in list', in_bisect(word_list, word)) 73 | 74 | for word in ['aa', 'alien', 'allen', 'zymurgy']: 75 | print(word, 'in list', in_bisect_cheat(word_list, word)) 76 | 77 | 78 | -------------------------------------------------------------------------------- /code/interlock.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | from inlist import make_word_list, in_bisect 15 | 16 | 17 | def interlock(word_list, word): 18 | """Checks whether a word contains two interleaved words. 19 | 20 | word_list: list of strings 21 | word: string 22 | """ 23 | evens = word[::2] 24 | odds = word[1::2] 25 | return in_bisect(word_list, evens) and in_bisect(word_list, odds) 26 | 27 | 28 | def interlock_general(word_list, word, n=3): 29 | """Checks whether a word contains n interleaved words. 30 | 31 | word_list: list of strings 32 | word: string 33 | n: number of interleaved words 34 | """ 35 | for i in range(n): 36 | inter = word[i::n] 37 | if not in_bisect(word_list, inter): 38 | return False 39 | return True 40 | 41 | 42 | if __name__ == '__main__': 43 | word_list = make_word_list() 44 | 45 | for word in word_list: 46 | if interlock(word_list, word): 47 | print(word, word[::2], word[1::2]) 48 | 49 | for word in word_list: 50 | if interlock_general(word_list, word, 3): 51 | print(word, word[0::3], word[1::3], word[2::3]) 52 | 53 | 54 | -------------------------------------------------------------------------------- /code/invert_dict.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def invert_dict(d): 16 | """Inverts a dictionary, returning a map from val to a list of keys. 17 | 18 | If the mapping key->val appears in d, then in the new dictionary 19 | val maps to a list that includes key. 20 | 21 | d: dict 22 | 23 | Returns: dict 24 | """ 25 | inverse = {} 26 | for key in d: 27 | val = d[key] 28 | inverse.setdefault(val, []).append(key) 29 | return inverse 30 | 31 | 32 | if __name__ == '__main__': 33 | d = dict(a=1, b=2, c=3, z=1) 34 | inverse = invert_dict(d) 35 | for val in inverse: 36 | keys = inverse[val] 37 | print(val, keys) 38 | 39 | -------------------------------------------------------------------------------- /code/koch.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import turtle 15 | 16 | 17 | def koch(t, n): 18 | """Draws a koch curve with length n.""" 19 | if n < 10: 20 | t.fd(n) 21 | return 22 | m = n/3 23 | koch(t, m) 24 | t.lt(60) 25 | koch(t, m) 26 | t.rt(120) 27 | koch(t, m) 28 | t.lt(60) 29 | koch(t, m) 30 | 31 | 32 | def snowflake(t, n): 33 | """Draws a snowflake (a triangle with a Koch curve for each side).""" 34 | for i in range(3): 35 | koch(t, n) 36 | t.rt(120) 37 | 38 | 39 | bob = turtle.Turtle() 40 | 41 | bob.pu() 42 | bob.goto(-150, 90) 43 | bob.pd() 44 | snowflake(bob, 300) 45 | 46 | turtle.mainloop() 47 | 48 | -------------------------------------------------------------------------------- /code/list_exercises.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import random 15 | 16 | 17 | def nested_sum(t): 18 | """Computes the total of all numbers in a list of lists. 19 | 20 | t: list of list of numbers 21 | 22 | returns: number 23 | """ 24 | total = 0 25 | for nested in t: 26 | total += sum(nested) 27 | return total 28 | 29 | 30 | def cumsum(t): 31 | """Computes the cumulative sum of the numbers in t. 32 | 33 | t: list of numbers 34 | 35 | returns: list of numbers 36 | """ 37 | total = 0 38 | res = [] 39 | for x in t: 40 | total += x 41 | res.append(total) 42 | return res 43 | 44 | 45 | def middle(t): 46 | """Returns all but the first and last elements of t. 47 | 48 | t: list 49 | 50 | returns: new list 51 | """ 52 | return t[1:-1] 53 | 54 | 55 | def chop(t): 56 | """Removes the first and last elements of t. 57 | 58 | t: list 59 | 60 | returns: None 61 | """ 62 | del t[0] 63 | del t[-1] 64 | 65 | 66 | def is_sorted(t): 67 | """Checks whether a list is sorted. 68 | 69 | t: list 70 | 71 | returns: boolean 72 | """ 73 | return t == sorted(t) 74 | 75 | 76 | def is_anagram(word1, word2): 77 | """Checks whether two words are anagrams 78 | 79 | word1: string or list 80 | word2: string or list 81 | 82 | returns: boolean 83 | """ 84 | return sorted(word1) == sorted(word2) 85 | 86 | 87 | def has_duplicates(s): 88 | """Returns True if any element appears more than once in a sequence. 89 | 90 | s: string or list 91 | 92 | returns: bool 93 | """ 94 | # make a copy of t to avoid modifying the parameter 95 | t = list(s) 96 | t.sort() 97 | 98 | # check for adjacent elements that are equal 99 | for i in range(len(t)-1): 100 | if t[i] == t[i+1]: 101 | return True 102 | return False 103 | 104 | 105 | def main(): 106 | t = [[1, 2], [3], [4, 5, 6]] 107 | print(nested_sum(t)) 108 | 109 | t = [1, 2, 3] 110 | print(cumsum(t)) 111 | 112 | t = [1, 2, 3, 4] 113 | print(middle(t)) 114 | chop(t) 115 | print(t) 116 | 117 | print(is_sorted([1, 2, 2])) 118 | print(is_sorted(['b', 'a'])) 119 | 120 | print(is_anagram('stop', 'pots')) 121 | print(is_anagram('different', 'letters')) 122 | print(is_anagram([1, 2, 2], [2, 1, 2])) 123 | 124 | print(has_duplicates('cba')) 125 | print(has_duplicates('abba')) 126 | 127 | 128 | if __name__ == '__main__': 129 | main() 130 | -------------------------------------------------------------------------------- /code/markov.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import sys 15 | import string 16 | import random 17 | 18 | # global variables 19 | suffix_map = {} # map from prefixes to a list of suffixes 20 | prefix = () # current tuple of words 21 | 22 | 23 | def process_file(filename, order=2): 24 | """Reads a file and performs Markov analysis. 25 | 26 | filename: string 27 | order: integer number of words in the prefix 28 | 29 | returns: map from prefix to list of possible suffixes. 30 | """ 31 | fp = open(filename) 32 | skip_gutenberg_header(fp) 33 | 34 | for line in fp: 35 | for word in line.rstrip().split(): 36 | process_word(word, order) 37 | 38 | 39 | def skip_gutenberg_header(fp): 40 | """Reads from fp until it finds the line that ends the header. 41 | 42 | fp: open file object 43 | """ 44 | for line in fp: 45 | if line.startswith('*END*THE SMALL PRINT!'): 46 | break 47 | 48 | 49 | def process_word(word, order=2): 50 | """Processes each word. 51 | 52 | word: string 53 | order: integer 54 | 55 | During the first few iterations, all we do is store up the words; 56 | after that we start adding entries to the dictionary. 57 | """ 58 | global prefix 59 | if len(prefix) < order: 60 | prefix += (word,) 61 | return 62 | 63 | try: 64 | suffix_map[prefix].append(word) 65 | except KeyError: 66 | # if there is no entry for this prefix, make one 67 | suffix_map[prefix] = [word] 68 | 69 | prefix = shift(prefix, word) 70 | 71 | 72 | def random_text(n=100): 73 | """Generates random wordsfrom the analyzed text. 74 | 75 | Starts with a random prefix from the dictionary. 76 | 77 | n: number of words to generate 78 | """ 79 | # choose a random prefix (not weighted by frequency) 80 | start = random.choice(list(suffix_map.keys())) 81 | 82 | for i in range(n): 83 | suffixes = suffix_map.get(start, None) 84 | if suffixes == None: 85 | # if the start isn't in map, we got to the end of the 86 | # original text, so we have to start again. 87 | random_text(n-i) 88 | return 89 | 90 | # choose a random suffix 91 | word = random.choice(suffixes) 92 | print(word, end=' ') 93 | start = shift(start, word) 94 | 95 | 96 | def shift(t, word): 97 | """Forms a new tuple by removing the head and adding word to the tail. 98 | 99 | t: tuple of strings 100 | word: string 101 | 102 | Returns: tuple of strings 103 | """ 104 | return t[1:] + (word,) 105 | 106 | 107 | def main(script, filename='emma.txt', n=100, order=2): 108 | try: 109 | n = int(n) 110 | order = int(order) 111 | except ValueError: 112 | print('Usage: %d filename [# of words] [prefix length]' % script) 113 | else: 114 | process_file(filename, order) 115 | random_text(n) 116 | print() 117 | 118 | 119 | if __name__ == '__main__': 120 | main(*sys.argv) 121 | -------------------------------------------------------------------------------- /code/metathesis.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import anagram_sets 15 | 16 | 17 | def metathesis_pairs(d): 18 | """Print all pairs of words that differ by swapping two letters. 19 | 20 | d: map from word to list of anagrams 21 | """ 22 | for anagrams in d.values(): 23 | for word1 in anagrams: 24 | for word2 in anagrams: 25 | if word1 < word2 and word_distance(word1, word2) == 2: 26 | print(word1, word2) 27 | 28 | 29 | def word_distance(word1, word2): 30 | """Computes the number of differences between two words. 31 | 32 | word1, word2: strings 33 | 34 | Returns: integer 35 | """ 36 | assert len(word1) == len(word2) 37 | 38 | count = 0 39 | for c1, c2 in zip(word1, word2): 40 | if c1 != c2: 41 | count += 1 42 | 43 | return count 44 | 45 | 46 | if __name__ == '__main__': 47 | sets = anagram_sets.all_anagrams('words.txt') 48 | metathesis_pairs(sets) 49 | -------------------------------------------------------------------------------- /code/most_frequent.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def most_frequent(s): 16 | """Sorts the letters in s in reverse order of frequency. 17 | 18 | s: string 19 | 20 | Returns: list of letters 21 | """ 22 | hist = make_histogram(s) 23 | 24 | t = [] 25 | for x, freq in hist.items(): 26 | t.append((freq, x)) 27 | 28 | t.sort(reverse=True) 29 | 30 | res = [] 31 | for freq, x in t: 32 | res.append(x) 33 | 34 | return res 35 | 36 | 37 | def make_histogram(s): 38 | """Make a map from letters to number of times they appear in s. 39 | 40 | s: string 41 | 42 | Returns: map from letter to frequency 43 | """ 44 | hist = {} 45 | for x in s: 46 | hist[x] = hist.get(x, 0) + 1 47 | return hist 48 | 49 | 50 | def read_file(filename): 51 | """Returns the contents of a file as a string.""" 52 | return open(filename).read() 53 | 54 | 55 | if __name__ == '__main__': 56 | string = read_file('emma.txt') 57 | letter_seq = most_frequent(string) 58 | for x in letter_seq: 59 | print(x) 60 | -------------------------------------------------------------------------------- /code/pace_calc.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | """ 16 | 17 | If you run a 10 kilometer race in 43 minutes 30 seconds, what is your 18 | average time per mile? What is your average speed in miles per hour? 19 | (Hint: there are 1.61 kilometers in a mile). 20 | 21 | """ 22 | 23 | minutes = 43.5 24 | hours = minutes / 60 25 | 26 | km_per_mile = 1.61 27 | km = 10 28 | miles = km / km_per_mile 29 | 30 | pace = minutes / miles 31 | mph = miles / hours 32 | 33 | print('Pace in minutes per mile:', pace) 34 | print('Average speed in mph:', mph) 35 | -------------------------------------------------------------------------------- /code/palindrome_soln.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def first(word): 16 | """Returns the first character of a string.""" 17 | return word[0] 18 | 19 | 20 | def last(word): 21 | """Returns the last of a string.""" 22 | return word[-1] 23 | 24 | 25 | def middle(word): 26 | """Returns all but the first and last characters of a string.""" 27 | return word[1:-1] 28 | 29 | 30 | def is_palindrome(word): 31 | """Returns True if word is a palindrome.""" 32 | if len(word) <= 1: 33 | return True 34 | if first(word) != last(word): 35 | return False 36 | return is_palindrome(middle(word)) 37 | 38 | 39 | print(is_palindrome('allen')) 40 | print(is_palindrome('bob')) 41 | print(is_palindrome('otto')) 42 | print(is_palindrome('redivider')) 43 | 44 | -------------------------------------------------------------------------------- /code/pi.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import math 15 | 16 | 17 | def factorial(n): 18 | """Computes factorial of n recursively.""" 19 | if n == 0: 20 | return 1 21 | else: 22 | recurse = factorial(n-1) 23 | result = n * recurse 24 | return result 25 | 26 | 27 | def estimate_pi(): 28 | """Computes an estimate of pi. 29 | 30 | Algorithm due to Srinivasa Ramanujan, from 31 | http://en.wikipedia.org/wiki/Pi 32 | """ 33 | total = 0 34 | k = 0 35 | factor = 2 * math.sqrt(2) / 9801 36 | while True: 37 | num = factorial(4*k) * (1103 + 26390*k) 38 | den = factorial(k)**4 * 396**(4*k) 39 | term = factor * num / den 40 | total += term 41 | 42 | if abs(term) < 1e-15: 43 | break 44 | k += 1 45 | 46 | return 1 / total 47 | 48 | print(estimate_pi()) 49 | -------------------------------------------------------------------------------- /code/pie.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import math 15 | import turtle 16 | 17 | 18 | def draw_pie(t, n, r): 19 | """Draws a pie, then moves into position to the right. 20 | 21 | t: Turtle 22 | n: number of segments 23 | r: length of the radial spokes 24 | """ 25 | polypie(t, n, r) 26 | t.pu() 27 | t.fd(r*2 + 10) 28 | t.pd() 29 | 30 | 31 | def polypie(t, n, r): 32 | """Draws a pie divided into radial segments. 33 | 34 | t: Turtle 35 | n: number of segments 36 | r: length of the radial spokes 37 | """ 38 | angle = 360.0 / n 39 | for i in range(n): 40 | isosceles(t, r, angle/2) 41 | t.lt(angle) 42 | 43 | 44 | def isosceles(t, r, angle): 45 | """Draws an icosceles triangle. 46 | 47 | The turtle starts and ends at the peak, facing the middle of the base. 48 | 49 | t: Turtle 50 | r: length of the equal legs 51 | angle: peak angle in degrees 52 | """ 53 | y = r * math.sin(angle * math.pi / 180) 54 | 55 | t.rt(angle) 56 | t.fd(r) 57 | t.lt(90+angle) 58 | t.fd(2*y) 59 | t.lt(90+angle) 60 | t.fd(r) 61 | t.lt(180-angle) 62 | 63 | 64 | bob = turtle.Turtle() 65 | 66 | bob.pu() 67 | bob.bk(130) 68 | bob.pd() 69 | 70 | # draw polypies with various number of sides 71 | size = 40 72 | draw_pie(bob, 5, size) 73 | draw_pie(bob, 6, size) 74 | draw_pie(bob, 7, size) 75 | draw_pie(bob, 8, size) 76 | 77 | bob.hideturtle() 78 | turtle.mainloop() 79 | 80 | -------------------------------------------------------------------------------- /code/polygon.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import math 15 | import turtle 16 | 17 | 18 | def square(t, length): 19 | """Draws a square with sides of the given length. 20 | 21 | Returns the Turtle to the starting position and location. 22 | """ 23 | for i in range(4): 24 | t.fd(length) 25 | t.lt(90) 26 | 27 | 28 | def polyline(t, n, length, angle): 29 | """Draws n line segments. 30 | 31 | t: Turtle object 32 | n: number of line segments 33 | length: length of each segment 34 | angle: degrees between segments 35 | """ 36 | for i in range(n): 37 | t.fd(length) 38 | t.lt(angle) 39 | 40 | 41 | def polygon(t, n, length): 42 | """Draws a polygon with n sides. 43 | 44 | t: Turtle 45 | n: number of sides 46 | length: length of each side. 47 | """ 48 | angle = 360.0/n 49 | polyline(t, n, length, angle) 50 | 51 | 52 | def arc(t, r, angle): 53 | """Draws an arc with the given radius and angle. 54 | 55 | t: Turtle 56 | r: radius 57 | angle: angle subtended by the arc, in degrees 58 | """ 59 | arc_length = 2 * math.pi * r * abs(angle) / 360 60 | n = int(arc_length / 4) + 1 61 | step_length = arc_length / n 62 | step_angle = float(angle) / n 63 | 64 | # making a slight left turn before starting reduces 65 | # the error caused by the linear approximation of the arc 66 | t.lt(step_angle/2) 67 | polyline(t, n, step_length, step_angle) 68 | t.rt(step_angle/2) 69 | 70 | 71 | def circle(t, r): 72 | """Draws a circle with the given radius. 73 | 74 | t: Turtle 75 | r: radius 76 | """ 77 | arc(t, r, 360) 78 | 79 | 80 | # the following condition checks whether we are 81 | # running as a script, in which case run the test code, 82 | # or being imported, in which case don't. 83 | 84 | if __name__ == '__main__': 85 | bob = turtle.Turtle() 86 | 87 | # draw a circle centered on the origin 88 | radius = 100 89 | bob.pu() 90 | bob.fd(radius) 91 | bob.lt(90) 92 | bob.pd() 93 | circle(bob, radius) 94 | 95 | # wait for the user to close the window 96 | turtle.mainloop() 97 | -------------------------------------------------------------------------------- /code/pronounce.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def read_dictionary(filename='c06d'): 16 | """Reads from a file and builds a dictionary that maps from 17 | each word to a string that describes its primary pronunciation. 18 | 19 | Secondary pronunciations are added to the dictionary with 20 | a number, in parentheses, at the end of the key, so the 21 | key for the second pronunciation of "abdominal" is "abdominal(2)". 22 | 23 | filename: string 24 | returns: map from string to pronunciation 25 | """ 26 | d = dict() 27 | fin = open(filename) 28 | for line in fin: 29 | 30 | # skip over the comments 31 | if line[0] == '#': continue 32 | 33 | t = line.split() 34 | word = t[0].lower() 35 | pron = ' '.join(t[1:]) 36 | d[word] = pron 37 | 38 | return d 39 | 40 | 41 | if __name__ == '__main__': 42 | d = read_dictionary() 43 | for k, v in d.items(): 44 | print(k, v) 45 | -------------------------------------------------------------------------------- /code/reducible.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def make_word_dict(): 16 | """Reads a word list and returns a dictionary.""" 17 | d = dict() 18 | fin = open('words.txt') 19 | for line in fin: 20 | word = line.strip().lower() 21 | d[word] = None 22 | 23 | # have to add single letter words to the word list; 24 | # also, the empty string is considered a word. 25 | for letter in ['a', 'i', '']: 26 | d[letter] = letter 27 | return d 28 | 29 | 30 | """memo is a dictionary that maps from each word that is known 31 | to be reducible to a list of its reducible children. It starts 32 | with the empty string.""" 33 | 34 | memo = {} 35 | memo[''] = [''] 36 | 37 | 38 | def is_reducible(word, word_dict): 39 | """If word is reducible, returns a list of its reducible children. 40 | 41 | Also adds an entry to the memo dictionary. 42 | 43 | A string is reducible if it has at least one child that is 44 | reducible. The empty string is also reducible. 45 | 46 | word: string 47 | word_dict: dictionary with words as keys 48 | """ 49 | # if have already checked this word, return the answer 50 | if word in memo: 51 | return memo[word] 52 | 53 | # check each of the children and make a list of the reducible ones 54 | res = [] 55 | for child in children(word, word_dict): 56 | if is_reducible(child, word_dict): 57 | res.append(child) 58 | 59 | # memoize and return the result 60 | memo[word] = res 61 | return res 62 | 63 | 64 | def children(word, word_dict): 65 | """Returns a list of all words that can be formed by removing one letter. 66 | 67 | word: string 68 | 69 | Returns: list of strings 70 | """ 71 | res = [] 72 | for i in range(len(word)): 73 | child = word[:i] + word[i+1:] 74 | if child in word_dict: 75 | res.append(child) 76 | return res 77 | 78 | 79 | def all_reducible(word_dict): 80 | """Checks all words in the word_dict; returns a list reducible ones. 81 | 82 | word_dict: dictionary with words as keys 83 | """ 84 | res = [] 85 | for word in word_dict: 86 | t = is_reducible(word, word_dict) 87 | if t != []: 88 | res.append(word) 89 | return res 90 | 91 | 92 | def print_trail(word): 93 | """Prints the sequence of words that reduces this word to the empty string. 94 | 95 | If there is more than one choice, it chooses the first. 96 | 97 | word: string 98 | """ 99 | if len(word) == 0: 100 | return 101 | print(word, end=' ') 102 | t = is_reducible(word, word_dict) 103 | print_trail(t[0]) 104 | 105 | 106 | def print_longest_words(word_dict): 107 | """Finds the longest reducible words and prints them. 108 | 109 | word_dict: dictionary of valid words 110 | """ 111 | words = all_reducible(word_dict) 112 | 113 | # use DSU to sort by word length 114 | t = [] 115 | for word in words: 116 | t.append((len(word), word)) 117 | t.sort(reverse=True) 118 | 119 | # print the longest 5 words 120 | for _, word in t[0:5]: 121 | print_trail(word) 122 | print('\n') 123 | 124 | 125 | if __name__ == '__main__': 126 | word_dict = make_word_dict() 127 | print_longest_words(word_dict) 128 | -------------------------------------------------------------------------------- /code/reverse_pair.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | from inlist import in_bisect, make_word_list 16 | 17 | 18 | def reverse_pair(word_list, word): 19 | """Checks whether a reversed word appears in word_list. 20 | 21 | word_list: list of strings 22 | word: string 23 | """ 24 | rev_word = word[::-1] 25 | return in_bisect(word_list, rev_word) 26 | 27 | 28 | if __name__ == '__main__': 29 | word_list = make_word_list() 30 | 31 | for word in word_list: 32 | if reverse_pair(word_list, word): 33 | print(word, word[::-1]) 34 | 35 | 36 | -------------------------------------------------------------------------------- /code/rotate.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def rotate_letter(letter, n): 16 | """Rotates a letter by n places. Does not change other chars. 17 | 18 | letter: single-letter string 19 | n: int 20 | 21 | Returns: single-letter string 22 | """ 23 | if letter.isupper(): 24 | start = ord('A') 25 | elif letter.islower(): 26 | start = ord('a') 27 | else: 28 | return letter 29 | 30 | c = ord(letter) - start 31 | i = (c + n) % 26 + start 32 | return chr(i) 33 | 34 | 35 | def rotate_word(word, n): 36 | """Rotates a word by n places. 37 | 38 | word: string 39 | n: integer 40 | 41 | Returns: string 42 | """ 43 | res = '' 44 | for letter in word: 45 | res += rotate_letter(letter, n) 46 | return res 47 | 48 | 49 | if __name__ == '__main__': 50 | print(rotate_word('cheer', 7)) 51 | print(rotate_word('melon', -10)) 52 | print(rotate_word('sleep', 9)) 53 | -------------------------------------------------------------------------------- /code/rotate_pairs.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | from rotate import rotate_word 15 | 16 | 17 | def make_word_dict(): 18 | """Read the words in words.txt and return a dictionary 19 | that contains the words as keys""" 20 | d = dict() 21 | fin = open('words.txt') 22 | for line in fin: 23 | word = line.strip().lower() 24 | d[word] = None 25 | 26 | return d 27 | 28 | 29 | def rotate_pairs(word, word_dict): 30 | """Prints all words that can be generated by rotating word. 31 | 32 | word: string 33 | word_dict: dictionary with words as keys 34 | """ 35 | for i in range(1, 14): 36 | rotated = rotate_word(word, i) 37 | if rotated in word_dict: 38 | print(word, i, rotated) 39 | 40 | 41 | if __name__ == '__main__': 42 | word_dict = make_word_dict() 43 | 44 | for word in word_dict: 45 | rotate_pairs(word, word_dict) 46 | -------------------------------------------------------------------------------- /code/run_all_solutions.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import os 15 | 16 | 17 | def pipe(cmd): 18 | """Runs a command in a subprocess. 19 | 20 | cmd: string Unix command 21 | 22 | Returns (res, stat), the output of the subprocess and the exit status. 23 | """ 24 | # Note: os.popen is deprecated 25 | # now, which means we are supposed to stop using it and start using 26 | # the subprocess module. But for simple cases, I find 27 | # subprocess more complicated than necessary. So I am going 28 | # to keep using os.popen until they take it away. 29 | 30 | fp = os.popen(cmd) 31 | res = fp.read() 32 | stat = fp.close() 33 | assert stat is None 34 | return res, stat 35 | 36 | 37 | filenames = """ 38 | ackermann_memo.py grid.py PokerHand.py 39 | ackermann.py has_duplicates.py PokerHandSoln.py 40 | anagram_db.py header.py polygon.py 41 | anagram_sets.py inlist.py reducible.py 42 | analyze_book1.py interlock.py reverse_pair.py 43 | analyze_book2.py invert_dict.py rotate_pairs.py 44 | analyze_book3.py koch.py rotate.py 45 | BadKangaroo.py letters.py sed.py 46 | birthday.py Map.py spiral.py 47 | Card.py markov.py structshape.py 48 | cartalk1.py Markov.py Time1.py 49 | cartalk2.py metathesis.py Time1_soln.py 50 | cartalk3.py most_frequent.py Time2.py 51 | do_four.py pace_calc.py Time2_soln.py 52 | palindrome_soln.py typewriter.py 53 | find_duplicates_copy.py pie.py unstable_sort.py 54 | find_duplicates.py pi.py walk.py 55 | flower.py Point1.py wordlist.py 56 | GoodKangaroo.py Point1_soln.py zipf.py 57 | """ 58 | 59 | slow_ones = """ 60 | spiral.py typewriter.py pie.py 61 | flower.py wordlist.py polygon.py 62 | koch.py letters.py zipf.py 63 | """.split() 64 | 65 | 66 | for filename in filenames.split(): 67 | print(filename) 68 | if filename in slow_ones: 69 | print('Skipping') 70 | continue 71 | 72 | res, stat = pipe('python ' + filename) 73 | print(stat) 74 | 75 | -------------------------------------------------------------------------------- /code/sed.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | 15 | def sed(pattern, replace, source, dest): 16 | """Reads a source file and writes the destination file. 17 | 18 | In each line, replaces pattern with replace. 19 | 20 | pattern: string 21 | replace: string 22 | source: string filename 23 | dest: string filename 24 | """ 25 | fin = open(source, 'r') 26 | fout = open(dest, 'w') 27 | 28 | for line in fin: 29 | line = line.replace(pattern, replace) 30 | fout.write(line) 31 | 32 | fin.close() 33 | fout.close() 34 | 35 | 36 | def main(): 37 | pattern = 'pattern' 38 | replace = 'replace' 39 | source = 'sed_tester.txt' 40 | dest = source + '.replaced' 41 | sed(pattern, replace, source, dest) 42 | 43 | 44 | if __name__ == '__main__': 45 | main() 46 | -------------------------------------------------------------------------------- /code/spiral.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import turtle 15 | 16 | 17 | def draw_spiral(t, n, length=3, a=0.1, b=0.0002): 18 | """Draws an Archimedian spiral starting at the origin. 19 | 20 | Args: 21 | n: how many line segments to draw 22 | length: how long each segment is 23 | a: how loose the initial spiral starts out (larger is looser) 24 | b: how loosly coiled the spiral is (larger is looser) 25 | 26 | http://en.wikipedia.org/wiki/Spiral 27 | """ 28 | theta = 0.0 29 | 30 | for i in range(n): 31 | t.fd(length) 32 | dtheta = 1 / (a + b * theta) 33 | 34 | t.lt(dtheta) 35 | theta += dtheta 36 | 37 | 38 | # create the world and bob 39 | bob = turtle.Turtle() 40 | draw_spiral(bob, n=1000) 41 | 42 | turtle.mainloop() 43 | 44 | -------------------------------------------------------------------------------- /code/structshape.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | """ 15 | This module provides one function, structshape(), which takes 16 | an object of any type and returns a string that summarizes the 17 | "shape" of the data structure; that is, the type, size and 18 | composition. 19 | """ 20 | 21 | def structshape(ds): 22 | """Returns a string that describes the shape of a data structure. 23 | 24 | ds: any Python object 25 | 26 | Returns: string 27 | """ 28 | typename = type(ds).__name__ 29 | 30 | # handle sequences 31 | sequence = (list, tuple, set, type(iter(''))) 32 | if isinstance(ds, sequence): 33 | t = [] 34 | for i, x in enumerate(ds): 35 | t.append(structshape(x)) 36 | rep = '%s of %s' % (typename, listrep(t)) 37 | return rep 38 | 39 | # handle dictionaries 40 | elif isinstance(ds, dict): 41 | keys = set() 42 | vals = set() 43 | for k, v in ds.items(): 44 | keys.add(structshape(k)) 45 | vals.add(structshape(v)) 46 | rep = '%s of %d %s->%s' % (typename, len(ds), 47 | setrep(keys), setrep(vals)) 48 | return rep 49 | 50 | # handle other types 51 | else: 52 | if hasattr(ds, '__class__'): 53 | return ds.__class__.__name__ 54 | else: 55 | return typename 56 | 57 | 58 | def listrep(t): 59 | """Returns a string representation of a list of type strings. 60 | 61 | t: list of strings 62 | 63 | Returns: string 64 | """ 65 | current = t[0] 66 | count = 0 67 | res = [] 68 | for x in t: 69 | if x == current: 70 | count += 1 71 | else: 72 | append(res, current, count) 73 | current = x 74 | count = 1 75 | append(res, current, count) 76 | return setrep(res) 77 | 78 | 79 | def setrep(s): 80 | """Returns a string representation of a set of type strings. 81 | 82 | s: set of strings 83 | 84 | Returns: string 85 | """ 86 | rep = ', '.join(s) 87 | if len(s) == 1: 88 | return rep 89 | else: 90 | return '(' + rep + ')' 91 | return 92 | 93 | 94 | def append(res, typestr, count): 95 | """Adds a new element to a list of type strings. 96 | 97 | Modifies res. 98 | 99 | res: list of type strings 100 | typestr: the new type string 101 | count: how many of the new type there are 102 | 103 | Returns: None 104 | """ 105 | if count == 1: 106 | rep = typestr 107 | else: 108 | rep = '%d %s' % (count, typestr) 109 | res.append(rep) 110 | 111 | 112 | if __name__ == '__main__': 113 | 114 | t = [1, 2, 3] 115 | print(structshape(t)) 116 | 117 | t2 = [[1, 2], [3, 4], [5, 6]] 118 | print(structshape(t2)) 119 | 120 | t3 = [1, 2, 3, 4.0, '5', '6', [7], [8], 9] 121 | print(structshape(t3)) 122 | 123 | class Point: 124 | """trivial object type""" 125 | 126 | t4 = [Point(), Point()] 127 | print(structshape(t4)) 128 | 129 | s = set('abc') 130 | print(structshape(s)) 131 | 132 | lt = zip(t, s) 133 | print(structshape(lt)) 134 | 135 | d = dict(lt) 136 | print(structshape(d)) 137 | 138 | it = iter('abc') 139 | print(structshape(it)) 140 | -------------------------------------------------------------------------------- /code/typewriter.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import string 15 | import turtle 16 | 17 | """ 18 | To use this typewriter, you have to provide a module named letters.py 19 | that contains functions with names like draw_a, draw_b, etc. 20 | """ 21 | 22 | # check if the reader has provided letters.py 23 | try: 24 | import letters 25 | except ImportError as e: 26 | message = e.args[0] 27 | if message.startswith('No module'): 28 | raise ImportError(message + 29 | '\nYou have to provide a module named letters.py') 30 | 31 | 32 | def teleport(t, x, y): 33 | """Moves the turtle without drawing a line. 34 | 35 | Postcondition: pen is down 36 | 37 | t: Turtle 38 | x: coordinate 39 | y: coordinate 40 | """ 41 | t.pu() 42 | t.goto(x, y) 43 | t.pd() 44 | 45 | 46 | def keypress(char): 47 | """Handles the event when a user presses a key. 48 | 49 | Checks if there is a function with the right name; otherwise 50 | it prints an error message. 51 | 52 | char: string, letter to draw 53 | """ 54 | # if we're still drawing the previous letter, bail out 55 | if bob.busy: 56 | return 57 | else: 58 | bob.busy = True 59 | 60 | # figure out which function to call, and call it 61 | try: 62 | name = 'draw_' + char 63 | func = getattr(letters, name) 64 | except AttributeError: 65 | print("I don't know how to draw an", char) 66 | bob.busy = False 67 | return 68 | 69 | func(bob, size) 70 | 71 | letters.skip(bob, size/2) 72 | bob.busy = False 73 | 74 | 75 | def carriage_return(): 76 | """Moves to the beginning of the next line. 77 | """ 78 | teleport(bob, -180, bob.ycor() - size*3) 79 | bob.busy = False 80 | 81 | 82 | def presser(char): 83 | """Returns a function object that executes keypress. 84 | 85 | char: character to draw when the function is executed 86 | 87 | returns: function with no arguments 88 | """ 89 | def func(): 90 | keypress(char) 91 | return func 92 | 93 | 94 | # create and position the turtle 95 | size = 20 96 | bob = turtle.Turtle() 97 | bob.busy = False 98 | teleport(bob, -180, 150) 99 | 100 | # tell world to call keypress when the user presses a key 101 | screen = bob.getscreen() 102 | 103 | for char in string.ascii_lowercase: 104 | screen.onkey(presser(char), char) 105 | 106 | screen.onkey(carriage_return, 'Return') 107 | 108 | screen.listen() 109 | turtle.mainloop() 110 | -------------------------------------------------------------------------------- /code/unstable_sort.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import random 15 | 16 | 17 | def sort_by_length(words): 18 | """Sort a list of words in reverse order by length. 19 | 20 | This is the version in the book; it is stable in the sense that 21 | words with the same length appear in the same order 22 | 23 | words: list of strings 24 | 25 | Returns: list of strings 26 | """ 27 | t = [] 28 | for word in words: 29 | t.append((len(word), word)) 30 | 31 | t.sort(reverse=True) 32 | 33 | res = [] 34 | for length, word in t: 35 | res.append(word) 36 | return res 37 | 38 | 39 | def sort_by_length_random(words): 40 | """Sort a list of words in reverse order by length. 41 | 42 | This is the solution to the exercise. It is unstable in the 43 | sense that if two words have the same length, their order in 44 | the output list is random. 45 | 46 | It works by extending the list of tuples with a column of 47 | random numbers; when there is a tie in the first column, 48 | the random column determines the output order. 49 | 50 | words: list of strings 51 | 52 | Returns: list of strings 53 | """ 54 | t = [] 55 | for word in words: 56 | t.append((len(word), random.random(), word)) 57 | 58 | t.sort(reverse=True) 59 | 60 | res = [] 61 | for length, _, word in t: 62 | res.append(word) 63 | return res 64 | 65 | 66 | if __name__ == '__main__': 67 | words = ['John', 'Eric', 'Graham', 'Terry', 'Terry', 'Michael'] 68 | 69 | t = sort_by_length_random(words) 70 | for x in t: 71 | print(x) 72 | -------------------------------------------------------------------------------- /code/walk.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import os 15 | 16 | 17 | def walk(dirname): 18 | """Prints the names of all files in dirname and its subdirectories. 19 | 20 | This is the version in the book. 21 | 22 | dirname: string name of directory 23 | """ 24 | for name in os.listdir(dirname): 25 | path = os.path.join(dirname, name) 26 | 27 | if os.path.isfile(path): 28 | print(path) 29 | else: 30 | walk(path) 31 | 32 | 33 | def walk2(dirname): 34 | """Prints the names of all files in dirname and its subdirectories. 35 | 36 | This is the exercise solution, which uses os.walk. 37 | 38 | dirname: string name of directory 39 | """ 40 | for root, dirs, files in os.walk(dirname): 41 | for filename in files: 42 | print(os.path.join(root, filename)) 43 | 44 | 45 | if __name__ == '__main__': 46 | walk('.') 47 | walk2('.') 48 | 49 | -------------------------------------------------------------------------------- /code/wordlist.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import time 15 | 16 | 17 | def make_word_list1(): 18 | """Reads lines from a file and builds a list using append.""" 19 | t = [] 20 | fin = open('words.txt') 21 | for line in fin: 22 | word = line.strip() 23 | t.append(word) 24 | return t 25 | 26 | 27 | def make_word_list2(): 28 | """Reads lines from a file and builds a list using list +.""" 29 | t = [] 30 | fin = open('words.txt') 31 | for line in fin: 32 | word = line.strip() 33 | t = t + [word] 34 | return t 35 | 36 | 37 | start_time = time.time() 38 | t = make_word_list1() 39 | elapsed_time = time.time() - start_time 40 | 41 | print(len(t)) 42 | print(t[:10]) 43 | print(elapsed_time, 'seconds') 44 | 45 | start_time = time.time() 46 | t = make_word_list2() 47 | elapsed_time = time.time() - start_time 48 | 49 | print(len(t)) 50 | print(t[:10]) 51 | print(elapsed_time, 'seconds') 52 | 53 | -------------------------------------------------------------------------------- /code/zipf.py: -------------------------------------------------------------------------------- 1 | """This module contains a code example related to 2 | 3 | Think Python, 2nd Edition 4 | by Allen Downey 5 | http://thinkpython2.com 6 | 7 | Copyright 2015 Allen Downey 8 | 9 | License: http://creativecommons.org/licenses/by/4.0/ 10 | """ 11 | 12 | from __future__ import print_function, division 13 | 14 | import sys 15 | 16 | import matplotlib.pyplot as plt 17 | 18 | from analyze_book1 import process_file 19 | 20 | 21 | def rank_freq(hist): 22 | """Returns a list of (rank, freq) tuples. 23 | 24 | hist: map from word to frequency 25 | 26 | returns: list of (rank, freq) tuples 27 | """ 28 | # sort the list of frequencies in decreasing order 29 | freqs = list(hist.values()) 30 | freqs.sort(reverse=True) 31 | 32 | # enumerate the ranks and frequencies 33 | rf = [(r+1, f) for r, f in enumerate(freqs)] 34 | return rf 35 | 36 | 37 | def print_ranks(hist): 38 | """Prints the rank vs. frequency data. 39 | 40 | hist: map from word to frequency 41 | """ 42 | for r, f in rank_freq(hist): 43 | print(r, f) 44 | 45 | 46 | def plot_ranks(hist, scale='log'): 47 | """Plots frequency vs. rank. 48 | 49 | hist: map from word to frequency 50 | scale: string 'linear' or 'log' 51 | """ 52 | t = rank_freq(hist) 53 | rs, fs = zip(*t) 54 | 55 | plt.clf() 56 | plt.xscale(scale) 57 | plt.yscale(scale) 58 | plt.title('Zipf plot') 59 | plt.xlabel('rank') 60 | plt.ylabel('frequency') 61 | plt.plot(rs, fs, 'r-', linewidth=3) 62 | plt.show() 63 | 64 | 65 | def main(script, filename='emma.txt', flag='plot'): 66 | hist = process_file(filename, skip_header=True) 67 | 68 | # either print the results or plot them 69 | if flag == 'print': 70 | print_ranks(hist) 71 | elif flag == 'plot': 72 | plot_ranks(hist) 73 | else: 74 | print('Usage: zipf.py filename [print|plot]') 75 | 76 | 77 | if __name__ == '__main__': 78 | main(*sys.argv) 79 | -------------------------------------------------------------------------------- /source/_static/cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/_static/cover.jpg -------------------------------------------------------------------------------- /source/_templates/epub_cover.html: -------------------------------------------------------------------------------- 1 | {% extends "layout.html" %} 2 | {%- block rootrellink %}{% endblock %} 3 | {%- block relbaritems %}{% endblock %} 4 | {%- block sidebarlogo %}{% endblock %} 5 | {%- block linktags %}{% endblock %} 6 | {%- block relbar1 %}{% endblock %} 7 | {%- block sidebar1 %}{% endblock %} 8 | {%- block sidebar2 %}{% endblock %} 9 | {%- block footer %}{% endblock %} 10 | 11 | {% block content %} 12 |

13 | Cover image 14 |
15 | {% endblock %} -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/__init__.py: -------------------------------------------------------------------------------- 1 | """Sphinx ReadTheDocs theme. 2 | 3 | From https://github.com/ryan-roemer/sphinx-bootstrap-theme. 4 | 5 | """ 6 | import os 7 | 8 | VERSION = (0, 1, 9) 9 | 10 | __version__ = ".".join(str(v) for v in VERSION) 11 | __version_full__ = __version__ 12 | 13 | 14 | def get_html_theme_path(): 15 | """Return list of HTML theme paths.""" 16 | cur_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) 17 | return cur_dir 18 | -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/breadcrumbs.html: -------------------------------------------------------------------------------- 1 | {# Support for Sphinx 1.3+ page_source_suffix, but don't break old builds. #} 2 | 3 | {% if page_source_suffix %} 4 | {% set suffix = page_source_suffix %} 5 | {% else %} 6 | {% set suffix = source_suffix %} 7 | {% endif %} 8 | 9 |
10 | 31 |
32 |
33 | -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/epub-layout.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | {{ metatags }} 9 | 10 | {%- block htmltitle %} 11 | {{ title|striptags|e }}{{ titlesuffix }} 12 | {%- endblock %} 13 | 14 | 15 | 16 | {%- for cssfile in css_files %} 17 | 18 | {%- endfor %} 19 | 20 | 21 | 22 | 23 | {# Omit the top navigation bar. #} 24 | {% block relbar1 %} 25 | {% endblock %} 26 | 27 | {%- block content %} 28 | 29 |
s 30 | 31 | {%- block document %} 32 | 33 |
34 | {% block body %} {% endblock %} 35 |
36 | {%- endblock %} 37 |
38 | {%- endblock %} 39 | 40 | {# Omit the bottom navigation bar. #} 41 | {% block relbar2 %} 42 | {% endblock %} 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/footer.html: -------------------------------------------------------------------------------- 1 |
2 | {% if next or prev %} 3 | 11 | {% endif %} 12 | 13 |
14 | 15 |
16 |

17 | {%- if show_copyright %} 18 | {%- if hasdoc('copyright') %} 19 | {% trans path=pathto('copyright'), copyright=copyright|e %}© Copyright {{ copyright }}.{% endtrans %} 20 | {%- else %} 21 | {% trans copyright=copyright|e %}© Copyright {{ copyright }}.{% endtrans %} 22 | {%- endif %} 23 | {%- endif %} 24 | 25 | {%- if build_id and build_url %} 26 | {% trans build_url=build_url, build_id=build_id %} 27 | 28 | Build 29 | {{ build_id }}. 30 | 31 | {% endtrans %} 32 | {%- elif commit %} 33 | {% trans commit=commit %} 34 | 35 | Revision {{ commit }}. 36 | 37 | {% endtrans %} 38 | {%- elif last_updated %} 39 | {% trans last_updated=last_updated|e %}Last updated on {{ last_updated }}.{% endtrans %} 40 | {%- endif %} 41 | 42 |

43 |
44 | 45 | {%- if show_sphinx %} 46 | {% trans %}Built with Sphinx using a theme provided by Read the Docs{% endtrans %}. 47 | {%- endif %} 48 | 49 | {%- block extrafooter %} {% endblock %} 50 | 51 |
52 | 53 | -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/search.html: -------------------------------------------------------------------------------- 1 | {# 2 | basic/search.html 3 | ~~~~~~~~~~~~~~~~~ 4 | 5 | Template for the search page. 6 | 7 | :copyright: Copyright 2007-2013 by the Sphinx team, see AUTHORS. 8 | :license: BSD, see LICENSE for details. 9 | #} 10 | {%- extends "layout.html" %} 11 | {% set title = _('Search') %} 12 | {% set script_files = script_files + ['_static/searchtools.js'] %} 13 | {% block footer %} 14 | 17 | {# this is used when loading the search index using $.ajax fails, 18 | such as on Chrome for documents on localhost #} 19 | 20 | {{ super() }} 21 | {% endblock %} 22 | {% block body %} 23 | 31 | 32 | {% if search_performed %} 33 |

{{ _('Search Results') }}

34 | {% if not search_results %} 35 |

{{ _('Your search did not match any documents. Please make sure that all words are spelled correctly and that you\'ve selected enough categories.') }}

36 | {% endif %} 37 | {% endif %} 38 |
39 | {% if search_results %} 40 |
    41 | {% for href, caption, context in search_results %} 42 |
  • 43 | {{ caption }} 44 |

    {{ context|e }}

    45 |
  • 46 | {% endfor %} 47 |
48 | {% endif %} 49 |
50 | {% endblock %} 51 | -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/searchbox.html: -------------------------------------------------------------------------------- 1 | {%- if builder != 'singlehtml' %} 2 |
3 |
4 | 5 | 6 | 7 |
8 |
9 | {%- endif %} 10 | -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/static/css/badge_only.css: -------------------------------------------------------------------------------- 1 | .fa:before{-webkit-font-smoothing:antialiased}.clearfix{*zoom:1}.clearfix:before,.clearfix:after{display:table;content:""}.clearfix:after{clear:both}@font-face{font-family:FontAwesome;font-weight:normal;font-style:normal;src:url("../font/fontawesome_webfont.eot");src:url("../font/fontawesome_webfont.eot?#iefix") format("embedded-opentype"),url("../font/fontawesome_webfont.woff") format("woff"),url("../font/fontawesome_webfont.ttf") format("truetype"),url("../font/fontawesome_webfont.svg#FontAwesome") format("svg")}.fa:before{display:inline-block;font-family:FontAwesome;font-style:normal;font-weight:normal;line-height:1;text-decoration:inherit}a .fa{display:inline-block;text-decoration:inherit}li .fa{display:inline-block}li .fa-large:before,li .fa-large:before{width:1.875em}ul.fas{list-style-type:none;margin-left:2em;text-indent:-0.8em}ul.fas li .fa{width:0.8em}ul.fas li .fa-large:before,ul.fas li .fa-large:before{vertical-align:baseline}.fa-book:before{content:""}.icon-book:before{content:""}.fa-caret-down:before{content:""}.icon-caret-down:before{content:""}.fa-caret-up:before{content:""}.icon-caret-up:before{content:""}.fa-caret-left:before{content:""}.icon-caret-left:before{content:""}.fa-caret-right:before{content:""}.icon-caret-right:before{content:""}.rst-versions{position:fixed;bottom:0;left:0;width:300px;color:#fcfcfc;background:#1f1d1d;border-top:solid 10px #343131;font-family:"Lato","proxima-nova","Helvetica Neue",Arial,sans-serif;z-index:400}.rst-versions a{color:#2980B9;text-decoration:none}.rst-versions .rst-badge-small{display:none}.rst-versions .rst-current-version{padding:12px;background-color:#272525;display:block;text-align:right;font-size:90%;cursor:pointer;color:#27AE60;*zoom:1}.rst-versions .rst-current-version:before,.rst-versions .rst-current-version:after{display:table;content:""}.rst-versions .rst-current-version:after{clear:both}.rst-versions .rst-current-version .fa{color:#fcfcfc}.rst-versions .rst-current-version .fa-book{float:left}.rst-versions .rst-current-version .icon-book{float:left}.rst-versions .rst-current-version.rst-out-of-date{background-color:#E74C3C;color:#fff}.rst-versions .rst-current-version.rst-active-old-version{background-color:#F1C40F;color:#000}.rst-versions.shift-up .rst-other-versions{display:block}.rst-versions .rst-other-versions{font-size:90%;padding:12px;color:gray;display:none}.rst-versions .rst-other-versions hr{display:block;height:1px;border:0;margin:20px 0;padding:0;border-top:solid 1px #413d3d}.rst-versions .rst-other-versions dd{display:inline-block;margin:0}.rst-versions .rst-other-versions dd a{display:inline-block;padding:6px;color:#fcfcfc}.rst-versions.rst-badge{width:auto;bottom:20px;right:20px;left:auto;border:none;max-width:300px}.rst-versions.rst-badge .icon-book{float:none}.rst-versions.rst-badge .fa-book{float:none}.rst-versions.rst-badge.shift-up .rst-current-version{text-align:right}.rst-versions.rst-badge.shift-up .rst-current-version .fa-book{float:left}.rst-versions.rst-badge.shift-up .rst-current-version .icon-book{float:left}.rst-versions.rst-badge .rst-current-version{width:auto;height:30px;line-height:30px;padding:0 6px;display:block;text-align:center}@media screen and (max-width: 768px){.rst-versions{width:85%;display:none}.rst-versions.shift{display:block}img{width:100%;height:auto}} 2 | /*# sourceMappingURL=badge_only.css.map */ 3 | -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/static/fonts/Inconsolata-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/_theme/sphinx_rtd_theme/static/fonts/Inconsolata-Bold.ttf -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/static/fonts/Inconsolata-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/_theme/sphinx_rtd_theme/static/fonts/Inconsolata-Regular.ttf -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/static/fonts/Lato-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/_theme/sphinx_rtd_theme/static/fonts/Lato-Bold.ttf -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/static/fonts/Lato-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/_theme/sphinx_rtd_theme/static/fonts/Lato-Regular.ttf -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/static/fonts/RobotoSlab-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/_theme/sphinx_rtd_theme/static/fonts/RobotoSlab-Bold.ttf -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/static/fonts/RobotoSlab-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/_theme/sphinx_rtd_theme/static/fonts/RobotoSlab-Regular.ttf -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/static/fonts/fontawesome-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/_theme/sphinx_rtd_theme/static/fonts/fontawesome-webfont.eot -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/static/fonts/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/_theme/sphinx_rtd_theme/static/fonts/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/static/fonts/fontawesome-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/_theme/sphinx_rtd_theme/static/fonts/fontawesome-webfont.woff -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/theme.conf: -------------------------------------------------------------------------------- 1 | [theme] 2 | inherit = basic 3 | stylesheet = css/theme.css 4 | 5 | [options] 6 | typekit_id = hiw1hhg 7 | analytics_id = 8 | sticky_navigation = False 9 | logo_only = 10 | collapse_navigation = False 11 | display_version = True 12 | -------------------------------------------------------------------------------- /source/_theme/sphinx_rtd_theme/versions.html: -------------------------------------------------------------------------------- 1 | {% if READTHEDOCS %} 2 | {# Add rst-badge after rst-versions for small badge style. #} 3 |
4 | 5 | Read the Docs 6 | v: {{ current_version }} 7 | 8 | 9 |
10 |
11 |
Versions
12 | {% for slug, url in versions %} 13 |
{{ slug }}
14 | {% endfor %} 15 |
16 |
17 |
Downloads
18 | {% for type, url in downloads %} 19 |
{{ type }}
20 | {% endfor %} 21 |
22 |
23 |
On Read the Docs
24 |
25 | Project Home 26 |
27 |
28 | Builds 29 |
30 |
31 |
32 | Free document hosting provided by Read the Docs. 33 | 34 |
35 |
36 | {% endif %} 37 | 38 | -------------------------------------------------------------------------------- /source/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/favicon.ico -------------------------------------------------------------------------------- /source/figs/assign2.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5b 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 2760 1339 3135 1467 13 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 14 | 2766 1271 3141 1174 15 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 0 0 2 16 | 3075 1080 3300 1230 17 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 0 0 2 18 | 3300 1080 3075 1230 19 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 20 | 2475 975 3450 975 3450 1650 2475 1650 2475 975 21 | 4 0 0 50 0 16 11 0.0000 4 120 105 3150 1530 7\001 22 | 4 0 0 50 0 16 11 0.0000 4 120 105 3150 1230 5\001 23 | 4 2 0 50 0 16 11 0.0000 4 90 90 2730 1340 x\001 24 | -------------------------------------------------------------------------------- /source/figs/assign2.fig.bak: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 6 2325 1080 3300 1530 11 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 12 | 0 0 1.00 60.00 120.00 13 | 2760 1339 3135 1467 14 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 15 | 2766 1271 3141 1174 16 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 0 0 2 17 | 3075 1080 3300 1230 18 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 0 0 2 19 | 3300 1080 3075 1230 20 | 4 0 0 50 0 16 11 0.0000 4 120 90 3150 1530 7\001 21 | 4 0 0 50 0 16 11 0.0000 4 120 90 3150 1230 5\001 22 | 4 2 0 50 0 16 11 0.0000 4 120 375 2730 1340 bruce\001 23 | -6 24 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 25 | 2175 975 3450 975 3450 1650 2175 1650 2175 975 26 | -------------------------------------------------------------------------------- /source/figs/assign2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/assign2.pdf -------------------------------------------------------------------------------- /source/figs/assign2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/assign2.png -------------------------------------------------------------------------------- /source/figs/back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/back.png -------------------------------------------------------------------------------- /source/figs/banana.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5-alpha5 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 2100 1080 2475 1080 13 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 14 | 2700 825 2700 1500 15 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 16 | 3000 825 3000 1500 17 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 18 | 3300 825 3300 1500 19 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 20 | 3600 825 3600 1500 21 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 22 | 3900 825 3900 1500 23 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 24 | 4200 825 4200 1500 25 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 26 | 4500 825 4500 1500 27 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 28 | 1575 825 4800 825 4800 1350 1575 1350 1575 825 29 | 4 2 0 50 0 16 11 0.0000 4 135 300 2025 1125 fruit\001 30 | 4 0 0 50 0 16 22 0.0000 4 285 210 2775 1200 b\001 31 | 4 0 0 50 0 16 22 0.0000 4 210 195 3075 1200 a\001 32 | 4 0 0 50 0 16 22 0.0000 4 210 210 3375 1200 n\001 33 | 4 0 0 50 0 16 22 0.0000 4 210 210 3975 1200 n\001 34 | 4 0 0 50 0 16 22 0.0000 4 210 195 3675 1200 a\001 35 | 4 0 0 50 0 16 22 0.0000 4 210 195 4275 1200 a\001 36 | 4 0 0 50 0 16 22 0.0000 4 90 90 4575 1200 '\001 37 | 4 0 0 50 0 16 11 0.0000 4 135 105 2625 1650 0\001 38 | 4 0 0 50 0 16 11 0.0000 4 135 105 2925 1650 1\001 39 | 4 0 0 50 0 16 11 0.0000 4 135 105 3225 1650 2\001 40 | 4 0 0 50 0 16 11 0.0000 4 135 105 3525 1650 3\001 41 | 4 0 0 50 0 16 11 0.0000 4 135 105 3825 1650 4\001 42 | 4 0 0 50 0 16 11 0.0000 4 135 105 4125 1650 5\001 43 | 4 0 0 50 0 16 11 0.0000 4 135 105 4425 1650 6\001 44 | 4 0 0 50 0 16 11 0.0000 4 135 450 2025 1650 index\001 45 | 4 0 0 50 0 16 22 0.0000 4 90 90 2550 1200 '\001 46 | -------------------------------------------------------------------------------- /source/figs/banana.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/banana.pdf -------------------------------------------------------------------------------- /source/figs/banana.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/banana.png -------------------------------------------------------------------------------- /source/figs/card1.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 825 1200 1200 1200 13 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 14 | 3150 1050 3525 1050 3525 1425 3150 1425 3150 1050 15 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 16 | 0 0 1.00 60.00 120.00 17 | 2625 1205 3150 1198 18 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 19 | 3150 1800 3525 1800 3525 2175 3150 2175 3150 1800 20 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 21 | 0 0 1.00 60.00 120.00 22 | 2625 1953 3150 1946 23 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 24 | 1200 1050 2850 1050 2850 2175 1200 2175 1200 1050 25 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 26 | 0 0 1.00 60.00 120.00 27 | 1800 3172 2175 3172 28 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 29 | 0 0 1.00 60.00 120.00 30 | 1800 2872 2175 2872 31 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 32 | 1275 2700 2625 2700 2625 3375 1275 3375 1275 2700 33 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 34 | 0 0 1.00 60.00 120.00 35 | 900 2850 1275 2850 36 | 4 0 0 50 0 16 11 0.0000 4 135 225 3150 975 list\001 37 | 4 2 0 50 0 16 11 0.0000 4 165 930 2550 1275 suit_names\001 38 | 4 0 0 50 0 16 11 0.0000 4 135 225 3150 1725 list\001 39 | 4 2 0 50 0 16 11 0.0000 4 165 1005 2550 2025 rank_names\001 40 | 4 2 0 50 0 16 11 0.0000 4 135 405 750 1275 Card\001 41 | 4 0 0 50 0 16 11 0.0000 4 180 360 1200 975 type\001 42 | 4 0 0 50 0 16 11 0.0000 4 135 105 2250 2925 1\001 43 | 4 0 0 50 0 16 11 0.0000 4 135 210 2250 3225 11\001 44 | 4 2 0 50 0 16 11 0.0000 4 135 285 1725 2925 suit\001 45 | 4 2 0 50 0 16 11 0.0000 4 135 360 1725 3225 rank\001 46 | 4 2 0 50 0 16 11 0.0000 4 135 480 825 2925 card1\001 47 | 4 0 0 50 0 16 11 0.0000 4 135 405 1275 2625 Card\001 48 | -------------------------------------------------------------------------------- /source/figs/card1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/card1.pdf -------------------------------------------------------------------------------- /source/figs/card1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/card1.png -------------------------------------------------------------------------------- /source/figs/class1.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5-alpha5 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 11 | 2625 1050 3525 1050 3525 1500 2625 1500 2625 1050 12 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 13 | 2625 2025 3525 2025 3525 2475 2625 2475 2625 2025 14 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 15 | 1 0 1.00 150.00 120.00 16 | 3075 2025 3075 1500 17 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 18 | 0 0 1.00 120.00 120.00 19 | 3525 1275 4200 1275 20 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 21 | 4200 1050 5100 1050 5100 1500 4200 1500 4200 1050 22 | 4 0 0 50 0 16 11 0.0000 4 135 450 2850 2325 Hand\001 23 | 4 0 0 50 0 16 11 0.0000 4 135 435 2850 1350 Deck\001 24 | 4 2 0 50 0 16 14 0.0000 4 75 90 4125 1275 *\001 25 | 4 2 0 50 0 16 11 0.0000 4 135 405 4800 1350 Card\001 26 | -------------------------------------------------------------------------------- /source/figs/class1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/class1.pdf -------------------------------------------------------------------------------- /source/figs/class1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/class1.png -------------------------------------------------------------------------------- /source/figs/compile.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5b 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 90.00 180.00 12 | 3525 750 3854 750 13 | 2 1 0 2 0 7 63 0 -1 0.000 0 0 -1 0 0 4 14 | 3936 655 3840 548 3840 915 3936 813 15 | 2 1 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 4 16 | 5002 1248 5100 1350 5100 984 5002 1089 17 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 18 | 0 0 1.00 90.00 180.00 19 | 5100 1125 5429 1125 20 | 2 2 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 5 21 | 3936 548 5010 548 5010 1342 3936 1342 3936 548 22 | 2 2 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 5 23 | 2700 525 3525 525 3525 1350 2700 1350 2700 525 24 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 25 | 0 0 1.00 90.00 180.00 26 | 892 742 1221 742 27 | 2 1 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 6 28 | 67 442 742 442 892 592 892 1342 67 1342 67 442 29 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 0 0 3 30 | 742 442 742 592 892 592 31 | 2 2 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 5 32 | 1304 540 2274 540 2274 1327 1304 1327 1304 540 33 | 2 1 0 2 0 7 63 0 -1 0.000 0 0 -1 0 0 4 34 | 1304 647 1207 540 1207 907 1304 805 35 | 2 1 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 4 36 | 2274 1225 2371 1327 2371 961 2274 1066 37 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 38 | 0 0 1.00 90.00 180.00 39 | 2377 1117 2706 1117 40 | 2 2 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 5 41 | 5694 1296 6151 1296 6151 1356 5694 1356 5694 1296 42 | 2 2 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 5 43 | 5598 1357 6259 1357 6259 1412 5598 1412 5598 1357 44 | 2 2 0 2 0 7 50 0 20 0.000 0 0 -1 0 0 5 45 | 5413 463 6437 463 6437 1296 5413 1296 5413 463 46 | 2 2 0 1 0 7 50 0 20 0.000 0 0 -1 0 0 5 47 | 5467 519 6381 519 6381 1237 5467 1237 5467 519 48 | 4 0 0 50 0 16 10 0.0000 4 120 465 2775 1125 CODE\001 49 | 4 0 0 50 0 16 10 0.0000 4 120 600 2775 900 OBJECT\001 50 | 4 0 0 50 0 16 10 0.0000 4 120 855 4005 975 EXECUTOR\001 51 | 4 0 0 50 0 16 10 0.0000 4 120 465 142 1117 CODE\001 52 | 4 0 0 50 0 16 10 0.0000 4 120 675 142 892 SOURCE\001 53 | 4 0 0 50 0 16 10 0.0000 4 120 825 1342 967 COMPILER\001 54 | 4 0 0 50 0 16 10 0.0000 4 120 615 5588 953 OUTPUT\001 55 | -------------------------------------------------------------------------------- /source/figs/compile.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/compile.pdf -------------------------------------------------------------------------------- /source/figs/dict1.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 6 450 1500 2400 3300 11 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 12 | 0 0 1.00 60.00 120.00 13 | 1575 2197 1950 2197 14 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 15 | 0 0 1.00 60.00 120.00 16 | 1575 1897 1950 1897 17 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 18 | 0 0 1.00 60.00 120.00 19 | 750 1875 1125 1875 20 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 21 | 0 0 1.00 60.00 120.00 22 | 1575 2797 1950 2797 23 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 24 | 0 0 1.00 60.00 120.00 25 | 1575 3097 1950 3097 26 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 27 | 0 0 1.00 60.00 120.00 28 | 1575 2497 1950 2497 29 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 30 | 1125 1725 2400 1725 2400 3300 1125 3300 1125 1725 31 | 4 2 0 50 0 16 11 0.0000 4 120 135 1500 1950 'a'\001 32 | 4 0 0 50 0 16 11 0.0000 4 120 90 2025 1950 1\001 33 | 4 0 0 50 0 16 11 0.0000 4 120 90 2025 2250 1\001 34 | 4 0 0 50 0 16 11 0.0000 4 120 255 1125 1650 dict\001 35 | 4 0 0 50 0 16 11 0.0000 4 120 255 450 1950 hist\001 36 | 4 2 0 50 0 16 11 0.0000 4 150 150 1500 2250 'p'\001 37 | 4 0 0 50 0 16 11 0.0000 4 120 90 2025 2850 1\001 38 | 4 2 0 50 0 16 11 0.0000 4 120 150 1500 3150 'o'\001 39 | 4 0 0 50 0 16 11 0.0000 4 120 90 2025 3150 1\001 40 | 4 2 0 50 0 16 11 0.0000 4 120 120 1500 2550 'r'\001 41 | 4 0 0 50 0 16 11 0.0000 4 120 90 2025 2550 2\001 42 | 4 2 0 50 0 16 11 0.0000 4 120 120 1500 2850 't'\001 43 | -6 44 | 6 4350 1500 5700 3000 45 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 46 | 0 0 1.00 60.00 120.00 47 | 4800 2197 5175 2197 48 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 49 | 0 0 1.00 60.00 120.00 50 | 4800 1897 5175 1897 51 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 52 | 0 0 1.00 60.00 120.00 53 | 4800 2497 5175 2497 54 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 55 | 0 0 1.00 60.00 120.00 56 | 4800 2797 5175 2797 57 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 58 | 4350 1725 5700 1725 5700 3000 4350 3000 4350 1725 59 | 4 2 0 50 0 16 11 0.0000 4 120 90 4725 1950 0\001 60 | 4 2 0 50 0 16 11 0.0000 4 120 90 4725 2250 1\001 61 | 4 0 0 50 0 16 11 0.0000 4 120 135 5250 1950 'a'\001 62 | 4 0 0 50 0 16 11 0.0000 4 150 150 5250 2250 'p'\001 63 | 4 0 0 50 0 16 11 0.0000 4 120 195 4350 1650 list\001 64 | 4 2 0 50 0 16 11 0.0000 4 120 90 4725 2550 2\001 65 | 4 0 0 50 0 16 11 0.0000 4 120 120 5250 2550 't'\001 66 | 4 0 0 50 0 16 11 0.0000 4 120 150 5250 2850 'o'\001 67 | 4 2 0 50 0 16 11 0.0000 4 120 90 4725 2850 3\001 68 | -6 69 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 70 | 0 0 1.00 60.00 120.00 71 | 3825 1897 4350 1890 72 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 73 | 0 0 1.00 60.00 120.00 74 | 3000 1875 3375 1875 75 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 76 | 3375 1725 4050 1725 4050 3750 3375 3750 3375 1725 77 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 78 | 0 0 1.00 60.00 120.00 79 | 3825 3529 4350 3522 80 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 81 | 0 0 1.00 60.00 120.00 82 | 4800 3547 5175 3547 83 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 84 | 4350 3375 5700 3375 5700 3750 4350 3750 4350 3375 85 | 4 2 0 50 0 16 11 0.0000 4 120 90 3750 1950 1\001 86 | 4 0 0 50 0 16 11 0.0000 4 120 255 3375 1650 dict\001 87 | 4 0 0 50 0 16 11 0.0000 4 120 210 2700 1950 inv\001 88 | 4 2 0 50 0 16 11 0.0000 4 120 90 3750 3600 2\001 89 | 4 2 0 50 0 16 11 0.0000 4 120 90 4725 3600 0\001 90 | 4 0 0 50 0 16 11 0.0000 4 120 195 4350 3300 list\001 91 | 4 0 0 50 0 16 11 0.0000 4 120 120 5250 3600 'r'\001 92 | -------------------------------------------------------------------------------- /source/figs/dict1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/dict1.pdf -------------------------------------------------------------------------------- /source/figs/dict1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/dict1.png -------------------------------------------------------------------------------- /source/figs/dict2.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 3300 1822 3675 1822 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 3300 1522 3675 1522 16 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 17 | 0 0 1.00 60.00 120.00 18 | 3300 2422 3675 2422 19 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 20 | 0 0 1.00 60.00 120.00 21 | 3300 2722 3675 2722 22 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 23 | 0 0 1.00 60.00 120.00 24 | 3300 2122 3675 2122 25 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 26 | 0 0 1.00 60.00 120.00 27 | 3300 3000 3675 3000 28 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 29 | 1275 1350 5100 1350 5100 3225 1275 3225 1275 1350 30 | 4 2 0 50 0 16 11 0.0000 4 150 1125 3225 1575 ('Cleese', 'John')\001 31 | 4 0 0 50 0 16 11 0.0000 4 120 1140 3750 1575 '08700 100 222'\001 32 | 4 0 0 50 0 16 11 0.0000 4 120 1140 3750 1875 '08700 100 222'\001 33 | 4 0 0 50 0 16 11 0.0000 4 120 1140 3750 2175 '08700 100 222'\001 34 | 4 0 0 50 0 16 11 0.0000 4 120 1140 3750 2475 '08700 100 222'\001 35 | 4 0 0 50 0 16 11 0.0000 4 120 1140 3750 2775 '08700 100 222'\001 36 | 4 2 0 50 0 16 11 0.0000 4 150 1530 3225 1875 ('Chapman', 'Graham')\001 37 | 4 2 0 50 0 16 11 0.0000 4 150 840 3225 2175 ('Idle', 'Eric')\001 38 | 4 2 0 50 0 16 11 0.0000 4 150 1080 3225 2775 ('Jones', 'Terry')\001 39 | 4 2 0 50 0 16 11 0.0000 4 150 1110 3225 2475 ('Gilliam', 'Terry')\001 40 | 4 2 0 50 0 16 11 0.0000 4 150 1170 3225 3075 ('Palin', 'Michael')\001 41 | 4 0 0 50 0 16 11 0.0000 4 120 1140 3750 3075 '08700 100 222'\001 42 | 4 0 0 50 0 16 11 0.0000 4 120 255 1275 1275 dict\001 43 | -------------------------------------------------------------------------------- /source/figs/dict2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/dict2.pdf -------------------------------------------------------------------------------- /source/figs/dict2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/dict2.png -------------------------------------------------------------------------------- /source/figs/fibonacci.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/fibonacci.pdf -------------------------------------------------------------------------------- /source/figs/fibonacci.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/fibonacci.png -------------------------------------------------------------------------------- /source/figs/flower.test.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/flower.test.pdf -------------------------------------------------------------------------------- /source/figs/flowers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/flowers.pdf -------------------------------------------------------------------------------- /source/figs/flowers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/flowers.png -------------------------------------------------------------------------------- /source/figs/interpret.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5b 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 90.00 180.00 12 | 3225 750 3600 750 13 | 2 1 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 6 14 | 2325 1350 2325 450 3000 450 3150 600 3150 1350 2325 1350 15 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 0 0 3 16 | 3000 450 3000 600 3150 600 17 | 2 1 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 4 18 | 3748 640 3652 533 3652 900 3748 798 19 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 20 | 0 0 1.00 90.00 180.00 21 | 5197 1117 5572 1117 22 | 2 1 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 4 23 | 5039 1173 5137 1275 5137 909 5039 1014 24 | 2 2 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 5 25 | 3750 525 5047 525 5047 1290 3750 1290 3750 525 26 | 2 2 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 5 27 | 5919 1296 6376 1296 6376 1356 5919 1356 5919 1296 28 | 2 2 0 2 0 7 50 0 -1 0.000 0 0 -1 0 0 5 29 | 5823 1357 6484 1357 6484 1412 5823 1412 5823 1357 30 | 2 2 0 2 0 7 50 0 20 0.000 0 0 -1 0 0 5 31 | 5638 463 6662 463 6662 1296 5638 1296 5638 463 32 | 2 2 0 1 0 7 50 0 20 0.000 0 0 -1 0 0 5 33 | 5692 519 6606 519 6606 1237 5692 1237 5692 519 34 | 4 0 0 50 0 16 10 0.0000 4 120 675 2400 900 SOURCE\001 35 | 4 0 0 50 0 16 10 0.0000 4 120 465 2400 1125 CODE\001 36 | 4 0 0 50 0 16 10 0.0000 4 120 1050 3825 975 INTERPRETER\001 37 | 4 0 0 50 0 16 10 0.0000 4 120 615 5813 953 OUTPUT\001 38 | -------------------------------------------------------------------------------- /source/figs/interpret.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/interpret.pdf -------------------------------------------------------------------------------- /source/figs/koch.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/koch.pdf -------------------------------------------------------------------------------- /source/figs/koch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/koch.png -------------------------------------------------------------------------------- /source/figs/list1.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5-alpha5 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 4125 1447 4500 1350 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 4125 1147 4500 1275 16 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 17 | 3825 975 5400 975 5400 1650 3825 1650 3825 975 18 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 19 | 0 0 1.00 60.00 120.00 20 | 2100 1447 2475 1447 21 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 22 | 0 0 1.00 60.00 120.00 23 | 2100 1147 2475 1147 24 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 25 | 1800 975 3375 975 3375 1650 1800 1650 1800 975 26 | 4 2 0 50 0 16 11 0.0000 4 105 105 4050 1200 a\001 27 | 4 2 0 50 0 16 11 0.0000 4 135 105 4050 1500 b\001 28 | 4 0 0 50 0 16 11 0.0000 4 135 720 4575 1350 'banana'\001 29 | 4 2 0 50 0 16 11 0.0000 4 105 105 2025 1200 a\001 30 | 4 2 0 50 0 16 11 0.0000 4 135 105 2025 1500 b\001 31 | 4 0 0 50 0 16 11 0.0000 4 135 720 2550 1200 'banana'\001 32 | 4 0 0 50 0 16 11 0.0000 4 135 720 2550 1500 'banana'\001 33 | -------------------------------------------------------------------------------- /source/figs/list1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/list1.pdf -------------------------------------------------------------------------------- /source/figs/list1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/list1.png -------------------------------------------------------------------------------- /source/figs/list2.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 2100 1447 2475 1447 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 2100 1147 2475 1147 16 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 17 | 1800 975 3375 975 3375 1650 1800 1650 1800 975 18 | 4 2 0 50 0 16 11 0.0000 4 90 75 2025 1200 a\001 19 | 4 2 0 50 0 16 11 0.0000 4 120 90 2025 1500 b\001 20 | 4 0 0 50 0 16 11 0.0000 4 150 630 2550 1200 [ 1, 2, 3 ]\001 21 | 4 0 0 50 0 16 11 0.0000 4 150 630 2550 1500 [ 1, 2, 3 ]\001 22 | -------------------------------------------------------------------------------- /source/figs/list2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/list2.pdf -------------------------------------------------------------------------------- /source/figs/list2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/list2.png -------------------------------------------------------------------------------- /source/figs/list3.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 2100 1447 2475 1350 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 2100 1147 2475 1275 16 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 17 | 1800 975 3375 975 3375 1650 1800 1650 1800 975 18 | 4 2 0 50 0 16 11 0.0000 4 90 75 2025 1200 a\001 19 | 4 2 0 50 0 16 11 0.0000 4 120 90 2025 1500 b\001 20 | 4 0 0 50 0 16 11 0.0000 4 150 630 2550 1350 [ 1, 2, 3 ]\001 21 | -------------------------------------------------------------------------------- /source/figs/list3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/list3.pdf -------------------------------------------------------------------------------- /source/figs/list3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/list3.png -------------------------------------------------------------------------------- /source/figs/liststate.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5b 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 2100 2497 2475 2497 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 1200 2475 1575 2475 16 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 17 | 0 0 1.00 60.00 120.00 18 | 2100 2850 2475 3075 19 | 2 1 2 1 0 7 50 0 -1 4.000 0 0 -1 0 0 2 20 | 2100 2797 2475 2797 21 | 2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 2 22 | 2550 2700 2850 2850 23 | 2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 2 24 | 2850 2700 2550 2850 25 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 26 | 0 0 1.00 60.00 120.00 27 | 1200 3825 1575 3825 28 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 29 | 1650 3675 1950 3675 1950 3975 1650 3975 1650 3675 30 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 31 | 1650 975 3675 975 3675 1950 1650 1950 1650 975 32 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 33 | 0 0 1.00 60.00 120.00 34 | 2100 1447 2475 1447 35 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 36 | 0 0 1.00 60.00 120.00 37 | 2100 1747 2475 1747 38 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 39 | 0 0 1.00 60.00 120.00 40 | 2100 1147 2475 1147 41 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 42 | 0 0 1.00 60.00 120.00 43 | 1200 1125 1575 1125 44 | 2 2 0 1 7 7 50 -1 -1 0.000 0 0 -1 0 0 5 45 | 225 675 3825 675 3825 4050 225 4050 225 675 46 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 47 | 1650 2325 3225 2325 3225 3300 1650 3300 1650 2325 48 | 4 2 0 50 0 16 11 0.0000 4 120 105 2025 2550 0\001 49 | 4 2 0 50 0 16 11 0.0000 4 120 105 2025 2850 1\001 50 | 4 0 0 50 0 16 11 0.0000 4 135 225 1650 2250 list\001 51 | 4 2 0 50 0 16 11 0.0000 4 135 720 1125 2550 numbers\001 52 | 4 0 0 50 0 16 11 0.0000 4 120 315 2550 2850 123\001 53 | 4 0 0 50 0 16 11 0.0000 4 120 105 2550 3150 5\001 54 | 4 0 0 50 0 16 11 0.0000 4 135 225 1650 3600 list\001 55 | 4 2 0 50 0 16 11 0.0000 4 165 495 1125 3900 empty\001 56 | 4 2 0 50 0 16 11 0.0000 4 120 105 2025 1200 0\001 57 | 4 2 0 50 0 16 11 0.0000 4 120 105 2025 1500 1\001 58 | 4 2 0 50 0 16 11 0.0000 4 120 105 2025 1800 2\001 59 | 4 0 0 50 0 16 11 0.0000 4 135 780 2550 1200 'Cheddar'\001 60 | 4 0 0 50 0 16 11 0.0000 4 135 540 2550 1500 'Edam'\001 61 | 4 0 0 50 0 16 11 0.0000 4 135 615 2550 1800 'Gouda'\001 62 | 4 0 0 50 0 16 11 0.0000 4 135 225 1650 900 list\001 63 | 4 2 0 50 0 16 11 0.0000 4 135 690 1125 1200 cheeses\001 64 | 4 0 0 50 0 16 11 0.0000 4 120 210 2550 2550 42\001 65 | -------------------------------------------------------------------------------- /source/figs/liststate.fig.bak: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 2100 2497 2475 2497 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 1200 2475 1575 2475 16 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 17 | 0 0 1.00 60.00 120.00 18 | 2100 2850 2475 3075 19 | 2 1 2 1 0 7 50 0 -1 4.000 0 0 -1 0 0 2 20 | 2100 2797 2475 2797 21 | 2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 2 22 | 2550 2700 2850 2850 23 | 2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 2 24 | 2850 2700 2550 2850 25 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 26 | 1650 2325 3225 2325 3225 3300 1650 3300 1650 2325 27 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 28 | 0 0 1.00 60.00 120.00 29 | 1200 3825 1575 3825 30 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 31 | 1650 3675 1950 3675 1950 3975 1650 3975 1650 3675 32 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 33 | 1650 975 3675 975 3675 1950 1650 1950 1650 975 34 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 35 | 0 0 1.00 60.00 120.00 36 | 2100 1447 2475 1447 37 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 38 | 0 0 1.00 60.00 120.00 39 | 2100 1747 2475 1747 40 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 41 | 0 0 1.00 60.00 120.00 42 | 2100 1147 2475 1147 43 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 44 | 0 0 1.00 60.00 120.00 45 | 1200 1125 1575 1125 46 | 2 2 0 1 7 7 50 -1 -1 0.000 0 0 -1 0 0 5 47 | 225 675 3825 675 3825 4050 225 4050 225 675 48 | 4 2 0 50 0 16 11 0.0000 4 135 105 2025 2550 0\001 49 | 4 2 0 50 0 16 11 0.0000 4 135 105 2025 2850 1\001 50 | 4 0 0 50 0 16 11 0.0000 4 135 225 1650 2250 list\001 51 | 4 2 0 50 0 16 11 0.0000 4 135 705 1125 2550 numbers\001 52 | 4 0 0 50 0 16 11 0.0000 4 135 210 2550 2550 17\001 53 | 4 0 0 50 0 16 11 0.0000 4 135 315 2550 2850 123\001 54 | 4 0 0 50 0 16 11 0.0000 4 135 105 2550 3150 5\001 55 | 4 0 0 50 0 16 11 0.0000 4 135 225 1650 3600 list\001 56 | 4 2 0 50 0 16 11 0.0000 4 180 495 1125 3900 empty\001 57 | 4 2 0 50 0 16 11 0.0000 4 135 105 2025 1200 0\001 58 | 4 2 0 50 0 16 11 0.0000 4 135 105 2025 1500 1\001 59 | 4 2 0 50 0 16 11 0.0000 4 135 105 2025 1800 2\001 60 | 4 0 0 50 0 16 11 0.0000 4 135 810 2550 1200 'Cheddar'\001 61 | 4 0 0 50 0 16 11 0.0000 4 135 555 2550 1500 'Edam'\001 62 | 4 0 0 50 0 16 11 0.0000 4 135 645 2550 1800 'Gouda'\001 63 | 4 0 0 50 0 16 11 0.0000 4 135 225 1650 900 list\001 64 | 4 2 0 50 0 16 11 0.0000 4 135 705 1125 1200 cheeses\001 65 | -------------------------------------------------------------------------------- /source/figs/liststate.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/liststate.pdf -------------------------------------------------------------------------------- /source/figs/liststate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/liststate.png -------------------------------------------------------------------------------- /source/figs/listsum1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/listsum1.pdf -------------------------------------------------------------------------------- /source/figs/listsum2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/listsum2.pdf -------------------------------------------------------------------------------- /source/figs/loop.py: -------------------------------------------------------------------------------- 1 | import sys, os 2 | from glob import glob 3 | 4 | def pipe(cmd): 5 | fp = os.popen(cmd) 6 | res = fp.read() 7 | stat = fp.close() 8 | return res, stat 9 | 10 | def main(script, files='*.eps'): 11 | for filename in sorted(glob(files)): 12 | destination = '.'.join(filename.split('.')[:-1]) + '.pdf' 13 | cmd = 'convert %s %s' % (filename, destination) 14 | print cmd 15 | 16 | res, stat = pipe(cmd) 17 | print res, stat 18 | 19 | if __name__ == '__main__': 20 | main(*sys.argv) 21 | -------------------------------------------------------------------------------- /source/figs/next.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/next.png -------------------------------------------------------------------------------- /source/figs/pies.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/pies.pdf -------------------------------------------------------------------------------- /source/figs/pies.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/pies.png -------------------------------------------------------------------------------- /source/figs/point.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 2100 1447 2475 1447 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 2100 1147 2475 1147 16 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 17 | 1800 975 2925 975 2925 1650 1800 1650 1800 975 18 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 19 | 0 0 1.00 60.00 120.00 20 | 1425 1117 1800 1117 21 | 4 2 0 50 0 16 11 0.0000 4 90 90 2025 1200 x\001 22 | 4 2 0 50 0 16 11 0.0000 4 120 75 2025 1500 y\001 23 | 4 0 0 50 0 16 11 0.0000 4 120 225 2550 1200 3.0\001 24 | 4 0 0 50 0 16 11 0.0000 4 120 225 2550 1500 4.0\001 25 | 4 2 0 50 0 16 11 0.0000 4 120 360 1350 1170 blank\001 26 | 4 0 0 50 0 16 11 0.0000 4 120 375 1800 900 Point\001 27 | -------------------------------------------------------------------------------- /source/figs/point.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/point.pdf -------------------------------------------------------------------------------- /source/figs/point.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/point.png -------------------------------------------------------------------------------- /source/figs/rectangle.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5b 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 6 3300 1350 4275 1950 11 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 12 | 0 0 1.00 60.00 120.00 13 | 3525 1822 3900 1822 14 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 15 | 0 0 1.00 60.00 120.00 16 | 3525 1522 3900 1522 17 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 18 | 3300 1350 4275 1350 4275 1950 3300 1950 3300 1350 19 | 4 2 0 50 0 16 11 0.0000 4 150 105 3450 1875 y\001 20 | 4 0 0 50 0 16 11 0.0000 4 135 255 3975 1575 0.0\001 21 | 4 2 0 50 0 16 11 0.0000 4 105 90 3450 1575 x\001 22 | 4 0 0 50 0 16 11 0.0000 4 135 255 3975 1875 0.0\001 23 | -6 24 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 25 | 0 0 1.00 60.00 120.00 26 | 2100 1447 2475 1447 27 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 28 | 0 0 1.00 60.00 120.00 29 | 2100 1747 3295 1745 30 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 31 | 0 0 1.00 60.00 120.00 32 | 2100 1147 2475 1147 33 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 34 | 0 0 1.00 60.00 120.00 35 | 1028 1140 1413 1140 36 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 37 | 1425 975 3075 975 3075 1950 1425 1950 1425 975 38 | 4 2 0 50 0 16 11 0.0000 4 135 435 2025 1200 width\001 39 | 4 0 0 50 0 16 11 0.0000 4 135 465 2550 1200 100.0\001 40 | 4 2 0 50 0 16 11 0.0000 4 105 540 2025 1800 corner\001 41 | 4 0 0 50 0 16 11 0.0000 4 135 465 2550 1500 200.0\001 42 | 4 0 0 50 0 16 11 0.0000 4 135 420 3300 1275 Point\001 43 | 4 0 0 50 0 16 11 0.0000 4 180 840 1500 900 Rectangle\001 44 | 4 2 0 50 0 16 11 0.0000 4 135 300 975 1200 box\001 45 | 4 2 0 50 0 16 11 0.0000 4 180 510 2025 1500 height\001 46 | -------------------------------------------------------------------------------- /source/figs/rectangle.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/rectangle.pdf -------------------------------------------------------------------------------- /source/figs/rectangle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/rectangle.png -------------------------------------------------------------------------------- /source/figs/rectangle2.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5b 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 6 3300 1350 4275 1950 11 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 12 | 0 0 1.00 60.00 120.00 13 | 3525 1822 3900 1822 14 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 15 | 0 0 1.00 60.00 120.00 16 | 3525 1522 3900 1522 17 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 18 | 3300 1350 4275 1350 4275 1950 3300 1950 3300 1350 19 | 4 2 0 50 0 16 11 0.0000 4 150 105 3450 1875 y\001 20 | 4 0 0 50 0 16 11 0.0000 4 135 255 3975 1575 0.0\001 21 | 4 2 0 50 0 16 11 0.0000 4 105 90 3450 1575 x\001 22 | 4 0 0 50 0 16 11 0.0000 4 135 255 3975 1875 0.0\001 23 | -6 24 | 6 2100 1050 2475 1500 25 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 26 | 0 0 1.00 60.00 120.00 27 | 2100 1447 2475 1447 28 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 29 | 0 0 1.00 60.00 120.00 30 | 2100 1147 2475 1147 31 | -6 32 | 6 5025 1050 5400 1500 33 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 34 | 0 0 1.00 60.00 120.00 35 | 5400 1447 5025 1447 36 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 37 | 0 0 1.00 60.00 120.00 38 | 5400 1147 5025 1147 39 | -6 40 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 41 | 0 0 1.00 60.00 120.00 42 | 2100 1747 3295 1745 43 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 44 | 0 0 1.00 60.00 120.00 45 | 5467 1757 4272 1755 46 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 47 | 0 0 1.00 60.00 120.00 48 | 1080 1147 1440 1147 49 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 50 | 1425 975 3075 975 3075 1950 1425 1950 1425 975 51 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 52 | 0 0 1.00 60.00 120.00 53 | 6435 1147 6090 1147 54 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 55 | 4500 975 6075 975 6075 1950 4500 1950 4500 975 56 | 4 2 0 50 0 16 11 0.0000 4 135 435 2025 1200 width\001 57 | 4 2 0 50 0 16 11 0.0000 4 180 510 2025 1500 height\001 58 | 4 0 0 50 0 16 11 0.0000 4 135 465 2550 1200 100.0\001 59 | 4 2 0 50 0 16 11 0.0000 4 105 540 2025 1800 corner\001 60 | 4 0 0 50 0 16 11 0.0000 4 135 465 2550 1500 200.0\001 61 | 4 2 0 50 0 16 11 0.0000 4 135 300 1050 1200 box\001 62 | 4 0 0 50 0 16 11 0.0000 4 135 465 4575 1200 100.0\001 63 | 4 0 0 50 0 16 11 0.0000 4 135 465 4575 1500 200.0\001 64 | 4 0 0 50 0 16 11 0.0000 4 135 435 5475 1200 width\001 65 | 4 0 0 50 0 16 11 0.0000 4 180 510 5475 1500 height\001 66 | 4 0 0 50 0 16 11 0.0000 4 105 540 5475 1800 corner\001 67 | 4 0 0 50 0 16 11 0.0000 4 135 405 6450 1200 box2\001 68 | -------------------------------------------------------------------------------- /source/figs/rectangle2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/rectangle2.pdf -------------------------------------------------------------------------------- /source/figs/rectangle2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/rectangle2.png -------------------------------------------------------------------------------- /source/figs/stack.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5c 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 6 2100 1897 2475 2647 11 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 12 | 0 0 1.00 60.00 120.00 13 | 2100 2272 2475 2272 14 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 15 | 0 0 1.00 60.00 120.00 16 | 2100 2572 2475 2572 17 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 18 | 0 0 1.00 60.00 120.00 19 | 2100 1972 2475 1972 20 | -6 21 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 22 | 0 0 1.00 60.00 120.00 23 | 2100 1447 2475 1447 24 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 25 | 0 0 1.00 60.00 120.00 26 | 2100 1147 2475 1147 27 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 28 | 0 0 1.00 60.00 120.00 29 | 2092 3097 2467 3097 30 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 31 | 1424 2925 4950 2925 4950 3300 1424 3300 1424 2925 32 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 33 | 1425 1800 4950 1800 4950 2775 1425 2775 1425 1800 34 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 35 | 1425 975 4950 975 4950 1650 1425 1650 1425 975 36 | 4 2 0 50 0 16 11 0.0000 4 135 450 2025 1200 line1\001 37 | 4 2 0 50 0 16 11 0.0000 4 135 450 2025 1500 line2\001 38 | 4 0 0 50 0 16 11 0.0000 4 165 1260 2550 1500 'tiddle bang.'\001 39 | 4 2 0 50 0 16 11 0.0000 4 165 450 2025 2025 part1\001 40 | 4 2 0 50 0 16 11 0.0000 4 165 450 2025 2325 part2\001 41 | 4 2 0 50 0 16 11 0.0000 4 120 270 2025 2625 cat\001 42 | 4 2 0 50 0 16 11 0.0000 4 135 450 2017 3150 bruce\001 43 | 4 0 0 50 0 16 11 0.0000 4 165 1260 2550 1200 'Bing tiddle '\001 44 | 4 0 0 50 0 16 11 0.0000 4 165 1260 2550 2025 'Bing tiddle '\001 45 | 4 0 0 50 0 16 11 0.0000 4 165 1260 2550 2325 'tiddle bang.'\001 46 | 4 0 0 50 0 16 11 0.0000 4 165 2340 2550 2625 'Bing tiddle tiddle bang.'\001 47 | 4 0 0 50 0 16 11 0.0000 4 165 2340 2550 3150 'Bing tiddle tiddle bang.'\001 48 | 4 2 0 50 0 16 11 0.0000 4 135 810 1275 2325 cat_twice\001 49 | 4 2 0 50 0 16 11 0.0000 4 150 990 1275 3150 print_twice\001 50 | # __main__ 51 | 4 2 0 50 0 16 11 0.0000 4 135 720 1275 1350 __main__\001 52 | -------------------------------------------------------------------------------- /source/figs/stack.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/stack.pdf -------------------------------------------------------------------------------- /source/figs/stack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/stack.png -------------------------------------------------------------------------------- /source/figs/stack2.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5c 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 11 | 1800 450 3000 450 3000 825 1800 825 1800 450 12 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 13 | 1800 975 3000 975 3000 1350 1800 1350 1800 975 14 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 15 | 1800 1500 3000 1500 3000 1875 1800 1875 1800 1500 16 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 17 | 1800 2025 3000 2025 3000 2400 1800 2400 1800 2025 18 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 19 | 1800 2550 3000 2550 3000 2925 1800 2925 1800 2550 20 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 21 | 0 0 1.00 60.00 120.00 22 | 2175 1147 2550 1147 23 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 24 | 0 0 1.00 60.00 120.00 25 | 2175 1672 2550 1672 26 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 27 | 0 0 1.00 60.00 120.00 28 | 2175 2197 2550 2197 29 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 30 | 0 0 1.00 60.00 120.00 31 | 2175 2722 2550 2722 32 | 4 2 0 50 0 16 11 0.0000 4 135 720 1650 675 __main__\001 33 | 4 2 0 50 0 16 11 0.0000 4 135 810 1650 1200 countdown\001 34 | 4 2 0 50 0 16 11 0.0000 4 135 810 1650 1725 countdown\001 35 | 4 2 0 50 0 16 11 0.0000 4 135 810 1650 2250 countdown\001 36 | 4 2 0 50 0 16 11 0.0000 4 135 810 1650 2775 countdown\001 37 | 4 2 0 50 0 16 11 0.0000 4 90 90 2100 1200 n\001 38 | 4 0 0 50 0 16 11 0.0000 4 135 90 2625 1200 3\001 39 | 4 2 0 50 0 16 11 0.0000 4 90 90 2100 1725 n\001 40 | 4 0 0 50 0 16 11 0.0000 4 135 90 2625 1725 2\001 41 | 4 2 0 50 0 16 11 0.0000 4 90 90 2100 2250 n\001 42 | 4 0 0 50 0 16 11 0.0000 4 135 90 2625 2250 1\001 43 | 4 2 0 50 0 16 11 0.0000 4 90 90 2100 2775 n\001 44 | 4 0 0 50 0 16 11 0.0000 4 135 90 2625 2775 0\001 45 | -------------------------------------------------------------------------------- /source/figs/stack2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/stack2.pdf -------------------------------------------------------------------------------- /source/figs/stack2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/stack2.png -------------------------------------------------------------------------------- /source/figs/stack3.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5c 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 5 1 0 1 0 7 50 0 -1 0.000 0 1 1 0 5700.000 2475.000 5700 2700 5925 2475 5700 2250 11 | 0 0 1.00 60.00 120.00 12 | 5 1 0 1 0 7 50 0 -1 0.000 0 1 1 0 5700.000 1950.000 5700 2175 5925 1950 5700 1725 13 | 0 0 1.00 60.00 120.00 14 | 5 1 0 1 0 7 50 0 -1 0.000 0 1 1 0 5700.000 900.000 5700 1125 5925 900 5700 675 15 | 0 0 1.00 60.00 120.00 16 | 5 1 0 1 0 7 50 0 -1 0.000 0 1 1 0 5700.000 1425.000 5700 1650 5925 1425 5700 1200 17 | 0 0 1.00 60.00 120.00 18 | 6 1950 1050 2775 1200 19 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 20 | 0 0 1.00 60.00 120.00 21 | 2175 1147 2550 1147 22 | 4 2 0 50 0 16 11 0.0000 4 90 90 2100 1200 n\001 23 | 4 0 0 50 0 16 11 0.0000 4 135 90 2625 1200 3\001 24 | -6 25 | 6 3075 1050 4275 2250 26 | 6 3075 1050 4275 1200 27 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 28 | 0 0 1.00 60.00 120.00 29 | 3675 1147 4050 1147 30 | 4 2 0 50 0 16 11 0.0000 4 90 630 3600 1200 recurse\001 31 | 4 0 0 50 0 16 11 0.0000 4 135 90 4125 1200 2\001 32 | -6 33 | 6 3075 1575 4275 1725 34 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 35 | 0 0 1.00 60.00 120.00 36 | 3675 1672 4050 1672 37 | 4 2 0 50 0 16 11 0.0000 4 90 630 3600 1725 recurse\001 38 | 4 0 0 50 0 16 11 0.0000 4 135 90 4125 1725 1\001 39 | -6 40 | 6 3075 2100 4275 2250 41 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 42 | 0 0 1.00 60.00 120.00 43 | 3675 2197 4050 2197 44 | 4 2 0 50 0 16 11 0.0000 4 90 630 3600 2250 recurse\001 45 | 4 0 0 50 0 16 11 0.0000 4 135 90 4125 2250 1\001 46 | -6 47 | -6 48 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 49 | 0 0 1.00 60.00 120.00 50 | 2175 1672 2550 1672 51 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 52 | 0 0 1.00 60.00 120.00 53 | 2175 2197 2550 2197 54 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 55 | 0 0 1.00 60.00 120.00 56 | 2175 2722 2550 2722 57 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 58 | 1800 975 5700 975 5700 1350 1800 1350 1800 975 59 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 60 | 1800 1500 5700 1500 5700 1875 1800 1875 1800 1500 61 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 62 | 1800 2025 5700 2025 5700 2400 1800 2400 1800 2025 63 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 64 | 1800 2550 5700 2550 5700 2925 1800 2925 1800 2550 65 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 66 | 1800 450 5700 450 5700 825 1800 825 1800 450 67 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 68 | 0 0 1.00 60.00 120.00 69 | 5025 2197 5400 2197 70 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 71 | 0 0 1.00 60.00 120.00 72 | 5025 1672 5400 1672 73 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 74 | 0 0 1.00 60.00 120.00 75 | 5025 1147 5400 1147 76 | 4 2 0 50 0 16 11 0.0000 4 135 720 1650 675 __main__\001 77 | 4 2 0 50 0 16 11 0.0000 4 135 810 1650 1200 factorial\001 78 | 4 2 0 50 0 16 11 0.0000 4 90 90 2100 1725 n\001 79 | 4 0 0 50 0 16 11 0.0000 4 135 90 2625 1725 2\001 80 | 4 2 0 50 0 16 11 0.0000 4 90 90 2100 2250 n\001 81 | 4 0 0 50 0 16 11 0.0000 4 135 90 2625 2250 1\001 82 | 4 2 0 50 0 16 11 0.0000 4 90 90 2100 2775 n\001 83 | 4 0 0 50 0 16 11 0.0000 4 135 90 2625 2775 0\001 84 | 4 2 0 50 0 16 11 0.0000 4 135 810 1650 1725 factorial\001 85 | 4 2 0 50 0 16 11 0.0000 4 135 810 1650 2250 factorial\001 86 | 4 2 0 50 0 16 11 0.0000 4 135 810 1650 2775 factorial\001 87 | 4 0 0 50 0 16 11 0.0000 4 135 90 6000 2550 1\001 88 | 4 0 0 50 0 16 11 0.0000 4 135 90 6000 2025 1\001 89 | 4 0 0 50 0 16 11 0.0000 4 135 90 6000 1500 2\001 90 | 4 0 0 50 0 16 11 0.0000 4 135 90 6000 975 6\001 91 | 4 0 0 50 0 16 11 0.0000 4 135 90 5475 2250 1\001 92 | 4 2 0 50 0 16 11 0.0000 4 135 540 4950 2250 result\001 93 | 4 0 0 50 0 16 11 0.0000 4 135 90 5475 1725 2\001 94 | 4 0 0 50 0 16 11 0.0000 4 135 90 5475 1200 6\001 95 | 4 2 0 50 0 16 11 0.0000 4 135 540 4950 1200 result\001 96 | 4 2 0 50 0 16 11 0.0000 4 135 540 4950 1725 result\001 97 | -------------------------------------------------------------------------------- /source/figs/stack3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/stack3.pdf -------------------------------------------------------------------------------- /source/figs/stack3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/stack3.png -------------------------------------------------------------------------------- /source/figs/stack4.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 6 2025 825 2775 1425 11 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 12 | 2175 1125 2550 1125 13 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 14 | 2175 1125 2550 900 15 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 16 | 0 0 1.00 60.00 120.00 17 | 2175 1125 2550 1350 18 | 4 2 0 50 0 16 11 0.0000 4 120 30 2100 1200 i\001 19 | 4 0 0 50 0 16 11 0.0000 4 120 90 2625 1200 2\001 20 | 4 0 0 50 0 16 11 0.0000 4 120 90 2625 975 1\001 21 | 4 0 0 50 0 16 11 0.0000 4 120 90 2625 1425 3\001 22 | -6 23 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 24 | 0 0 1.00 60.00 120.00 25 | 2175 2100 2550 2100 26 | 2 1 2 1 0 7 50 0 -1 3.000 0 0 -1 0 0 2 27 | 3300 2100 3675 1875 28 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 29 | 0 0 1.00 60.00 120.00 30 | 3300 2100 3675 2100 31 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 32 | 1800 1725 4350 1725 4350 2400 1800 2400 1800 1725 33 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 34 | 1800 750 4350 750 4350 1575 1800 1575 1800 750 35 | 4 2 0 50 0 16 11 0.0000 4 90 90 2100 2175 n\001 36 | 4 0 0 50 0 16 11 0.0000 4 120 90 2625 2175 3\001 37 | 4 2 0 50 0 16 11 0.0000 4 120 30 3225 2175 i\001 38 | 4 0 0 50 0 16 11 0.0000 4 120 90 3750 2175 2\001 39 | 4 0 0 50 0 16 11 0.0000 4 120 90 3750 1950 1\001 40 | 4 0 0 50 0 16 11 0.0000 4 150 975 675 975 printMultTable\001 41 | 4 0 0 50 0 16 11 0.0000 4 150 930 750 1950 printMultiples\001 42 | -------------------------------------------------------------------------------- /source/figs/stack4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/stack4.pdf -------------------------------------------------------------------------------- /source/figs/stack5.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5c 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 4575 2197 4950 2197 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 4575 2497 4950 2497 16 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 17 | 0 0 1.00 60.00 120.00 18 | 4575 1897 4950 1897 19 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 20 | 2700 1500 3675 1500 3675 1950 2700 1950 2700 1500 21 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 22 | 2700 2100 3675 2100 3675 2550 2700 2550 2700 2100 23 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 24 | 0 0 1.00 60.00 120.00 25 | 3507 2318 4050 1950 26 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 27 | 0 0 1.00 60.00 120.00 28 | 3510 1725 4050 1875 29 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 30 | 4125 1725 5550 1725 5550 2700 4125 2700 4125 1725 31 | 4 2 0 50 0 16 11 0.0000 4 135 90 4500 1950 0\001 32 | 4 2 0 50 0 16 11 0.0000 4 135 90 4500 2250 1\001 33 | 4 2 0 50 0 16 11 0.0000 4 135 90 4500 2550 2\001 34 | 4 0 0 50 0 16 11 0.0000 4 135 270 5025 1950 'a'\001 35 | 4 0 0 50 0 16 11 0.0000 4 135 270 5025 2250 'b'\001 36 | 4 0 0 50 0 16 11 0.0000 4 135 270 5025 2550 'c'\001 37 | 4 0 0 50 0 16 11 0.0000 4 135 360 4125 1650 list\001 38 | 4 2 0 50 0 16 11 0.0000 4 120 90 3450 2377 t\001 39 | 4 2 0 50 0 16 11 0.0000 4 135 630 3465 1770 letters\001 40 | 4 2 0 50 0 16 11 0.0000 4 150 990 2632 2377 delete_head\001 41 | 4 2 0 50 0 16 11 0.0000 4 135 720 2617 1770 __main__\001 42 | -------------------------------------------------------------------------------- /source/figs/stack5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/stack5.pdf -------------------------------------------------------------------------------- /source/figs/stack5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/stack5.png -------------------------------------------------------------------------------- /source/figs/state.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 6 525 750 3675 1950 11 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 12 | 1650 975 3675 975 3675 1950 1650 1950 1650 975 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 2100 1447 2475 1447 16 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 17 | 0 0 1.00 60.00 120.00 18 | 2100 1747 2475 1747 19 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 20 | 0 0 1.00 60.00 120.00 21 | 2100 1147 2475 1147 22 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 23 | 0 0 1.00 60.00 120.00 24 | 1200 1125 1575 1125 25 | 4 2 0 50 0 16 11 0.0000 4 120 90 2025 1200 0\001 26 | 4 2 0 50 0 16 11 0.0000 4 120 90 2025 1500 1\001 27 | 4 2 0 50 0 16 11 0.0000 4 120 90 2025 1800 2\001 28 | 4 0 0 50 0 16 11 0.0000 4 120 615 2550 1200 'cheddar'\001 29 | 4 0 0 50 0 16 11 0.0000 4 120 420 2550 1500 'edam'\001 30 | 4 0 0 50 0 16 11 0.0000 4 150 480 2550 1800 'gouda'\001 31 | 4 0 0 50 0 16 11 0.0000 4 120 195 1650 900 list\001 32 | 4 2 0 50 0 16 11 0.0000 4 120 540 1125 1200 cheeses\001 33 | -6 34 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 35 | 1650 2325 3675 2325 3675 3300 1650 3300 1650 2325 36 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 37 | 0 0 1.00 60.00 120.00 38 | 2100 2497 2475 2497 39 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 40 | 0 0 1.00 60.00 120.00 41 | 1200 2475 1575 2475 42 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 43 | 1650 3675 3675 3675 3675 4650 1650 4650 1650 3675 44 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 45 | 0 0 1.00 60.00 120.00 46 | 1200 3825 1575 3825 47 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 48 | 0 0 1.00 60.00 120.00 49 | 2100 2850 2475 3075 50 | 2 1 2 1 0 7 50 0 -1 4.000 0 0 -1 0 0 2 51 | 2100 2797 2475 2797 52 | 4 2 0 50 0 16 11 0.0000 4 120 90 2025 2550 0\001 53 | 4 2 0 50 0 16 11 0.0000 4 120 90 2025 2850 1\001 54 | 4 0 0 50 0 16 11 0.0000 4 120 195 1650 2250 list\001 55 | 4 0 0 50 0 16 11 0.0000 4 120 195 1650 3600 list\001 56 | 4 2 0 50 0 16 11 0.0000 4 120 585 1125 2550 numbers\001 57 | 4 2 0 50 0 16 11 0.0000 4 150 420 1125 3900 empty\001 58 | 4 0 0 50 0 16 11 0.0000 4 120 180 2550 2550 17\001 59 | 4 0 0 50 0 16 11 0.0000 4 120 270 2550 2850 123\001 60 | 4 0 0 50 0 16 11 0.0000 4 120 90 2550 3150 5\001 61 | -------------------------------------------------------------------------------- /source/figs/state.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/state.pdf -------------------------------------------------------------------------------- /source/figs/state2.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 Produced by xfig version 3.2.5b 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 6 2100 1072 2475 1822 11 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 12 | 0 0 1.00 60.00 120.00 13 | 2100 1447 2475 1447 14 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 15 | 0 0 1.00 60.00 120.00 16 | 2100 1747 2475 1747 17 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 18 | 0 0 1.00 60.00 120.00 19 | 2100 1147 2475 1147 20 | -6 21 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 22 | 1200 975 6375 975 6375 1950 1200 1950 1200 975 23 | 4 2 0 50 0 16 11 0.0000 4 150 735 2025 1200 message\001 24 | 4 2 0 50 0 16 11 0.0000 4 105 105 2025 1500 n\001 25 | 4 2 0 50 0 16 11 0.0000 4 180 150 2025 1800 pi\001 26 | 4 0 0 50 0 16 11 0.0000 4 135 210 2550 1500 17\001 27 | 4 0 0 50 0 16 11 0.0000 4 180 3690 2550 1200 'And now for something completely different'\001 28 | 4 0 0 50 0 16 11 0.0000 4 135 1830 2550 1800 3.1415926535897932\001 29 | -------------------------------------------------------------------------------- /source/figs/state2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/state2.pdf -------------------------------------------------------------------------------- /source/figs/state2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/state2.png -------------------------------------------------------------------------------- /source/figs/state3.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 2100 1447 2475 1447 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 2100 1747 2475 1747 16 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 17 | 0 0 1.00 60.00 120.00 18 | 2100 1147 2475 1147 19 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 20 | 0 0 1.00 60.00 120.00 21 | 2100 2025 2475 2025 22 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 23 | 1200 975 3375 975 3375 2250 1200 2250 1200 975 24 | 4 2 0 50 0 16 11 0.0000 4 120 450 2025 1200 word1\001 25 | 4 2 0 50 0 16 11 0.0000 4 120 450 2025 1500 word2\001 26 | 4 0 0 50 0 16 11 0.0000 4 150 375 2550 1200 'pots'\001 27 | 4 0 0 50 0 16 11 0.0000 4 150 375 2550 1500 'stop'\001 28 | 4 2 0 50 0 16 11 0.0000 4 120 30 2025 1800 i\001 29 | 4 0 0 50 0 16 11 0.0000 4 120 90 2550 1800 0\001 30 | 4 2 0 50 0 16 11 0.0000 4 150 30 2025 2100 j\001 31 | 4 0 0 50 0 16 11 0.0000 4 120 90 2550 2100 3\001 32 | -------------------------------------------------------------------------------- /source/figs/state3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/state3.pdf -------------------------------------------------------------------------------- /source/figs/state4.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 6 1800 3375 2550 3525 11 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 12 | 0 0 1.00 60.00 120.00 13 | 1950 3472 2325 3472 14 | 4 2 0 50 0 16 11 0.0000 4 120 30 1875 3525 i\001 15 | 4 0 0 50 0 16 11 0.0000 4 120 90 2400 3525 0\001 16 | -6 17 | 6 3900 3375 4650 3600 18 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 19 | 0 0 1.00 60.00 120.00 20 | 4050 3450 4425 3450 21 | 4 2 0 50 0 16 11 0.0000 4 150 30 3975 3525 j\001 22 | 4 0 0 50 0 16 11 0.0000 4 120 90 4500 3525 3\001 23 | -6 24 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 25 | 0 0 1.00 60.00 120.00 26 | 1950 2947 2325 2947 27 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 28 | 0 0 1.00 60.00 120.00 29 | 3750 2947 4125 2947 30 | 2 1 2 1 0 7 50 -1 -1 3.000 0 0 -1 0 0 2 31 | 2475 3300 2475 3075 32 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 33 | 1050 2775 5100 2775 5100 3675 1050 3675 1050 2775 34 | 2 1 2 1 0 7 50 -1 -1 3.000 0 0 -1 0 0 2 35 | 4537 3307 4537 3082 36 | 4 2 0 50 0 16 11 0.0000 4 120 450 1875 3000 word1\001 37 | 4 0 0 50 0 16 11 0.0000 4 150 375 2400 3000 'pots'\001 38 | 4 2 0 50 0 16 11 0.0000 4 120 450 3675 3000 word2\001 39 | 4 0 0 50 0 16 11 0.0000 4 150 375 4200 3000 'stop'\001 40 | -------------------------------------------------------------------------------- /source/figs/state4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/state4.pdf -------------------------------------------------------------------------------- /source/figs/state4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/state4.png -------------------------------------------------------------------------------- /source/figs/state5.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 6 450 1500 2400 3300 11 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 12 | 0 0 1.00 60.00 120.00 13 | 1575 2197 1950 2197 14 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 15 | 0 0 1.00 60.00 120.00 16 | 1575 1897 1950 1897 17 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 18 | 0 0 1.00 60.00 120.00 19 | 750 1875 1125 1875 20 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 21 | 0 0 1.00 60.00 120.00 22 | 1575 2797 1950 2797 23 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 24 | 0 0 1.00 60.00 120.00 25 | 1575 3097 1950 3097 26 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 27 | 0 0 1.00 60.00 120.00 28 | 1575 2497 1950 2497 29 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 30 | 1125 1725 2400 1725 2400 3300 1125 3300 1125 1725 31 | 4 2 0 50 0 16 11 0.0000 4 120 135 1500 1950 'a'\001 32 | 4 0 0 50 0 16 11 0.0000 4 120 90 2025 1950 1\001 33 | 4 0 0 50 0 16 11 0.0000 4 120 90 2025 2250 1\001 34 | 4 0 0 50 0 16 11 0.0000 4 120 255 1125 1650 dict\001 35 | 4 0 0 50 0 16 11 0.0000 4 120 255 450 1950 hist\001 36 | 4 2 0 50 0 16 11 0.0000 4 150 150 1500 2250 'p'\001 37 | 4 0 0 50 0 16 11 0.0000 4 120 90 2025 2850 1\001 38 | 4 2 0 50 0 16 11 0.0000 4 120 150 1500 3150 'o'\001 39 | 4 0 0 50 0 16 11 0.0000 4 120 90 2025 3150 1\001 40 | 4 2 0 50 0 16 11 0.0000 4 120 120 1500 2550 'r'\001 41 | 4 0 0 50 0 16 11 0.0000 4 120 90 2025 2550 2\001 42 | 4 2 0 50 0 16 11 0.0000 4 120 120 1500 2850 't'\001 43 | -6 44 | 6 4350 1500 5700 3000 45 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 46 | 0 0 1.00 60.00 120.00 47 | 4800 2197 5175 2197 48 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 49 | 0 0 1.00 60.00 120.00 50 | 4800 1897 5175 1897 51 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 52 | 0 0 1.00 60.00 120.00 53 | 4800 2497 5175 2497 54 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 55 | 0 0 1.00 60.00 120.00 56 | 4800 2797 5175 2797 57 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 58 | 4350 1725 5700 1725 5700 3000 4350 3000 4350 1725 59 | 4 2 0 50 0 16 11 0.0000 4 120 90 4725 1950 0\001 60 | 4 2 0 50 0 16 11 0.0000 4 120 90 4725 2250 1\001 61 | 4 0 0 50 0 16 11 0.0000 4 120 135 5250 1950 'a'\001 62 | 4 0 0 50 0 16 11 0.0000 4 150 150 5250 2250 'p'\001 63 | 4 0 0 50 0 16 11 0.0000 4 120 195 4350 1650 list\001 64 | 4 2 0 50 0 16 11 0.0000 4 120 90 4725 2550 2\001 65 | 4 0 0 50 0 16 11 0.0000 4 120 120 5250 2550 't'\001 66 | 4 0 0 50 0 16 11 0.0000 4 120 150 5250 2850 'o'\001 67 | 4 2 0 50 0 16 11 0.0000 4 120 90 4725 2850 3\001 68 | -6 69 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 70 | 0 0 1.00 60.00 120.00 71 | 3825 1897 4350 1890 72 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 73 | 0 0 1.00 60.00 120.00 74 | 3000 1875 3375 1875 75 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 76 | 3375 1725 4050 1725 4050 3750 3375 3750 3375 1725 77 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 78 | 0 0 1.00 60.00 120.00 79 | 3825 3529 4350 3522 80 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 81 | 0 0 1.00 60.00 120.00 82 | 4800 3547 5175 3547 83 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 84 | 4350 3375 5700 3375 5700 3750 4350 3750 4350 3375 85 | 4 2 0 50 0 16 11 0.0000 4 120 90 3750 1950 1\001 86 | 4 0 0 50 0 16 11 0.0000 4 120 255 3375 1650 dict\001 87 | 4 0 0 50 0 16 11 0.0000 4 120 210 2700 1950 inv\001 88 | 4 2 0 50 0 16 11 0.0000 4 120 90 3750 3600 2\001 89 | 4 2 0 50 0 16 11 0.0000 4 120 90 4725 3600 0\001 90 | 4 0 0 50 0 16 11 0.0000 4 120 195 4350 3300 list\001 91 | 4 0 0 50 0 16 11 0.0000 4 120 120 5250 3600 'r'\001 92 | -------------------------------------------------------------------------------- /source/figs/state5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/state5.pdf -------------------------------------------------------------------------------- /source/figs/time.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 2175 1447 2550 1447 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 2175 1747 2542 1747 16 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 17 | 0 0 1.00 60.00 120.00 18 | 2175 1147 2550 1147 19 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 20 | 1425 975 2925 975 2925 1950 1425 1950 1425 975 21 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 22 | 0 0 1.00 60.00 120.00 23 | 1027 1147 1412 1147 24 | 4 0 0 50 0 16 11 0.0000 4 120 180 2625 1500 59\001 25 | 4 0 0 50 0 16 11 0.0000 4 120 180 2625 1800 30\001 26 | 4 2 0 50 0 16 11 0.0000 4 120 315 2100 1200 hour\001 27 | 4 2 0 50 0 16 11 0.0000 4 120 450 2100 1500 minute\001 28 | 4 2 0 50 0 16 11 0.0000 4 120 495 2100 1800 second\001 29 | 4 0 0 50 0 16 11 0.0000 4 120 180 2625 1200 11\001 30 | 4 0 0 50 0 16 11 0.0000 4 120 300 1425 900 Time\001 31 | 4 2 0 50 0 16 11 0.0000 4 120 285 975 1200 time\001 32 | -------------------------------------------------------------------------------- /source/figs/time.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/time.pdf -------------------------------------------------------------------------------- /source/figs/time.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/time.png -------------------------------------------------------------------------------- /source/figs/towers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/towers.pdf -------------------------------------------------------------------------------- /source/figs/towers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/towers.png -------------------------------------------------------------------------------- /source/figs/tuple1.fig: -------------------------------------------------------------------------------- 1 | #FIG 3.2 2 | Landscape 3 | Center 4 | Inches 5 | Letter 6 | 100.00 7 | Single 8 | -2 9 | 1200 2 10 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 11 | 0 0 1.00 60.00 120.00 12 | 2100 1447 2475 1447 13 | 2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 1 0 2 14 | 0 0 1.00 60.00 120.00 15 | 2100 1147 2475 1147 16 | 2 2 0 1 0 7 51 0 18 0.000 0 0 -1 0 0 5 17 | 1650 975 3675 975 3675 1650 1650 1650 1650 975 18 | 4 2 0 50 0 16 11 0.0000 4 120 90 2025 1200 0\001 19 | 4 2 0 50 0 16 11 0.0000 4 120 90 2025 1500 1\001 20 | 4 0 0 50 0 16 11 0.0000 4 120 510 2550 1200 'Cleese'\001 21 | 4 0 0 50 0 16 11 0.0000 4 120 405 2550 1500 'John'\001 22 | 4 0 0 50 0 16 11 0.0000 4 150 330 1650 900 tuple\001 23 | -------------------------------------------------------------------------------- /source/figs/tuple1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/tuple1.pdf -------------------------------------------------------------------------------- /source/figs/tuple1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/tuple1.png -------------------------------------------------------------------------------- /source/figs/up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/earlgrey0103/ThinkPython2-CN/9589d8f9b91837094979d9dc6f4b1b1718ecaf9a/source/figs/up.png -------------------------------------------------------------------------------- /source/foreword.rst: -------------------------------------------------------------------------------- 1 | 译者序 2 | ==================================== 3 | 4 | 我是一名自学Python的编程爱好者。之所以说是爱好者,是因为我不是计算机专业毕业的。我的第一个单位是一家媒体,因为工作关系对当时新闻界流行的数据视觉化十分感兴趣,从《鲜活的数据》一书中了解到Python编程语言。而我使用的第一本教材,就是Allen Downey的《Think Python》,副标题叫“如何像计算机科学家一样思考”。 5 | 6 | 后来,我到了一家互联网公司,接触运营工作。我也利用业余时间开发出了自己的网站——\ `编程派 `_\ ,而网站的定位就是专注Python编程,这也是网站名称和网站域名的由来:“编程派”中的“派”字,取自Python前两个字母的发音;codingpy,则是coding python的缩略。了解Python的朋友都知道,Python脚本的默认文件名后缀就是`.py`。 7 | 8 | 搭建“编程派”网站(后来还开通了“编程派”微信订阅号和今日头条号)的目的,就是为了能够让更多的人了解Python语言,更快地获取国外的Python教程和资源,一起交流、学习Python。因为我相信,每个人都值得学习一点编程知识,而在我看来,最好的入门语言就是Python。 9 | 10 | 为什么想要翻译一本书? 11 | ---------------------- 12 | 13 | 呃,其实我是语言类专业出身的,所以难免会有想要翻译一本书的冲动,可惜还没有出版社请我去翻书。所以只有自己上啦。 14 | 15 | 另外,这四个多月来通过网站和微信号,已经和大家分享了近百篇原创编译的Python教程,很多都得到了各大技术网站的转载。虽然大家在阅读文章的时候,可能并不太会注意“编程派”这个名字,但能够帮助到更多的人,还是不错的。但是这些原创编译的教程存在一个问题,就是各自是不关联的,而且也有一定的难度,对于完全零基础的人来说,根本没有什么卵用。 16 | 17 | 所以,我想成系列地与大家分享文章,而我最想帮助的人,就是那些零编程基础的朋友。因此,最适合的系列文章莫过于一本Python入门教材啦。 18 | 19 | 为什么选择《Think Python》 20 | --------------------------- 21 | 22 | 选择《Think Python》,一是因为它是我自己入门使用的第一本教材;二是,它确实非常浅显易懂,把很多概念用非常朴实的话进行了解释。至少,当时零基础的我,看这本书时没有大的障碍。 23 | 24 | 这和作者在设计、编写此书时的出发点密不可分。你可以在本书的“序言”部分(今天微信推送的另一篇文章),看到更加详细的解释。 25 | 26 | 为什么翻译Python 3版? 27 | --------------------------- 28 | 29 | 此书已经有了Python 2版,为什么还要更新到Python 3呢? 30 | 31 | (本书最新版与此前版本的区别,请看“序言”部分。) 32 | 33 | 这是因为Python 3才是Python的未来。Python之父Guido van Rossum早就坚决指出,不会再延长Python 2的支持协议。更具体一点说,就是到2020年时,Python核心开发团队就会停止对Python 2.7的开发、维护工作。而且,他们也早就不再往2.7版中添加新特性了。 34 | 35 | 虽然现在很多公司还在使用2.7,从Python 2迁移到Python 3的过程也非常的艰难,但是可以肯定的是,Python 3才是Python的未来! 36 | 37 | 所以,我们需要Python 3版的《Think Python》。 38 | (另外一个原因,当然是因为Python 2版的已经有中译本,而且已经出版啦。。。本书在翻译过程中,会参考[车万翔老师组织翻译的Python 2版](https://github.com/carfly/thinkpython-cn)。) 39 | 40 | 41 | 贡献者 42 | ------------------- 43 | 44 | 感谢下面这些朋友的贡献: 45 | 46 | 1. ipyher 47 | 2. theJian 48 | 3. lroolle 49 | 4. xpgeng 50 | 5. obserthinker 51 | 6. SeikaScarlet 52 | 7. cxyfreedom 53 | 54 | 尤其感谢SeikaScarlet辛苦制作精校PDF版。 55 | 56 | 声明 57 | ------------------ 58 | 59 | 本书的原作是一本自由书籍(Free Book),以知识共享协议([Creative Commons Attribution-NonCommercial 3.0 Unported License](https://creativecommons.org/licenses/by-nc/3.0/us/))发布。因此,该中译版将以同样的协议发布,不会用于商业用途。你可以自由分享、修改、复制该中译版,但请不要用于商业用途。 60 | 61 | 此书原著版权归[Allen Downey](http://greenteapress.com/wp/think-python-2e/)所有,原作链接如下:[https://github.com/AllenDowney/ThinkPython2](https://github.com/AllenDowney/ThinkPython2)。 62 | 63 | 该中译版在翻译过程中,参考了车万翔老师组织翻译的Python 2版:[https://github.com/carfly/thinkpython-cn](https://github.com/carfly/thinkpython-cn)。 64 | 65 | -------------------------------------------------------------------------------- /source/hevea.sty: -------------------------------------------------------------------------------- 1 | % hevea : hevea.sty 2 | % This is a very basic style file for latex document to be processed 3 | % with hevea. It contains definitions of LaTeX environment which are 4 | % processed in a special way by the translator. 5 | % Mostly : 6 | % - latexonly, not processed by hevea, processed by latex. 7 | % - htmlonly , the reverse. 8 | % - rawhtml, to include raw HTML in hevea output. 9 | % - toimage, to send text to the image file. 10 | % The package also provides hevea logos, html related commands (ahref 11 | % etc.), void cutting and image commands. 12 | \NeedsTeXFormat{LaTeX2e} 13 | \ProvidesPackage{hevea}[2002/01/11] 14 | \RequirePackage{comment} 15 | \newif\ifhevea\heveafalse 16 | \@ifundefined{ifimagen}{\newif\ifimagen\imagenfalse} 17 | \makeatletter% 18 | \newcommand{\heveasmup}[2]{% 19 | \raise #1\hbox{$\m@th$% 20 | \csname S@\f@size\endcsname 21 | \fontsize\sf@size 0% 22 | \math@fontsfalse\selectfont 23 | #2% 24 | }}% 25 | \DeclareRobustCommand{\hevea}{H\kern-.15em\heveasmup{.2ex}{E}\kern-.15emV\kern-.15em\heveasmup{.2ex}{E}\kern-.15emA}% 26 | \DeclareRobustCommand{\hacha}{H\kern-.15em\heveasmup{.2ex}{A}\kern-.15emC\kern-.1em\heveasmup{.2ex}{H}\kern-.15emA}% 27 | \DeclareRobustCommand{\html}{\protect\heveasmup{0.ex}{HTML}} 28 | %%%%%%%%% Hyperlinks hevea style 29 | \newcommand{\ahref}[2]{{#2}} 30 | \newcommand{\ahrefloc}[2]{{#2}} 31 | \newcommand{\aname}[2]{{#2}} 32 | \newcommand{\ahrefurl}[1]{\texttt{#1}} 33 | \newcommand{\footahref}[2]{#2\footnote{\texttt{#1}}} 34 | \newcommand{\mailto}[1]{\texttt{#1}} 35 | \newcommand{\imgsrc}[2][]{} 36 | \newcommand{\home}[1]{\protect\raisebox{-.75ex}{\char126}#1} 37 | \AtBeginDocument 38 | {\@ifundefined{url} 39 | {%url package is not loaded 40 | \let\url\ahref\let\oneurl\ahrefurl\let\footurl\footahref} 41 | {}} 42 | %% Void cutting instructions 43 | \newcounter{cuttingdepth} 44 | \newcommand{\tocnumber}{} 45 | \newcommand{\notocnumber}{} 46 | \newcommand{\cuttingunit}{} 47 | \newcommand{\cutdef}[2][]{} 48 | \newcommand{\cuthere}[2]{} 49 | \newcommand{\cutend}{} 50 | \newcommand{\htmlhead}[1]{} 51 | \newcommand{\htmlfoot}[1]{} 52 | \newcommand{\htmlprefix}[1]{} 53 | \newenvironment{cutflow}[1]{}{} 54 | \newcommand{\cutname}[1]{} 55 | \newcommand{\toplinks}[3]{} 56 | %%%% Html only 57 | \excludecomment{rawhtml} 58 | \newcommand{\rawhtmlinput}[1]{} 59 | \excludecomment{htmlonly} 60 | %%%% Latex only 61 | \newenvironment{latexonly}{}{} 62 | \newenvironment{verblatex}{}{} 63 | %%%% Image file stuff 64 | \def\toimage{\endgroup} 65 | \def\endtoimage{\begingroup\def\@currenvir{toimage}} 66 | \def\verbimage{\endgroup} 67 | \def\endverbimage{\begingroup\def\@currenvir{verbimage}} 68 | \newcommand{\imageflush}[1][]{} 69 | %%% Bgcolor definition 70 | \newsavebox{\@bgcolorbin} 71 | \newenvironment{bgcolor}[2][] 72 | {\newcommand{\@mycolor}{#2}\begin{lrbox}{\@bgcolorbin}\vbox\bgroup} 73 | {\egroup\end{lrbox}% 74 | \begin{flushleft}% 75 | \colorbox{\@mycolor}{\usebox{\@bgcolorbin}}% 76 | \end{flushleft}} 77 | %%% Style sheets macros, defined as no-ops 78 | \newcommand{\newstyle}[2]{} 79 | \newcommand{\addstyle}[1]{} 80 | \newcommand{\setenvclass}[2]{} 81 | \newcommand{\getenvclass}[1]{} 82 | \newcommand{\loadcssfile}[1]{} 83 | \newenvironment{divstyle}[1]{}{} 84 | \newenvironment{cellstyle}[2]{}{} 85 | %%% Postlude 86 | \makeatother 87 | -------------------------------------------------------------------------------- /source/index.rst: -------------------------------------------------------------------------------- 1 | .. Think Python 2nd Edition Chinese Translation documentation master file, created by 2 | sphinx-quickstart on Sun Feb 21 14:36:11 2016. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | 《Think Python 2e》中文版 7 | ======================================================================== 8 | 9 | Contents: 10 | 11 | .. toctree:: 12 | :maxdepth: 1 13 | 14 | 00-preface 15 | 01-the-way-of-the-program 16 | 02-variables-expressions-and-statements 17 | 03-functions 18 | 04-case-study-interface-design 19 | 05-conditionals-and-recursion 20 | 06-fruitful-functions 21 | 07-iteration 22 | 08-strings 23 | 09-case-study-word-play 24 | 10-lists 25 | 11-dictionaries 26 | 12-tuples 27 | 13-case-study-data-structure-selection 28 | 14-files.rst 29 | 15-classes-and-objects 30 | 16-classes-and-functions 31 | 17-classes-and-methods 32 | 18-inheritance 33 | 19-the-goodies 34 | 20-debugging 35 | 21-analysis-of-algorithms 36 | foreword 37 | 38 | 39 | .. Indices and tables 40 | ================== 41 | * :ref:`genindex` 42 | * :ref:`modindex` 43 | * :ref:`search` 44 | --------------------------------------------------------------------------------