Table Of ContentThe Coder’s Apprentice
Learning Programming with Python 3
Pieter Spronck
The Coder’s Apprentice
Learning Programming with Python 3
Pieter Spronck
Version1.0.16
November11,2017
Copyright©2016,2017PieterSpronck.
Permissionisgrantedtocopy,distribute,and/ormodifythisdocumentunderthetermsoftheCre-
ative Commons Attribution-NonCommercial 3.0 Unported License, which is available at https:
//creativecommons.org/licenses/by-nc/3.0/.
TheoriginalformofthisbookisLATEXsourcecode.CompilingthisLATEXsourcehastheeffectofgen-
eratingadevice-independentrepresentationofatextbook,whichcanbeconvertedtootherformats
andprinted.
TheLATEXsourceforthisbookwill(atsomepointintime)beavailablefromhttp://www.spronck.
net/pythonbook
The latest version of this book will always be available from http://www.spronck.net/
pythonbook.
Preface
Computationaltechnologycausestheworldtochangerapidly.
Almost30yearsagoIgotmyfirstjobasacomputerprogrammer. Atthetime,onlylarger
companies with a big administrative overload used computers. Or rather, “a computer,”
becauseitwasrareforacompanytohavemorethanone. Therewerenopersonalcomput-
ers,noInternet,nomobilephones. Peoplestillusedtypewriters.
Inthose30years,thewaypeopleworkandlivehasundergonehugechanges. Thatisex-
ceptionallyclearwhenlookingatthekindofworkthatpeopledo. Mailmen,forinstance,
deliveredthemailtwiceperdaywhenIwasakid–nowtheydelivermailtwiceperweek,
which means that the contingent of professional mailmen has been decimated. Bank of-
fices are closed because banking can be done much easier online. Information desks can
be manned by digital avatars or be replaced by online information systems. Large de-
partmentstoresgooutofbusinessbecausepeoplemaketheirpurchasesonline,leadingto
an enormous decline in the need for having salespeople. And though this has currently
causedasmallincreaseinthedemandforpeoplewhoworkintransportation,wecansee
self-drivingcarsonthehorizon,replacingtheneedtohaveanychauffeursatall.
These are all “low profile” jobs, but “high profile” jobs aren’t safe either. I have taught
programmingtoprofessionaljournalists,whotoldmethatcomputersaretakingoverlarge
partsoftheirjobs,writingbasicarticlesanddoingautomatedbackgroundresearch–they
wantedtotakemycoursesbecausetheyrealizedthatwithoutskillsindigitaltechnology,
they would be out of a job in a few years time. Programs have been developed that take
over a menial but oh-so time consuming part of lawyers’ jobs, namely researching case
histories. Computers can write music, produce paintings, and even sculpt – why would
you have someone hammer away at a block of granite for six months when a 3D-printer
canproduceasculpturewithafewhoursofwork? Evendesigningandrunningscientific
experimentshasbeenoffloadedtocomputersinsomeresearchdomains.
In the 30 years in which I have been a professional worker, I have seen the job market
change from hardly incorporating computers at all, to a situation in which the need for
human employees has been reduced considerably – regardless the job. And that change
hasnotcometoanendyet.
This does not mean that there is no place for humans in the job market. It does mean,
however,thatonlyhumanswhocanmakecontributionsthatacomputerhasahardtime
makingonitsown,canbeassuredofajob. Inthenearfuture,employabilitywillbeinvari-
ably linked to the ability to integrate the power of humans and computers in a way that
enhancesbothofthem.
Theproblemisthattobeabletousecomputerstoimprovethequalityofone’swork,itdoes
notsufficetobeabletouseawordprocessororspreadsheet.Oneshouldactuallybeableto
vi Preface
expandthecapabilitiesofcomputersfromtheperspectiveofone’schosenprofession. For
example,ajournalistwhocanonlyrunafact-findingcomputerprogramthatsomeoneelse
wrote,isnotneeded. However,ajournalistwhoisabletoexpandafact-findingprogram
sothatitcancomeupwithfactsfromnewsources,isanasset.
To be able to employ computers in such a way, one needs the skills to think and solve
problems like a computer programmer. Having taught students computer programming
formanyyears,Iknowthatthisdoesnotcomenaturallytomost. Toacquirethenecessary
skills,studentsneedtospendseveralintensivecoursesonthetopic.
Consideringthefactthatuniversitiesandcollegesaresupposedtopreparestudentsforthe
job market in which they have to function for 40 or more years, and considering the fact
thatintheverynearfuture(ifnotrightnowalready)theabilitytoincorporatethepower
of computers in any job is a necessity to being a valued worker, one would expect that
“computerprogramming”isoneofthebasiccoursesthatanystudentneedstotake. Un-
fortunately,itisnot. Typically,basicrequiredcoursesare“scientificwriting,”“philosophy
ofscience,”and“statistics,”but“computerprogramming”isstillseen,bymosteducation
programs,asanoptionalskill. Itisnot.
Inmyview,anycourseprogramthatdoesnotmake“computerprogramming”arequired
course, is doing its students a disservice, as it is not preparing them for the job market.
Actually, I would prefer it if secondary, or even primary schools would incorporate such
courses, as programming skills tend to be easier to learn at a younger age. The reason is
thattheyneedaparticularwayofcreativethinking,whichishardertoacquirewhenone
isalreadyusedtosolvingproblemsinthereproductivewaysthatarenormallytaughtat
schools.
Allstudents,regardlessoftheirchosentopic,needtolearnhowtoprogram. Notbecause
weshouldraiseagenerationofcomputerprogrammers–professionalprogrammingisa
specialization that only a few people need to be able to do. But the ability to create pro-
gramsprovidesstudentswiththeskillstothinkandsolveproblemslikeacomputerpro-
grammer,togaininsightinthepossibilitiesandlimitationsofcomputers,andtoleverage
thepowerofcomputersinaparticulardomaininauniquelyhumanway.
ThegoalofthisbookistoteachanyonehowtocreateusefulprogramsinPython.Itshould
be usable by secondary school students, and university and college students for whom
computerprogrammingisnotnaturallyincorporatedintheircourseprogram. Itsaimisto
giveanyonethemeanstobecomeproficientinprogramming,andassuchgetpreparedto
performwellinthe21stcenturyjobmarket.
PieterSpronck
May2,2016
Maastricht,TheNetherlands
PieterSpronckisaProfessorofComputerScienceatTilburgUniversity,TheNetherlands.
Acknowledgments
Many thanks to Allen B. Downey, who wrote the excellent Python 2 book Think Python:
How to Think Like a Computer Scientist. I myself learned Python programming from his
book, and used the LATEX template that he graciously provided as the basis for this book.
DowneyrecentlyreleasedaPython3versionofhisbook. Ifyouarealreadyfamiliarwith
Preface vii
programmingingeneralandjustwanttogettoknowPython,hisbookmightbetheway
togo.
I am grateful to Peter Wentworth, who produced a Python 3 version of Downey’s book.
PeterusesaparticularstyleofteachingthatIfinddoesnotworktoowellwiththestudents
Ihavehad,butIdefinitelygotalotofinformationfromhisbook.
ManythanksalsotoGuidovanRossum,theoriginalcreatorofPython. Ilovetheconcept
ofprogramming,butveryfewprogramminglanguagesareactuallyajoytouse. Pythonis
oneofthem,andforthatIamgrateful.
ThanksalsotoÁkosKádár,NannevanNoord,andSanderWubben,whoworkedwithme
onanearlyversionofaPythoncourse,onwhichIlaterbasedthisbook.
Thanks to the members of Monty Python, whose television shows and audio recordings
taughtmeEnglishinahighlyenjoyableway. TheirshowgavePythonitsname,andIhave
usedquotesoftheirshowsinsomeofthedemonstrationsandexercisesinthisbook.
Many thanks to Myrthe Spronck, for creating the website for this book, found at http:
//www.spronck.net/pythonbook.
Thankstoallthecontributors(listedbelow)whosentincorrectionsandsuggestions.
Ifyouhaveasuggestionorcorrection,[email protected](not
tobeusedforassistancewithprogrammingproblems,ofcourse–thereareplentyofplaces
ontheInternetwhereyoucangetsuchhelp),orleaveamessageattheforumhttp://www.
spronck.net/forum. If I make a change based on your feedback, I will add you to the
contributorlist(unlessyouasktobeomitted).
Contributor list
• “oajns” indicated some spelling mistakes in Chapter 9 and in Appendices C and D
(fixedinversion1.0.4).
• Larry Cali pointed out an error in the code for Exercise 4.3, which could give prob-
lemswithfloating-pointvalueswhichPythoncannotstoreexactly.Ifixedtheexercise
andmadearemarkonthisinChapter3(fixedinversion1.0.5).
• IsaacKramernotedaprobleminExercise9.5,whichmadetheissueinthecodeun-
noticable. I fixed this to make the error actually occur as I explain in the Answers
section(fixedinversion1.0.6).
• Ruud van Cruchten indicated that my discussion of providing multi-line commen-
tary in Chapter 4 was incomplete and could lead to problems. I have extended the
textinthisrespect(fixedinversion1.0.7).
• NadeKangpointedoutthattheanswertoExercise7.9(secondguessinggame)could
beconfusing. Ichangedthecodealittletocompensate(fixedinversion1.0.7).
• Shiyu Zhang noticed that listing 8.16 contained useless parameters. I corrected this
(fixedinversion1.0.8).
• MustafaAmjedindicatedseveralspellingandlogicalmistakesinthefirst100pages
(fixedinversion1.0.8).
• WoodgirlMartyrindicatedaspellingmistakeinChapter1(fixedinversion1.0.9).
viii Preface
• Claudia Dai pointed out a small mistake in the answer to Exercise 10.1 (counting
vowels;fixedinversion1.0.9).
• Several of my students suggested adding flow charts to the chapters on conditions
and iterations, as they would help understanding of how these concepts work. I
followedthatsuggestion(addedtoversion1.0.9).
• MauroCrociarapointedoutmultipletyposandgavemanyideasforimprovements
(incorporatedinversion1.0.11).
• ChrisSpinksnoticedsomeproblemswiththeanswerforExercise21.4,theextended
fruit basket, and the regular expressions in the answers to Exercises 25.3 and 25.4,
whereoneissupposedtoextractnamesfromtexts(fixedinversion1.0.12).
• Patrick Vekemans noticed an error in the code in Subsection 7.3.2 (fixed in version
1.0.13).
• JosePerez-CarballopointedouttomethatthelistofreservedwordsthatIpresented
wasactuallythePython2list,whichhasundergoneafewchangesinPython3(fixed
inversion1.0.13). Healsopointedoutatypo(fixedinversion1.0.14).
• Jos Kaats indicated an erroneous detail in the calling of functions from functions
(fixedin1.0.14).
• LuisMendoTomashadquiteafewremarkswhichallleadtochangesinthebook,in
particular the inclusion of a section on default values for function parameters (ver-
sion1.0.14).
• Abdulkader Abdullah pointed out a mistake in the ranges used in the answer to
exercise14.2(fixedinversion1.0.16).
• CornéHeerenprovidedmanyideasforimprovements. Iparticularlyappreciatehis
simple way to determine the middle of three numbers in the chapter on functions
(version1.0.16).
Contents
Preface v
1 Introduction 1
1.1 Howtousethisbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Prerequisitesandassumptions . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 WhyPython? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Python’slimitationsasaprogramminglanguage . . . . . . . . . . . . . . . 4
1.5 Whatdoesitmean“tothinklikeaprogrammer?” . . . . . . . . . . . . . . 4
1.6 Theartofprogramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 Startsmall,growbig . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.8 Python2orPython3? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.9 Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2 UsingPython 11
2.1 GettingPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 CreatingPythonprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3 RunningPythonprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4 Referencematerial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3 Expressions 15
3.1 Displayingresults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.2 Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.3 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4 Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
x Contents
4 Variables 25
4.1 Variablesandvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.2 Variablenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.3 Debuggingvariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.4 Softtyping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.5 Shorthandoperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.6 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5 SimpleFunctions 35
5.1 Elementsofafunction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.2 Somebasicfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.3 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
6 Conditions 49
6.1 Booleanexpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.2 Conditionalstatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
6.3 Earlyexits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
7 Iterations 65
7.1 whileloop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
7.2 forloop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
7.3 Loopcontrolstatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7.4 Nestedloops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
7.5 Theloop-and-a-half . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
7.6 Beingsmartaboutloops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.7 Ondesigningalgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
8 Functions 93
8.1 Whycreatefunctions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
8.2 Creatingfunctions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
8.3 Scopeandlifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
8.4 Managingprogramcomplexity . . . . . . . . . . . . . . . . . . . . . . . . . 108
8.5 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
8.6 Anonymousfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Description:Downey recently released a Python 3 version of his book. Text in a PDF file is not stored in such a way that spaces are inserted in the correct.