Table Of ContentPython for Informatics
Exploring Information
Version0.0.6
Charles Severance
Copyright©2009-2011CharlesSeverance.
Printinghistory:
December2009: BegintoproducePythonforInformatics: ExploringInformationbyre-mixingThink
Python:HowtoThinkLikeaComputerScientist
June2008: Majorrevision,changedtitletoThinkPython:HowtoThinkLikeaComputerScientist.
August2007: Majorrevision,changedtitletoHowtoThinkLikea(Python)Programmer.
April2002: FirsteditionofHowtoThinkLikeaComputerScientist.
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. This
licenseisavailableatcreativecommons.org/licenses/by-sa/3.0/.
TheoriginalformofthisbookisLATEXsourcecode. CompilingthisLATEXsourcehastheeffectofgen-
eratingadevice-independentrepresentationofatextbook, whichcanbeconvertedtootherformatsand
printed.
TheLATEXsourcefortheThinkPython: HowtoThinkLikeaComputerScientistversionofthisbookis
availablefromhttp://www.thinkpython.com.
ThecoverimageshowssocialconnectivityofNSFgrantinvestigatorsattheUniversityofMichiganfrom
September1999throughOctober2010andwasprovidedbyEricHoferandvisualizedusingtheGUESS
software developed by Eytan Adar, both of the University of Michigan. The cover design is by Terri
GeitgeyoftheUniversityofMichiganLibrary.
Preface
Python for Informatics: Remixing an Open Book
Itisquitenaturalforacademicswhoarecontinuouslytoldto“publishorperish”towanttoal-
wayscreatesomethingfromscratchthatistheirownfreshcreation. Thisbookisanexperiment
innotstartingfromscratch,butinstead“re-mixing”thebooktitledThinkPython:HowtoThink
LikeaComputerScientistwrittenbyAllenB.Downey,JeffElknerandothers.
InDecemberof2009,IwaspreparingtoteachSI502-NetworkedProgrammingattheUni-
versity of Michigan for the fifth semester in a row and decided it was time to write a Python
textbookthatfocusedonexploringdatainsteadofunderstandingalgorithmsandabstractions.
My goal in SI502 is to teach people life-long data handling skills using Python. Few of my
studentswereplanningtobebeprofessionalcomputerprogrammers. Instead,theyplannedbe
librarians, managers, lawyers, biologists, economists, etc. whohappened towanttoskillfully
usetechnologyintheirchosenfield.
Ineverseemedtofindtheperfectdata-orientedPythonbookformycoursesoIsetouttowrite
justsuchabook. LuckilyatafacultymeetingthreeweeksbeforeIwasabouttostartmynew
bookfromscratchovertheholidaybreak,Dr. AtulPrakashshowedmetheThinkPythonbook
which he had used to teach his Python course that semester. It is a well-written Computer
Sciencetextwithafocusonshort,directexplanationsandeaseoflearning.
Theoverallbookstructurehasbeenchangedtogettodoingdataanalysisproblemsasquickly
as possible and have a series of running examples and exercises about data analysis from the
verybeginning.
The first10 chapters aresimilartotheThink Python book butthere have been some changes.
Nearly all number-oriented exercises have been replaced with data-oriented exercises. Topics
arepresentedintheordertoneededtobuildincreasinglysophisticateddataanalysissolutions.
Some topics like try and except are pulled forward and presented as part of the chapter on
conditionalswhileotherconceptslikefunctionsareleftuntiltheyareneededtohandleprogram
complexityratherintroducedasanearlylessoninabstraction. Theword“recursion”doesnot
appearinthebookatall.
Inchapters11-15,nearlyallofthematerialisbrandnew,focusingonreal-worldusesandsimple
examplesofPythonfordataanalysisincludingregularexpressionsforsearchingandparsing,
vi Chapter0. Preface
automatingtasksonyourcomputer,retrievingdataacrossthenetwork,scrapingwebpagesfor
data,usingwebservices,parsingXMLdata,andcreatingandusingdatabasesusingStructured
QueryLanguage.
TheultimategoalofallofthesechangesisashiftfromaComputerSciencetoanInformatics
focus is to only include topics into a first technology class that can be applied even if one
choosesnottobecomeaprofessionalprogrammer.
Students who find this book interesting and want to further explore should look at Allen B.
Downey’sThinkPythonbook.Becausethereisalotofoverlapbetweenthetwobooks,students
will quickly pick up skills in the additional areas of computing in general and computational
thinking that are covered in Think Python. And given that the books have a similar writing
styleandattimeshaveidenticaltextandexamples,youshouldbeabletomovequicklythrough
ThinkPythonwithaminimumofeffort.
AsthecopyrightholderofThinkPython,Allenhasgivenmepermissiontochangethebook’s
licensefromtheGNUFreeDocumentationLicensetothemorerecentCreativeCommonsAt-
tribution — Share Alike license. This follows a general shift in open documentation licenses
movingfromtheGFDLtotheCC-BY-SA(i.e.Wikipedia).UsingtheCC-BY-SAlicensemain-
tains the book’s strong copyleft tradition while making it even more straightforward for new
authorstoreusethismaterialastheyseefit.
I feel that this book serves an example of why open materials are so important to the future
of education, and want to thank Allen B. Downey and Cambridge University Press for their
forward looking decision to make the book available under an open Copyright. I hope they
are pleased with the results of my efforts and I hope that you the reader are pleased with our
collectiveefforts.
CharlesSeverance
www.dr-chuck.com
AnnArbor,MI,USA
July25,2010
Charles Severance is a Clinical Associate Professor at the University of Michigan School of
Information.
Preface for “Think Python”
Thestrangehistoryof“ThinkPython”
(AllenB.Downey)
In January 1999 I was preparing to teach an introductory programming class in Java. I had
taughtitthreetimesandIwasgettingfrustrated. Thefailurerateintheclasswastoohighand,
evenforstudentswhosucceeded,theoveralllevelofachievementwastoolow.
OneoftheproblemsIsawwasthebooks. Theyweretoobig,withtoomuchunnecessarydetail
about Java, and not enough high-level guidance about how to program. And they all suffered
vii
from the trap door effect: they would start out easy, proceed gradually, and then somewhere
around Chapter 5 the bottom would fall out. The students would get too much new material,
toofast,andIwouldspendtherestofthesemesterpickingupthepieces.
Twoweeksbeforethefirstdayofclasses,Idecidedtowritemyownbook. Mygoalswere:
• Keepitshort. Itisbetterforstudentstoread10pagesthannotread50pages.
• Be careful with vocabulary. I tried to minimize the jargon and define each term at first
use.
• Buildgradually. Toavoidtrapdoors,Itookthemostdifficulttopicsandsplitthemintoa
seriesofsmallsteps.
• Focusonprogramming,nottheprogramminglanguage. Iincludedtheminimumuseful
subsetofJavaandleftouttherest.
Ineededatitle,soonawhimIchoseHowtoThinkLikeaComputerScientist.
Myfirstversionwasrough,butitworked.Studentsdidthereading,andtheyunderstoodenough
that I could spend class time on the hard topics, the interesting topics and (most important)
lettingthestudentspractice.
IreleasedthebookundertheGNUFreeDocumentationLicense, whichallowsuserstocopy,
modify,anddistributethebook.
Whathappenednextisthecoolpart. JeffElkner,ahighschoolteacherinVirginia,adoptedmy
bookandtranslateditintoPython. Hesentmeacopyofhistranslation,andIhadtheunusual
experienceoflearningPythonbyreadingmyownbook.
JeffandIrevisedthebook,incorporatedacasestudybyChrisMeyers,andin2001wereleased
How to Think Like a Computer Scientist: Learning with Python, also under the GNU Free
Documentation License. As Green Tea Press, I published the book and started selling hard
copies through Amazon.com and college book stores. Other books from Green Tea Press are
availableatgreenteapress.com.
In2003IstartedteachingatOlinCollegeandIgottoteachPythonforthefirsttime. Thecon-
trastwithJavawasstriking. Studentsstruggledless,learnedmore,workedonmoreinteresting
projects,andgenerallyhadalotmorefun.
OverthelastfiveyearsIhavecontinuedtodevelopthebook,correctingerrors,improvingsome
of the examples and adding material, especially exercises. In 2008 I started work on a major
revision—at the same time, I was contacted by an editor at Cambridge University Press who
wasinterestedinpublishingthenextedition. Goodtiming!
Ihopeyouenjoyworkingwiththisbook, andthatithelpsyoulearntoprogramandthink, at
leastalittlebit,likeacomputerscientist.
viii Chapter0. Preface
Acknowledgementsfor“ThinkPython”
(AllenB.Downey)
Firstandmostimportantly,IthankJeffElkner,whotranslatedmyJavabookintoPython,which
gotthisprojectstartedandintroducedmetowhathasturnedouttobemyfavoritelanguage.
IalsothankChrisMeyers,whocontributedseveralsectionstoHowtoThinkLikeaComputer
Scientist.
And I thank the Free Software Foundation for developing the GNU Free Documentation Li-
cense,whichhelpedmakemycollaborationwithJeffandChrispossible.
IalsothanktheeditorsatLuluwhoworkedonHowtoThinkLikeaComputerScientist.
Ithankallthestudentswhoworkedwithearlierversionsofthisbookandallthecontributors
(listedinanAppendix)whosentincorrectionsandsuggestions.
AndIthankmywife,Lisa,forherworkonthisbook,andGreenTeaPress,andeverythingelse,
too.
AllenB.Downey
NeedhamMA
AllenDowneyisanAssociateProfessorofComputerScienceattheFranklinW.OlinCollege
ofEngineering.
Contents
Preface v
1 Whyshouldyoulearntowriteprograms? 1
1.1 Creativityandmotivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Computerhardwarearchitecture . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Understandingprogramming . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Wordsandsentences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 ConversingwithPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Terminology: interpreterandcompiler . . . . . . . . . . . . . . . . . . . . . 8
1.7 Writingaprogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.8 Whatisaprogram? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.9 Thebuildingblocksofprograms . . . . . . . . . . . . . . . . . . . . . . . . 12
1.10 Whatcouldpossiblygowrong?. . . . . . . . . . . . . . . . . . . . . . . . . 12
1.11 Thelearningjourney . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.12 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2 Variables,expressionsandstatements 19
2.1 Valuesandtypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2 Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.3 Variablenamesandkeywords . . . . . . . . . . . . . . . . . . . . . . . . . . 21
x Contents
2.4 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.5 Operatorsandoperands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.6 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.7 Orderofoperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.8 Modulusoperator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.9 Stringoperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.10 Askingtheuserforinput . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.11 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.12 Choosingmnemonicvariablenames . . . . . . . . . . . . . . . . . . . . . . 27
2.13 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.14 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.15 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3 Conditionalexecution 33
3.1 Booleanexpressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.2 Logicaloperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3 Conditionalexecution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.4 Alternativeexecution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.5 Chainedconditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.6 Nestedconditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.7 Catchingexceptionsusingtryandexcept . . . . . . . . . . . . . . . . . . . . 38
3.8 Shortcircuitevaluationoflogicalexpressions . . . . . . . . . . . . . . . . . 40
3.9 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43