Sei sulla pagina 1di 364

8/23/2016 Yuzu: Python Programming in Context

CHAPTER1
Introduction

1.1Objectives
Toprovideexamplesofcomputerscienceintherealworld
Toprovideanoverviewofcommonproblemsolvingstrategies
TointroducePythonsnumericdatatypes
Toshowexamplesofsimpleprograms
Tointroduceloopsandsimplefunctions
Tointroduceturtlegraphics

1.2WhatIsComputerScience?
Computers are a vital part of our lives. They help to control the planes we fly in and the cars we drive in.
They keep track of the buying and selling of stocks in financial markets around the world. They control
complex surgical machinery and the pacemakers that are embedded in our bodies. Computers help us to
communicatequicklyandefficientlywithothersthroughouttheworld.Ifyouarereadingthisbook,itislikely
thatyouhaveusedapersonalcomputerforwritingapaper,surfingtheWeb,orsendinganemail.Youmay
have more experience. Perhaps you have hooked up a home network, or even built your own web page.
However, even though computers have become appliances that are part of the backdrop of our lives, the
chancesarethatyouhavenotexploredtheworldofprogramming.
Theimportantquestionthatwemustanswerinthischapteriswhatiscomputerscience?Theproblemwith
answeringthisquestionisthatthetermismisleadingrightfromthestart.Alookinadictionarywilltellyou
thatscienceisthetheoreticalandexperimentalstudyofthenaturalworld.Itseekstounderstandhowthings
workbyforminghypotheses,conductingexperiments,andanalyzingresults.Giventhatdefinition,youmight
think that computer science is the exploration and discovery of how a computer works. As a novice, that
mightbeinterestingtoyou,butasadisciplineitmakeslittlesensegiventhatcomputersaremanufacturedby
humans.Computerscienceisnotlikebiologyorphysics,disciplineswherewearetryingtounderstandthe
workingofthehumanbody,orhowtheuniverseworks.InthewordsofEdsgarDijkstra:Computerscience
isnomoreaboutcomputersthanastronomyisabouttelescopes.
So,whatiscomputerscience?Computerscienceisthestudyofalgorithms.
Toputitanotherway,computerscienceisprimarilyaboutproblemsolvingandcomputationalprocess.For
beginningcomputerscientists,findingthesolutiontoaproblemisoftentheeasiestpart.Turningthesolution
intoasetofstepbystepinstructionsthatcanbeperformedbyacomputer(creatingacomputationalprocess)
is often difficult. Computer scientists often call this set of instructions a program. You may think of it as
somethinglikearecipeforabeginningcook.First,bringthewatertoaboil,thenaddthemacaroni,andso

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 1/30
8/23/2016 Yuzu: Python Programming in Context

on.
Sinceyouarefamiliarwithusingadvancedprogramsthataredesignedtomakethecomputerlookintelligent,
itisimportanttodispelthatidearightaway.Herearesiximportantthingstorememberaboutcomputersas
youarelearningtoprogram:
1.Computersaredumb.
2.Computersonlydowhatyoutellthemtodo.
3.Computersdowhatyoutellthemtodoreallyfast,sotheyappearsmart(buttheyarenot).
4.Computersdontrememberanythingunlessyoutellthemhowtoremember.
5.Computerstakeyourinstructionsliterally.Ifyoutellthemtodosomethingdumb,theydoit.
6.Acomputeronlydoeswhatitistoldandinexactlytheorderyoutellit.

1.3WhyStudyComputerScience?
Nowthatyouhaveabetterideaofwhatcomputerscienceis,youmaybewonderingwhyyoushouldlearn
anymoreaboutit.Ourbeliefisthatcomputerscienceisforeveryone.Someofthebiggestcomputersuccess
storiescomefrompeoplewhowerenotevencomputerscientistsbytrainingbutcametocomputerscience
laterintheircareersbecausetheyhadaninterestingproblemtosolve.Forexample,aphysicistbythenameof
TimBernersLeeattheEuropeanLaboratoryforParticlePhysics(CERN)neededabetterwayforphysicists
aroundtheworldtoshareinformation.Thesolutiontothisinformationsharingproblembecamewhatistoday
calledtheWorldWideWeb.BernersLeewrotethecomputerprogramsforthefirstwebserverandbrowser.
TodayheisDirectoroftheWorldWideWebConsortiumandaprofessorattheMITComputerScienceand
ArtificialIntelligenceLaboratory.

1.3.1EverydayApplicationsofComputerScience
Evenifyouknowyouwanttobeacomputerscientist,thefactisthatfewcomputerscientistsworkonlyon
problemslimitedtocomputerssuchasbuildingabetteroperatingsystemorimprovingalocalareanetwork.
Mostcomputerscientistsworkwithpeople,writingprogramsinareassuchasbiology,chemistry,business,
economics,publishing,automotivedesign,orentertainment.Lookaroundyouandthinkaboutthecomputer
applicationsyouusemanytimeseachday:fromyourbrowsertoaninstantmessagingprogramandemails,
fromwordprocessingtoiTunes,cellphones,andiPods.
Inadditiontoourdesktopcomputers,therearethecomputersweusedailythatwedonoteventhinkabout.
For example, the computer in your car that checks your gas mileage several times every second that
examines the wear on your brake pads that monitors your speed and emissions that updates your GPS
displaytokeeptrackofyourexactpositionandplotsitonamaponthedashboarddisplay.Moreandmore
applianceshavecomputercapabilitiesaspartoftheirfunction.
If you go to the doctor and need medical imaging such as a CAT scan or MRI, you are relying on
sophisticatedcomputerprogramstomakeandinterprettheimagesofyourbody.Ifyoulookinthecockpitof
anewerairplane,youwillseethattheentirecockpitisnothingmorethanabunchofdisplayswithvirtual
switches on them. In addition, the pilot of the airplane has spent many hours training in a simulated
environmentcontrolledbyacomputer.
Therearethecomputerprogramsthatrunbehindthescenesthatmaketheworldamoreorderlyplacetolive.
Asastudent,youprobablyknowthatacomputerkeepstrackofyourlunchmoneyorcreditatthecafeteria,
yourgradepointaverage,andhowmuchmoneyyouowethecollegethismonth.Thecomputersinthelibrary
keeptrackofwhichbooksthelibraryowns,whohasbookscheckedout,andwhentheyareduebackinthe
library.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 2/30
8/23/2016 Yuzu: Python Programming in Context

Ifyouhaveeverflowninanairplane,youmaynothaverealizedhowmuchtheroutingandpositioningof
airplanesrelyoncomputers.Eachairplaneistrackedbytwodifferentradarsystems,plusatransponderinthe
airplaneitselfthattransmitsinformationtoacomputertoidentifyitself,alongwithitscurrentpositionand
altitude.Onthegroundamultitudeofcomputerssharethisinformationtohelproutetheairplaneonasafe
andefficientpathfromtakeofftotouchdown.Evenaftertheplaneisontheground,computerscontinueto
trackitasitmovesfromtherunwaytoitsgate.
When you arrange for a package to be delivered by UPS, that package is routed and tracked by a very
sophisticated computer program. At the UPS worldport in Louisville, Kentucky, every package is
automaticallyphotographed,measured,andweighed,andithastheinformationonitssuperbarcodeanalyzed
by computers to determine its trajectory along some of the 17,000 conveyor belts. This requires awesome
computingpower:moredataareprocessedhereevery30minutesthaninanentiredayoftradingontheNew
YorkStockExchange.Eventuallyeverypackageslidesdownachuteandisplacedintoabagoranairfreight
container.Andbeforedawnitisoffagaintocompleteitsjourneyinanotheraircraftorinoneofafleetof
waitingtrucks[Eco06].
Inlargecitiesallthestoplightsarecontrolledbycomputers.Sensorsintheroadmonitortrafficconditions.
Largesimulationsareusedtodeterminehowtokeeptrafficflowingasquicklyaspossible.Whenhightraffic
isdetectedinanarea,thecomputeradjuststhetimingofthestoplightstoincreasetheflowoftraffic[How97].
Computersareprovidingkeyhelpontheforefrontofbiologicalresearchwithproteins.Beforeproteinscan
carry out their important functions as biologys workhorses, they assemble themselves and undergo the
process of protein folding. While critical and fundamental to virtually all of biology, in many ways the
processremainsamystery.Moreover,whenproteinsdonotfoldcorrectly,therecanbeseriousconsequences
fromAlzheimersdiseasetomadcow,HuntingtonsandParkinsonsdisease,aswellasmanycancersand
cancerrelated syndromes. The folding@home project is using computers throughout the world to try to
understandhowproteinfoldingworks.Forexample,thebestknowngenethathelpstofightcanceriscalled
p53.Roughlyhalfofallknowncancersresultfrommutationsinthisgene.Oneofthefirstpublishedresults
fromthefolding@homeprojectinvolvesaninvestigationintop53folding.ResearchersatStanfordsayWe
predicthowp53foldsandindoingso,wecanpredictwhichaminoacidmutationswouldberelevant.When
compared with experiments, our predictions have appeared to agree with the experiment and give a new
interpretationtoexistingdata.[fold]
In the war on terror, computers are not only busy inside government agencies analyzing millions of emails
fromsuspectedterroristsbutalsoworkingonwaystorespondtoeverypossiblekindofattack.Forexample,
supposethatthesmallpoxviruswerereleasedintothepopulationofacitylikePortland,Oregon.Whatwould
bethebestresponsetopreventanepidemic?Shouldthegovernmentinstituteapolicyofmassvaccination?
Shouldonlyexposedindividualsandtheircircleofcontactsbequarantinedand/orvaccinated?Shouldmajor
centerswherepeoplecongregate,likeschoolsandmalls,beclosed?Thesequestionscanbestbeansweredby
usingmassivecomputersimulationprogramsthatcananticipatethemovementsofindividualsinalargecity,
alongwithvariousstrategiesforreducingthespreadofadisease[BES05].
Countless motion pictures today are produced with various kinds of computergenerated special effects.
Obviously there are the computeranimated movies such as Toy Story, Cars, and Shrek, but the line is
continuallyblurringbetweenliveactionandcomputergeneratedfilms.Forexample,thevastbattlesbetween
theOrcsandthehumansintheLordoftheRingsmovieswerecompletelycomputergenerated,includingthe
actionsofeachindividualbattleparticipant.InthetraditionallyanimatedmovieTheLionKing,thewildebeest
stampede was computergenerated. For his role in ThePolarExpress, Tom Hanks wore a special suit with
sensorsonitsothathisbodymotionswerecontinuouslymonitored.Aftertheliveactionpartofthefilmwas
completed,anewskinwascomputergeneratedandputonthebodythatfollowedthelivemovementsofthe
actor.

1.3.2WhyComputerScienceIsImportant
Inonesense,studyingcomputerscienceisimportantbecauseitgivesyouabetterunderstandingofhowthe
https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 3/30
8/23/2016 Yuzu: Python Programming in Context

technological world around you works. After reading this book, you may have a new appreciation for the
applicationsyouuseeveryday.Hopefully,youwillevenaspiretowriteyourownimprovedversionofsome
application.
In another sense, computer science helps to prepare you for your future. Many of the interesting jobs are
going to be at the intersection of computer science and some other domain. Speaking at the Microsoft
ResearchFacultysummit,BillGatesmadethisobservation:Thenatureofthesejobsisnotjustclosingthe
doorandcoding.Thegreatestmissingskillissomebodywhosgoodatunderstandingengineeringandbridges
thattoworkingwithcustomersandmarketing.Recentlyamanageratasuccessfulsoftwarecompanysaid,I
sometimesturnawayapplicantswhohaveperfectscoresonthestandardizedtests.Thereasonhesaidthis
wasthattheseapplicantsmaybetechnicallyperfect,buttheylacktheabilitytocommunicatewithpeopleand
solverealworldproblems.
Mostimportantly,afirstcourseincomputersciencewillhelpyouinthefollowingways:

Youwillbeabletoapplynewproblemsolvingskills.
Youwilllearntoapplylogic.
Youwilllearnaboutprocess(aseriesofactionsorstepstakeninordertoachieveaspecificoutcome).
Youwillunderstandandapplyabstraction.
Youwilllearntothinkandcommunicatemoreclearly.

1.4ProblemSolvingStrategies
Problemsolvinghappensonthreedifferentlevels:

Strategy:Ahighlevelideaforfindingasolution.
Tactics:Methodsorpatternsthatworkinmanydifferentsettings.
Tools:Specifictricksandtechniquesthatareusedinspecificsituations.

Paul Zeitz [Zei99] provides us with a helpful analogy for illustrating the three different levels of problem
solving:

Youarestandingatthebaseofamountain,hopingtoclimbtothesummit.Yourfirststrategymaybe
totakeseveralsmalltripstovariouseasierpeaksnearby,soastoobservethetargetmountainfrom
different angles. After this, you may consider a somewhat more focused strategy, perhaps to try
climbingthemountainviaaparticularridge.Nowthetacticalconsiderationsbegin:howtoactually
achievethechosenstrategy.Forexample,supposethatourstrategysuggestsclimbingthesouthridge
ofthepeak,buttherearesnowfieldsandriversinourpath.Differenttacticsareneededtonegotiate
eachoftheseobstacles.Forthesnowfield,ourtacticmaybetotravelearlyinthemorningwhilethe
snowishard.Fortheriver,ourtacticmaybescoutingthebanksforthesafestcrossing.Finally,move
intothemosttightlyfocusedlevel,thatoftools:specifictechniquestoaccomplishspecializedtasks.
Forexample,tocrossthesnowfieldwemaysetupaparticularsystemofropesforsafetyandwalk
withiceaxes.Therivercrossingmayrequirethepartytostripfromthewaistdownandholdhandsfor
balance. These are all tools. They are very specific. You would never summarize, To climb the
mountainwehadtotakeourpantsoffandholdhands,becauseitwasaminorthoughessential
component of the entire climb. On the other hand, strategic and sometimes tactical ideas are often
describedinyoursummary:Wedecidedtoreachthesummitviathesouthridgeandhadtocrossa
difficultsnowfieldandadangerousrivertogettotheridge.

As you progress through this book, you will encounter several different problemsolving strategies. In
addition, you will see that computer science uses many different problemsolving tactics. In particular you
will learn to recognize patterns in the problems you solve that lead to patterns in the programs you write.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 4/30
8/23/2016 Yuzu: Python Programming in Context

Finally,asyouusethePythonprogramminglanguageyouwilllearnaboutthetoolsthatPythonprovidesto
writeyoursolutionasaprogram.
Asimpleexamplewillillustratesomeofwhatwearetalkingabout.Thequestionisasfollows:Aclasshas
12students.Atthebeginningofclasseachstudentshakeshandswitheachoftheotherstudents.Howmany
handshakestakeplace?
Yourfirstinstinctmightbetosimplysaythatsinceeachpersonmustshakehandswith11otherpeoplethe
answeris1211=132handshakes,butyouwouldbewrong.Tohelpyoumakeprogresstowardthecorrect
answer,youcanemployastrategycalledsimplification.Simplificationisastrategythatreducesaproblemto
atrivialsize.
Letsassumethatinsteadof12peoplethereisonlyonepersonintheclassroom.Whenthereisonlyasingle
person,nohandshakeswilltakeplace.Butwhathappenswhenasecondpersonenterstheclassroom?Upon
enteringtheroom,thesecondpersonmustshakehandswiththefirst(andonlyother)personintheroomfor
onehandshake.Nowsupposeathirdpersonenterstheclassroom.Thethirdpersonmustshakehandswiththe
first two, making a total of 2 + 1 + 0 = 3 handshakes. The fourth person who enters the room must shake
handswiththethreepeoplealreadyintheroom,soourtotalhandshakecountisnow3+2+1+0=6.Bythis
time you should see a pattern of generalizationa technique that enables you to go from some specific
examplestoasolutionthatcanbeimplementedasaprogram.
InourhandshakingproblemthepatternistellingusthattheNthpersontoentertheclassroomshakeshands
withN1otherpeople,andthetotalnumberofhandshakesisthesum1+2+3+N1.Atthispointwe
mightsimplywriteacomputerprogramthataddsupthenumbersfrom1toN1forus.Addingnumbersis
something that computers are particularly good at. However, we will also point out that there is a general
solutionforthisproblemforaddingasequenceofnumbers:

For our handshake problem, we need to add up the numbers from 1 to 1 less than the number of students.
Giventhatthereare12students,n=11.Plugging11intotheformulagivesus:

Figure1.1Representingthesumofthenumbersfrom1toNgraphically

Youcanverifythisresultyourselfbysimplyaddingthenumbersfrom1to11.
Infactwecanprovethattheformulagivesusthecorrectanswerbyusingrepresentation,anotherimportant
strategy that will solve our problem. Proving that is true for all values of n using

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 5/30
8/23/2016 Yuzu: Python Programming in Context

mathematical induction would be a daunting task for most people. However, lets visualize the problem of
addingupthenumbersfrom1toNasshowninFigure1.1.
Inthisrepresentationoftheproblem,weshoweachofthenumberswewanttoaddasarowofcircles,thus
representingtheadditionof1+2+3+4.Nowwecouldjustcountthecirclestogetouranswer,butthatis
not very interesting and does not prove anything. The interesting part comes in Figure1.2, where we have
takenallfourrowsofdots,duplicatedthem,andflippedthemdiagonally.Thedotsnowformarectanglethat
is4rowshighand5columnswide.Itisnoweasytoseethatthetotalnumberofdotsisjust45=20.Butwe
havetwiceasmanydotsaswestartedwith,sothenumberofdotswestartedwithis202=10.
Ifyougeneralizeourexamplealittlebit,itiseasytoseethatthisgraphicaltrickworksnomatterhowmany
rowsofdotsweuse.Therefore,wehaveshownaproofforaninterestingmathematicalsequence,butbecause
wechoseagoodrepresentationfortheproblemwehavenothadtodoanythingmorecomplicatedthansimple
multiplicationanddivision.

Figure1.2Thesumofthenumbers1toNis

1.5PythonOverview
InthisbookthelanguageyouwillusetowriteyourcomputerprogramsiscalledPython.Whydidwechoose
PythoninsteadofalanguagelikeC++orJava?Theanswerissimple:Wewantyoutofocusonlearningthe
problemsolvingstrategiesandtechniquesthatacomputerscientistuses.Programminglanguagesaretools
andPythonisagoodbeginningtool.LanguageslikeJavaandC++arefinetoolsaswell,buttheyrequireyou
tokeeptrackofmanymoredetailsandtheyarehardertolearnthanPython.
ThebestwaytolearnPythonistotryitoutsoletsgetstarted.Thefirstthingwearegoingtodoisstartthe
Python interpreter. Depending on your operating system, there are any number of ways to do this. For
example,youmightstartaprogramcalledIDLEnamedafterEricIdleofMontyPythonfame.Oryoumight
justtypePythonatthecommandprompt.Nomatterhowyoustartit,youwillknowyouaresuccessfulwhen
youseeawindowsuchastheoneshowninFigure1.3.Inthiscase,wehavestartedthePythoninterpreter
from a terminal window on a MacBook Pro. For detailed instructions on installing and starting Python on
yoursystem,refertoAppendixA.
As you progress through this chapter, you will see that example programs are in boxes called listings, and
commandsthatyoucantypeinteractivelyatthePythonshellareinboxescalledsessions.Wheneveryousee
a session box, we strongly encourage you to try the session for yourself. Also, once you have typed in the
examplewehaveshown,feelfreetotrysomevariationsinordertofindoutforyourselfwhatworksandwhat
doesnot.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 6/30
8/23/2016 Yuzu: Python Programming in Context

Figure1.3ThePythonshell

As we begin to explore Python, we will answer three important questions you should ask about any
programminglanguage:

Whataretheprimitiveelements?
Howcanwecombinetheprimitiveelements?
Howcanwecreateourownabstractions?

1.5.1PrimitiveElements
At the deepest level, the one primitive element in Python is the object. In fact, everything in Python is an
object,andyouwillreadthisrefrainofteninthisbook.Bynowyouareprobablywonderingwhatwemean
by object. After all, if you look around you will see many objects: this book, pencils, pens, your chair, a
computer.WhatdotheseitemshavetodowithPython?Likeyou,Pythonthinksofthethingsinitsworldas
objects. Python even considers numbers to be objectsan idea that may be a bit confusing to you as you
probablydontthinkofnumbersasobjects.ButPythondoes,andwellseewhythisisimportantshortly.
Pythonclassifiesthedifferentkindsofobjectsinitsworldintotypes.Someoftheeasiesttypestoworkwith
arenumbers.Pythonknowsaboutseveraldifferenttypesofnumbers:

Integernumbers
Floatingpointnumbers
Complexnumbers

IntegerNumbers

Integers are the whole numbers that you learned about in math class. We will introduce more of Pythons
primitivetypesasweprogressthroughthischapter.ButbeforewemoveonletslookatPythonsintegersin
moredetail.WecanalreadydoalotwithPythonjustusingintegers.Forstarters,wecanusethePythonshell
westartedafewmomentsagoasacalculator.Letstryafewmathematicalexpressions.Typeinthefollowing
examplesusingthePythoninterpreter.Afteryouhavetypedinanexpression,pressthereturnkeytoseethe
result.
https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 7/30
8/23/2016 Yuzu: Python Programming in Context

>>>2+2
4
>>>10075
25
>>>7*9
63
>>>14//2
7
>>>15//2
7
>>>15%2
1

Session1.1Simpleintegermath

TheexamplesinSession1.1illustratesomeveryimportantPythonconceptsthatyoushouldbecomefamiliar
withassoonaspossible.ThefirstconceptisPythonsevaluationloop.

Figure1.4TheReadEvalPrintloopinPython

Atahighlevel,thePythoninterpreterisreallyverysimple.Itdoesthreethingsoverandoveragain:(1)read,
(2)evaluate,and(3)print.TheseareillustratedinFigure1.4.

First,Pythonreadsonelineofinput.Inthefirstexample,Pythonreads2+2,thenitevaluatestheexpression
2+2anddeterminesthattheansweris4.Finally,Pythonprintstheresultingvalueof4.Afterdisplayingthe
result,Pythonprintsthecharacters>>>toshowyouthatitiswaitingtoreadanotherexpression.Thethree
characters>>>arecalledthePythonprompt.

Ingeneral,aPythonexpressionisacombinationofoperatorsandoperands.Whenweevaluateanexpression,
wegetaresult.IntheexamplesinourPythonsession,theoperatorsarefamiliarmathematicaloperators+,
,*,and//.Youmaybemoreusedtoandformultiplicationanddivision,butyouwillnotfindthose
symbolsonastandardkeyboard,soPython,andalmostallotherprogramminglanguagesuse*and/.

Onethingthatmaysurpriseyouintheexampleistheresultoftheexpression15//2.Ofcourse,weallknow
that 15 divided by 2 is really 7.5. However, because both operands are integer objects and // is the integer
divisionoperator,Pythonproducesanintegerobjectasaresult.Integerdivisionworkslikethedivisionyou
learnedwhenyouwereyoung.15dividedby2equals7,remainder1.Youcanfindouttheremainderpartof
theresultusingthemodulooperator(%).Forexample,15%7evaluatestotheremaindervalueof1.
Integerdivisionisreallyusefulinsomecases,butitcanalsotripyouup.Whatifyouwanttodivide15by2
https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 8/30
8/23/2016 Yuzu: Python Programming in Context

andget7.5astheanswer?Inordertogettheresultasafloatingpointnumberyoumustusethefloatingpoint
divisionoperator/.

Exercises

1.1Findthesumofthenumbers8,9,and10.

1.2Findtheproductofthenumbers8,9,and10.

1.3Computethenumberofsecondsinayear.

1.4Computethenumberofinchesin1mile.

1.5Computethenumberof2ftsquaretilestocoverthefloorofa10by12ftroom.

1.6Computethenumberofhandshakesrequiredforeachpersoninyourclasstoshakehandswithevery
otherpersonexactlyonetime.

1.7Findtheaverageageoffivepeoplearoundyouusingintegerdivision.Doublecheckyouranswer.

FloatingPointNumbers

Floatingpointnumbersare Pythons approximation ofwhat youcalled real numbersinmath class. We say


thatfloatingpointnumbersareanapproximationbecauseunlikerealnumbers,floatingpointnumberscannot
haveaninfinitenumberofdigitsfollowingthedecimalpoint.InPythonyoucantellthedifferencebetweena
floatingpoint number and an integer because a floatingpoint number has a decimal point. Session 1.2
presentssomeexamplesofmathusingfloatingpointnumbers.

>>>2.0+2.0
4.0
>>>2+2.0
4.0
>>>15/2
7.5
>>>2.0**50
1125899906842624.0
>>>2.5**25
8881784197.0012531
>>>2.0**500
3.2733906078961419e+150
>>>1.33e+5+1.0
133001.0

Session1.2Floatingpointmath

Noticethatwehaveaddedsomethingnewinthisexample:the**symbol,whichiscalledtheexponentiation
operator. So 2.0 ** 50 is really two to the fiftieth power. You should also notice that when the result of a
floatingpoint operation gets really big, Python uses scientific notation to express the results. The Python
number3.273e+150reallymeans3.273times10tothe150thpower,or3273followedby147zeros!Avery
big number indeed. Notice also that you can use floatingpoint numbers in scientific notation as part of a
Pythonexpression.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 9/30
8/23/2016 Yuzu: Python Programming in Context

Exercises

1.8 Find the average age of five people around you using floatingpoint division. Doublecheck your
answer.

1.9Findthevolumeofaspherewitharadiusof1usingtheformula4/3r3.

1.10Compute1/3of15.Didyougettherightanswer?

1.11TheAndromedagalaxyis2.9millionlightyearsaway.Thereare5.8781012milesperlightyear.
HowmanymilesawayistheAndromedagalaxy?

1.12HowmanyyearswouldittaketotraveltotheAndromedagalaxyat65milesperhour?

Although3.273e+150isagoodapproximation,weknowthattherearenotreally147zerosintheresult.One
ofthedisadvantagesofusingscientificnotationisthatyoulosesomeprecisioninyourresult.Ifyouwantto
getveryexactresults,integersallowustodocalculationstounlimitedprecision.Session1.3showsthereal
valueof2**500usingintegers.

>>>2**500
327339060789614187001318969682759915221664204604306478948329136809
613379640467455488327009232590415715088668412756007100921725654588
5393053328527589376
>>>

Session1.3Theuseofintegerstoobtainverypreciseanswersforlargenumbers

Exercises

1.13Computethefactorialof13.

1.14Compute2tothe120thpower.

1.15Iftheuniverseis15billionyearsold,howmanysecondsoldisit?

1.16 How many handshakes would it take for each person in Chicago to shake hands with every other
person?

ComplexNumbers

The final primitive numeric type in Python is the complex number. As you may remember, complex
numbershavetwopartstothem,arealpartandanimaginarypart.InPythonacomplexnumberisdisplayed
asreal+imaginaryj.Forexample,5.0+3jhasarealpartof5.0andanimaginarypartof3.Althoughwe
mentioncomplexnumbersheretogiveyouacompletelistofthenumericprimitives,wewillnotgointoany
additionaldetailsatthispoint.

SummaryofNumericTypes

What happens when we mix integers and floatingpoint numbers? Lets look at the examples shown in
Session1.4tofindout.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 10/30
8/23/2016 Yuzu: Python Programming in Context

>>>100*3.4
340.0
>>>100000000000000000000000000*3.4
3.4000000000000003e+26
>>>10000000*1000000
10000000000000
>>>1000000000/1000000000
1
>>>1000//10.0
100.0
>>>1000/10.2
98.039215686274517
>>>1000//10.2
98.0
>>>5+4+3j
(9+3j)

Session1.4Mixingintegers,longintegers,floats,andcomplexnumbers

Whenmixingdifferenttypesofnumbers,youcanfigureoutwhattheresultwillbeconvertedtobyapplying
thefollowingrules:

1.Ifeitherargumentisacomplexnumber,theotherisconvertedtocomplex.
2.Ifeitherargumentisafloatingpointnumber,theotherisconvertedtofloatingpoint.
3.Forallotherarguments,bothmustbeplainintegersandnoconversionisneeded.

Noticethatwhenusingfloatingpointnumberswiththeintegerdivisionoperatortheresultisafloatingpoint
numberwiththefractionalparttruncated.YoucanalsotellPythontoexplicitlyconvertanumbertoeitheran
integer or floatingpoint number by using the int or float functions. For example, float(5) will convert the
integer 5 to the floatingpoint number 5.0. When converting floatingpoint numbers to integers, Python
alwaystruncatesthefractionalpartofthenumber.Forexample,int(3.99999)willconvertthefloatingpoint
number3.99999totheinteger3.
In summary, we have seen that Python supports several different types of primitive objects in the number
family: integers for ordinary simple math or, when precision is required or when dealing with very large
numbers floatingpoint numbers, for working with scientific applications or accounting applications where
weneedtokeeptrackofdollarsandcents.WehaveseenthatPythoncanbeusedtomakesimplenumerical
calculations.However,atthispoint,Pythonisnothingmorethanacalculator.Inthenextsectionwewilladd
someadditionalPythonprimitivesthatwillgiveusalotmorepower.

1.5.2NamingObjects
Veryoftenwehaveanobjectthatwewouldliketoremember.Pythonallowsustonameobjectssothatwe
can refer to them later. For example, we might want to use the name pi rather than the value 3.14159 in a
mathematicalexpression.Wemightalsowanttogiveanametoavaluethatwearegoingtouseoverandover
againratherthanrecalculatingiteachtime.
InPythonwecannameobjectsusinganassignmentstatement.Astatementislikeanexpressionexceptthat
itdoesnotproduceavalueforthereadevalprintlooptoprint.Anassignmentstatementhasthreeparts:(1)
thelefthandside,(2)therighthandside,and(3)theassignmentoperator(=).Theleftsidecontainsthename
weareassigningtoandtherightsidecanbeanyPythonexpression.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 11/30
8/23/2016 Yuzu: Python Programming in Context

name=pythonexpression

WhenthePythoninterpreterevaluatesanassignmentstatement,itfirstevaluatestheexpressionthatitfinds
ontherighthandsideoftheequalssign.Oncetherighthandsideexpressionhasbeenevaluated,theresulting
object is referred to using the name found on the left side of the equals sign. In computer science, we call
thesenamesvariables.Moreformally,wedefineavariabletobeanamedreferencetoadataobject.Inother
words,avariableissimplyanamethatallowsustolocateaPythonobject.
Supposewewanttocalculatethevolumeofacylinderwheretheradiusofthebaseis8cmandtheheightis
16cm.Wewillusetheformulavolume=areaofbase*height.Ratherthancalculateeverythinginonebig
expression, we will divide the work into several assignment statements. First, we will name the numeric
objectspi,radius,andheight.Next,wewillusethenamedobjectstocalculatetheareaofthebaseand
finallythevolumeofthecylinder.Session1.5showshowweusethissequenceofassignmentstatementsand
Pythonarithmetictosolveourproblem.

>>>pi=3.14159
>>>radius=8.0
>>>height=16
>>>baseArea=pi*radius**2
>>>cylinderVolume=baseArea*height
>>>baseArea
201.06175999999999
>>>cylinderVolume
3216.9881599999999
>>>

Session1.5Calculatingthevolumeofacylinderwithassignmentstatements

AfterstudyingSession1.5,youmayhavesomequestions:

HowistheuseoftheequalssigninPythondifferentfromwhatyoulearnedinmathclass?
IfyouchangethevalueforbaseAreawillcylinderVolumeautomaticallychange?
WhydoesntPythonprintoutthevalueofpiafterthefirstassignmentstatement?
WhatnamesarelegalinPython?

Letslookatthesequestionsoneatatime.TheequalssigninPythonisverydifferentfromwhatyoulearned
inmathclass.Infact,youshouldthinkofitnotintermsofequalitybutratherastheassignmentoperator,
whichhasthejobofassociatinganamewithanobject.Figure1.5illustrateshownamesareassociatedwith
objectsinPython.AllthenamesandobjectsinthisfigurecomefromSession1.5.Therelationshipsbetween
namesandtheobjectstheyreferenceareindicatedbythearrowsbetweenthem.
Anotherwayofthinkingaboutassignmentistoimaginethatanassignmentstatementisliketakingasticky
labelwithanamewrittenonitandattachingittoanobject.Youknowthatyoucanputmorethanonesticky
labelonanobjectintherealworld,andtheanalogyholdstrueinthePythonworldaswell.APythonobject
canhavemorethanonename.Forexample,supposeyoumadethefollowingadditionalassignmentx=8.0.
Afterexecutingthatstatement,youcouldaddanotherlabelcalledxwithanotherarrowpointingattheobject
8.0,asshowninFigure1.6.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 12/30
8/23/2016 Yuzu: Python Programming in Context

Figure1.5AreferencediagramillustratingsimpleassignmentinPython

Variables can take the place of the actual object in a Python expression. When Python evaluates the
expressionpi*radius**2,itfirstlooksuppiandradiustoseewhatobjectstheyrefertoandthensubstitutes
thevaluesintotheexpression.Theexpressionthusbecomes3.14159*8.0**2.Next,Pythonevaluates8.0
**2togetanintermediateresultof64.0.Pythonthenevaluates3.14159*64.0togetthevalue201.06176.
Aftertherighthandsideoftheexpressionisevaluated,PythonassignsthenamebaseAreato201.06176.
Letslookatonemoreexampleusingassignment.ConsiderthePythonstatementsinSession1.6.

Figure1.6Referencediagramafterx=8.0

>>>a=10
>>>b=20

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 13/30
8/23/2016 Yuzu: Python Programming in Context

>>>a=b

Session1.6Pythonevaluatesassignmentstatementsinsequence

After these three assignment statements, what object does a refer to? As you think about this question it is
veryimportanttorememberthatPythonevaluatesthestatementsfromtoptobottom,oneafteranother.Lets
rephrasewhatisgoingonintheorderthatPythonperformsitswork.

1.Assignthenameatotheintegerobject10.
2.Assignthenamebtotheintegerobject20.
3.Findtheobjectnamedb,thenassignthenameatothatobject.

Figure1.7Resultofassignmenta=b

Theanswertoourquestionisthatanowreferstotheobject20.ThisisshowninFigure1.7.Inaddition,since
wehavenotchangedwhatbreferstosincetheoriginalassignment,bcontinuestorefertotheobject20.If
youareconfusedbythisexample,trytodrawthereferencediagramyourselfonestepatatime.

Now that you understand more about the assignment operator you will understand that attaching the name
baseAreatoadifferentobjectwillhavenoimpactonthenamecylinderVolume.Tomakethenewvaluefor
baseArea change the value of cylinderVolume, you will need to ask Python to execute the statement
cylinderVolume=baseArea*height.

Sinceassignmentisastatementratherthananexpression,itdoesnotreturnavalue.Thismeansthatthereis
nothing for the readevalprint loop to print out. That is why you do not see any output following the
assignmentstatementsinSessions1.5and1.6.

A name on a line all by itself is a very simple Python expression. Notice that just typing a name causes
Pythontofindthevaluefortheobjectandreturnitastheresultoftheexpression.Youcanseeexamplesof
thisinSession1.5.

ThereareseveralimportantrulestorememberaboutnamingthingsinPython.Namescanincludeanyletter,
number,oran_(underscore).Namesmuststartwitheitheraletteroran_.Pythoniscasesensitive,which
meansthatthenamesbaseArea,basearea,andBaseAreaarealldifferent.SomenamesarereservedbyPython
for its own use. These are called keywords and correspond to important Python capabilities that you will
learnabout.Table1.1showsyouallofPythonsreservedkeywords.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 14/30
8/23/2016 Yuzu: Python Programming in Context

Table1.1Pythonsreservedwords

Exercises

1.17GiventhefollowingPythonstatements:

a=79
b=a
a=89

(a)Drawareferencediagramtoshowthelabelsandobjectsafterthefirsttwostatements.

(b)Drawareferencediagramtoshowthelabelsandobjectsafterthelaststatement.

1.18Whichofthefollowingarelegalvariablenames:

(a)_abc123

(b)123abc

(c)abc123_

(d)_123

1.19Considerthefollowingstatements:
a=10
b=20
c=a*b
d=a+b
Drawareferencediagramtoshowalltheobjectsandnamesafterevaluatingthesestatements

1.20WhatarethevaluesofaandbafterPythonevaluateseachofthefollowingfourstatements?
a=10
b=20
a=b
b=15

1.21Considerthefollowingstatements:
idx=0
idx=idx+1
idx=idx+2

WhatisthevalueofidxafterPythonevaluateseachofthethreestatements?

1.5.3Abstraction
Abstractionisdefinedasaconceptorideanotassociatedwithanyspecificinstance.Forexample,youcan
thinkofmathematicalfunctionsonyourcalculatorsuchassquareroot,sine,andcosineasabstractions.These
functionscancalculateavalueforanynumber,butthemethodofcalculatingasquarerootisindependentof
theparticularnumber.Inthatwaywecanthinkofafunctionthatcalculatesasquarerootasbeingthegeneral
https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 15/30
8/23/2016 Yuzu: Python Programming in Context

idea.Thesquarerootfunctionworksequallywellforallnumbersbecauseitisageneralfunction.Wedonot
haveaspecialsquarerootfunctionforeachpossiblenumber,justonefunctionthatworksforallnumbers.
Onewayofthinkingaboutfunctionsisasablackbox.Yousendinformationintotheblackboxononeside
andnewinformationcomesoutontheotherside.Youdontknowexactlywhatgoesoninsidetheboxbut
youdoknowthebehaviorthattheboxshouldexhibit.Figure1.8illustratesthisconceptforthesquareroot
function.
ThePythonlanguagecontainsmanysuchabstractions.ManyofthenewthingswewillseeinPythonfrom
here on are in fact abstractions built using the Python primitives we have already talked about or will talk
aboutinthefirstfewchaptersofthisbook.Inotherwords,muchofPythoniswritteninPython.

Figure1.8Ablackboxviewofthesquarerootfunction

TheturtleModule

ManyoftheadditionalpartsofPythonfunctionalityarefoundinmodulesanoptionalpartofPythonthat
implements an abstraction that is designed to make programming easier. In order to get the power of a
module,youhavetotellPythontoloadthemoduleyouwant.Thestatementyouneedtousetoloadamodule
isimportmodulename.
When you import a module, an object is created inside Python. That object has the type module and has a
name attached to it that matches the name you used on the import line. Every object in Python has three
importantcharacteristics:(1)anidentity,(2)atype,and(3)avalue.Inaddition,somePythonobjectshave
specialvaluescalledattributes,andsomePythonobjectsalsohavemethodsthatallowustoasktheobjectto
dosomethinginteresting.Letslookatasimpleexamplebeforewegoanyfurther.
The example we will use is the turtle module. The turtle module provides us with a simple graphics
programmingtoolknownasaturtle.Verysimply,aturtleisanobjectthatwecancontrol.Aturtlecanmove
forwardorbackward,anditcanturninanydirection.Whenaturtlemoves,itdrawsalineifitstailisdown.
AturtleisaPythonobjectthathasbothattributesandmethods.Someoftheattributesofaturtleareshownin
Table1.2.

position Thecoordinatesoftheturtleonthescreen
heading Thedirectiontheturtleisfacing
color Thecoloroftheturtle
tailposition Theturtlestailcanbeupordown

Table1.2Turtleattributes

Name Parameter(s) Description


Turtle None Createsandreturnsanewturtleobject
forward Distance Movestheturtleforward
backward Distance Movestheturlebackward
right Angle Turnstheturtleclockwise
https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 16/30
8/23/2016 Yuzu: Python Programming in Context

left Angle Turnstheturtlecounterclockwise


up None Picksuptheturtlestail
down None Putsdowntheturtlestail
color Colorname Changesthecoloroftheturtlestail
fillcolor Colorname Changesthecolorthattheturtlewillusetofillapolygon
heading None Returnsthecurrentheading
position None Returnsthecurrentposition
goto x,y Movestheturtletopositionx,y
begin_fill None Remembersthestartingpointforafilledpolygon
end_fill None Closesthepolygonandfillsitwiththecurrentfillcolor
dot None Leavesadotatthecurrentposition

Table1.3Summaryofsimpleturtlemethods

ThemethodsoftheturtlearesummarizedinTable1.3.Tostart,wewilljustconcernourselveswithafewof
them.Forexample,wecantelltheturtletogoforward,backward,turnleft,turnright,oraskforitsposition.
Theturtlehasatailthatcanbeupordown.Whenthetailisdownandtheturtlemoves,itdrawsaline.Ifthe
tailisupandtheturtlemoves,nothingisdrawn.Session1.7showsaPythonsessionwherewecreateaturtle
objectandtryoutsomeoftheturtlescapabilities.

Letslookatthisexamplelinebyline.Inline1weuseanimportstatementtoloadtheturtlemodule.Inline2
weaskPythontoevaluatethenameturtle.Pythonrewardsusbytellingustheidentityoftheobjectthatturtle
is assigned to. If you look at the identity, you will see it is telling the location of the source code for the
module. This location will vary according to the kind of computer you are using. If we were really
adventurous,wecouldgothereandlookatthePythonmethodsthatarestoredintheturtle.pyfile.

>>>importturtle
>>>turtle
'/Library/Frameworks/Python.framework/Versions/3.2/lib
/python3.2/turtle.py'>
>>>gertrude=turtle.Turtle()
>>>gertrude
>>>gertrude.forward(100)
>>>gertrude.right(90)
>>>gertrude.forward(50)
>>>gertrude.position()
(100.00,50.00)
>>>gertrude.heading()
270.0
>>>

Session1.7Usingtheturtlemodule

Once we have the turtle module loaded, we will start to use the methods in the module to do something
interesting. In line 6 we have an assignment statement, in which we are making a new Turtle object and
givingitthenamegertrude.
Before we go any further with this example, we need to explain line 6 in more detail. In particular the

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 17/30
8/23/2016 Yuzu: Python Programming in Context

expressionturtle.Turtle()containsanewoperatorthedot(.).ThedotoperatortellsPythontolookupthe
namerightinfrontofthedotandreturntheobjectitnames.Thisprocessisreferredtoasdereferencing.In
this case the dereferencing operation allows Python to find the turtle module. Once we have the module,
Pythoncontinueslookingforthenametotherightofthedot.InthiscasePythonlooksforthenameTurtle.A
goodwaytothinkofthisisthatTurtleisinsidetheturtlemodule.Sothefirst.getsustotheturtlemodule
andtheninsidetheturtlemodulewefindtheobjectnamedTurtle.
Wecanthenseethatturtle.Turtle()isamethodthatcreatesanewobject.ThetypeofthenewobjectisTurtle.
Amethodthatcreatesanewobjectiscalledaconstructor.Newturtlesthatweconstructarecalledinstances
ofthetypeTurtle.
Ifyouaretypingthissessioninteractivelyasyouarereading,youwillseethatwhenaturtleiscreatedanew
windowappearsonthescreen.Thetriangleinthemiddleofthescreenistheturtle.Whenanewinstanceof
Turtle is first created, it is at position (0, 0) in the middle of the window. The turtles initial heading is 0
degrees,orstraighttotheright.Thecolorattributeforthenewturtleisblack.Asyoumovetheturtlearound,
itremembersitslatestposition,whatdirectionitisfacing,andwhetheritstailisupordown.
Thenexttwolinessimplydemonstratethatwhenyouevaluateanamethatcorrespondstoamorecomplicated
objectyougetbackPythonsrepresentationofthatobject.Unlikeanumberwheretherepresentationisself
evident, a turtles representation gives you a unique identification for the object. In this case we see that
gertrude is . To be even more specific, the result is telling us that the type of gertrude is turtle.Turtle.
Furthermore,theturtlecanbefoundatlocation0x101393950inthecomputersmemory.
Now that we have our new turtle object, named gertrude, the next three lines instruct gertrude to do some
drawing. As you might guess, the line gertrude.forward(100) causes the turtle to move forward 100 units.
Onceagain,thedotnotationisveryimportanttounderstandinghowPythoninterpretsthestatement.First,the
dot tells Python to dereference the name gertrude. When Python finds the object, it evaluates the method
forwardthatisinsidetheturtle.Theforwardmethodknowsthatitneedstomoveforward100unitsbecause
wepassitaparameterof100.Justasmathematicalfunctionslikecos(20)or takeparameters,Python
functionsandmethodsmayalsoacceptparameters.
Functionsareabstractionsofgeneralizedbehaviors.Parametersarethewaywetellthefunctionspecifically
whatitshoulddo.Inthiscasewewantgertrudetomoveforward100units,thenturnright90degrees,then
moveforwardagain,butthistimebyonly50units.Ifyoutrytorunthisexampleonyourown,yourwindow
shouldlookjustlikeFigure1.9.
ThelastfourlinesofSession1.7showthatwecanalsousemethodstoasktheturtleforinformationabout
itself.First,weaskgertrudetotelluswhereitiswiththemethodcallgertrude.position().gertruderepliesthat
itisat(100.0,50.0).Whatisinfacthappeningisthattheposition()methodreturnsthevalue(100.0,50.0)
andtheprintpartofthereadevalprintloopprintsoutthatvalue.Allfunctionscanreturnvalues,andsothey
canbeincludedinPythonexpressions.Thefactthatthepositionmethodreturnsavalueisnodifferentthan
thecosinefunctionreturningavalue.Similarlytheheadingmethodtellsusthatgertrudeiscurrentlyfacing
270degrees.
Noticethatintheworldofourturtle,thecoordinates(0,0)areinthecenterofthewindow.Thexcoordinate
growsinapositivedirectionastheturtlemovestowardtheright.Iftheturtlemovestowardtheleftsideofthe
window,thexcoordinatesgetsmallerandarenegativetotheleftofthemiddleofthewindow.Similarly,they
coordinate grows as the turtle moves toward the top of the window and gets smaller as the turtle moves
towardthebottom.Onepixelonyourcomputerscreencorrespondsto1unitofturtlemovement.Iftheturtles
headingis0degrees,itisfacingtotheright90degreesisup,180degreesistotheleft,and270degreesis
down.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 18/30
8/23/2016 Yuzu: Python Programming in Context

Figure1.9Usingmethodstocontrolaturtle

Theturtleobjectsfoundintheturtlemodulehavemanyothermethods.Table1.3showsjustafewofthem.
Wellintroduceadditionalmethodsaswehaveneedfortheminfutureexamples.

Exercises
1.22Createaturtlecalledsven.Nowtellsventogoforward10.Whatissvenspositionnow?

1.23Createaturtlecalledoleandtelloletoturnright45degreesandgoforward50.Noticethatyounow
havetwoturtlesinthesamewindow.

1.24Onasheetofgraphpapersketchoutasimplelinedrawingofsomething.Usingtheturtlemethodsin
Table1.3,recreateyourlinedrawing.

WritingYourOwnFunctions

We are not limited to the functions that the authors of Python have given us. We, too, can write our own
functions to add our own abstractions to the Python language. In fact, functions are just another kind of
Pythonobjectwithacoupleofspecialcapabilities.WedefineafunctioninPythonusingthedefstatement.
Listing1.1showsageneraltemplateforusingthedefstatementtodefineafunction.

Listing1.1Atemplateforfunctiondefinition

The def statement begins by giving the function a name. Next we specify any parameters we want our
function to accept. In Python we can have zero or more parameters for any function we write. All the
parameters must be listed inside the parentheses that follow the function name. Next we have a colon
character,whichtellsthePythoninterpreterthatthesequenceofindentedstatementsthatfollowareallpartof
thefunction.Pythonknows tostopreadinglines for thedef statement when it encounters a line that is not
indented.WecallagroupofPythonstatementsthatareindentedatthesamelevelablock.
https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 19/30
8/23/2016 Yuzu: Python Programming in Context

Nowletstrytowritearealfunction.Supposethatweareworkingonagraphicsprogramthatrequiresusto
draw a lot of squares. Telling the turtle how to draw a square each time we need one is tiresome and
repetitious.Infact,asquareisanexampleofanabstraction.Weknowthatasquareisageometricshapethat
hasfoursidesofequallength,andfour90degreecorners.Whatwedontknowishowlongthesidesarefor
anyparticularsquare.
Ourgoalistowriteafunctionthatcanuseanyturtletodrawasquareofanysize.Thatstatementsuggests
thatweneedtwopiecesofinformationtosolveourproblemnamely,theturtlethatwilldrawthesquareand
thesizeofthesquare.Thesepiecesofinformationwillbecometheparameterstoourfunction.
Thenextstepistousetheparametersalongwiththebuiltinmethodsofaturtle.Wecandrawasquareby
movingtheturtleforwardandturningrightby90degreesfourtimesinarow.Listing1.2showsacomplete
Pythonfunctionfordrawingasquarewithaturtle.
Noticethatthestatementsinthefunctionarethesameturtlecommandsthatwetypedininteractivelywhen
wefirststartedusingtheturtle.Whenthecommandsareinsidethefunction,theyarejustgroupedtogetherso
thatwecanhaveallthecommandsrunasifwehadtypedthemoneafteranother.Thisisoneofthegreat
powersofwritingafunction.

Listing1.2Afunctiontodrawasquareusingaturtle

ItisalsoimportanttorememberthatPythonwilldothecommandsinexactlytheordertheyaretypedinthe
function.
OnefinalthingtonoteaboutthecodeinListing1.2isthe#characterasseenonLines3,5,7,and9.Thisis
known as the comment marker. Any text following the comment marker is ignored by the Python
interpreter.CommentsallowtheprogrammertoplacedescriptivedocumentationintoPythoncodethatwill
notimpacttheexecutionoftheprogram.
TypethedrawSquarefunctionintoatextfileexactlyasshownhere.Afteryoutypeinthefunction,saveittoa
filecalledds.py.Youhavejustcreatedyourfirstmodule!Inthesamewaythatturtleisamodulecreatedby
otherPythonprogrammers,youhavecreatedamodulecalledds.WecannowusethedrawSquarefunction,as
showninSession1.8.Afterrunningthecommandsinthissession,youshouldhaveanimagethatlookslike
Figure1.10.

>>>fromdsimport*
>>>importturtle
>>>t=turtle.Turtle()
>>>drawSquare(t,150)
>>>

Session1.8APythonsessiontodemonstratecallingafunction

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 20/30
8/23/2016 Yuzu: Python Programming in Context

Thereisalotgoingoninthissimpleexample,butthereareonlyacoupleofnewthingsthatyouhavenot
seenbefore.First,weimporttheturtleanddsmodules.Wethenmakeanewturtleobjectnamedt,andnext
wecalldrawSquare.PythonknowsthatitshouldtreattheobjectdrawSquarelikeafunctioncallbecauseof
theparenthesesafterthename.TheparenthesesareoperatorsthattellPythontotreattheobjectasafunction.
YoucanevenshowyourselfthatdrawSquareisaplainoldobjectbysimplytypingdrawSquarewithoutthe
parenthesesafterthename.Ifyoudoso,Pythonwilltellyousomethinglike.

Figure1.10TheresultofthestatementsinSession1.8

When we call the function drawSquare, we pass it two objects, t and 150. When a function is called, the
objects are matched up with the parameter names they were given when we defined the function. The first
parameterinthelistnamesthefirstobject,thesecondparameterinthelistnamesthesecondobject,andso
on.ThismeansthatinsidethedrawSquarefunctiontgoesbythenamemyTurtle.Theturtleobjectnowhas
twonames.Similarly,insidethedrawSquarefunction,150nowhasthenamesideLength.
Figure1.11showsareferencediagramtohelpyouunderstandhowallthenamesandobjectsarematchedup.
This diagram is simplified somewhat, but we will add more details to such diagrams in later chapters. The
importantthingtonoticeistherelationshipbetweenthenamesoftheparametersofafunctionandtheobjects
thatarepassedtothefunctionwhenitiscalled.
In particular, notice the difference in location between the Turtle constructor and the drawSquare function.
Whenweexecutethestatementimportturtle,wearecreatingamodule,andanyfunctionsinsidethemodule
arehiddenfromusunlessweusetheturtleprefix.However,ifweusethestatementfromdsimport*,all
thefunctionsdefinedinthefileds.pyarevisibletouswithoutthedsprefix.
InFigure1.11therearethreenames:(1)turtle,whichreferencestheturtlemoduleweimportedpreviously
(2)t,whichreferencestheturtleobjectwecreatedwiththecalltotheturtleconstructor(turtle.Turtle()).You
can see the turtle constructor inside the turtle module. (3) drawSquare references the function object we
defined for drawing squares. In the function object named drawSquare, you can see that there are two
additional names: (1) myTurtle, which references the turtle object named t, and (2) sideLength, which
referencestheintegerobject150andhasnoothername.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 21/30
8/23/2016 Yuzu: Python Programming in Context

Figure1.11AreferencediagramforthefunctiondrawSquare

Exercises

1.25ModifythedrawSquarefunctiontodrawarectanglewhosewidthistwicethesideLength.

1.26CreateanewfunctioncalleddrawRectanglethattakesthreeparameters:myTurtle,width,andheight.

1.27 Suppose that in Session 1.8 we had used the expression import ds instead of from ds import *.
Continuetherestofthesession.Drawareferencediagramthatillustratesthissession.

1.28Callyourfunctionasfollows:drawRectangle(t,50,300).

1.29Drawareferencediagramforthepreviousproblem.

1.5.4Repetition
AlthoughthedrawSquarefunctionwediscussedearlierworkedjustfine,thereisonethingthatisunsatisfying
aboutoursolution.Wehadtoduplicatethemoveforwardandturntotherightstatementsfourtimes.Inorder
to eliminate this duplication, Python provides us with a statement that allows us to repeat a block of code
multiple times. This statement is called a for loop. The for loop is an example of using repetition in our
program.
BeforewerewriteourdrawSquarefunction,letslookatthestructureofaforlooptogetabetterideaofhow
thisstatementworks:

foriinrange(n):
statement1
statement2

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 22/30
8/23/2016 Yuzu: Python Programming in Context

Noticethattheforlooptemplatehassomesimilaritytothedeftemplate.Thereisabeginninglineendingwith
acolon,followedbyanindentedblockofcode.Allyouneedtoknowaboutaforloopatthispointisthat
eachstatementintheindentedblockwillbeevaluatedntimes,wherenistheparametertotherangefunction.
Wewilltalkmoreabouttherangefunctionaswellastheiandinpartofthestatementshortly.Ifthefirstline
wasforiinrange(10):,theneachstatementwouldbeevaluatedtentimes.
NowletsseehowwecanusethisideaofsimplerepetitiontomakeourdrawSquarefunctionmoreelegant.In
fact,allwearegoingtodoiswrapthetwolinesthattelltheturtletomoveforwardandturnrightinsidea
loopthatwillbeevaluatedfourtimes.YoucanseethenewandimproveddrawSquarefunctioninListing1.3.

Listing1.3AbetterversionofthedrawSquarefunction

DrawingaSpiral

Our goal for this section is to understand how we can use the for loop to have the turtle create a more
complicated square spiral pattern. To create a spiral pattern, we need to have the sides of our square grow
eachtimetheturtlemovesforward.Wecanmakethathappeneasilywhenweunderstandthetwocomponents
fromtheforstatementweignoredamomentago.First,letslookattherangefunction.IfyouaskPythonto
evaluatetheexpressionrange(5),youwillgetbackanobjectrepresentingthesequenceofnumbers[0,1,2,3,
4].
Therangefunctionisveryversatileinthatitcancreateallkindsofsequencesdependingontheparameters
thatwesupply.Therearethreewayswecancallrange:

range(stop):Createsasequenceofnumbersstartingat0goinguptostop1.
range(start,stop):Createsasequenceofnumbersbeginningatstartgoinguptostop1.
range(start,stop,step):Createsasequenceofnumbersbeginningatstartgoinguptostop1counting
bystep.

Exercises

1.30Usetherangefunctiontocreateasequenceofthemultiplesof5upto50.

1.31Usetherangefunctiontocreateasequenceofnumbersfrom10to10.

1.32Usetherangefunctiontocreateasequenceofnumbersfrom10to10.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 23/30
8/23/2016 Yuzu: Python Programming in Context

Figure1.12Aloopvariablenamingeachiteminasequence

Nowthatyouhaveabetterunderstandingoftherangefunction,letslookattheloopvariablethevariable
thatalwaysfollowsthekeywordfor.InListing1.3theloopvariableisnamedi.Thewaytheforloopworks
inPythonisthattheloopvariablestartsoutasthenameforthefirstiteminthesequenceproducedbythe
rangefunctionthefirsttimethroughtheloop.Thesecondtimethrough,theloopvariableisthenameforthe
seconditeminthesequenceandsoonuntiltherearenomoreitemsinthesequence.Wecanuseanyvalid
Pythonnamefortheloopvariable.
Figure1.12showsthesequenceproducedbythecalltorangeandtheiteminthelistnamedbyitemduring
thefourthrepetitionoftheloop.Notethat55isthelastiteminthesequencebecausetheupperbound,60,is
neverincludedintheresultoftherangefunction.
LoopvariablescanbeusedinexpressionsandfunctioncallslikeanyotherPythonnames.Tosolvethespiral
problem,wewillusealoopvariableinafunctionthatdrawsaspiral.Theparameterstothisfunctionwillbe
theturtleandaboundforthelongestsideonthespiral.Wewillstartwiththefirstsidehavinglength1,then
eachtimethroughtheloopwewillincreasethelengthofthesideofthespiralby5.Listing1.4showsthe
functiondrawSpiral.

Listing1.4APythonfunctiontodrawaspiral

Figure1.13ThefirstfouriterationsoftheloopindrawSpiral(t,150)

Figure1.13 illustrates the first four iterations of the for loop in Listing 1.4. In the first iteration, the turtle
draws a line that is 1 unit long because sideLength refers to the first item in the sequence produced by
range(1,maxSide+1,5), which is 1. In the second iteration, sideLength refers to the number 6, so the turtle
draws a line that is 6 units long. In the third iteration, sideLength refers to 11 in the fourth iteration,
sideLengthreferstothefourthnumberinthesequence,whichis16.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 24/30
8/23/2016 Yuzu: Python Programming in Context

Exercises

1.33Modifythespiralfunctiontoturnmorethan90degreesforeachiteration.

1.34Modifythespiralfunctiontoturnlessthan90degreesforeachiteration.

1.35Modifythespiralfunctiontousetheloopvariableasthenumberofdegreestoturn.

1.36Modifythespiralfunctiontouseasecondturtleandcreatetwospiralsinoppositedirections.

1.37WriteafunctiondrawTrianglethattakestwosidelengthsandtheanglebetweenthemanddrawsthe
triangle.(Hint:Youneedtorememberthestartingpoint.)

1.38Writeafunctionthatdrawsaseriesof10squares,witheachsquarebeing5pixelssmalleroneach
side.Thesquaresshouldallstartatthesamelocation.

1.39Redothelastquestionsothatthesquaresareallcentered.

1.40Usetheturtletoplotthefunctiony=x2.

1.41Usetheturtletoplotthefunction

DrawingaCircle

Ourfinalproblemforthischapteristowriteafunctionthatusesaturtletodrawacircleofagivenradius.
Althoughthismayseemlikeadauntingtask,wewillhelpourselvesbysolvingasimplerproblemfirstand
then using what we learn to solve the more general problem. The first thing we must recognize is that the
turtles functionality allows us to draw only straight lines. We will approximate a curved line by drawing
manyshortstraightlines.
Supposethatourproblemwastodrawatriangleratherthanacircle.StartingfromthedrawSquarefunction,it
isnottoohardtoimaginehowwewouldmodifydrawSquaretowritedrawTriangle.Wewouldchangethe
call to range(4) to be range(3) since we only need three sides. The other change we need to make is the
numberofdegreeswepassastheparameterforourrightturnfunction.
Howmanydegreesdoweneedtoturneachtimetodrawanequilateraltriangle?Whenwearealldone,we
wantourturtletobepointedinthesamedirectionitwaswhenwestarted.Thatmeansthataswedrawthe
three sides of our triangle the turtle is going to turn through 360 degrees. That matches our drawSquare
functionwherewemadefour90degreeturns(490=360).Sotodrawatrianglewewilluse3603=120
forourturningangle.Listing1.5showsthesmallchangesmadetothedrawSquarefunction.

Listing1.5APythonfunctiontodrawatriangle

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 25/30
8/23/2016 Yuzu: Python Programming in Context

Table1.4Numberofsidesversusturningangleforseveralpolygons

Wenowhavetwosimpleexamplesofpolygons,theequilateraltriangleandthesquare.Itisrelativelyeasyto
imagine how to write a function to draw a pentagon or an octagon by following the pattern we have
establishedwiththetriangleandsquare.Infact,Table1.4illustratesthevalueswewouldsupplytotherange
andrightfunctionsforseveraldifferentpolygons.
WhatTable1.4suggestsisthatwecanwriteafunctionthatismoreabstractthandrawSquare,drawTriangle,
or even drawOctagon. The abstraction is a regular polygon. Creating one function that can replace many
simpler functions at a higher level of abstraction is a common and important problemsolving technique in
computerscience.
Givenwhatwehavelearned,wecanwriteafunctionthatdrawsanyregularpolygonifwepassthefunctiona
thirdparameter.Thethirdparametertellsthefunctionhowmanysideswewant.Onceweknowhowmany
sidesarerequired,wecaneasilyhavePythoncalculatetheturningangleforususingtheformulaturnAngle=
360/numSides.YoucanseethenewdrawPolygonfunctioninListing1.6.

Listing1.6APythonfunctiontodrawanysizeregularpolygon

Session 1.9 along with Figure 1.14 illustrate several calls to the drawPolygon function. Note that the call
drawPolygon(t,20,20)makesaprettygoodapproximationofacircle.Notealsothatweareassumingthatthe
drawPolygonfunctionhasbeensavedinafilecalleddp.py.

>>>fromdpimport*
>>>importturtle
>>>t=turtle.Turtle()
>>>t.up()
>>>t.backward(200)
>>>t.left(90)
>>>t.down()
>>>drawPolygon(t,100,4)
>>>drawPolygon(t,100,8)
>>>drawPolygon(t,50,20)
>>>drawPolygon(t,20,20)

Session1.9TestingthedrawPolygonfunction

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 26/30
8/23/2016 Yuzu: Python Programming in Context

Figure1.14SeveralpolygonsdrawnbythedrawPolygonfunction

We have worked our way through the problem of drawing a circle by using a large degree polygon as an
approximation.Nowletsreturntotheoriginalproblemstatement,whichasksustodrawacircleofagiven
radius.So,thelasttrickistofigureouthowtousetheradiustocomputethenumberofsidesandtheside
length.
Supposethattogetthesmoothestcirclepossible,evenaverylargecircle,wechoosetoalwayshave360sides
toourpolygon.Thismeansthattheturtlewillalwaysturn1degreeandshouldgiveusasmoothcircleeven
foralargeradius.
Only one question remains: How do we decide what side length to use for a given radius? One good
approximationistousetherelationshipoftheradiusofacircletothecircumference.Ifwearedoingagood
job of approximating a circle, then the circumference of a circle should be very close to the sum of the
individual sides of the polygon. Recall that the circumference of a circle can be calculated from the radius
using the formula circumference = 2 radius. Once we have the circumference, we can calculate an
individual side length by dividing by the number of sides, which we have decided will be 360. Listing 1.7
showsthedrawCirclefunction,whichmakesuseofourdrawPolygonfunctiontodrawacirclewithagiven
radius.

Listing1.7APythonfunctiontodrawacircle

ThedrawCircle function is extremely simple because we have reduced the problem of drawing a circle to
drawing a polygon with a particular number of sides and side lengths. We first calculate the circumference
andthencalculatethesidelengthgiventhecircumferenceandnumberofsides.Sincewealreadyknowhow
todrawapolygon,wedonothavetoredothatwork.Wecanbuildonwhatwehavealreadydoneandusethe
drawPolygon function. Session 1.10 and Figure 1.15 illustrate the use of the drawCircle function to draw
bothalargeandasmallcircle.

>>>fromdpimport*
>>>importturtle
>>>t=turtle.Turtle()
>>>t.up()
>>>t.backward(200)
https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 27/30
8/23/2016 Yuzu: Python Programming in Context

>>>t.left(90)
>>>t.down()
>>>drawCircle(t,20)
>>>drawCircle(t,200)
>>>

Session1.10Drawingacircle

Figure1.15Drawingacircleforagivenradius

Exercises

1.42 Modify the drawCircle function so that the circle is drawn with the center at the turtles present
position.

1.43ThedrawCirclefunctionissomewhatinefficient:forsmallcircles,360sidesisreallyoverkillandfor
verylargecircles360sidesmightbetoofew.Seeifyoucandeviseawaytomakethenumberofsides
andturningangledependentontheradiussothatsmallercirclesusefewersidesandlargercirclesuse
more.

1.6Summary
ThischapterintroducedthefollowingfundamentalbuildingblocksofprogrammingandPython:

Primitivetypes
Expressions
Namingobjects
UsingmodulesandfunctionsprovidedbyPython
WritingyourownfunctionstoextendthefunctionalityprovidedbyPython
In addition, the approach we followed to use the turtle to draw a circle illustrates an important problem
solving pattern that you will use many times as you progress through this book. That pattern can be
summarizedasfollows:

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 28/30
8/23/2016 Yuzu: Python Programming in Context

Simplifyinordertounderstandtheproblembetter.
Generalizetosolvemanyproblemswithonefunction.
Buildonwhatyouhavelearnedtosolvemorecomplexproblems.
Insubsequentchapterswewillcontinuetousethesebasicbuildingblocks.Therewillbemoretoolstoaddto
your toolbox. We have glossed over a few details on some of the ideas introduced in this chapter but will
returntothemlater.Keepinmindtheideabehindthisbook:tofocusonproblemsolvingwhilecontinually
addingtoyourknowledgeofprogrammingandcomputerscience.

KeyTerms
abstraction
algorithm
assignmentstatement
block
complexnumber
computerscience
constructor
dereferencing
dotoperator
expression
floatingpointnumber
generalization
instance
integer
keywords
list
loopvariable
module
name
object
parameter
program
programminglanguage
prompt
Python
repetition
representation
sequenceobject
simplification
type
variable

PythonKeywords
def
for
https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 29/30
8/23/2016 Yuzu: Python Programming in Context

from
import
in
range

Bibliography

[BES05] ChrisL.Barrett,StephenG.Eubank,andJamesP.Smith.Ifsmallpoxstrikesportland.Scientific
American,March2005.
[Eco06] Economist.Thephysicalinternet.TheEconomist,June2006.
[fold] Stanfordfolding@homeproject.http://folding.stanford.edu
[How97] KennethR.Howard.Unjammingtrafficwithcomputers.ScientificAmerican,1997.
[Zei99] PaulZeitz.TheArtandCraftofProblemSolving.Wiley,1999.

https://reader.yuzu.com/#/books/9781284089042/c/6/16!/4/752@0:56.5 30/30
8/23/2016 Yuzu: Python Programming in Context

CHAPTER2
thon

2.1Objectives
Tounderstandhowcomputerscanhelpsolverealproblems
Tofurtherexplorenumericexpressions,variables,andassignment
Tounderstandtheaccumulatorpattern
Toutilizethemathlibrary
Tofurtherexploresimpleiterationpatterns
Tounderstandsimpleselectionstatements
Touserandomnumberstoapproximateanarea

2.2WhatIsPi?
Inthischapterwewillcontinuetoexplorecomputerscience,problemsolving,andthePythonprogramming
language by considering one of the most famous numbers of all: the number pi, often represented by the
Greekletter.Almosteveryonehasatonetimeoranotherusedthevaluepi.Infact,weusedinChapter1
inourcirclecalculations.
Piisdefinedtobetheratioofacirclescircumferencetoitsdiameter(seeFigure2.1).Thisrelationship,=
C/d, gives rise to the familiar equation C = d used to compute the circumference of a circle given the
diameter.Sincethediameterofacircleistwicetheradius,thiscanalsobewrittenC=2rwherer is the
radius.

Figure2.1Pitheratioofcircumference(C)todiameter(d)

Other common formulas that utilize pi include the area of a circle with radius r, A = r2, the volume of a
spherewithradius ,andthesurfaceareaofaspherewithradiusr,S=r2.Inthischapter,we
https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 1/27
8/23/2016 Yuzu: Python Programming in Context

aremoreinterestedinthevalueofpiitself,notinthewaythatpiisused.

Thevalueofpihasbeenamatterofinterestforthousandsofyears.WritingsfromancientEgyptandBabylon
aswellastheBiblecontainreferencestothismysticalnumber.Inyourmathclass,itislikelythatyouwere
told to use 3.14 or 3.14159 as the value of pi. In fact, these values worked fairly well for many of the
problemsthatyouwereaskedtosolve.However,itturnsoutthattheexactvalueofpiisnotassimpleasthis
wouldmakeitseem.

Pi is known in mathematics as an irrational number. This means that pi is a floatingpoint number with an
infinite,nonrepeatingpatternofdecimaldigits.Theactualvalueofpiis

3.14159265358979323846264338327950288419716939937510

wheretheindicatestheinfinitedigits.

Becausepicannotbestatedexactly,thebestthatwecandoisprovideanapproximation,forexample3.14.
Otherusualapproximationsare andthemorecomplex .

Session2.1showstheinteractivePythonenvironmentasweevaluateeachoftheseapproximations.

>>>22/7
3.1428571428571428
>>>355/113
3.1415929203539825
>>>importmath
>>>9801/(2206*math.sqrt(2))
3.1415927300133055
>>>

Session2.1Simpleapproximationsforpi

There is one Python element that is new in Session 2.1: the statement import math. Recall that the import
statement allows us to gain access to a Python module that contains additional functionality that we might
finduseful.Inthiscase,weneedtocomputethesquarerootof2.Afterloadingthemathmodule,wecanuse
thesqrt()functiontodothisforus.

2.3MoreAboutthemathModule
As we mentioned earlier, there are many mathematical helper functions available in the math module.
Unfortunately, it is sometimes difficult to know what they are and what they are called. To remedy this,
Pythonhasabuiltinhelpsubsystemthatallowsyoutoseethedocumentationforaparticularmodule.Touse
help,simplygivethecommandhelp(modulename)wherethemodulenameistheparticularmoduleyouare
interestedin.Forexample,Session2.2showstheresultofaskingforhelponthemathmodule.Youmight
find it interesting that in addition to typical math functions such as sin, log, and pow, the math module
containstwoconstants,eandpi.

>>>help("math")
Helponmodulemath:
NAME

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 2/27
8/23/2016 Yuzu: Python Programming in Context

math
FILE
/libdynload/math.so
MODULEDOCS
http://www.python.org/doc/current/lib/modulemath.html
DESCRIPTION
This module is always available. It provides access to the mathematical functions defined by the C
standard.
FUNCTIONS
acos()
acos(x)
Returnthearccosine(measuredinradians)ofx.
asin()
asin(x)
Returnthearcsine(measuredinradians)ofx.
.
.manymorefunctionshere
.
sqrt()
sqrt(x)
Returnthesquarerootofx.
tan()
tan(x)
Returnthetangentofx(measuredinradians).

tanh()
tanh(x)
Returnthehyperbolictangentofx.
DATA
e=2.7182818284590451
pi=3.1415926535897931
>>>

Session2.2AskingforhelponaPythonmodule

In the sections that follow, we will explore a number of interesting techniques for approximating pi.
Rememberthatnoneofthemcangiveusanexactanswer.

Exercises

2.1Runthehelpcommandasfollows:help('turtle').Notethequotesaroundturtle.

2.2Runthehelpcommandasfollows:help(turtle).Whatisthedifference?

2.3importturtleandrepeatthelastexercise.

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 3/27
8/23/2016 Yuzu: Python Programming in Context

2.4Runthehelpcommandonthemath.sinfunction.

2.5Exploretherandommoduleusingthehelpfacility.Whatdoestherandrangefunctiondo?Whatdoes
therandintfunctiondo?

2.4TheArchimedesApproach
Thefirsttechniquewewillconsider,attributedtothemathematicianArchimedes,makesuseofamanysided
polygontoapproximatethecircumferenceofacircle.Recallthatpiisrelatedtothecircumferenceofacircle
bytheequationC=2rwhereristheradiusofthecircle.Givenacircleofradius1,sometimescalledaunit
circle,thisequationcanberearrangedtogive

The Archimedes approach, shown in Figure 2.2, will use the distance around a polygon that is inscribed
withinaunitcircle.Byusingapolygonwithincreasingnumbersofsides(thereforedecreasingsidelength),
the total distance around the polygon will come closer and closer to the actual circumference of the circle.
Doesthissoundfamiliar?Ofcourse,nomatterhowlargethenumberofsides,wewillnevertrulyreachthe
actualcircle.
Figure2.2showsmoreofthedetailsthatwillbeneededtounderstandhowthisapproximationwillwork.If
weassumethatthepolygonhasnsidesoflengths,wecanthenfocusourattentiononasmallsliceofthe
polygon.Inthetriangleshown,thesidelabeledhwillhavealengthof1sinceweareassumingaunitcircle.
The angle labeled B can be easily computed by remembering that there are 360 degrees in a circle. This
meansthatangleBis360n.Therefore,angle Inaddition,wealsoknowthatthehighlightedtriangleis
arighttrianglesothatthesideoppositeangleAhasalengthof .

Nowwehavetouseabitoftrigonometry.Inarighttriangle(seeFigure2.3),theratiooftheoppositesideto
thelongside(orhypotenuse)isequaltothesineofangleA.Becauseourtrianglehasahypotenuseoflength
1,weknowthat willsimplybeequaltothesineofangleA.HowdowecomputethesineofangleA?The
answeristousethemathlibrary.Aswithsqrtearlier,thesinfunction(aswellasalloftheothertrigonometric
relationships)isavailableonceweimportthemathmodule.

Figure2.2DevelopingtheArchimedesApproachusingan8sidedpolygon

Figure2.3Thesinfunction

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 4/27
8/23/2016 Yuzu: Python Programming in Context

2.4.1ThePythonImplementation
Session 2.3 shows that it is possible to implement Archimedes algorithm by using the interactive Python
environment.Wecansimplytypeinandevaluatestatementsthatfollowthestepsoutlinedhere.Notethatthe
laststepevaluatesthevalueofpiascomputedinthepreviousstatement.

>>>importmath
>>>numsides=8
>>>innerangleB=360.0/numsides
>>>halfangleA=innerangleB/2
>>>onehalfsideS=math.sin(math.radians(halfangleA))
>>>sideS=onehalfsideS*2
>>>polygonCircumference=numsides*sideS
>>>pi=polygonCircumference/2
>>>pi
3.0614674589207183
>>>

Session2.3TheArchimedesapproach:Interactiveimplementation

2.4.2DevelopingaFunctiontoComputePi
Whatifwenowwanttochangethenumberofsidesandtrythecalculationagain?Unfortunately,wewould
needtoretypeallofthestatementssincethecalculationsneedtoberedone.Abetterwaytodothisistouse
abstraction.
Recall that abstraction allows us to think about a collection of steps as a logical group. In Python, we can
defineafunctionthatnotonlyservesasanameforasequenceofactionsbutalsoreturnsavaluewhenitis
called.Wehavealreadyseenthistypeofbehaviorwiththesqrtfunction.Whenwecallsqrt(16),itreturns4.
Listing2.1showsthefunctiondefinitiontemplateasshownearlierwithoneadditionalstatement.Thereturn
statement causes two related actions. First, the expression contained in the statement will be evaluated,
producing a result object. Second, the function will terminate immediately and the reference to the result
object will be returned to the calling statement. It is important to realize that no matter where the return
statementoccurs,itwillbethefinalstatementperformedasreturncausesthefunctiontoterminate.Forthis
reason,returnistypicallythelaststatementinthefunction.
InthecaseoftheArchimedespiapproximation,weneedtodevelopafunctionthathasthebehaviorshownin
Figure 2.4. This function will need one parameternamely, the number of sides we wish to use in the
polygon.Thefunctionwillreturnthevalueofpiusingthestepsaswedevelopedthemearlier.
Listing2.2showsthearchimedesfunction.Asyoulookateachstatement,youshouldnotethatwehaveused
variablenamesthatmatchthoseweusedasweworkedthroughthepreviousproblemsolvingprocess.This
makesiteasiertoseehowthestepswediscoveredinthesolutioncanbemappedintoPythonstatements.In
fact,oneofthecharacteristicsofawellwrittenfunctionistheabilitytoreadthecodeandseetheunderlying
algorithm.

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 5/27
8/23/2016 Yuzu: Python Programming in Context

Listing2.1Templateforafunctiondefinitionwithreturn

Figure2.4AbstractionwiththeArchimedesapproximation

Listing2.2ImplementingtheArchimedesapproximationasafunction

Beforemovingonweshouldnoteoneadditionaluseofthemathlibrary.Recallthatthesinfunctiontakesan
angle and returns the ratio of opposite side to hypotenuse. However, it assumes that the angle will be in
radiansinsteadofdegrees.WhenwecomputeangleBbydividing360bythenumberofsides,wegetavalue
in degrees. Fortunately, the math library contains a function (called radians) that will convert a value in
degreestotheequivalentvalueinradians.
Session 2.4 shows how we can use the archimedes function. The first evaluation shows that the name
archimedes evaluates to a reference to a function object. The subsequent lines invoke the function passing
different numbers of sides. Note that the accuracy of the pi approximation gets better and better as we
increasethenumberofsidesinthepolygon.

>>>archimedes
>>>archimedes(8)
3.0614674589207183
>>>archimedes(16)
3.1214451522580524
>>>archimedes(100)
3.1410759078128301

>>>forsidesinrange(8,100,8):print(sides,archimedes(sides))

83.06146745892
163.12144515226
243.13262861328
323.13654849055
403.13836382911
483.13935020305
https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 6/27
8/23/2016 Yuzu: Python Programming in Context

563.13994504528
643.14033115695
723.1405958903
803.14078526073
883.14092537783
963.14103195089
>>>

Session2.4Usingthearchimedesfunction

Inthefinalcodefragmentofthesession,weuseaforstatementtocallthearchimedesfunction,repeatedly
usingarangeconstructtoautomaticallyprovidedifferentnumbersofsides.Recallthatrange(8,100,8)will
producevaluesforthevariablesidesstartingat8andstoppingatorbefore99,increasingby8everytime.
YouwillalsonoticeanothernewPythonfunction,print.Theprintfunctionshowsboththenumberofsides
usedandtheresultreturnedfromthefunction.Inasense,thisisnothingmorethanexplicitlycallingtheprint
action as was described as part of the readevalprint loop. It is possible to print more than one value by
includingmultipleparameters.

Exercises

2.6RepeattheloopinSession2.4.Inadditiontothevalueofpi,printoutthedifferencebetweenthevalue
calculated by the archimedes function and math.pi. How many sides does it take to make the two
close?

2.7Modifythearchimedesfunctiontotaketheradiusasaparameter.Canyougetabetteranswermore
quicklyusingalargercircle?

2.5AccumulatorApproximations
Thepiapproximationtechniquesthatfollowwillusemathematicsbasedonwhatarecalledinfiniteseriesand
infiniteproductexpansions.Thebasicideaisthatbyaddingormultiplyinganinfinitenumberofarithmetic
terms,wecangetcloserandclosertotheactualvaluewearetryingtocompute.Althoughthemathematicsof
these approaches is beyond the scope of this book, the patterns provide excellent examples of arithmetic
processing.

2.5.1TheAccumulatorPattern
Inordertousethesetechniques,wewillneedtointroduceanotherimportantproblemsolvingpatternknown
astheaccumulatorpattern.Thiscommonpatterncomesupveryoften.Yourabilitytorecognizethepattern
andthenimplementitwillbeespeciallyusefulasyouencounternewproblemsthatneedtobesolved.
As an example, consider the simple problem of computing the sum of the first five integer numbers. Of
course,thisisreallyquiteeasysincewecanjustevaluatetheexpression1+2+3+4+5.Butwhatifwewanted
to sum the first ten integers? Or perhaps the first hundred? In this case we would find that the size of the
expression would become quite long. To remedy this, we can develop a more general solution that uses
iteration.
ExaminethePythoncodeshowninSession2.5.Asyoucansee,thevariableaccstartsoffwithavalueof0,
sometimescalledtheinitialization.Recallthatthestatementforxinrange(1,6):willcausetheloopvariable
xtoiterateoverthevaluesfrom1to5.Figure2.5 shows how this can then be used to create the running

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 7/27
8/23/2016 Yuzu: Python Programming in Context

sum. Every time we pass through the body of the for loop, the assignment statement acc = acc + x is
performed.Sincetherighthandsideofthestatementisevaluatedfirst,itisthecurrentvalueofaccthatis
usedintheaddition.Tocompletetheassignmentstatement,thenameaccwillnowbeupdatedtorefertothis
newsum.Thefinalvalueofaccisthen15.

>>>acc=0
>>>forxinrange(1,6):
acc=acc+x

>>>acc
15
>>>

Session2.5Computingarunningsumwithiterationandanaccumulatorvariable

Figure2.5Usingtheaccumulatorpattern

This may seem strange to see the same name appearing on both the lefthand and righthand sides of the
assignmentstatement.However,ifyourememberthesequenceofeventsforanassignmentstatementyouwill
notbeconfused.
1.Evaluatetherighthandside
2.Letthevariablelefthandsiderefertotheresultingobject
Thevariableaccisoftenreferredtoastheaccumulatorvariablesinceitiscontinuouslyupdatedwiththe
currentvalueoftherunningsum.Now,whetherwewantthesumofthefirstfiveintegersorthefirst5000,
thetaskisthesame.Simplychangetheupperboundontheiterationandallowtheaccumulatorpatterntodo
itswork.Itisveryimportanttonotethattheentireprocessdependsoncorrectlyinitializingtheaccumulator
variable.Ifaccdoesnotstartat0inthiscase,theendingsumwillnotbecorrect.

Exercises

2.8Computethesumofthefirst100evennumbers.

2.9Computethesumofthefirst50oddnumbers.

2.10Computetheaverageofthefirst100oddnumbers.

2.11WriteafunctionthatreturnstheaverageofthefirstNnumberswhereNisaparameter.

2.12 Write a function called factorial that computes the product of the first N numbers where N is a
parameter.

2.13 Each number in the Fibonacci sequence is the sum of the previous two numbers. The first two
numbersinthesequenceare1and1.ComputethetenthFibonaccinumber.
https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 8/27
8/23/2016 Yuzu: Python Programming in Context

2.14WriteafunctiontocomputetheNthFibonaccinumberwhereNisaparameter.Youmayassumethat
Nwillbegreaterthanorequalto3.

2.5.2SummationofTerms:TheLeibnizFormula
Wecannowturnourattentionbacktocomputingapproximationsofpi.Aswesuggestedearlier,therearea
number of formulas that use the idea of infinite expansion. One of these is the Leibniz formula:
Abitofalgebranamelymultiplyingeveryfractionby4willgiveusamuch
easierform:

InordertosuccessfullyturnthisformulaintoPython,wemustlookforthefollowingpatterns:

Allthenumeratorsare4.
Thedenominatorsarealloddnumbers.
Thesequencealternatesbetweenadditionandsubtraction.
Wewilldealwitheachofthesepatternsshortly.
Theaccuracyoftheapproximationwilldependonhowmanytermswedecidetouse.Moretermswillleadto
abetterapproximation.Giventhat,wecanthinkabouttheabstractionpatternandconstructafunctionthat
will need the number of terms as a parameter and will return the value of pi that those terms produce (see
Figure2.6).
Becausetherighthandsideofthisformulaisasumofterms,weareinclinedtothinkabouttheaccumulator
pattern.Tosetupthispattern,wewillneedanaccumulatorvariableandaniteration.Inthiscase,sincethe
numberoftermswillbeaparametertothefunction,wecanstartwiththefollowingstructure:

Figure2.6AbstractionfortheLeibnizformula

defleibniz(terms):
acc=0

foraterminrange(terms):
#bodygoeshere

returnacc

Sinceeachtermisafraction,wehavetobuildboththenumeratoranddenominator.Eachfractionwillhavea
common numerator of 4. The denominator in each case will change, starting at 1 and increasing by 2 each
time. We can achieve this effect using the accumulator pattern. In this case we will start the accumulator
variabledenat1andthestatementden=den+2willcauseanaccumulationeachtimethroughtheiteration.

defleibniz(terms):
acc=0
num=4

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 9/27
8/23/2016 Yuzu: Python Programming in Context

den=1

foraterminrange(terms):

den=den+2

returnacc

Wenowneedtodeterminehowtocomputeeachfractionterm.Tostart,wecandividethenumeratorbythe
denominator.However,westillneedtomakesomeofthetermspositiveandsomenegative.Aninteresting
waytodothisistotakeadvantageoftheloopvariableaterm.

Figure2.7ThefractionsoftheLeibnizformula

Figure2.7showsthefirstfivefractionsofthesequenceandhoweachtermisrelatedtothevalueoftheloop
variableaterm.Noticethatwhenatermis0oranevennumber,thetermispositivewhenatermis1oranodd
number,thetermisnegative.
Onewaytoproducethispatternistoraise1tothepowerofaterm.Thiswillcauseasequenceofalternating
1and1thatcanbemultipliedbythefraction.Theresultcanthenbeaddedtotheaccumulatorvariable,as
showninListing2.3.Eachtimethroughtheiteration,nexttermiscomputedbydividingthenumeratorbythe
denominatorandmultiplyingtheresultbyapowerof1.Session2.6showsthefunctioninaction.Noteagain
thatincreasingthenumberoftermsgivesabetterapproximation.

Listing2.3AfunctiontocomputepiusingtheLeibnizformula

>>>leibniz(100)
3.1315929035585537
>>>leibniz(1000)

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 10/27
8/23/2016 Yuzu: Python Programming in Context

3.1405926538397941

Session2.6Usingtheleibnizfunction

Exercises

2.15Runtheleibnizfunctionusing10,000and100,000terms.

2.16Comparetheresultsoftheleibnizfunctionwitharchimedes.Howdoesthenumberoftermscompare
withthenumberofsides?

2.17Modifytheleibnizfunctionsothatyoucanalternatesignswithoutusing(1)**term.

2.5.3ProductofTerms:TheWallisFormula
Oursecondexampleoftheaccumulatorpatternwilluseaninfiniteproductinsteadofaninfinitesum.The
Wallis formula, will again require that we decide how many terms we
wanttouse.Inthiscase,youcanseethattherighthandsidemakesuseofthemultiplicationoperator.
One thing to immediately note is the impact this multiplication will have on the initialization of the
accumulatorvariable.Ifweuse0asbefore,wewillalwaysgetaresultof0.Inordertostartouttheprocess,
wewillwanttouse1.
Thenextpatternweshouldconsideristhefractionsthemselves.Atfirstglanceitmightseemthattheyare
unrelated.However,ifyoulookattheminpairs(seeFigure2.8),youcanseethattheyarerelated.Eachpair
of fractions shares a common numerator, starting with 2 in the first pair and increasing by 2 for each
subsequentpair.
Usingabstractiononceagain,wecannowbegintoconstructafunctionthatwillacceptthenumberofpairsas
the input parameter and will return the value of pi produced by the infinite product. We can initialize our
accumulatorvariableto1andournumeratorto2.

Figure2.8FractionpairsintheWallisformula

defwallis(pairs):
acc=1
num=2

forapairinrange(pairs):
#computeafractionpair

num=num+2

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 11/27
8/23/2016 Yuzu: Python Programming in Context

Ournextstepistocalculatetheactualfractions.Sincewehavealreadynotedthattheycomeinpairs,weneed
to look for the relationship between the numerator and denominator within each pair. Given a specific
numerator,thedenominatorscanbeseentobeonelessandonemore.Inotherwords,ifthenumeratorisn,
theleftfractionofthepairhasadenominatorofn1andtherightfractionofthepairhasadenominatorof
n+1.Wecannowcalculatethetwofractionsandperformtheaccumulatingmultiplication.

defwallis(pairs):
acc=1
num=2

forapairinrange(pairs):
leftterm=num/(num1)
rightterm=num/(num+1)

acc=acc*leftterm*rightterm

Afinalobservationtonote:IntheoriginalWallisformulathevalueoftheinfiniteproductisequalto .This
meansthatwemustmultiplytheresultingproductby2togettheapproximationofpithatwillbereturnedby
thefunction.Listing2.4showsthecompletefunction.Notealsothatthemultiplicationstatementbeforethe
returnisatthesamelevelasthereturn,notinsidetheforloop.Weonlywantthatmultiplicationtobedone
once.Session2.7showstheresultofinvokingthefunctiononanumberofdifferentpairs.

Listing2.4AfunctiontocomputepiusingtheWallisformula

>>>wallis(100)
3.1337874906281575
>>>wallis(1000)
3.1408077460303785
>>>wallis(10000)
3.1415141186818549

Session2.7Usingthewallisfunction

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 12/27
8/23/2016 Yuzu: Python Programming in Context

Exercises
2.18Runthewallisfunctionusing10,000and100,000terms.

2.19Comparetheresultsofthewallisfunctionwitharchimedesandleibniz.Howdoesthenumberofpairs
comparewithtermsandsides?

2.20Itisalsopossibletowritethewallisfunctionbypairingthefractionsbytheirdenominators.Rewrite
thewallisfunctionusingthispairing.Hint:Thefirsttermwillnotbepartofapair.

2.6AMonteCarloSimulation
Ourfinaltechniqueforapproximatingpiwillmakeuseofprobabilityandrandombehavior.Thesetypesof
solutions are often referred to as Monte Carlo simulations because they use features that are similar to
gamesofchance.Inotherwords,insteadofknowingspecificallywhatwillhappenduringthesimulation,a
randomelementwillbeintroducedintothesimulationsothatitwillbehavedifferentlyeachtime.
To set up our simulation, consider Figure2.9. Pretend that we are looking at a dartboard in the shape of a
squarethatis2unitswideand2unitshigh.Acirclehasbeeninscribedwithinthesquaresothattheradiusof
thecircleis1unit.
Nowassumethatwecutouttheupperrighthandquadrantofthesquare(seeFigure2.10).Theresultwillbea
squarethatis1unithighand1unitwidewithaquartercircletranscribedinside.Thispieceiswhatwewill
usetoplayoursimulation.Theareaoftheoriginalsquarewas4units.Theareaoftheoriginalcirclewas
r2 = units since the circle had a radius of 1 unit. After we cut the upperright quarter, the area of the
quartercircleis andtheareaoftheentirequartersquareis1.

ThesimulationwillworkbyrandomlythrowingdartsatthedartboardinFigure2.10.Wewillassumethat
everydartwillhittheboardbutthatthelocationofthatstrikewillberandom.Itiseasytoseethateachdart
will hit the square but some will also land inside the quartercircle. The number of darts that hit inside or
outsidethequartercirclewillbeproportionaltotheareasofeach.Morespecifically,thefractionofdartsthat
landinsidethequartercirclewillbeequalto .

Figure2.9SettinguptheMonteCarlosimulation

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 13/27
8/23/2016 Yuzu: Python Programming in Context

Figure2.10Theupperrightquadrant

Inordertoimplementthissimulation,wewillusetherandommodule.Youmayfinditusefultoinvokethe
helpcommandonthemoduletoexplorethefunctionality.Forourpurposeshere,wewilluseonlytherandom
functiona random number generator that returns a floatingpoint number between 0 and 1 each time it is
called.Session2.8showstherandomfunctioninaction.

>>>importrandom
>>>random.random()
0.57346717619779752
>>>random.random()
0.54832767404135008
>>>foriinrange(5):
print(random.random())
0.355533646663
0.607688967224
0.464142190998
0.814134550939
0.712540183514
>>>

Session2.8Exercisingtherandomfunction

Wewillusetheserandomnumberstosimulatetherandomdartthrowwedescribedearlier.Foreachroundof
thesimulation,wewillrandomlygeneratetwonumbersbetween0and1.Thesewillrepresenttheposition
thateachdartlandsonoursquare.Wecanthinkoftheboardashavingahorizontalandaverticalaxis,both
labeledbetween0and1.Ifweletthefirstnumberrepresentthehorizontaldistanceandthesecondrepresent
theverticaldistance,thenthetwonumbersgiveusanexactlocationforthepoint.
Nowrecallthatthelocationoftherandompointisimportantinthatweneedtoknowwhetheritoccursinside
oroutsidethequartercircle.Ourplanistokeeptrackofthenumberofpointsthatlandinsidethatcircleand
thenusethatnumbertocomputeourestimatefor.Inordertodothis,weneedtobeabletodecidewhether
thepointisinthecircle.Thiswillrequireafewnewideas.

2.6.1BooleanExpressions
Tosolvetheproblemofdecidingwhetherapointisinsideoroutsideofthecircle,weneedtobeabletoaska

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 14/27
8/23/2016 Yuzu: Python Programming in Context

question.Incomputerscience,questionsareoftenreferredtoasBooleanexpressionsbecausetheresultof
askingthemisoneoftheBooleandatavaluesTrueorFalse.TheseBooleanvaluesareanotherprimitivetype
inPython.

>>>6
6
>>>4.5
4.5
>>>True
True
>>>False
False
>>>

The easiest type of Boolean expression to write compares the results of two expressions. To make this
comparison,thecommonrelationaloperatorsfrommathematicssuchasequalto,lessthan,andgreaterthan
areused.Table2.1showstheoperatorsandtheirmeaninginPython.
Comparisons between two data values using a relational operator are called relational expressions. Like
otherexpressionsinPython,aresultisproducedwhenrelationalexpressionsareevaluated.Inthiscase,the
resultwillbeaBooleanvalue.Session2.9showsafewsimpleexamplesofrelationalexpressions.Notethat
the variable apple is assigned the value 25 and is then used in an equality comparison. It is important to
distinguishbetweentheuseoftheassignmentoperator,=,andtheequalityoperator,==.Notealsothatinthe
lastexamplethevalueofthevariableiscomparedagainsttheresultofanarithmeticexpressionnamely,5*
5.

RelationalOperator Meaning
< Lessthan
<= Lessthanorequalto
> Greaterthan
>= Greaterthanorequalto
== Equal
!= Notequal

Table2.1Relationaloperatorsandtheirmeaning

>>>6<10
True
>>>4!=4
False
>>>apple=25
>>>apple==25
True
>>>apple==5*5
True
>>>

Session2.9Evaluatingsimplerelationalexpressions

2.6.2CompoundBooleanExpressionsandLogicalOperators
https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 15/27
8/23/2016 Yuzu: Python Programming in Context

InPythoncompoundBooleanexpressionsarecomposedofsimpleBooleanexpressionsthatareconnected
bythelogicaloperatorsand,or,andnot.Table2.2definesthebehaviorofthePythonlogicaloperators.Note
thatanyPythonexpressioncanbeusedaspartofaBooleanexpression.Mostoftentheexpressionswillbe
relationalexpressions.
It is important to understand that Python evaluates a Boolean expression exactly as indicated in Table 2.2.
Python uses shortcircuit evaluation of a Boolean expression. This means that Python evaluates only as
much of the expression, from left to right, as it needs to in order to determine if the expression is True or
False.

xandy IfxisFalse,returnxotherwisereturny.
xory IfxisFalse,returnyotherwisereturnx.
notx IfxisFalse,returnTrueotherwisereturnFalse.

Table2.2Logicaloperatorbehavior

Forexample,intheBooleanexpression3<7or10<20:Pythonneedstoevaluateonlytheexpression3<7
sinceitwouldnotmatterwhether10<20isTrueorFalse.Similarly,Pythonwouldneedtoevaluateonlythe
10>20partoftheBooleanexpression10>20and3<7sincethefullexpressionwillbeFalseregardlessof
whether3<7.Session2.10showsthelogicaloperatorsinaction.

>>>6<10and3<7
True
>>>4!=4or5<8
True
>>>6<10and10<6
False
>>>not6<10
False
>>>

Session2.10EvaluatingcompoundBooleanexpressions

Exercises
2.21WhatistheresultofevaluatingtheBooleanexpressionFalseorTrue?

2.22WhatistheresultofevaluatingtheBooleanexpressionTrueandFalse?

2.23WhatistheresultofevaluatingtheBooleanexpressionnot7>3?

2.24WhatistheresultofevaluatingtheBooleanexpressionnot(TrueorFalse)?

2.25WhatistheresultofevaluatingtheBooleanexpression(notTrue)and(notFalse)?

2.26WriteanequivalentBooleanexpressionfor(not(TrueandFalse)).

2.27WriteacompoundBooleanexpressionthatreturnsTrueifthevalueofthevariablecountisbetween1
and10inclusive.

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 16/27
8/23/2016 Yuzu: Python Programming in Context

2.6.3SelectionStatements
OnceweareabletoaskaquestionbywritingaBooleanexpression,wecanturnourattentiontousingthat
questionformakingadecision.Incomputerscience,decisionsareoftencalledselectionbecausewewishto
selectbetweenpossibleoutcomesbasedontheresultofthequestionwehaveasked.Forexample,whenwe
gooutsideinthemorningwemightaskthequestionIsitraining?Ifitisraining,wewillgrabanumbrella.
Otherwise,wewillpackoursunglasses.Weareselectingwhichitemtotakewithusbasedontheconditionof
theweather.
Upuntilnow,thestatementsinourPythonprogramshavebeenexecutedinsequenceoneatatime,inthe
orderthattheywerewritten.Wenowintroduceanewstatementcalledaselectionstatement,alsoknownas
anifstatement.Theselectionstatementwillcontainaquestionandothergroupsofstatementsthatmayor
may not be executed, depending on the result of the question. Most programming languages provide two
versionsofthisusefulstatement:theifelseandtheif.
Theifelsestatementlookslikethis:

if:
else:

ThekeywordifisfollowedbyaBooleanexpressionthatisusedasaconditionfortheselection.Whenthe
Boolean expression is evaluated, the result will either be True or False. If it is True, the first group of
statementsisexecutedinsequenceandthesecondgroupisskipped.Noticethateachgroupofstatementsis
indentedjustlikeaforlooporfunctiondefinition.
On the other hand, if the condition is False, the first group is skipped and the second group is executed in
sequence(seeFigure2.11).Inthisway,itispossibletoperformdifferentactionsbasedontheresultofthe
questionthatwasasked.

Figure2.11Thelogicalflowofanifelsestatement

Forexample,inSession2.11thevariablesaandbareassignedinitialvalues.Theifelsestatementthatfollows
asksthequestionwhetheraisgreaterthanb.Sinceaisgreaterthanb,thefirstgroupofstatements(inthis
caseonlyone)isperformedandthevalueofthevariablecissetto10.Wecancheckthatthisdidoccurby
evaluatingthevariablecafterthestatementisdone.

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 17/27
8/23/2016 Yuzu: Python Programming in Context

>>>a=5
>>>b=3
>>>ifa>b:
c=10
else:
c=20

>>>c
10
>>>

Session2.11Executingasimpleifelseselectionstatement

Theothervariationoftheifstatementdoesnotprovideanelseclause.Itlookslikethefollowing:

if:

Asbefore,theconditionisevaluatedandtheresultwillbeeitherTrueorFalse.IftheresultisTrue,thefirst
group of statements is executed in sequence. However, if the condition is False, nothing is performed, the
statementcompletes,andtheprogramgoesontothenextstatementinsequence(seeFigure2.12).
For example, in Session 2.12 the variables a and b are again assigned initial values. The if statement that
followsasksthequestionwhetheraisgreaterthanb.Sinceitis,thegroupofstatements(again,inthiscase
onlyone)isperformedandthevalueofthevariablecissetto10.However,iftheconditionevaluatestoFalse
(asinthesecondselection),thenthevalueofthevariablecwouldnothavebeenmodified.Inthiscase,the
valueforcremainsthesameasitwasbeforetheselection.

Figure2.12Thelogicalflowofanifstatement

>>>a=5
>>>b=3
>>>c=44
>>>ifa>b:
https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 18/27
8/23/2016 Yuzu: Python Programming in Context

c=10

>>>c
10
>>>ifb>a:
c=25

>>>c
10

Session2.12Executingasimpleifselectionstatement

SincethestatementscontainedwithinaselectionstatementcanbeanylegalPythonstatement,itiscertainly
possible to place one if statement inside of another. This is sometimes referred to as nestedselection. The
followingifelsestatementhastwonestedifelsestatements:

if:
if:

else:

else:
if:

else:

Theresultofthisstructureistodecidebetweenfourgroupsofstatements.IftheouterconditionisTrue,then
thefirstnestedifelseisperformed.IfthenestedconditionisalsoTrue,thenthefirstgroupofstatementsis
performed.However,ifthenestedconditionisFalse,thesecondgroupofstatementsisexecuted.Likewise,if
the outer condition is False, the second nested ifelse will be performed. In this way we are able to decide
betweenfourgroupsofstatementsbyusingtheresultsoftwoconditions.
A common pattern for nested selection is called tail nesting. With tail nesting, all of the nested selection
occursintheelseofthepreviousifelsestatement.Thestructurelookslikethis:

if:
else:
if:

else:
if:

This tail nesting pattern is so common that Python provides a shorter version known as the elif statement.
Heretheelseandthenextifarecombinedtoallowustosimplylisttheconditionsandassociatedstatements
oneatatimewithoutadditionalindentation.Notethatthelastcasestillusesasimpleelse.

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 19/27
8/23/2016 Yuzu: Python Programming in Context

if:
elif:
elif:
elif:

else:

Exercises
2.28HowdoesPythonknowtowhichifanelsebelongs?

2.29Writeaselectionstatementthatsetsthevalueofavariablenamedanswerto1ifavariablenamed
resultisequalto100.Setanswerto2otherwise.

2.30 Write a nested selection using ifelse that sets the value of a variable gradepoint to 4 if a variable
namedscoreisgreaterthan90,3ifscoreisbetween80and89,2ifscoreisbetween70and79,1if
scoreisbetween60and69,and0otherwise.

2.31RewritetheselectioninExercise2.30usinganelifstatement.

2.32Ayearisaleapyearifitisdivisibleby4unlessitisacenturythatisnotdivisibleby400.Writea
functionthattakesayearasaparameterandreturnsTrueiftheyearisaleapyearandFalseotherwise.

2.33Afruitcompanysellsorangesfor32centsapoundplus$7.50perorderforshipping.Ifanorderis
over 100 pounds, shipping cost is reduced by $1.50. Write a function that will take the number of
poundsoforangesasaparameterandreturnthecostoftheorder.

2.34Writeafunctionthattakesthreeintegersasparametersandreturnsthelargest.

2.35Writeafunctionthattakestwoparametersapayrateandthenumberofhoursworkedandreturns
thepay.Anyhoursover40arepaidattimeandahalf.

2.6.4CompletingtheImplementation
WearenowreadytocompleteourMonteCarloapproximationof.Againwewillusetheabstractionideato
buildafunctionthatwillreturntheapproximatevalueof.Forthisfunction,wewillusethenumberofdarts
astheinputparameter.Themoredartswethrow,thebetterourapproximationshouldbe.
Recallthebasicstepsofthesimulation.First,pickarandompointonthesquareboard.Next,decidewhether
thatpointisalsointhetranscribedcircle,keepingacountofthosethatfallinthecircle.Afteralltherandom
pointshavebeentested,theapproximatevalueofwillbefourtimesthefractionofdartsthatlandinsidethe
circle.
Tokeepacountofthenumberofpointsthatfallinthecircle,wewillonceagainusetheaccumulatorpattern
withavariableinitializedto0.Everypointthatlandsinthecirclewillcauseouraccumulatorvariabletobe
incrementedby1.Rememberthattheinitializationneedstooccuroutsidetheiteration.
Toprocesseachpoint,wemustfirstgeneratethetwovaluesthatwillmakeuptherandomlocation.Forthis
we will use the random function from the random module. Since we will think of the first value as the
horizontalmeasureandthesecondasthevertical,wewillusexandyasthenames.

defmontePi(numDarts):
https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 20/27
8/23/2016 Yuzu: Python Programming in Context

inCircle=0

foriinrange(numDarts):
x=random.random()
y=random.random()

returnpi

We now need to decide whether the random point is inside the circle. For this we can use the formula for
findingthedistancebetweenapointandtheorigin(0,0): .Themathmodulewillprovideus
withthesqrtfunction.
Oncewehavethedistance,wecanuseourselectionstatementtodecidewhetherwewanttocountthepoint.
Again,rememberthatifthepointiswithin1unitofthecenter,itisinthecircleandweshouldperformour
incrementstep.Asimpleifstatementwillworknicely.

defmontePi(numDarts):

d=math.sqrt(x**2+y**2)

ifd<=1:
inCircle=inCircle+1

Finally, as shown in Listing 2.5, the calculation of pi is the ratio of the number of points that occur in the
circletothenumberoftotalpoints.Thatratiomustthenbemultipliedby4togettheapproximation.Session
2.13showsthefunctionatwork.Notethatasweincreasethenumberofpoints(darts)usedinthesimulation,
theaccuracyimproves.

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 21/27
8/23/2016 Yuzu: Python Programming in Context

Listing2.5AfunctiontocomputepiusingtheMonteCarlosimulation

>>>montePi(100)
3.0
>>>montePi(1000)
3.1080000000000001
>>>montePi(10000)
3.1543999999999999
>>>

Session2.13UsingthemontePifunction

Exercises

2.36RunthemontePifunctionusing10,000,100,000,andmoreterms.

2.37ComparetheresultsofthemontePifunctionwiththefunctionswehavewritteninprevioussections.

2.38WriteafunctionisInCirclethattakesapointandaradiusasaparameter.Thefunctionshouldreturn
TrueifthepointisinsidethecircleandFalseotherwise.

2.39ModifymontePitousetheisInCirclefunctionyouwroteinthepreviousexercise.

2.6.5AddingGraphics
As a final variation on this simulation, we will use our Turtle to provide a graphical animation of the
algorithm. Recall that a turtle object can provide a basic graphics capability. We can use this to show the
random points as they are being generated. In addition, depending on the location of the point, we can use
colortoshowthelocationaseitherinsideoroutsidethecircle.
Tobeginusingtheturtlewemustimporttheturtlemodule.Inallofourpreviousturtleexamples,theturtle
windowwascreatedautomaticallywhenwecreatedanewturtleobject.Theturtlemodulemakesitpossible
tocreatethedrawingwindow,orscreen,separately,andthenaddtheturtlelater.Bydoingthis,wecancall
methodstomanipulatethedrawingwindow.
The way we create a drawing window is to use the Screen constructor contained in the turtle module. The
statementwn=turtle.Screen()willcreateanewdrawingwindowandnameitwn.Ifwewanttochangethe
background color of the drawing window, we can use the bgcolor method. For example, wn.bgcolor("light
green")willchangethecolorofthewindowfromthedefaultwhitetoalightshadeofgreen.
Now,itispossibletocreatethedrawingwindowandthenthedrawingturtle.

importturtle
wn=turtle.Screen()
drawingT=turtle.Turtle()

Ournextstepwillbetosetupthedartboardthatwasdescribedearlier.Rememberthatthesimulationworks
by generating numbers between 0 and 1 as locations for the random points. In order to show this, we can
adjustthecoordinatesystemusedbythedrawingturtlesothatthedrawingwindowmorecloselycontainsthe
points.Figure2.13showstheoriginaldiagramforthesimulationwithawindowsetuptocontainit.

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 22/27
8/23/2016 Yuzu: Python Programming in Context

Figure2.13Settingtheworldcoordinatesforthegraphicalsimulation

To modify the coordinate system of the drawing window, we will use a Screen method called
setworldcoordinates.Thismethodwilltakefourpiecesofinformation:(1)thexcoordinateofthelowerleft
corner,(2)theycoordinateofthelowerleftcorner,(3)thexcoordinateoftheupperrightcorner,and(4)the
ycoordinate of the upperright corner. These two points will denote the coordinates of the lowerleft and
upperrightcornersofthewindowusedbythedrawingturtle.

wn.setworldcoordinates(2,2,2,2)

It is important to note that we could have made this window any size we wish. However, the larger the
window,thesmallertheactualareaofthesimulation.
Nowthatthewindowissetupwewilldrawtheinitialhorizontalandverticalaxessothatwhenwebeginto
plot points, it is clear where they are being placed. Remember that the drawing turtle will draw a line
wheneveritstailisinthedownposition.Soallweneedtodoisraisethetail,gotothestartingpositionfor
thefirstaxis,lowerthetail,andthengototheendingpositionfortheaxis.ThePythoncodetoaccomplish
thisisshownonthenextpage.

drawingT.up()
drawingT.goto(1,0)
drawingT.down()
drawingT.goto(1,0)

Likewise,wecandothesamethingfortheverticalaxis:

drawingT.up()
drawingT.goto(0,1)
drawingT.down()
drawingT.goto(0,1)

Theremainingportionoftheprogramwillsimplyaskthedrawingturtletoplacedotsattherandompointsas
theyaregenerated.Todothis,wecangotothelocationandthenusethedotmethodtoasktheturtletomake

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 23/27
8/23/2016 Yuzu: Python Programming in Context

adotatthecurrentlocation.Theonlyadditionalfeaturethatwewilladdiscolor.Ifthelocationisinsidethe
circle,wecanchangethecolorofthedrawingturtletoanewcolorsay,blue.Otherwise,ifitisoutside
thecircle,wecanuseadifferentcolor,suchasred.
Since there are now two choices depending on the location of the point, it makes sense to use the ifelse
versionoftheselectionstatementdescribedearlier.Ifthelocationisinsidethecircle,changethetailcolorto
blueandcountthepoint.Otherwise,ifthepointisoutsidethecircle,changethetailcolortored.InPython,
thistranslatestothefollowing:

drawingT.goto(x,y)

ifd<=1:
circle=circle+1
drawingT.color("blue")
else:
drawingT.color("red")

drawingT.dot()

Listing2.6showsthecompletefunctionincludingthecommandstousetheturtletoshowthesimulationin
progress.Itisimportanttonotethatthedotgetsdrawnaftertheifstatement.Thisneedstobedoneonlyonce
foreachiteration.Figure2.14showswhatthesimulationmightlooklike.Youcanseehowthequartercircle
isbeginningtoemerge.Asmoreandmorepointsareused,thiswouldbecomeevenmoreclear.

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 24/27
8/23/2016 Yuzu: Python Programming in Context

Listing2.6AddinggraphicstotheMonteCarlosimulation

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 25/27
8/23/2016 Yuzu: Python Programming in Context

Figure2.14Visualizingthesimulationwith1,000points

WehaveusedoneadditionalScreenmethodattheendoftheshowMontePifunction.Itiscalledexitonclick.
Thismethodtellsthedrawingwindowtofreezeuntiltheuserclickssomewhereinsidethewindow.Ifyou
dontusethismethod,theturtledrawingwindowwillbeautomaticallyclosedwhentheprogramterminates.
Inourcase,wehaveplacedacalltoexitonclickimmediatelybeforereturningfromthefunction.

Exercises
2.40Adjusttheworldcoordinatessothatthewindowcontainsonlytheupperrightquadrantofthecircle.

2.41Modifythesimulationtoplotpointsintheentirecircle.Youwillhavetoadjustthecalculatedvalueof
accordingly.

2.7Summary
ThischapterhasintroducedthefollowingadditionalbuildingblocksofprogrammingandPython:

Mathmodules
Booleanexpressions
Selectionstatements
Oneveryimportantpattern,theaccumulatorpattern,wasalsointroduced.Thispatternusesanaccumulator
variable that is continuously updated as part of an iteration. It is important to initialize the accumulator
variable to an appropriate starting value. Finally, we used the Monte Carlo simulation technique, which is
basedonrandomnumbers,toapproximatethevalueofpi.

KeyTerms

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 26/27
8/23/2016 Yuzu: Python Programming in Context

accumulatorpattern
accumulatorvariable
approximation
Archimedesapproach
Booleanexpression
compoundBooleanexpression
degrees
initialization
Leibnizformula
logicaloperator
MonteCarlosimulation
nestedselection
pi
radians
relationalexpression
relationaloperator
runningsum
selectionstatement
shortcircuitevaluation
tailnesting
Wallisformula

PythonKeywords
and
elif
False
for
help
ifelse
if
import
math
not
or
print
random
return
sin
True

https://reader.yuzu.com/#/books/9781284089042/c/6/18!/4@0:0 27/27
8/23/2016 Yuzu: Python Programming in Context

CHAPTER3
CodesandOtherSecrets

3.1Objectives
Tointroducethestringdatatype
Todemonstratetheuseofstringmethodsandoperators
Tointroducesimplecryptographicalgorithms

3.2Introduction
Foralmostaslongaspeoplehavebeenwriting,peoplehavealsobeentryingtohidewhattheywerewriting
from others. Herodotus describes the use of secret writing that saved Greece from being conquered by
XerxesandthePersians.TheKamaSutra,basedonwritingsdatingbacktothefourthcenturybce,instructs
womenintheartofsecretwritinginordertohidethedetailsoftheirliaisons.Inthischapterweexploresome
simpleformsofsecretwritingusingPython.
Todaythisartofsecretwritingiscalledcryptography.Youusecryptographynearlyeverydaywithouteven
thinking about it. Not because you are sending secret messages to your friends, (although there are several
cryptographicaddonsforemailthesedays)butwheneveryouuseyourbrowser.Whenyoumakeapurchase
ontheWeborcheckyourgradesorbankonline,youareusingcryptography.
BeforewediveintocryptographywemustlearnaboutanotherimportantPythondatatypecalledthestring
a data structure that allows us to represent the written word in our programs. After the numeric data types
introduced in Introduction (Chapter 1), the next most common type of data used in programming is the
string.
Youarealreadymorefamiliarwithstringsthanyouthink.Stringsaresimplysequencesofcharacters,such
asthelettersofthealphabetandalltheothersymbolscommonlyusedinwriting.Mostoften,thesesequences
ofcharactersareputtogethertoformfamiliarwords,butaswewillseeinthischapter,charactersequences
canbeusedformanyotherinterestingpurposes.
InPythonyouwillknowthatanobjectisastringbecauseitissurroundedbyeithersinglequotes(')ordouble
quotes(").Likenumbers,stringsareobjectsthatcanbenamedbyvariables.PythonSession3.1illustratesa
numberofsimplestrings.
Likethenumericdatatypes,Pythonprovidesuswithoperatorswecanuseonstringstotransformthem.In
thenextfoursectionswewillexaminesomecommonlyusedstringoperators.
Stringscancontainanycharacters:letters,numbers,punctuationmarks,andevenquotationmarks.Whenyou
want to have a string that contains a single quote, you use double quotes around the outside of the string.
Whenyouwanttohavedoublequotesinsideyourstring,youusesinglequotesaroundtheoutside.Ifyour
https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 1/25
8/23/2016 Yuzu: Python Programming in Context

string does not contain either single or double quotes, it does not matter whether you use single or double
quotesaroundtheoutside.

>>>"hello"
'hello'
>>>'world'
'world'
>>>a="hello"
>>>a
'hello'
>>>b='world'
>>>"let'sgo"
"let'sgo"
>>>'shesaid"howareyou?"thenleft'
'shesaid"howareyou?"thenleft'
>>>

Session3.1Avarietyofsimplestrings

3.2.1Concatenation
As a first example of an operator applied to a string, we will look at the addition operator (+). When + is
appliedtotwostrings,wecallittheconcatenationoperator.Whenyouconcatenatetwostrings,yousimply
putthetwostringsoneaftertheother,asshowninSession3.2.Noticethattheconcatenationoperatordoes
not automatically add a space between two strings. To add a space, you must concatenate a string that
containsonlyaspace.

>>>"hello"+"world!"
'helloworld!'
>>>fname='John'
>>>lname='Smith'
>>>fname+lname
'JohnSmith'
>>>fullName=fname+''+lname
>>>fullName
'JohnSmith'
>>>

Session3.2Stringconcatenation

3.2.2Repetition
Thenextoperatorwecanapplytoastringistherepetitionoperator(*).Asyoumightguess,thisoperator
takesastringandrepeatsitasmanytimesasyouwouldlike.Forexample,supposeyouwanttocreatethe
string 'gogogo'. Rather than typing go multiple times, you can simply apply the repetition operator as
follows:'go'*3
As with their numeric counterparts, the repetition operator has a higher precedence than concatenation. We
canthusconstructstringsusingbothoperatorstogether,asshowninSession3.3.

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 2/25
8/23/2016 Yuzu: Python Programming in Context

>>>'go'*3
'gogogo'
>>>'go'*3+'twins'*2
'gogogotwinstwins'
>>>('hello'+'world')*3
'helloworldhelloworldhelloworld'
>>>

Session3.3Stringrepetition

3.2.3Indexing
We next examine the index operator [], an operator that has no mathematical counterpart. This operator is
useful when we want to get at particular characters in a string. For example, if we want to get at the first
characterofastring,wecanusetheindexoperatorasfollows:'John'[0].Thefirstcharacterinastringisat
index 0, the second character is at index 1, and so on. In addition, Python allows us to index the string in
reverse.Thelastcharacterinastringhasindexvalue1,thesecondtolastcharacterhasindexvalue2,and
soon.Figure3.1showstherelationshipbetweenindexvaluesandthecharactersinastring.
Whenindexingastring,itisoftenimportanttoknowtheoveralllengthofthatstring.Thelenfunctiontells
ushowmanycharactersareinastring.Itisimportanttounderstandtherelationshipbetweenthelengthofa
stringandtheindexofthelastcharacterinthestring.Forexample,althoughthelen('abc')returns3,wewould
use'abc'[2]toaccessthecharacter'c'.
Wecangoonestepfurtherandcombinetherangefunctionwiththelenfunctiontoallowustoaccesseach
characterinastring.Session3.4illustratessomeusesoftheindexoperator.

Figure3.1Indexvaluesforeachcharacterinastring

>>>name="RoyGBiv"
>>>first=name[0]
>>>first
'R'
>>>middleChar=name[4]
>>>middleChar
'G'
>>>foriinrange(len(name)):
print(name[i])

R
o
y

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 3/25
8/23/2016 Yuzu: Python Programming in Context

B
i
v
>>>name[1]
'v'
>>>name[len(name)1]
'v'

Session3.4Stringindexing

3.2.4Slicing
Thesliceoperator[:]issimilartotheindexoperatorexceptthatitcangetmulticharacterpartsofthestring.
Weoftencallthesepartsofastringsubstrings.Forexample,wecangetthefirstthreecharactersofastring
as follows: "john[0:3]". The first number after the left square bracket gives the starting index of the first
characterinthesubstring.Thenumberafterthe':'istheindexthatisoneafterthelastcharacterselectedfor
thesubstring.Thisissimilartotheboundsoftherangefunctionwheretheupperboundisnotincluded.
The proper prefixes of a string are all the substrings that start with the first character of the string and are
shorterthantheoriginalstring.Wecaneasilygeneratetheprefixesofastringusingthesliceoperator.Notice
thatthelastexampleinSession3.5doesnotincludethe'v'sincetheupperboundontherangefunctionstops
onecharacterbeforetherequiredindex.Also,noticethatthefirstsubstringcreatedistheemptystring.

>>>name='RoyGBiv'
>>>name[0:3]
'Roy'
>>>name[6:9]
'Biv'
>>>foriinrange(len(name)):
print(name[0:i])

R
Ro
Roy
Roy
RoyG
RoyG
RoyGB
RoyGBi

Session3.5Stringslicing

Two additional operators, in and not in, determine whether one string is contained in another. Table 3.1
providesashortsummaryofthestringoperatorswehaveintroducedsofar.

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 4/25
8/23/2016 Yuzu: Python Programming in Context

Table3.1Summaryofthestringoperators

Exercises

3.1Createastringvariablethatisinitializedtoyourentirenamefirst,middle,andlast.

3.2Usingthesliceoperator,printyourfirstname.

3.3Usingthesliceoperator,printyourlastname.

3.4Usingthesliceandconcatenationoperators,printyournameintheformLastname,Firstname.

3.5Printthelengthofyourfirstname.

3.6 Assume you have two variables: s='s', and p='p'. Using concatenation and repetition, write an
expressionthatproducesthestringmississippi.

3.7ModifytheprefixexampleinSession3.5toprintallprefixesofRoyGBiv,includingtheentire
string.

3.2.5StringMethods
In addition to the string operators, strings are objects that have methods just like turtles. Some of these
methodsaresummarizedinTable3.2.Severalhelpyoutoformatyourstringstofitinacertainamountof
space,suchasljust,rjust,andcenter.Othersconvertthestringtobealluppercaseorlowercase.
Themostusefulmethodsforusinthischapterarethosethatallowustosearchforonestringwithinanother.
Thecountmethodfindsthenumberofoccurrencesofonestringwithinanother.Theindexandfindmethods
are similar in that they return the index of the first occurrence of one string within another. However, they
differ in their behavior when the string we are looking for is not found. The find method returns 1 if the
stringyouarelookingforisnotfound.Theindexmethodgeneratesanerrorandhaltsyourprogramifthe
stringisnotfound.Session3.6illustratestheuseofthesestringmethods.

Method Use Explanation


center astring.center(w) returnsthestringastringsurroundedbyspacestomakeastringwcharacters
long.

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 5/25
8/23/2016 Yuzu: Python Programming in Context

count astring.count(item) Returnsthenumberofoccurrencesofiteminastring.


ljust astring.ljust(w) Returnsastringleftjustifiedinafieldofwidthw.
rjust astring.rjust(w) Returnsastringrightjustifiedinafieldofwidthw.
upper astring.upper() Returnsastringinalluppercase.
lower astring.lower() Returnsastringinalllowercase.
index astring.index(item) Returnstheindexofthefirstoccurrenceofiteminastring,oranerrorifnot
found.
find astring.find(item) Returnstheindexofthefirstoccurrenceofiteminastring,or1ifnot
found.
replace astring.replace(old, Replacesalloccurrencesofoldsubstringwithnewsubstringinastring
new)

Table3.2Summaryofstringmethods

Session3.6Demonstratingstringmethods

Exercises

3.8Usingthecountmethod,findthenumberofoccurrencesofthecharactersinthestring'mississippi'.

3.9Replacealloccurrencesofthesubstring'iss'with'ox'.

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 6/25
8/23/2016 Yuzu: Python Programming in Context

3.10Findtheindexofthefirstoccurrenceof'p'in'mississippi'.

3.11Maketheword'python'centeredandallcapitallettersinastringoflength20.

3.2.6CharacterFunctions
As you will see shortly, one of the things our stringbased algorithms will do is convert a character into a
numberandbackagain.Pythoncontainssomebuiltinfunctionsforconvertingacharactertoanumberand
viceversa.Thefunctionsordandchrdothejobforus.Inaddition,thestrfunctionconvertsanumberintoits
stringform.Session3.7presentsanexample.

>>>ord('a')
97
>>>ord('c')
99
>>>ord('z')
122
>>>chr(104)
'h'
>>>chr(97+13)
'n'
>>>str(10980)
'10980'
>>>

Session3.7Usingord,chr,andstr

It may seem a bit odd to you that the letter 'a' is represented by the number 97. Why not 1 or 0? Each
characterthatweseeisactuallystoredinthememoryofacomputerasaseriesof1sand0s.Thesequence
of1sand0sthatcorrespondtotheletter'a'isrepresentedasdecimalnumber97.Themappingfromletters
tonumbersisdefinedbytheAmericanStandardCodeforInformationInterchange(ASCII)andhasbeen
inusesincethe1970s.
Forourpurposes,wewouldmuchprefertheletter'a'tomaptothenumber0andtheletter'z'tomaptothe
number25.Toillustratetheuseofthefindmethodandstringindexing,wedefineourownhelperfunctions
formappingfromcharacterstonumbersandbackinListing3.1.
In indexToLetter, we use the elif form of an if statement. This form allows us to check for a sequence of
conditionsonavariable.Forexample,iftheindexofacharacterisgreaterthan26,weprintanerrormessage.
Oriftheindexislessthan0weprintanerrormessage.Otherwise,wejustreturntheletterfromthealphabet
attheindexprovided.

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 7/25
8/23/2016 Yuzu: Python Programming in Context

Listing3.1Twohelperfunctionsforstringlookups

Exercises

3.12Whatisthedifferencebetweenord('A')andord('a')?

3.13Writeafunctionthattakesasinglecharacterdigitandreturnsitsintegervalue.

3.14WritetheletterToIndexfunctionusingordandchr.

3.15WritetheindexToLetterfunctionusingordandchr

3.16Writeafunctionthattakesanexamscorefrom0100andreturnsthecorrespondinglettergrade.Use
thesamegradingscaleyourprofessordoesforthisclass.

3.3EncodingandDecodingMessages
Cryptographyisthescienceofmakingmessagessecure,oftransformingreadablemessagesintounreadable
messagesandbackagain.Inthischapterwerefertomessagesthatarereadableasplaintext.Messagesthat
are unreadable are called ciphertext. The process of turning plaintext into ciphertext is called encryption.
Thereverseprocessofturningciphertextintoplaintextiscalleddecryption.Figure3.2showsanoverviewof
theencryptionanddecryptionprocesses.Algorithmsforbothencryptionanddecryptionarethesubjectofthis
section.

Figure3.2Encryptinganddecryptingamessage

Oneoftheeasiestwaystoencryptamessageistosimplyscrambletheletters.Forexample,thewordapple
couldberandomlytransformedtolapep.Infact,thereare120differentpossiblearrangementsoftheword
https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 8/25
8/23/2016 Yuzu: Python Programming in Context

apple. However, if the encryption algorithm randomly scrambles the letters, the task of the decryption
algorithmisprettyhard.Encryptionanddecryptionalgorithmsmustworktogetherinsomeagreeduponway,
withtheencryptionalgorithmscramblinglettersandthedecryptionalgorithmunscramblingthem.

3.4TranspositionCipher
3.4.1EncryptingUsingTransposition
Onewaytoscramblethelettersofamessageistoseparatethemessageintotwogroupsofcharacters:thefirst
group composed of the evennumbered characters and the second group composed of the oddnumbered
characters.Ifwecreateonestringoutoftheevennumberedcharactersandanotheroutoftheodd,wecan
concatenatethetwonewstringstogethertoformtheciphertextstring.Becausethisresultsinastringwiththe
characters shuffled to new positions, we call this a transpositioncipher. In some books you will find this
ciphercalledtherailfencecipher.Figure3.3illustratestheideabehindthisencryptionalgorithm.
Nowthatwehaveaschemeforscramblingtheplaintextmessage,letswriteaPythonfunctionthattakesthe
plaintext message as a parameter and returns the ciphertext message. The key to this algorithm lies in our
abilitytoputtheevencharactersfromtheplaintextinonestringandtheoddcharactersinanother.Oneway
todothisistouseacounterandastringiterator.Astringiteratorisaforloopwheretheloopvariabletakes
onthevalueofeachcharacterinthestring.Asweiterateovereachcharacterofthestring,weincrementthe
counter.Ifthecounterisanevennumber,weconcatenatethecurrentcharactertoourevencharacterstring.If
the counter is odd, we concatenate the character to our odd character string. This transposition cipher is
sometimescalledtherailfencecipherwhereeachrailcontainspartoftheoriginalstring.Inthiscase,since
wearedividingthestringintotwoparts,wecallitatworailcipher.

Figure3.3Encryptingastringusinganevenoddshuffle

Howcanwetellifanumberisevenorodd?RecallthatforanyevennumberN,whenyoudivideNby2,
thereisnoremainder.ForanyoddnumberN,Ndividedbytwohasaremainderofone.InPython,wecan
obtaintheremainderbyusingthemodulooperator.ForanyevennumberN,N%2is0.Foranyoddnumber
N,N%2is1.Wecanusetheexpressioncounter%2==0astheconditioninanifstatementasatestfor
whetherthecounterisevenorodd.Listing3.2showstheencryptionalgorithmusingthestrategydescribed
here.

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 9/25
8/23/2016 Yuzu: Python Programming in Context

Listing3.2Scramblingaplaintextmessage

The scramble2Encrypt function makes use of the accumulator pattern in several different places. First,
evenChars and oddChars accumulate characters. The accumulator pattern works just as well when
concatenatingstringsasitdoesforaddingnumbers.Whenweapplytheaccumulatorpatterntostrings,we
buildupastringthatstartsfromnothing(theemptystring''),growingthestringonecharacteratatime.The
nextuseoftheaccumulatorpatternisafamiliarnumericaccumulatorusingthecharCountvariable.
Online5ofListing3.2webeginaloopthatwilliterateovereachcharacterintheplainTextstring.Whenwe
iterateoverastring,theloopvariablereferenceseachcharacterinthestringoneafteranother.Online6we
makeuseofthetestwedevisedtodecideifcharCountisevenorodd.IfcharCountiseven,weconcatenate
the old value of evenChars with the current character referenced by the loop variable ch. Because
concatenationcreatesanewstring,evenCharsreferencesthisnewlyconstructedstringwhenweperformthe
assignment. If charCount is odd, we concatenate oddChars and ch to create a new string referenced by
oddChars.
Finally, after all characters in the plaintext string have been processed, we create the cipherText string by
concatenatingoddCharsandevenChars.Theorderofvariablesintheconcatenationisnotaccidental.Weput
oddChars first for a reason: If there is an odd number of characters in our string, oddChars will be one
charactershorterthanevencharacters.Canyouexplainwhy?Wewillseeinamomentthatthisisimportant
whenwewanttosplittheciphertextbackintotwopieces.
Session3.8showstheoutputofthescramble2Encryptfunctionforfivedifferenttestinputs.Noticethatsome
ofthetestcasesarejustnonsensestrings,buttheyarechosencarefullytomakeiteasytoseeifthefunctionis
workingthewaywethinkitshould.Alsonoticethatwetestsomeboundarycaseslikeastringoflength1and
evenanemptystring.

>>>scramble2Encrypt('abababab')
'bbbbaaaa'
>>>scramble2Encrypt('ababababc')
'bbbbaaaac'
>>>scramble2Encrypt('Idonotlikegreeneggsandham')
'ontlkregsadhmIdoiegenegna'
>>>scramble2Encrypt('a')
'a'
>>>scramble2Encrypt('')
''
>>>

Session3.8Testingscramble2Encrypt

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 10/25
8/23/2016 Yuzu: Python Programming in Context

3.4.2DecryptingaTransposedMessage
Our next task is to write a function to decrypt a message that was encrypted by our scramble2Encrypt
function. The input to our decrypt function will be the ciphertext produced by our encrypt function. The
decryptfunctionwillreturntherestoredplaintextcopyofthestring.
To restore the plaintext string, we start out by splitting the ciphertext in half. The first half of the string
contains the odd characters from our original message, and the second half of the string contains the even
characters. To restore the plaintext version of the string, we start with an empty plaintext string and
concatenate the characters from the even and odd strings onto the end of the plaintext string. We alternate
takingacharacterfromtheevenstringfirst,thentheoddstring,andsoon.Figure3.4givesanexampleof
puttingtheplaintextbacktogetheragain.
Onedetailtoconsiderwhenreconstructingtheplaintextmessageisthatwemayhaveonemorecharacterin
theevencharacterstringthanwedointheoddcharacterstring.Wecaneasilycheckforthisbycomparingthe
lengths of the two strings. If the oddnumbered character string is shorter than the even, we simply
concatenatethelastcharacterfromtheevenstringontotheplaintext.Listing3.3showsthePythoncodefor
implementingthedecryptionfunction.
Tosplittheciphertextstringintotwohalves,weusetheslicingoperatorasshownonlines3and4ofListing
3.3.Tofindthemiddleofthestringweuseintegerdivisiontodividethelengthofthestringby2.Weusea
simple slicing operator shortcut on these lines. When the number is omitted before the :, the slice operator
startsatthebeginningofthestring.Whenthenumberafterthe:isomitted,thesliceoperationcontinuesuntil
the end of the string. A useful side effect of this shortcut is that we can make a copy of a string using the
followingsliceoperator:duplicateStr=oldStr[:].

Figure3.4Decryptingamessagebyalternatingcharactersfromtheciphertext

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 11/25
8/23/2016 Yuzu: Python Programming in Context

Listing3.3Decryptingatransposedmessage

Intheloopthatstartsonline7,weusethestringindexingoperatortogetthenextcharacterfromeachofthe
twohalves.Onceagain,weareusinganaccumulatorpatterntobuildtheplaintextstring.Finally,wecheck
for the case when the number of characters in the ciphertext is odd, and add the final character from
evenCharstotheendoftheplaintextstring.
Session3.9showshowyoucantestthedecryptfunction.Noticethatsincescramble2Encryptreturnsastring,
we can use the function call directly as a parameter to the scramble2Decrypt function. It is easy to test
whetherthedecryptfunctionworksbecauseitshouldprintouttheexactsamestringthatweprovidedasa
parametertotheencryptfunction.Onceagain,wehavetriedaneasycase,plusacoupleofboundarycases.

Session3.9Testingencryptionanddecryption

Exercises
3.17Withpaperandpencil,usethetranspositionalgorithmtoencryptthesentencethequickbrownfox
jumpsoverthelazydog.Checkyouranswerbycallingthescramble2Decryptfunction.

3.18WriteapythonfunctionstripSpaces(myString)thattakesastringrepresentingaphraseasaparameter
and returns the paragraph with the order of the letters intact but the spaces between each word
removed.

3.19Thetranspositionciphercanbegeneralizedtoanynumberofrails.Writeafunctiontoimplementa
threerailfencecipherthattakeseverythirdcharacterandputsitononeofthethreerails.
https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 12/25
8/23/2016 Yuzu: Python Programming in Context

3.4.3AskingforInput
Nowthatwehavefunctionsforencryptinganddecryptingamessage,itwouldbeniceiftherewasaneasier
way to get a message to pass to the encrypt function. Most programs today use a dialog box as a way of
askingtheuserforsomeinput.WhilePythonprovidesawaytocreatedialogboxes,thereisamuchsimpler
methodtouse.Pythonprovidesuswithafunctionthatallowsustoaskausertoentersomedataandreturnsa
referencetothedataintheformofastring.Thefunctioniscalledinput.
Pythonsinputfunctiontakesasingleparameterthatisastring.Thisstringisoftencalledthepromptbecause
it contains some helpful text prompting the user to enter something. For example, you might call input as
follows:msg=input('Enteramessagetoencrypt:').Nowwhatevertheusertypesafterthepromptwillbe
storedinthemsgvariable.
Usingtheinputfunction,wecaneasilywriteanotherfunctionthatwillprompttheusertoenteramessageand
thenprintouttheencryptedmessagewhenitisdone.Listing3.4illustrateshowtoincorporateinputintoyour
program.

Listing3.4Usinginput

3.5SubstitutionCipher
The transposition cipher is only interesting because it provides us with a simple example. It is not a very
secureformofencryption.Anattackerwhoistryingtoreadyoursecretmessagesandknowsyouareusinga
transpositioncipherwillfinditveryeasytousethesamealgorithmtodecryptyourmessage.
There is another kind of cipher, called the substitution cipher, which substitutes one letter for another
throughoutamessage.Forexample,youmaysaythat'a'='t'.Inthiscase,theletter't'wouldbeinsertedinto
theciphertextineachplacewheretherewasan'a'intheplaintext.Substitutionciphershavebeenusedsince
thetimeofCaesar.Theycontinuetobepopulartoday,wheretheymostoftenappearastheCryptoquipin
thedailynewspaper.
The substitution cipher has one big advantage over the transposition cipher: It uses a key. If attackers are
tryingtoreadyoursecretmessages,knowingthatyouareusingasubstitutioncipheronlyhelpsthemalittle.
They would also need to know how the letters were rearranged. We call this rearranged version of the
plaintextalphabettheciphertextalphabetorkey.Figure3.5illustratestheencryptionprocessusingakey.If
wejustconsiderthe26lettersofthealphabet,thereare26factorial,or403,291,461,126,605,635,584,000,000
differentpossiblerearrangementsofthealphabet.Sowehavealotofkeystochoosefrom!
Letslookatasimpleexampleofasubstitutioncipherinaction.Wewillencrypttheword'flow'usingthekey
'bpzhgocvjdqswkimlutneryaxf'. Letters are matched up according to their position in the alphabet. For
example,thesixthletterintheplaintextalphabetis'f',whichcorrespondstothesixthletterinthekey,which
is'o'.Figure3.6illustratestheprocess.Eachletterintheword'flow'ismappedfromtheregularalphabetinto
thecorrespondingletterinthekey.Inthiscase,'f'mapsto'o','l'mapsto's','o'mapsto'i',and'w'mapsto'y'.
Our next step is to turn this mapping process into a Python function. The important step in the encryption
processistotakealetterfromtheplaintextalphabetandmapitintoaletterintheciphertextalphabet.Wecan
dothisusingthestringindexmethod.Recallthatastring.index(ch)returnsthefirstoccurrenceofchinthe
stringastring.Usingthistechnique,wecancreateanalphabetstring"abcdefghijklmnopqrstuvwxyz"anduse
theindexmethodtoreturnthepositionofanyletterinthealphabet.Onceweknowthepositionofaletterin
https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 13/25
8/23/2016 Yuzu: Python Programming in Context

theplaintextalphabet,wecanusetheindexoperatortolookupadifferentletterinthekey.Forexample,lets
findamappingfromtheplaintextalphabettotheciphertextalphabetfortheletter'h'.

Figure3.5Usingakeytomakeencryptionmoresecure

Figure3.6Asubstitutioncipher

>>>alphabetString="abcdefghijklmnopqrstuvwxyz"
>>>key="zyxwvutsrqponmlkjihgfedcba"
>>>i=alphabetString.index('h')
>>>print(i)
7
>>>print(key[i])
s

Nowthatweknowhowtomapcharactersfromtheplaintexttotheciphertextalphabet,whatremainsisto
applythismappingprocesstoeachcharacteroftheplaintextmessage,andtoconstructthefinalmessage.For
theselasttwosteps,weuseiterationandtheaccumulatorpattern.Listing3.5showsafunctiontoencrypta
stringusingagivenkey.

Listing3.5Encryptingamessageusingasubstitutioncipher

If you look at the code carefully, you will notice that the alphabet contains the 26 lowercase letters of the
https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 14/25
8/23/2016 Yuzu: Python Programming in Context

alphabetalongwithaspace.Weaddthespaceattheendofthealphabetandthekeysowecanpreservethe
spacesbetweentheindividualwordsinoursecretmessage.Thisisabadideafromacryptographerspointof
view,butitmakesiteasierforustodemonstratethesealgorithms.Abetterapproachwouldbetostripoutall
ofthespacesfromplaintextbeforeapplyinganysubstitutions.Thiswouldeffectivelyreducethestringthe
quick brown fox to the string thequickbrownfox. Removing the spaces makes cracking the code much
harder,butitalsomakesthedecryptedmessagemoredifficulttoread.Wewillkeepthespacesintacttomake
iteasiertoread.
Session 3.10 shows the substitutionEncryption algorithm in action. Notice that even though we encrypt the
sameplaintextmessage,wegetdifferentciphertextmessageswhenweusedifferentkeys.Thisiswhatmakes
the substitution cipher superior to the transposition cipher. Even though an attacker may know that we are
using a substitution cipher, that does not do her any good if she does not have the key. The
substitutionDecryptfunctionisnearlythesameasthesubstitutionEncryptfunction.

>>>testKey1="zyxwvutsrqponmlkjihgfedcba"
>>>testKey2="ouwckbjmpzyexavrltsfgdqihn"
>>>cipherText=substitutionEncrypt("thequickbrownfox",testKey1)
>>>print(cipherText)
gsvjfrxpyildmulc
>>>cipherText=substitutionEncrypt("thequickbrownfox",testKey2)
>>>print(cipherText)
fmklgpwyutvqabvi
>>>

Session3.10UsingthesubstitutionEncryptfunction

Exercises

3.20Makeupyourownkeyandencryptamessage.Exchangeyourkeyandtheciphertextwithapartnerto
seeifyoucandecrypteachothersmessage.

3.21WritethesubstitutionDecryptmethod.

3.22RewritethesubstitutionEncryptfunction,butremoveallspacesfromtheplaintextmessage.

3.6CreatingaKey
Thestrengthofthesubstitutioncipherliesintheabilityoftwopeopletoshareakey.Thatkeymustrepresent
ascrambledversionofthe26lettersofthealphabet.Asafirstapproach,letswriteaPythonfunctionthat
returnsarandomlyscrambledversionofthealphabet.
Themainideainthisalgorithmisthatwewanttorandomlyselectoneletterfromthealphabettoincludein
our key, and then to randomly select another letter and another until we have selected all 26 letters in a
randomorder.Thisseemsprettysimpleatfirst,butwemustbecarefultokeeptrackoftheletterswehave
alreadyselectedsothatourkeydoesnotcontainduplicateletters.
Letsthinkoftheprobleminaslightlydifferentway.Supposethatwestartwithastringthatcontainsall26
lettersinthealphabet.Nextwepickarandomnumberfrom0to25.Thisrandomnumbergivesustheindex
ofthefirstletterwewilluseinthekey.Weconcatenatetheletterretrievedfromthealphabettoourkeystring.
Tomakesurethatwedonotpickthesameletteragain,wewillalsoremoveitfromouralphabetstring.Since
thealphabetstringisnowonecharactershorterwemustpickarandomnumberfrom0to24.Wecanrepeat

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 15/25
8/23/2016 Yuzu: Python Programming in Context

thisprocessofpickingrandomnumbersandremovingcharactersuntilwehaveusedupallthelettersinour
originalalphabetstring.
To implement this solution, lets break it up into two subproblems. First, we write a function that takes a
string and an index value and returns a new string with the character at the given index position removed.
Next,wewriteafunctionthatstartswithafullalphabetandreturnsthescrambledalphabetasourkey.
Listing3.6providesuswithashortPythonfunctionforremovingacharacterfromastring.Thisversionof
thefunctionusesthesliceoperatortofirstgetthesubstringfromthebeginninguptobutnotincludingthe
characterwewanttodelete,andthentogetthesubstringfromthecharacteraftertheonewewanttodeleteup
totheend.Wecanusestringconcatenationtocombinethesetwopiecesintoourfinalstring.

Listing3.6Removingonecharacterfromastring

LetstesttheremoveCharfunctionusingasimplestringandseveraldifferentindexvalues.Noticethatweget
anunexpectedresultwhenweusetheindexvalue1.Beforereadingon,seeifyoucanexplainwhyweget
theunexpectedresultforindex1.

>>>removeChar('abcdefg',0)
'bcdefg'
>>>removeChar('abcdefg',6)
'abcdef'
>>>removeChar('abcdefg',3)
'abcefg'
>>>removeChar('abcdefg',1)
'abcdefabcdefg'
>>>removeChar('abcdefg',2)
'abcdeg'

Thisexampleillustratestheimportanceoftestingeventhesimplestoffunctions.Thereasonwegettheresult
wedowhenusingcharacterindex1isasfollows.Thefirstsliceoperation(string[:1])worksjustfine.It
returns the entire string except for the last character. However, the second slice operation (string[1+1:])
returnstheentirestring.WecanmoveforwardwiththisbrokenversionoftheremoveCharfunctionsinceour
keygeneratingfunctionwillnevertrytoremoveanegativeindex.
Now that we have the removeChar function, lets turn our attention to the main keygenerating function,
keyGen(). Listing 3.7 shows the Python code for the strategy we outlined above. The for loop provides us
with numbers from 0 to 25 assigned to the loop variable i. We use the value of i to generate our random
numberandthenusethefunctionrandintfromtherandommodule,whichgeneratesarandomintegerinthe
rangeprovided.Becausewegeneraterandomnumbersusingthecallrandom.randint(0,25i),wegetasmaller
randomnumberrangeasigetslarger.Wecouldhaveusedtherangefunctionrange(len(alphabet)1,1,1),
butthatwouldlookmoreconfusing.Finally,weuseanaccumulatorpatternkey=key+alphabet[ch]tobuild
upthefinalkey.

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 16/25
8/23/2016 Yuzu: Python Programming in Context

Listing3.7Generatingarandomkeyfromthealphabet

Sinceremembering26randomlyscrambledlettersofthealphabetisprobablybeyondthecapabilitiesofmost
people, lets look at another approach to our keygenerating problem. Rather than generating a random
sequenceof26letters,letssupposethatwechooseawordorshortphraseasourpassword.Thequestionnow
becomes,howcanweusethispasswordasthebasisforscramblingupthealphabet?Theansweristhatwe
willusetwodifferentstringstoconstructthekey,thepassword,andthealphabet.
The first step is to remove all the letters from the alphabet that are in the password. Next we remove all
duplicatelettersinthepassword.Wethenusethealphabet,inorder,startingwiththeletterafterthelastletter
inthepassword.Whenwegetto'z',wegotothebeginningofthealphabetandusetherestofthecharacters.
Letslookatanexampleofhowwedothis.Supposeourpasswordistopsecret.Removingtheduplicates
leaves us with topsecr. Now if we take the alphabet and remove the letters in our password, we have
abdfghijklmnquvwxyz. The next letter after r in our alphabet is u so the three strings we will put
togethertomakeourfinalkeyaretopsecr,uvwxyz,andabdfghijklmnq.Puttingthesealltogethergives
ustopsecruvwxyzabdfghijklmnq.
Thismayseemlikeadauntingtask,butitismanageableifwebreakitdownintosomesmallerproblems:
1.Removeduplicatesfromthepassword.
2.SplitthealphabetintopartsbeforeLastandafterLast.
3.RemovethelettersfoundinthepasswordfrombeforeLastandafterLast.
4.Puttogetherthethreepartsofthekeyweneed.
WealreadyknowhowtodoSteps2and4,soletssolvetheproblemofremovingduplicatelettersfroma
string. To remove the duplicates from a string we will start by reconstructing the string, one character at a
time,usingtheaccumulatorpattern.However,wewillmodifytheaccumulatorpatternbyaddingonesimple
rule.Ifacharacterisnotalreadypartofthereconstructedstring,weaddittothenewstring.Otherwise,ifthe
characterhasalreadybeenaddedtothereconstructedstring,weignoreit.
Forexample,considerthestringbook.Westartbycreatinganemptystring,newStr,tobuildon.Thenwe
iteratethroughthecharactersinbook.SincebisnotalreadyapartofnewStrweaddit,givingusb.
Thenextcharacteriso.OnceagainoisnotinnewStrsoweaddit,givingusbo.Nextwehaveour
secondo,butsincenewStralreadyhasanoweignorethisone.Thefinalcharacter,kisnew,soweadd
ittonewStrgivingusbok.
Thekeytothisapproachisaskingthequestion,Isthecurrentcharacteralreadyapartofthenewstringwe
arecreating?Toanswerthisquestion,wecanusetheinoperatorastheconditionofanifstatement.Listing
3.8showsthePythoncodeweusetocreateastringwithduplicatesremoved.Noticethatalthoughthenatural
wayforyoutothinkabouttheconditionalistoaskthequestionifchinnewStr:itisawkwardtotellPython
todonothing.So,weusethenotinoperatortoasktheoppositequestionifchnotinnewStr:.Thisallowsus
towritetheifstatementinanaturalway.

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 17/25
8/23/2016 Yuzu: Python Programming in Context

Listing3.8Removingduplicatelettersfromastring

Wearegoingtouseasimilarideatoremovethecharactersfromonestringthatareinanother.Onceagainwe
beginwithanemptystring,myString,andapplytheaccumulatorpatterntobuildupanewstring,newStr.If
thenextcharacterintheoriginalstringisnotoneofthecharactersinremoveString,weaddittonewStr.Ifthe
nextcharacterinmyStringisinremoveString,wewillsimplyignoreit.Listing3.9showsthePythoncodefor
thisprocess.Letstestthetwonewfunctionswehavejustwritten.Session3.11showsbothremoveDupesand
removeMatchesinaction.

Listing3.9Removethecharactersinonestringfromanother

>>>removeDupes('topsecret')
'topsecr'
>>>removeMatches('abcdefghijklmnopqrstuvwxyz','topsecr')
'abdfghijklmnquvwxyz'
>>>removeMatches('abcdefghijklmnopqrstuvwxyz',removeDupes('bondjamesbond'))
'cfghiklpqrtuvwxyz'
>>>

Session3.11TestingremoveDupesandremoveMatches

Now that we have implemented removeDupes and removeMatches, the rest of our alphabet scrambling
algorithm is fairly easy. Listing 3.10 shows the Python code needed to provide a scrambled version of the
alphabetusingapasswordasthestartingpoint.

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 18/25
8/23/2016 Yuzu: Python Programming in Context

Listing3.10Generatingakeystartingfromapassword

ThegenKeyFromPassfunctionisagoodexampleofproblemsolvingbytopdowndesign.Lookatthecode
in Listing 3.10. Now imagine how much longer the code would be if we tried to include the code for
removeDupesandremoveMatches.Thefunctionwouldbesolongitwouldbemuchhardertodiscernwhatit
isdoing.NotonlydoremoveDupesandremoveMatchesmakegenKeyFromPassmorereadable,buttheycan
alsobeusedinotherfunctionsyouwrite.
Tocompletethissection,letsconsiderthesecurityofthesubstitutioncipher.Supposewedontknowthekey
butwewanttotrytoreadthesecretmessageanyway.Onepossibilitywouldbetosimplyusebruteforce,a
methodinwhichwetryallpossiblekeysuntilwegetplaintextthatmakessense.Sincethenumberofkeysis
equivalenttothenumberofdifferentwayswecanrearrangethe26lettersofthealphabet,thereare26!or403
1024possiblekeys.Supposethatwewereabletotry1,000,000keyseverysecond.Totryallpossiblekeys
would take us 12,788,288,341,153 years. That is 12 trillion years, roughly a thousand times older than
estimatesoftheageoftheuniverse!
However,aswewillseeinthechapteronCryptanalysis(Chapter8),thesubstitutioncipherisquiteeasyto
breakbyexploitingsomesimplecharacteristicsoftheEnglishlanguage.Soeasyinfactthatthesubstitution
cipherregularlyappearsindailynewspapersnexttothecrosswordpuzzleunderthenameofCryptoquip.
Themainreasonthesubstitutioncipheriseasytobreakisthatthereisaonetoonemappingbetweenletters
inthekeyandlettersintheplaintextalphabet.Thismeansthatifplaintextemapstociphertextkthenit
willdosoconsistentlythroughoutthemessage.Butsinceeisthemostcommonletterinthelanguage,k
willprobablybethemostcommonletterintheciphertext.Thisobservationgivesusaheadstartinfiguring
outtherestoftheletters.

Exercises

3.23WritetheremoveCharfunctionusingforloopsratherthansliceoperators.

3.24ModifytheremoveCharfunctionsothatitworksfornegativecharacterindexes.

3.25ModifythesubstitutionCipherfunctiontousethegenKeyFromPassfunction.

3.26 Encryption often involves the Caesar ciphernamed after Julius Caesar, who used the system to
encryptmilitarymessages.ManyearlyInternetusersalsoadoptedthiscipher.Calledrot13,thecipher
encryptsamessagebyrotatingtheplaintextcharacterby13positionsinthealphabet.Forexample,
abecomesnandlikewisenbecomesa.Thenicethingaboutrot13isthatthesamefunction
canbeusedtoencryptanddecryptamessage.Writeafunctioncalledrot13thattakesamessageasa
parameterandrotatesallthecharactersby13places.

3.27RewritetheCaesarciphersothatittakesthenumberofplacestorotateasaparameter.Youwillhave
towriteseparateencryptanddecryptfunctions.

3.7TheVignreCipher
ThekeytocrackingasubstitutioncipherwasfirstdiscoveredbyArabscholarsintheninthcentury,butitwas
not widely known in the west until the fifteenth century. Once frequency analysis of ciphers became
widespread, the plain substitution cipher became useless. To fix the problems inherent in the simple
substitution cipher, Blaise Vignre invented the strategy of using multiple letter mappings [Sin00]. Rather
than using one key for the whole message, Vignres idea was to use a different key for each letter of the
message.Usingadifferentkeyforeachlettermakesthefrequencyanalysismuchmoredifficult.
The key to the Vignre cipher is the Vignre square. Table3.3 shows the Vignre square. Each row in the
https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 19/25
8/23/2016 Yuzu: Python Programming in Context

tablecorrespondstoadifferentkeysuchthatwemightencodethefirstletterofoursecretmessageusingrow
12,thesecondletterofthemessagemightbeencodedwithrow7,andsoon.Therowsthatwehavechosen
herearesimplerotationsofthealphabet.Eachrowinthetableshiftsthelettersofthealphabetoneplaceto
theleft.Whenalettershiftspastthefirstcolumn,itmovesaroundtotheendoftherightside.
LetsnowseehowtousetheVignresquaretoencodethemessage,theeaglehaslanded.Thefirststepis
todecideonakey.ChoosingthewordDAVINCIasourkey,wenextalignthekeyacrossthetopofthe
message,repeatingthelettersofthekeyasneededtocoverthemessage.Table3.4showshowthelettersin
DAVINCIcoverthelettersinourmessage.
Toencodethemessage,weusetherowcorrespondingtotheletterinthekey,andthecolumncorresponding
totheplaintextlettertofindtheciphertextletter.Forexample,thefirstletterinthemessageist,sowelook
incolumntandrowDandfindtheletterw.So,wisthefirstcharacterintheciphertext.Wenext
lookatcolumnhandrowAtofindthathisthesecondcharacter.Thethirdcharacterisincolumne
androwV,whichmeansthatzisthethirdcharacter.Noticethatthenextcharacterinthemessageisalso
anebutthistimeweusecolumneandrowIsothiseisencodedasanm.Ifwecontinuefollowing
thispattern,theentirephraseisencodedaswhzrcooepnuoailrf.
AlthoughtheVignrecipherwasinventedin1562,itwentunusedfor200yearsbecausetheciphersecretaries
foundithardtoimplement.ObviouslytheydidnothavePython.Letswriteafunctiontoencryptamessage
usingtheVignrecipher.Wewillbeginbydoingatopdownanalysisoftheprocedureneededtoencodea
message.

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 20/25
8/23/2016 Yuzu: Python Programming in Context

Table3.3TheVignresquare

Table3.4Matchingkeyletterswithmessageletters

1.Initializeanemptyresultstring.
2.Foreachletterintheplaintextmessage

(a)determinewhichletterofthekeyweshoulduse.
(b)lookuptheciphertextletterintheVignresquare,usingthekeyletterrowandplaintext
charactercolumn.

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 21/25
8/23/2016 Yuzu: Python Programming in Context

(c)usetheaccumulatorpatterntoaddtheciphertextlettertotheciphertextmessage.
3.Returntheresultstringastheciphertextmessage.
AtfirstglanceitmayseemlikelookingupacharacterinTable3.3wouldbethemostdifficultpartofthis
procedure, but it is not as difficult as it might seem. Lets think about what we know (note that the term
ordinalpositionreferstothepositionoftheletterintherangefrom0to25.Thisismappedusingourhelper
functionsdevelopedearlier.):
1.Eachrowofthetablerotatesthelettersonemorepositiontotheleft.
2.Therowofthetablecorrespondstotheordinalvalueofthekeyletter.
3.Thecolumnofthetablecorrespondstotheordinalvalueoftheplaintextletter.

Fromthefactsoutlinedpreviously,thestepsweneedtofollowtofindourciphertextlettersareasfollows:We
startwithourplaintextalphabetandthenrotatethelettersinthealphabetbyipositionswhereiistheordinal
valueofthekeyletter.Forexample,iftheletterisc,theordinalvalueis2andthealphabetismodifiedso
thatitstartswithcandthefinaltwolettersareab.Finally,wefindtheciphertextletteratindexjofthe
rotatedtext,wherejistheordinalvalueoftheplaintextletter.
Before we write the Python code for this procedure, we can make one more simplification. Using modulo
arithmetic,wecanavoidcopyingandshiftinglettersinastring.Withoutshiftinganyletters,wecansimply
usetheformula:
cipherTextLetter=(plainTextLetterIndex+keyLetterIndex)%26
Suppose we want to encrypt the letter e using the key letter j. The ordinal value for e is 4, and the
ordinalvalueforjis9.Theordinalvalueoftheletterncomesfromapplyingtheformula9+4%26=
13.IfyoucheckrowJandcolumneofTable3.3,youwillseethisisexactlyright.Letslookatanother
example.Supposewewanttoencryptthelettertusingthekeyletters.Theordinalvalueoftis19,and
theordinalvalueofsis18.Thismeansthat(18+19)%26=37%26=11,and11istheordinalvalueforthe
letterl,whichyouwillfindinrowscolumnt.
The code for the vignereIndex function is shown in Listing 3.11. The vignereIndex simply uses our helper
functionsletterToIndexandindexToLettertodothelookupsweneed.

Listing3.11LookingupaletterintheVignresquare

The final step in implementing the Vignre cipher is to write a function that takes a key and a plaintext
messageasparametersandreturnstheciphertextmessage.Asbefore,wewilltakeacharacterbycharacter
approach to encrypting the message, applying our vignereIndex function to each character. The only
remaining question is how to decide which letter of the key to use in conjunction with each letter of the
message.
Onewaytosolvethisproblemistoduplicatethekeyasmanytimesasneededsowecancoveralltheletters
inthemessage,aswedidinTable3.4.Thisiseasytodousingthestringrepetitionoperatoraswellassome
division.IfthemessageisMcharacterslongandourkeyisKcharacterslong,weneedM/Krepetitionsofthe
keytocoverthemessage.Wecanthenusealoopsuchasthefollowingtoiterateoverthecharactersinthe
messageandthecharactersintheduplicatedkeyinparallel:

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 22/25
8/23/2016 Yuzu: Python Programming in Context

foriinrange(len(message)):
nc=vignereIndex(repeatKey[i],message[i])
If our message is long, making another long string by duplicating the key is wasteful. We can avoid
duplicatingthekeybyusingmoduloarithmetic.IfourkeyisKletterslong,wewantanindexcounterforour
key to repeatedly cycle through the numbers 0 through K1. The modulo operator allows us to do exactly
that.TrySession3.12foryourself.
(Notethattheend=''parameterpreventseachnumberfrombeingprintedonaseparateline.)

>>>foriinrange(100):
print(i%7,end='')

0123456012345601234560123456
0123456012345601234560123456
0123456012345601234560123456
0123456012345601>>>

Session3.12Demonstratingcircularcountingwithmoduloarithmetic

Usingthemoduloarithmeticdemonstratedintheprevioussession,wecanuseacountertokeeptrackofthe
current character in the message and then use counter%i as the index into the key. Listing 3.12 shows the
entirefunctionneededtoencodeamessageusingtheVignrecipher.

Listing3.12EncryptingamessageusingtheVignrecipher

Exercises

3.28WriteafunctionundoVig(keyLetter,ctLetter)thattakesaletterfromthekey,aciphertextletter,and
returnstheplaintextletter.

3.29WriteafunctiondecryptVignerethattakesakeyword,theciphtertextforthemessage,andreturnsthe
plaintextmessage.

3.8Summary
In this chapter we introduced an important Python data type called stringssequences of characters that
provideawiderangeofbuiltinoperatorsandmethods.Stringsprovide:

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 23/25
8/23/2016 Yuzu: Python Programming in Context

Concatenation
Repetition
Indexing
Slicing
Length
Membership
Weusedstringmanipulationtoimplementsomebasicencryptionalgorithms.Thesealgorithmsallowedusto
illustrateiterationovercharactersinastringaswellastheaccumulatorpatternasitappliestostrings.Finally,
weusedabstractiontobuildupacollectionofsmallfunctionsthatcouldbeusedtosolvelargerproblems.

KeyTerms
accumulatorpattern
AmericanStandardCodeforInformationInterchange(ASCII)
bruteforce
character
ciphertext
concatenation
cryptography
decryption
encryption
index
key
length
plaintext
prompt
railfencecipher
repetition
sequence
slice
string
substitutioncipher
substring
transpositioncipher

PythonKeywords
Center
Chr
Count
def
find
for
if
ifelse
index
https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 24/25
8/23/2016 Yuzu: Python Programming in Context

input
len
ljust
ord
rjust
str

https://reader.yuzu.com/#/books/9781284089042/c/6/20!/4/500@0:0 25/25
8/23/2016 Yuzu: Python Programming in Context

CHAPTER4
ANestofSnakes:IntroducingthePythonCollections

4.1Objectives
TounderstandPythonlists
Touselistsasameansofstoringdata
Tousedictionariestostoreassociativedata
Toimplementalgorithmstocomputeelementarystatistics

4.2WhatIsData?
Everydaytherearethousandsofearthquakesthatoccuraroundtheworld.Mostofthemaresomildthatthey
are hardly noticed except by those who have monitoring equipment. If we consider only those earthquakes
thatyoufeel,thentheremaybe35onatypicalday.Somedaystheremaybeonly10butonotherdaysthere
maybe40or50.
What we have just described is a way that earthquake occurrence might be reported through dataan
assortmentofitems,manytimesnumerical,thathavebeenobserved,measured,orcollectedbysomemeans.
Thesedataitemspertaintosomeexperiment,event,oractivitythatweareinterestedinknowingmoreabout.
Data, sometimes referred to as raw data, represents the starting point for analysis that can be done in an
attempt to discover underlying characteristics that might be present. These characteristics are typically
referredtoasinformation.Datacancontaininformationinvariousforms,andtheinformationcanbeusedin
a variety of ways. Sometimes this information allows us to make generalizations about the data items. We
may also be able to make predictions of future events based on the data. This analysis is based on the
mathematicalscienceofstatistics.
Thischapterisaboutdata,information,andstatistics.Wewillfocusonwaysthataprogramminglanguage
likePythoncanhelpperformsomeofthebasicdataprocessingtaskscommonwithlargeamountsofdata.

4.3StoringDataforProcessing
Anytimethatweworkwithlargeamountsofdataitisnecessarytohavesomemeansoforganizedstorageso
thatprocessingofthedatacantakeplaceinanorderlyandefficientmanner.Computerscienceprovidesus
with a number of alternatives for accomplishing this task. We will consider two of Pythons builtin
collectionsasameansofstoringourdatavalues:stringsandlists.

4.3.1StringsRevisited

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 1/28
8/23/2016 Yuzu: Python Programming in Context

Inthepreviouschapterweintroducedtheideaofastringasasequentialcollectionofcharacters.Eachstring
isconsideredtobeorderedfromlefttoright,andeachindividualcharactercanbeaccessedusingtheindexing
operation. Provided that our data items can be thought of as characters, a string might make the perfect
collectionmechanism.
Asanexample,assumethatwehavejusttakenamultiplechoiceexamwhereeachquestionhasfivepossible
answerslabeledA,B,C,D,andE.Ouranswersheetmightlooklikethis:

1.A
2.B
3.E
4.A
5.D
6.B
7.B
8.A
9.C
10.E

Inordertostoreouranswersforlaterprocessing,asimplestringof10characterscouldbeused.Thestring
willcontainthetendataitemsgatheredfromthestudent:

>>>myanswers="ABEADBBACE"
>>>foranswerinmyanswers:
print(answer)
A
B
E
A
D
B
B
A
C
E
>>>

Thistechniqueallowseasyaccesstoeachindividualanswer.Furthermore,usingiterationoverstrings,wecan
processtheentireexam.However,therearesomepotentialdrawbacks.Whatiftheexamwehadtakenwasin
mathclasswherewehavetosolvenumberproblemsandwritedownourfinalanswer?Nowtheanswersare
numbersinsteadofcharacters.Canastringstillbeused?
Forexample,hereisouranswersheetnow:

1.34
2.56
3.2
4.652
https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 2/28
8/23/2016 Yuzu: Python Programming in Context

5.26
6.1
7.99
8.865
9.22
10.16

Ifweattempttousethesamestringbasedstoragetechniqueasbefore,wemightgetsomethinglikethis:

>>>myanswers="34562652261998652216"
>>>

Ofcourse,itiseasytoseethatwehavearealproblem.Thisorganizationwillnotallowustoeasilyknowthe
correspondencebetweenquestionandanswer.Certainlyallofthenumericcharactersarepresentbutthereis
nodistinctionbetweentheindividualintegervalues.
The problem is that we need a way to store collections of integers instead of being restricted to just using
characters.Likethestring,wherethereisonecharacterperposition,wewouldlikeanorganizationthatcan
provideoneintegerperposition.Fortunately,thereissuchacollection,thelist.

4.3.2Lists
Inadditiontotheprimitivenumericclasses,Pythonhasanumberofpowerfulbuiltincollectionclasses.We
havealreadyseenstrings.Inthissectionweintroducethelist,acollectionthatisverysimilartostringsin
generalstructurebuthasspecificdifferencesthatmustbeunderstoodforthemtobeusedproperly.Figure4.1
showsthatstringsandlistsareexamplesofsequentialcollections.
Alistisanordered,sequentialcollectionofzeroormorePythondataobjects.Listsarewrittenascomma
delimited values enclosed in square brackets. We call a list with zero data objectstheempty list, which is
represented simply by [ ]. Recall in the previous chapter that strings are homogeneous collections because
eachiteminthecollectionisthesametypeofobjectacharacter.Lists,bycontrast,areheterogeneousand
canbecomposedofanykindofobject.InSession4.1,thelistmylistconsistsoftwointegers,afloatingpoint
valueandastring.

Figure4.1Listsandstringsassequentialcollections

>>>[3,"cat",6.5,2]
[3,'cat',6.5,2]
>>>mylist=[3,"cat",6.5,2]
>>>mylist
[3,'cat',6.5,2]
https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 3/28
8/23/2016 Yuzu: Python Programming in Context

>>>

Session4.1APythonlist

As with other values in Python, asking the interpreter to evaluate a list will simply return the list itself. In
ordertorememberthelistforlaterprocessing,itsreferenceneedstobeassignedtoavariable.Evaluatingthe
variable returns the list. Figure 4.2 shows the sequential organization of the items in the example list in
Session4.1.

Since lists are considered to be sequential, they support a number of operations that can be applied to any
Python sequence. These are the same operations that we used with strings since both are composed of a
sequentialcollectionofitems.Table4.1reviewstheseoperationsandSession4.2givesexamplesoftheiruse.

Figure4.2Sequentialstorageoftheelementsinalist

OperationName Operator Explanation


Indexing [] Accessanelementofasequence
Concatenation + Combinesequencestogether
Repetition * Concatenatearepeatednumberoftimes
Membership in Askwhetheranitemisinasequence
Membership notin Askwhetheranitemisnotinasequence
Length len Askthenumberofitemsinthesequence
Slicing [:] Extractapartofasequence

Table4.1OperationsonanysequenceinPython

>>>mylist
[1,3,'cat',4.5]
>>>mylist[2]
'cat'
>>>mylist+mylist
[1,3,'cat',4.5,1,3,'cat',4.5]
>>>mylist*3
[1,3,'cat',4.5,1,3,'cat',4.5,1,3,'cat',4.5]
>>>len(mylist)
4
>>>len(mylist*4)
16
>>>mylist[1:3]
[3,'cat']
>>>3inmylist
True
>>>"dog"inmylist

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 4/28
8/23/2016 Yuzu: Python Programming in Context

False
>>>delmyList[2]
>>>myList
[1,3,4.5]

Session4.2Usingsequenceoperatorswithlists

Note that the indices for lists, as with strings, start with 0. The slice operation mylist[1:3] returns a list of
itemsstartingwiththeitemindexedby1uptobutnotincludingtheitemindexedby3.
Stringsareimmutablecollectionsofdatawhereindividualitemswithinthestringcannotbechanged.Thisis
nottrueforlists.Itispossibletochangeanindividualmemberofalistbyusingtheassignmentstatementand
placing the indexed location on the lefthand side. Thus, lists are mutable collections of data that can be
modified.NotethatthelastexampleinSession4.2introducesthedeloperatorthatallowsyoutodeletean
itemfromalist.Thisoperationisnotpermittedonstrings.
Session 4.3 shows an assignment statement modifying the item at index 2 in the list changelist. Figure4.3
showsthatalistisactuallyacollectionofreferencestoPythonobjects.Wecallthisareference diagram.
Changinganiteminthelistsimplychangesthereferencestoredatthatposition.Notethatthesameoperation
doesnotworkwhenappliedtoastringvariable.ThePythoninterpreterreportsanerrorsincestringsdonot
supporttheabilitytochangeasinglecharacter.

>>>changelist=[1,2,"bucklemyshoe",3,4,"shutthedoor"]
>>>changelist
[1,2,'bucklemyshoe',3,4,'shutthedoor']
>>>changelist[2]="theskyisblue"
>>>changelist
[1,2,'theskyisblue',3,4,'shutthedoor']
>>>
>>>name="Monte"
>>>name[2]="x"

Traceback(mostrecentcalllast):
File"",line1,intoplevel
name[2]="x"
TypeError:objectdoesnotsupportitemassignment
>>>

Session4.3Mutatingalist

Therepetitionoperatorisalsoimpactedbythisideaofacollectionofreferences.Theresultofperforming
this action is a repetition of references to the data objects in the sequence. This can best be seen by
consideringthestatementsinSession4.4.

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 5/28
8/23/2016 Yuzu: Python Programming in Context

Figure4.3Thecollectionofreferencesformingalist

>>>mylist=[1,2,3,4]
>>>A=[mylist]*3
>>>A
[[1,2,3,4],[1,2,3,4],[1,2,3,4]]
>>>mylist[2]=45
>>>A
[[1,2,45,4],[1,2,45,4],[1,2,45,4]]
>>>

Session4.4Mutatingalistcreatedwithrepetition

ThevariableAholdsacollectionofthreereferencestotheoriginallistcalledmylist.Notethatachangeto
oneelementofmylistshowsupinallthreeoccurrencesinA.Thisisexplained,onceagain,bythefactthat
therepetitionresultisactuallyalistofthreereferencestothesamelist,asshowninFigure4.4.
Ausefulfunctionforcreatinglistsisthelistfunction.Thelistfunctionconvertsothersequencestolists.We
have seen two such sequences: strings and ranges. Recall that the range function returns an object
representingasequenceofintegers.Session4.5demonstratestheuseoflisttocreatesomesimplelistsfrom
stringsandranges.

Figure4.4Therepetitionoperatorcopiesreferences

>>>range(10)
range(10)
>>>list(range(10))
[0,1,2,3,4,5,6,7,8,9]
>>>list(range(10,2,2))
[10,8,6,4,2]
>>>list(''thequickfox'')
['t','h','e','','q','u','i','c','k','','f','o','x']
>>>

Session4.5Usingthelistfunction

Listssupportanumberofusefulmethods.Table4.2providesasummary.Examplesoftheirusecanbeseenin
https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 6/28
8/23/2016 Yuzu: Python Programming in Context

Session4.6.

MethodName Use Explanation


append alist.append(item) Addsanewitemtotheendofalist
insert alist.insert(i,item) Insertsanitemattheithpositioninalist
pop alist.pop() Removesandreturnsthelastiteminalist
pop alist.pop(i) Removesandreturnstheithiteminalist
sort alist.sort() Modifiesalisttobesorted
reverse alist.reverse() Modifiesalisttobeinreverseorder
index alist.index(item) Returnstheindexofthefirstoccurrenceofitem
count alist.count(item) Returnsthenumberofoccurrencesofitem
remove alist.remove(item) Removesthefirstoccurrenceofitem

Table4.2MethodsprovidedbylistsinPython

>>>mylist
[1024,3,True,6.5]
>>>mylist.append(False)
>>>mylist
[1024,3,True,6.5,False]
>>>mylist.insert(2,4.5)
>>>mylist
[1024,3,4.5,True,6.5,False]
>>>mylist.pop()
False
>>>mylist
[1024,3,4.5,True,6.5]
>>>mylist.pop(1)
3
>>>mylist
[1024,4.5,True,6.5]
>>>mylist.pop(2)
True
>>>mylist
[1024,4.5,6.5]
>>>mylist.sort()
>>>mylist
[4.5,6.5,1024]
>>>mylist.reverse()
>>>mylist
[1024,6.5,4.5]
>>>mylist.count(6.5)
1
>>>mylist.index(4.5)
2
>>>mylist.remove(6.5)
>>>mylist
https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 7/28
8/23/2016 Yuzu: Python Programming in Context

[1024,4.5]
>>>

Session4.6Examplesoflistmethods

Youcanseethatsomeofthemethods,suchaspop,returnavalueandalsomodifythelist.Others,suchas
reverse,simplymodifythelistwithnoreturnvalue.Althoughpopwilldefaulttotheendofthelist,itcanalso
removeandreturnanitemataspecificindexlocation.Notethefamiliardotnotationforaskinganobjectto
invokeamethod.Youcanreadmylist.append(False)asasktheobjectmylisttoperformitsappendmethod
usingthevalueFalseasaparameter.Alldataobjectsinvokemethodsinthisway.
Beforeleavingthissection,wewilldescribeoneadditionalstringmethodcalledsplit,whichtakesastringas
aparameterthatindicatestheplacestobreakthestringintosubstrings.Thesubstringsarereturnedinalist.
Bydefault,ifnoparameterispassedtosplit,itwillbreakthestringusingoneormorespacesasthedelimiter.
Session4.7demonstrateshowthesplitmethodworks.

>>>a="minnesotavikings"
>>>a.split()
['minnesota','vikings']
>>>a.split('i')
['m','nnesotav','k','ngs']
>>>a.split('nn')
['mi','esotavikings']
>>>

Session4.7Usingthesplitmethod

Exercises

4.1Createalistwiththefollowingfiveitems:7,9,'a','cat',False.AssignthislisttothevariablemyList.

4.2WritePythonstatementstodothefollowing:

(a)Append3.14and7tothelist.
(b)Insertthevalue'dog'atposition3.
(c)Findtheindexof'cat'.
(d)Countthenumberof7sinthelist.
(e)Removethefirst7fromthelist.
(f)Remove'dog'fromthelistusingpopandindex.

4.3Splitthestring"thequickbrownfox"intoalistofwords.

4.4Splitthestring"mississippi"intoalistusingthe'i'asthesplitpoint.

4.5Writeafunctionthattakesasentenceasaparameterandreturnsthenumberofwordsinthesentence.

4.6AlthoughPythonprovidesuswithmanylistmethods,itisgoodpracticeandveryinstructivetothink
abouthowtheyareimplemented.ImplementaPythonfunctionthatworkslikethefollowing:

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 8/28
8/23/2016 Yuzu: Python Programming in Context

(a)count
(b)inreturnTrueifitemisinlist
(c)reverse
(d)indexreturn1ifnotinthelist
(e)insert

4.7Writeafunctionshufflethattakesalistandreturnsanewlistwiththeelementsshuffledintoarandom
order.

4.8Writeafunctionshufflethattakesalistasaparameterandshufflesthelistinplace.

4.9Drawareferencediagramtoillustratethedifferencesbetweentheprevioustwoexercises.

4.10Supposeyouinitializethefollowinglist:mylist=[[]]*3.Evaluatethisexpression.

4.11Evaluatetheexpressionmylist[1].append(2).Explaintheresult.

4.12Drawareferencediagramtoillustratewhatishappeningintheprevioustwoexercises.

4.13Writeacodefragmenttoinitializemylistsothateachsublistisindependentthatis,sothatchanges
toonelistdonotaffecttheothers.

4.4SimpleDispersion
Wenowturnourattentiontothesimpleideaofdispersionameasureofhowspreadoutthedatavaluesare.
Throughoutthenextsections,weanalyzedatasetsusingsimplestatisticsandassumethatourdataisstoredin
liststructures.Forexample,duringonesevendayperiodlastyear,therewere20earthquakesonMonday,32
onTuesday,21onWednesday,26onThursday,33onFriday,22onSaturday,and18onSunday.Thisdata
canbestoredinthesimplelist[20,32,21,26,33,22,18].
The easiest way to look at dispersion is to consider the range of values and simply compute the difference
betweenthelargestandthesmallestvalueinthedataset.InPython,thisisveryeasytodobecausethereare
builtinfunctions,maxandmin,thatworkonanysequenceandreturnthemaximumandminimumvaluesin
thecollection,asshowninSession4.8.

>>>alist=[20,32,21,26,33,22,18]
>>>max(alist)
33
>>>min(alist)
18
>>>
>>>max("house")
'u'
>>>min("house")
'e'
>>>max(alist)min(alist)
15
>>>

Session4.8Usingtheminandmaxfunctions

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 9/28
8/23/2016 Yuzu: Python Programming in Context

Note that these functions work with strings as well as with lists since both are examples of sequences.
Charactersareorderedbytheirordinalvalues.ThefinalexampleinSession4.9showsthattherangeofthe
integerlistcanbecomputedbysubtractingtheminimumfromthemaximum.
Toconstructafunctiontoreturntherangeofadataset,wewillassumethatthedatavalueshavebeenplaced
in a list. The function can then take the list as a parameter and return the range. Listing 4.1 shows such a
functionandSession4.9showsitinuse.

Listing4.1Afunctiontoreturntherangeofalist

>>>getRange([2,4])
2
>>>getRange([20,32,21,26,33,22,18])
15
>>>

Session4.9UsingthegetRangefunction

An interesting question arises if we consider what would happen if we did not have a builtin function to
returnthemaximumvalueinthelist.ItwouldbepossibletoconstructourowngetMaxfunctionbyiterating
throughtheitemsinthelistandkeepingtrackofthelargestvalueseen.Thefunction,showninListing4.2,
makestheassumptionthatthereisatleastoneiteminthelist(otherwisetheideaofamaximummakesno
sense).ThevariablemaxSoFardoesmostofthework.WestartoutbylettingmaxSoFarbethefirstitemin
thelist(alist[0]).Then,aswevisitotheritems,wechecktoseeiftheyaregreaterthanmaxSoFar.Ifso,we
reassignthemaxSoFarvariabletothenewlargestitem.Aftercheckingtheremainingitems,maxSoFarholds
themaximumofall.

Listing4.2Constructingafunctiontoreturnthemaximumvalueinalist

Listing4.3showsanalternativeimplementationforthegetMaxfunction.Thistime,theiterationisperformed
byiteminsteadofbyindex.Ineithercase,eachelementofthelistiseventuallyprocessedandcompared
againstthemaximumitemseenuptothatpoint.AsimilarargumentcanbemadetoreimplementthegetMin
function.Wewouldsimplykeeptrackoftheminimumvalueseensofar.

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 10/28
8/23/2016 Yuzu: Python Programming in Context

Listing4.3Alternativefunctiontoreturnthemaximumvalueinalist

Exercises

4.14ImplementthegetMinfunctionusingiterationbyindex.

4.15ImplementthegetMinfunctionusingiterationbyitem.

4.16RewritethegetRangefunctionusinggetMinandgetMax.

4.5CentralTendency
Oneofthemostoftenusedmeasuresofacollectionofdataisknownascentraltendencya process that
estimateswherethecenterofacollectionwillbefound.Therearethreecommonwaystocomputecentral
tendency:(1)mean,(2)median,and(3)mode.

4.5.1Mean
Themostcommonmeasureofcentraltendencyisthemean(alsocalledtheaverage).Tocomputethemean,
wesimplyaddupthevaluesinacollectionanddividebythenumberofitems.Listing4.4showshowthisis
done.Givenalistofvalues,weusethesumfunctiontocomputeatotalandthelenfunctiontocomputethe
numberofvalues.Themeanissimplythesumdividedbythelength.Session4.10showsthemeanfunction
inaction.

Listing4.4Computingthemeanofalist

>>>mean([20,32,21,26,33,22,18])
24.571428571428573
>>>

Session4.10Usingthemeanfunction

Exercises

4.17Createalistofthenumberofstudentsineachofyourclasses.Usethemeanfunctiononthatlist.

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 11/28
8/23/2016 Yuzu: Python Programming in Context

4.18Replacethecalltothesumfunctionwithaniterationthatcomputesthetotalofthevaluesinalist.

4.5.2Median
Thesecondmeasureofcentraltendencyiscalledthemedian,foundbylocatingtheitemthatoccursinthe
exact middle of a collection. Another way to determine the median is to find the value located at a point
wherehalfofthevalueslieaboveitandhalfofthevaluesliebelow.
Onewaytocomputethemedianistoputtheitemsinorder,fromlowesttohighest,andthenfindthevalue
thatfallsinthemiddle.Ifthereareanoddnumberofitems,thiswillbeadistinctvalue.However,ifthereare
anevennumberofitems,theaverageofthetwomiddlevalueswillbeused.Figure4.5showsthetwocases.
Itisworthnotingthattheprocessoforderingvaluesisoftenreferredtoassorting.Sortingisaveryimportant
topicincomputerscienceandthereareanumberofalgorithmsthatcanbeusedtotakealistofdataandplace
theminorder.Asyouknow,thelistobjecthasasortmethodthatallowsyoutoputthelistinorder.Although
we will not cover sorting techniques here, you will undoubtedly study them in your algorithms and data
structurescourses.

Figure4.5Findingthemedianofalistofvalues

ThePythonstatementsinSession4.11showsomeofthecomputationthatwillberequired.Notethatwithan
oddnumberofvalues,integerdivisionby2willprovidethecorrectindexintothelist.However,withaneven
numberofitems,divisionby2givestheindexoftherightmostitemofthemiddlepair.

>>>alist=[24,5,8,2,9,15,10]
>>>alist.sort()
>>>alist
[2,5,8,9,10,15,24]
>>>len(alist)
7
>>>len(alist)//2
3
>>>alist[3]
9
>>>alist=[2,5,8,9,10,15,24,54]
>>>len(alist)
8
>>>len(alist)//2
4
>>>alist[4]
10
>>>

Session4.11Computationrequiredtofindthemedianitem

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 12/28
8/23/2016 Yuzu: Python Programming in Context

Listing4.5showsthecompletePythonfunctiontocomputeandreturnthemedianvalueofalist.Asnoted
above,thelistitemsmustbeinsortedorder.Todothis,wesimplyusethesortmethodfromthelistclass.
Beforesorting,wearecarefultomakeacopyoftheoriginallistincasetheoriginalorderisimportantfor
laterprocessing.Tomakethecopy,weusethesliceoperator(line2)withbeginningindexatthestartofthe
listandendingindexuptoandincludingthelastitem.

Listing4.5Computingthemedianofalist

The other important part of the solution is the decision about whether there are an even or odd number of
items. To make this choice, we can take advantage of the modulo operator (%) and check the result of
dividingby2.Evennumberswillhavenoremainder.Line4inListing4.5showsasimpleselectionstatement
tocheckforaremainderofzero.
Session4.12showsthemedianfunctioninaction.Sincewehavedataforsevendays,ourmediannumberof
earthquakesis22.Iftherehadbeenanadditionaldayofdata,saywith29quakes,themedianwouldbe24,
whichistheaverageof22and26.

>>>median([20,32,21,26,33,22,18])
22
>>>
>>>median([20,32,21,26,33,22,18,29])
24
>>>

Session4.12Usingthemedianfunction

Exercises
4.19Drawareferencediagramtoillustratewhyitisimportanttomakeacopyofalistbeforesorting.

4.20Findthemeanageoftenpeoplenearyou.

4.21Findthemeanageoftenpeoplenearyouplusyourprofessor,whois39yearsold.

4.22Findthemedianageoftenpeoplenearyou.

4.23Findthemedianageoftenpeoplenearyouplusyouryoungprofessor.

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 13/28
8/23/2016 Yuzu: Python Programming in Context

4.5.3Mode
Themodeofadatasetisthemostfrequentlyoccurringvalue.Itispossiblethattherecanbemorethanone
mode. For example, in the list [1,5,2,1,1,6,3,1,5], 1 is the mode since it occurs more than any other value.
However,ifthelistcontained[1,5,2,1,1,5,5,6,3,1,5],then1and5wouldbothbeconsideredthemodevalues
sincetheyeachoccurfourtimes.
Inordertocomputethemode,weneedtoprocesseachdataiteminourlistandkeepacountofhowmany
timesithasoccurredthusfar.Unfortunately,thisisnotquiteaseasyasitmightsound.Sincewedonotknow
how many different values there are, it is not possible to create individual variables to hold each count.
Instead,weneedtocreateacollectionofcountingvariablesandtrytokeepthemassociatedwiththecorrect
datavalue.

PythonDictionaries

Inordertosolvethemodeproblem,weneedtoassociateanoccurrencecountwitheachindividualdatavalue.
Thisunordered,associativecollectioniscalledadictionary.Pythondictionariesarecollectionsofassociated
pairsofitemswhereeachpairconsistsofakeyandavalue.Figure4.6showshowPythondictionaries,lists,
andstringsarerelatedtooneanother.

Figure4.6SequentialandnonsequentialPythoncollections

Indictionarynotation,akeyvaluepairiswrittenaskey:value.Dictionariesarewrittenascommadelimited
key:valuepairsenclosedincurlybraces.Theemptydictionaryiswrittenas{}.Inthefollowingexample,we
arecreatingadictionarywithtwoentries,eachofwhichisanassociationbetweenaname(astring)andan
age(aninteger):

>>>ages={'David':45,'Brenda':46}
>>>ages
{'Brenda':46,'David':45}
>>>

Wecanmanipulateadictionaryinanumberofways(seeSession4.13).Accessingadictionaryvaluebyits
keylooksmuchlikeasequenceindexoperationexceptthatdictionariesarenotrestrictedtonumericindices.
Toaccessavalue,weusethedictionarykeyinsidethesquarebracketindexoperator.Toaddanewkeyvalue
pair to the dictionary, we use the index operator with the new key on the lefthand side of the assignment
statementandthenewvalueontheright.InSession4.13weaddthreenewvaluestoouragesdictionary.

>>>ages['David']

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 14/28
8/23/2016 Yuzu: Python Programming in Context

45
>>>ages['Kelsey']=19
>>>ages
{'Kelsey':19,'Brenda':46,'David':45}
>>>ages['Hannah']=16
>>>ages
{'Hannah':16,'Kelsey':19,'Brenda':46,'David':45}
>>>ages['Rylea']=7
>>>ages
{'Hannah':16,'Kelsey':19,'Rylea':7,'Brenda':46,'David':45}
>>>len(ages)
5
>>>ages['David']=ages['David']+1
>>>ages['David']
46
>>>ages
{'Hannah':16,'Kelsey':19,'Rylea':7,'Brenda':46,'David':46}
>>>

Session4.13Examplesofaddingandmodifyingvaluesinadictionary

Itisimportanttonotethatthedictionaryismaintainedinnoparticularorderwithrespecttothekeys.Thepair
'Rylea':7 was placed in the middle of the dictionary. In fact, the placement of a key is dependent on some
internal storage algorithm designed to make the lookup operation work efficiently. This is one of the real
benefits of dictionaries: quick access to data values by key. We also show the len function performing the
sameroleithadinpreviouscollections,inthiscasethenumberofkeyvaluepairs.
Oneadditionalcharacteristicofdictionariesisthattheyaremutablethatis,theycanbemodified.Thisis
similartothewaythatlistsbehave.Recall,however,thatstringsareimmutableandcannotbechanged.For
example, if David has a birthday, then his age needs to be incremented by 1. To do this, we can use the
accumulatorpatternages['David']=ages['David']+1.
Aswithlistsandstrings,dictionarieshaveasetofcommonmethods.Table4.3describesthemandSession
4.14 shows them in action. You will see that there are two variations on the get method. If the key is not
presentinthedictionary,getwillreturnNone.None,alsoreferredtoasthenullobject,representsanobject
withnovalue.Asecond,optionalparameterforthegetmethodcanspecifyareturnvalueinthecasewhere
thekeyisnotpresent.
Thekeysmethodreturnsadict_keysobject.Toseethecontentsofadict_keysobjectwecanconvertittoa
list using the list function. It is possible to iterate over a dict_keys object using a for loop without first
convertingittoalist.Thevaluesanditemsmethodsworkinasimilarfashion.

MethodName Use Explanation


keys adict.keys() Returnsadict_keysobject
values adict.values() Returnsadict_valuesobject
items adict.items() Returnsadict_itemsobject
get adict.get(k) ReturnsthevalueassociatedwithkNoneotherwise
get adict.get(k,alt) Returnsthevalueassociatedwithkaltotherwise
in keyinadict ReturnsTrueifkeyisinthedictionaryFalseotherwise

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 15/28
8/23/2016 Yuzu: Python Programming in Context

notin keynotinadict ReturnsTrueifkeyisnotinthedictionaryFalseotherwise


index adict[key] Returnsthevalueassociatedwithkey
del deladict[key] Removestheentryfromthedictionary

Table4.3MethodsprovidedbydictionariesinPython

Theitemsmethodreturnsadict_itemsobject.Applyingthelistfunctiontoadict_itemsobjectreturnsalistof
keyvaluepairs.Thesepairsarestoredastuples.Tuplesareverysimilartolistsinthattheyareheterogeneous
sequences of data. The difference is that a tuple is immutable. Like a string, a tuple element cannot be
changed.Tuplesarewrittenascommadelimitedvaluesenclosedinparentheses.Assequences,theycanuse
anyoftheoperationsthatweredescribedearlierforlistsandstrings.

>>>ages.keys()
dict_keys(['Hannah','Kelsey','Rylea','Brenda','David'])
>>>list(ages.keys())
['Hannah','Kelsey','Rylea','Brenda','David']
>>>ages.values()
dict_values([16,19,7,46,46])
>>>list(ages.values())
[16,19,7,46,46]
>>>ages.items()
dict_items([('Hannah',16),('Kelsey',19),('Rylea',7),('Brenda',46),('David',46)])
>>>list(ages.items())
[('Hannah',16),('Kelsey',19),('Rylea',7),('Brenda',46),('David',46)]
>>>ages.get('Lena')
>>>print(ages.get('Lena'))
None
>>>ages.get('Lena','Noagelisted')
'Noagelisted'
>>>'Rylea'inages
True
>>>delages['David']
>>>ages
{'Hannah':16,'Kelsey':19,'Rylea':7,'Brenda':46}
>>>forkinages.keys():
print(k)

Hannah
Kelsey
Rylea
Brenda
>>>forkinages:
print(k)

Hannah
Kelsey
Rylea
Brenda
>>>

Session4.14Usingdictionarymethods

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 16/28
8/23/2016 Yuzu: Python Programming in Context

Exercises

4.24Youhavebeengiventhefollowinglistsofstudentsandtheirtestscores:

names=['joe','tom','barb','sue','sally']
scores=[10,23,13,18,12]
Writeafunction,makeDictionary,thattakesthetwolistsandreturnsadictionarywiththenamesas
thekeyandthescoresasthevalues.AssigntheresultofmakeDictionarytoscoreDict,whichwillbe
usedintheexercisesthatfollow.

4.25UsingscoreDict,findthescorefor'barb'.

4.26Addascoreof19for'john'.

4.27CreateasortedlistofallthescoresinscoreDict.

4.28CalculatetheaverageofallthescoresinscoreDict.

4.29Updatethescorefor'sally'tobe13.

4.30Tomhasjustdroppedthisclass.Delete'tom'andhisscorefromscoreDict.

4.31Printoutatableofstudentsandtheirscoreswiththestudentslistedinalphabeticalorder.

4.32WriteafunctioncalledgetScorethattakesanameandadictionaryasparametersandreturnsthescore
forthatnameifitisinthedictionary.Ifthenameisnotinthedictionary,printanerrormessageand
return1.

ComputingtheMode

Wecannowreturntoourproblemofcomputingthemodeofadataset.Usingadictionaryofcounts,onefor
eachdataitem,wewillbeabletotracktheinformationnecessarytofindthemode.Weassumethatourmode
functionwillreceivealistofdatavalues.Itwillreturnalistofmodessinceitispossiblethattherecanbe
morethanone.Listing4.6showstheinitialimplementationofthemodefunction.
Inordertocounteachoccurrenceofthedataitems,weuseadictionarywherethekeyisthedataitemitself
andtheassociatedvalueistheoccurrencecount.Toprocesseachitem,wefirstchecktoseeifitalreadyexists
inthedictionaryasakey.Ifitdoes,allweneedtodoisincrementtheassociatedcount.Ifitdoesnot,we
needtomakeanewentryinthedictionaryforthefirstoccurrenceofanewkey.

Listing4.6Startingthemodefunction

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 17/28
8/23/2016 Yuzu: Python Programming in Context

An alternative way to implement this if statement would be to use the second form of the get method, as
describedearlier.Inthisform,ifthekeyispresent,itsassociatedvalueisreturned.Ifthekeyisnotpresent,
themethodwillallowustoincludeadefaultvaluetobereturned.Inourcase,ifthekeyisnotpresent,wecan
justreturnazerosinceithasnotoccurred.Eitherway,whenweaddone,thecountwillbecorrect.Noticethat
this is another variation on the accumulator pattern, except that the get method allows us to include the
initializationoftheaccumulatorvariablerightintheinitialization:

countdict[item]=countdict.get(item,0)+1

To complete our mode calculation, we need to go through our dictionary, key by key, and find the largest
count.Thekey(orkeys)associatedwiththatcountwillbeappendedtoalistofmodesthatwillbereturnedat
theendofthefunction.Fortunately,wecanrelyonceagainonourcollectionstoprovidemethodsthatwilldo
mostofthework.Recallthatthevalues()methodreturnsadictvaluesobjectthatbehavesmuchlikealistof
thevaluesinadictionary.Byusingthemaxfunction,wecanthenlearnthemaximumcount.Itisthenjusta
matter of iterating through the dictionary keys looking for a key with a value that matches the maximum
count.Whenwefindsuchakey,wecanplacethatkeyinthemodelist.
Listing4.7showsthecompletefunction.Notethatthemodeliststartsoutempty.Wecaniteratethroughthe
dictionarykeysbyusingaforloop,appendingkeystothemodelistasneeded.
Session4.15showsthemodefunctioninuse.Thefinalexampleinthissessionusesourearthquakedata[20,
32,21,26,33,22,18]fromthebeginningofthischapter.Sinceeachvalueoccursonce,eachvalueisamode.

Listing4.7Computingthemodeofalist

>>>mode([1,1,4,5,6,2,4,7,1,4,6,1])
[1]
>>>mode([1,1,1,2,2,2,3,3,3,4,4,4,4,5,5,5,5,6,6])
[4,5]
>>>mode([3,6,8,2,6,4,9,2,8,9,4,3,8,3,5,7,1,3,4])
[3]
>>>mode([20,32,21,26,33,22,18])
[32,33,18,20,21,22,26]
>>>
https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 18/28
8/23/2016 Yuzu: Python Programming in Context

Session4.15Usingthemodefunction

Exercises
4.33 If you do not have a dictionary to work with, you can still compute the mode by creating a list of
integerswheretheindexvalueintothelististhekey.Youneedalistofsizemax(value)plusone.This
will work only when the keys are integers and positive. Note that this is a very inefficient use of
memorywhenthedatavaluesaresparse.Implementthemodefunctionusingtheapproachdescribed.

4.6FrequencyDistribution
As we worked through the steps required to compute the mode of a data set, we actually solved the basic
statisticsproblemoffindingthefrequencydistributionarepresentationofthenumberoftimeseachvalue
occursinthedata.

4.6.1UsingaDictionarytoComputeaFrequencyTable
Onewaytodisplayafrequencydistributionistoshowatwocolumntable.Thefirstcolumngivesthedata
item,andthesecondgivestheassociatedcount.Thisappearstobeverysimilartoourcountdictionary.In
fact,theonlydifferenceisthatthetablewillneedtoshowtheitemsinorder.Recallthatthedictionarydoes
notmaintainanyobviousorder.Ourchallengeistoorderthedata.
Towritethisfunction,wecanstartwiththemodefunctionfromListing4.7.Insteadofextractingthecounts
and looking for the maximum, we will extract the keys, convert the dict_keys object to a list, and use the
builtinsortmethodtoprovidetheordering.Wecantheniterateoverthesortedlistofkeysandprintatable
entryconsistingofthekeyandtheassociatedcountfromthecountdictionary.Listing4.8showsthecomplete
function.
Note that we are not ordering the dictionary. We are creating a list of keys using the keys method, the list
function,andthenorderingthatlist.Oncewehavethelistordered,wecangobacktothedictionarytofind
theassociatedcounts.Session4.16showsthefunctioninaction.

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 19/28
8/23/2016 Yuzu: Python Programming in Context

Listing4.8Usingadictionarytocomputethefrequencydistribution

>>>

Session4.16Demonstratingthefrequencytablefunction

4.6.2ComputingaFrequencyTableWithoutaDictionary
Itispossibletoimplementthefrequencytablefunctionwithouttheuseofadictionary,butdoingsorequiresa
bitofingenuitywithrespecttolistprocessing.Inthissectionwewillworkthroughthisalternativeapproach
andintroduceausefulpatternforsequentiallistprocessing.
Ourplanistostartwithasortedcopyofthelist.Becausethelistissorted,alloccurrencesofdistinctcounts
willbecontiguous(nexttooneanother)inthelist.Asweiteratethroughthecounts,welookfortransitions
betweengroups.Thedifficultpartisknowingwhenthesetransitionsoccur.
Figure4.7showshowwetrackthelocationsofthetransitionsbetweengroupsofequalcountsinthelist.We
usetwovariables:currentandprevious.Bothvariableswillalwaysrefertocontiguouspairsofcounts.The
current count will be compared against the previous count. If they are equal, we know that we are still
processingagroupofequalcounts.However,whenthecurrentcountnolongermatchesthepreviouscount
(orwhenwerunoutofitemsinthelist),weknowthatwehavefoundtheendofagroup.
Listing4.9showsthecompletefunction.Tobegin,previouswillneedtobeinitializedsothatthefirstcountin
thelistisconsideredtobethestartofanewgroupofequalcounts.Todothis,wecansimplysetpreviousto
the first item in the list. In addition, an accumulator variable, groupCount, will be initialized to 0 since no
countshavebeenprocessed.

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 20/28
8/23/2016 Yuzu: Python Programming in Context

Figure4.7Usingpreviousandcurrenttolocatetransitions

Listing4.9Alternativemethodforcomputingafrequencytable

Wecannowprocesseachcountinthelist.Recallthetwopossibilities.Ifthecurrentmatchestheprevious,
thenwearestillinagroupofidenticalcounts.WeshouldincrementgroupCountandmovepreviousahead.
Ontheotherhand,ifcurrentdoesnotmatchprevious,weknowthatwehavelocatedatransition.Atthispoint
wecanprintarowinourtableconsistingofpreviousandgroupCount.Wealsoneedtomovepreviousahead
andinitializegroupCountto1sothatthenextgroupwillbecountedcorrectly.
Whenallofthecountshavebeenprocessed,thelastrowcorrespondingtothelastgroupwillnothavebeen
printed,sowecansimplyprintoutthevalueofcurrentandgroupCount.

Exercises

4.34ModifyfrequencyTableAltsothatitreturnsalistofkeycounttuples.

4.35Supposeyouhavealistofkeyscorevalueslikethefollowing:
[('john',10),('bob',8),('john',5),('bob',17),]
Writeafunctionthattakessuchalistasaparameterandprintsoutatableofaveragescoresforeach
person.

4.6.3VisualizingaFrequencyDistribution
Frequencydistributionscanbebestvisualizedthroughtheconstructionofagraphicalrepresentationknown
asahistogramorbarchart.Tocreateahistogram,wecanusetheTurtleclasstodrawoneverticallinefor
eachkey.Theheightofthelinewillrepresentthefrequencywithwhichthatkeyappearsinthedataset.By
comparingtheheightsofthelines,itispossibletoseetherelationshipsthatexistbetweenthefrequencies.
We use the frequencyTable function as a starting point. The frequency dictionary will still be constructed.
However,insteadofprintingoutthetable,wedrawahistogram.Asanexample,considerthedatalist[3,3,5,
https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 21/28
8/23/2016 Yuzu: Python Programming in Context

7,1,2,5,2,3,4,6,3,4,6,3,4,5,6,6].Figure4.8showsthebarchartthatwewouldliketoproduce.Note
thateachverticallinerepresentsthecount(previouslythesecondcolumninthetable).
Listing4.10showsthecompletePythonfunctionusedtocreateFigure4.8.Lines410shouldlookfamiliar.
They are simply creating our count dictionary as before. The remainder of the function does the work of
drawingthebarchart.
In order to create the histogram, we first need to use the setworldcoordinates method to scale our turtle
window.Thiswillbenecessarysincethedatavaluescancomefromawiderangeofpossibilities.Byscaling
thewindow,ourhistogramwillfitnicelywithinthewindow.

Figure4.8Afrequencyhistogram

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 22/28
8/23/2016 Yuzu: Python Programming in Context

Listing4.10Creatingahistogram

Recallthatthesetworldcoordinatesmethodallowsustoresetthecoordinatesofthelowerleftandupperright
cornersofthedrawingwindow.Inordertofindthebestsetofcoordinates,weshouldlookatourdata.Line
12extractsthelistofkeysfromthedictionary.Thisprovidesuswiththenumberofindividualdataitemsthat
willdirectlytranslateintothenumberoflinesinourhistogram.Lines1617performasimilarfunction,but
thistimeweconsidertherangeoffrequencycounts.Again,thisprovidesuswithinformationthatcanbeused
toscaletheheightsofthelines.
Lines 1921 are responsible for creating the drawing turtle and rescaling the window. Note that instead of
using(0,0)asthelowerleftcornerwehavechosentomoveoneunitbeyondinboththexandy directions.
https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 23/28
8/23/2016 Yuzu: Python Programming in Context

Thiswillprovideuswithroomlaterontodrawlabels.Theupperrightcornerwillbesetslightlylargerthan
themaximumcountandvalue,againtoleavesomeroom.
Beforedrawingtheactualbars,itisnicetoprovidesomesortofscale.Wecandothisbydrawingasimple
representationofthexandyaxes.Forourpurposeshere,wedrawahorizontallinetorepresentthebaseof
thehistogram(lines2428),andweplaceminimumandmaximumlabelsontheyaxis(lines3033).
Theactualdrawingofeachlinetakesplaceinlines3942.Foreachkeyinitemlist,wecreatealabelinline
37andthendrawaverticallinefromthebaseofthehistogramtotheheightthatrepresentsthecountforthat
key.Wearecarefultolifttheturtlestailaswemovetheturtlearoundthehistogramsoastonotcreateany
additionallines.

Exercises

4.36Modifythefrequencychartfunctiontodrawwidebarsinsteadoflines.

4.37 Modify the frequency chart function so that the range of the x axis is not tightly bound to the the
numberofdataitemsinthelistbutratherusessomeminimumandmaximumvalues.

4.38 Another way to compute the frequency table is to obtain a list of keyvalue pairs using the items
method. This list of tuples can be sorted and printed without returning to the original dictionary.
Rewritethefrequencytablefunctionusingthisidea.

4.39Agoodprogrammingpracticeistotakeastepbackandlookatyourcodeandtofactorouttheparts
that you have reused into separate functions. This is the case with our frequencyTable function.
ModifyfrequencyTablesothatitreturnsthedictionaryoffrequencycountsitcreates.

4.40 Now write a separate function to print the frequency table using the dictionary created by the
frequencyTablefunctioninthepreviousproblem.

4.41ModifyfrequencyChartfromListing4.10tousethenewfrequencyTablefunction.

4.7Dispersion:StandardDeviation
Wenowturntostandarddeviationanalternativestatisticalmeasureofdispersionthatshowshowmuch
theindividualdataitemsofacollectiondifferfromthemean.Thelargerthespreadofthedataaroundthe
mean,thelargerthestandarddeviation.Datathathaveasmallstandarddeviationareclusteredtightlyaround
themean.Datathathavealargestandarddeviationaremoredispersed.
In mathematics, the equation for computing the standard deviation, usually denoted by s, is given by the
followingequation:

where is the mean of the data values. Expressed in words this equation says that we should perform the
followingsteps:
1.Findthemeanofthedatavalues.
2.Foreachdatavalueinthecollection

(a)subtractthemeanfromthedatavalue.
https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 24/28
8/23/2016 Yuzu: Python Programming in Context

(b)squarethisdifference.
(c)addtheresulttoarunningsum.
3.Dividethesumbyn1wherenisthenumberofdatavalues.
4.ThesquarerootofthequotientfromStep3isthestandarddeviation.
Toimplementafunctiontocomputethestandarddeviation,wewillagainexpectalistofvalues.Sincewe
needtousethesquarerootfunction,themathlibrarywillneedtobeimported.Finally,Step1canbeeasily
accomplishedbycallinguponthemeanfunctionweconstructedearlier.
Nowwecaniteratethrougheachvalueinthedatalistandperformthesubtractionandsquareoperations.The
accumulatorpatterncanbeusedtokeeptrackoftherunningsumofthesesquares(sometimescalledthesum
ofsquares).Listing4.11showsthecompletefunctionthatreturnsthecomputedsquareroot.

Listing4.11Computingthestandarddeviation

Session4.17providestwoexamplesoffindingthestandarddeviation.Notethatforbothdataliststhemeanis
the samenamely, 12.0. However, the standard deviations are different since the values in the first list are
much closer to the mean than those of the second. The final example shows the standard deviation for the
earthquakecountdata.

>>>datalist=[7,11,9,18,15,12]
>>>mean(datalist)
12.0
>>>standardDev(datalist)
4.0
>>>datalist=[2,10,6,24,18,12]
>>>mean(datalist)
12.0
>>>standardDev(datalist)
8.0
>>>standardDev([20,32,21,26,33,22,18])
5.9401779675119402
>>>

Session4.17UsingthestandardDevfunction

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 25/28
8/23/2016 Yuzu: Python Programming in Context

Exercises

4.42Modifythefrequencychartfunctiontoincludeagraphicalrepresentationofthemean.

4.43Modifythefrequencychartfunctiontodrawalinerepresentingplusorminus1standarddeviation
fromthemean.

4.44 Using random.uniform, generate a list of 1,000 numbers in the range 050. Graph the frequency
distributionalongwiththemeanandstandarddeviation.

4.45 Using random.gauss, generate a list of 1,000 numbers in the range 050. Graph the frequency
distributionalongwiththemeanandstandarddeviation.

4.8Summary
ThischapterintroducedthreetypesofPythoncollections:lists,tuples,anddictionaries.Togetherwithstrings,
thesefourdatatypesprovideuswithapowerfulsetoftoolsforsolvingproblems.Toinvestigatetheuseof
thesecollections,wedevelopedasetofalgorithmsforperformingbasicstatisticalanalysis.Inparticular,we
implementedfunctionstocomputethefollowing:

Dispersion(simplerangeandstandarddeviation)
Centraltendency(mean,median,andmode)
Frequencydistribution

WealsousedtheTurtleclasstocreateagraphicalrepresentationofafrequencyhistogram.

KeyTerms
average
barchart
centraltendency
concatenation
data
dictionary
dispersion
emptylist
frequencydistribution
histogram
indexing
key
length
list
mean
median
mode
mutable
range
referencediagram
repetition
https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 26/28
8/23/2016 Yuzu: Python Programming in Context

sequentialcollection
slicing
sorting
standarddeviation
statistics
tuple

PythonKeywords
append
del
dict
float
for
get
insert
items
keys
len
list
max
min
None
pop
range
reverse
sort
sum
values

Bibliography
[qua]U.S.GeologicalSurvey.2008.Retrievedfromhttp://earthquake.usgs.gov/.
[Wei02]EricW.Weisstein,CRCConciseEncyclopediaofMathematics.ChapmanHall/CRC,2002.

ProgrammingExercises
4.1 matplotlib is a powerful graphing package for Python that can draw histograms, bar charts, scatter
plots, and many other kinds of graphs. It is particularly useful if you are taking any science course
whereyoumayneedtographandanalyzedata.Youcanreadaboutanddownloadmatplotlibforyour
computer at: http://matplotlib.sourceforge.net/. Read the documentation to learn how to use this
moduletoplotahistogramofthedatashowninthischapter.

4.2Givenalistofpointsoftheform[(x1,y1),(x2,y2),,(xn,yn)],writeafunctioncalledplotRegression
thatacceptsalistasaparameterandusesaturtletoplotthosepointsandabestfitlineaccordingto
thefollowingformulas:

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 27/28
8/23/2016 Yuzu: Python Programming in Context

Where isthemeanofthexvalues, isthemeanoftheyvalues,andnisthenumberofpoints.The


Greek letter represents the sum operation. For example, xi indicates the addition of all the x
values.
Yourprogramshouldanalyzethepointsandcorrectlyscalethewindowusingsetworldcoordinatesso
thateachpointcanbeplotted.Youshouldthendrawthebestfitlineinadifferentcolorthroughthe
points.

https://reader.yuzu.com/#/books/9781284089042/c/6/22!/4/622/2@0:0 28/28
8/23/2016 Hello Everyone

CHAPTER5
Earthquakes,Floods,andOtherNaturalDisasters

5.1Objectives
Tousetextfilestostorelargedatasets
Toaccessonlinedatasources
Tointroducethewhileloop
TointroduceBooleanexpressions

5.2UsingFilesforLargeDataSets
AlltheexamplesinChapter4usedsmalldatasetssothatwecouldconcentrateondevelopingthestatistical
methodsneededtoanalyzethedata.Weassumedthatourdataitemswerestoredinlists,andweconstructed
our function to process those lists accordingly. Now that we have those functions in place we can turn our
attentiontousingstatisticaltoolstodescribelargerdatasets.
WehavealreadyseenthatPythonprovidesuswithanumberofpowerfulcollectionstostoreandmanipulate
data.However,astheamountofdatagetslarger,itbecomesmoreandmoredifficulttofillthesecollections
forlaterprocessing.Itwouldcertainlybepossibletohavetheuserenterthedatainteractively,butthiswould
requireasubstantialamountofeffort.Instead,largedatasetsareusuallyfoundindatafilesthatareprepared
aheadoftime.Wecanthenreadsuchdatafromthefileandfillourcollectionsforlaterprocessing.
Forourpurposeshere,wewillassumethatourdatafilesaretextfilesthat is,files filledwith characters.
ThePythonprogramsthatwewritearestoredastextfiles.Wecancreatethesefilesinanynumberofways.
For example, we can use a text editor to type in and save the data. We can also download the data from a
websiteandthensaveitinafile.Regardlessofhowthefileiscreated,Pythonwillallowustomanipulatethe
contents.

Method Use Explanation


Name
open open(filename,'r') Open a file called filename and use it for reading. This will return a
referencetoafileobject.
open open(filename,'w') Open a file called filename and use it for writing. This will also return a
referencetoafileobject.
close filevariable.close() Fileuseiscomplete.

Table5.1OpeningandclosingfilesinPython
https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 1/23
8/23/2016 Hello Everyone

InPython,wemustopenfilesbeforewecanusethemandclosethemwhenwearedonewiththem.Asyou
mightexpect,onceafilehasbeenopeneditbecomesaPythonobjectjustlikeallotherdata.Table5.1shows
thefunctionsthatcanbeusedtoopenandclosefiles.
As an example, suppose we have a text file called rainfall.txt that contains the data below representing the
totalannualrainfall(ininches)for25townsinIowa.Thefirstitemoneachlineistheraingaugelocation,
usuallyatownname,andthesecondistherainfallamount.

Akron25.81
Albia37.65
Algona30.69
Allison33.64
Alton27.43
AmesW34.07
AmesSE33.95
Anamosa35.33
Ankeny33.38
Atlantic34.77
Audubon33.41
Beaconsfield35.27
Bedford36.35
BellePlaine35.81
Bellevue34.35
Blockton36.28
Bloomfield38.02
Boone36.30
Brighton33.59
Britt31.54
Buckeye33.66
BurlingtonKBUR37.94
Burlington36.94
Carroll33.33
Cascade33.48

Althoughitwouldbepossibletoconsiderenteringthisdatabyhandeachtimeitisused,youcanimaginethat
suchataskwouldbetimeconsuminganderrorprone.Inaddition,itislikelythattherecouldbedatafrom
manymoretowns.
To open this file, we call the open function. The variable fileref now holds a reference to the file object
returnedbyopen.Whenwearefinishedwiththefile,wecancloseitbyusingtheclosemethod.Afterthefile
isclosed,anyfurtherattemptstousefilerefwillresultinanerror.

>>>fileref=open("rainfall.txt","r")
>>>
>>>fileref.close()
>>>

5.2.1IteratingoverLinesinaFile
Wenowusethisfileasinputinaprogramthatwilldosomedataprocessing.Intheprogram,wewillread
eachlineofthefileandprintitwithsomeadditionaltext.Becausetextfilesaresequencesoflinesoftext,we
canusetheforlooptoiteratethrougheachlineofthefile.

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 2/23
8/23/2016 Hello Everyone

A line of a file is defined to be a sequence of characters up to and including a special character called the
newline character. If you evaluate a string that contains a newline character you will see the character
representedas\n.Ifyouprintastringthatcontainsanewlinecharacter,youwillnotseethe\nyouwillsee
only its effects. When you are typing a Python program and you press the enter or return key on your
keyboard,theeditorinsertsanewlinecharacterintoyourtextatthatpoint.
Astheforloopiteratesthrougheachlineofthefile,theloopvariablewillcontainthecurrentlineofthefile
asastringofcharacters.Thegeneralpatternforprocessingeachlineofatextfileisasfollows:

forlineinmyFile:
statement1
statement2

Toprocessallofourrainfalldata,weuseaforlooptoiterateoverthelinesofthefile.Usingthesplitmethod,
we can break each line into a list containing the city code and the rainfall amount. We can then take these
values and construct a simple sentence, as shown in Listing 5.1. The output from the program is shown in
Session5.1.

Listing5.1SimplePythonprogramtoreadrainfalldatafromafile

Akronhad25.81inchesofrain.
Albiahad37.65inchesofrain.
Algonahad30.69inchesofrain.
Allisonhad33.64inchesofrain.
Altonhad27.43inchesofrain.
AmesWhad34.07inchesofrain.
AmesSEhad33.95inchesofrain.
Anamosahad35.33inchesofrain.
Ankenyhad33.38inchesofrain.
Atlantichad34.77inchesofrain.
Audubonhad33.41inchesofrain.
Beaconsfieldhad35.27inchesofrain.
Bedfordhad36.35inchesofrain.
BellePlainehad35.81inchesofrain.
Bellevuehad34.35inchesofrain.
Blocktonhad36.28inchesofrain.
Bloomfieldhad38.02inchesofrain.
Boonehad36.30inchesofrain.
Brightonhad33.59inchesofrain.
Britthad31.54inchesofrain.
Buckeyehad33.66inchesofrain.
BurlingtonKBURhad37.94inchesofrain.
Burlingtonhad36.94inchesofrain.
Carrollhad33.33inchesofrain.
https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 3/23
8/23/2016 Hello Everyone

Cascadehad33.48inchesofrain.

Session5.1Outputfromthefilereadingprogram

5.2.2WritingaFile
Letsthinkaboutanotherexampleoffileprocessing:convertingourfilefromdataaboutrainfallininchesto
data about rainfall in centimeters. This will require that we read the file contents as before, but, instead of
printingamessage,wewilldosomecomputationandthenwritetheresultsbacktoanotherfile.Thenewfile
willneedtobeopenedforwriting.
Listing5.2showsthePythonprogram.ThenewfilewillbecalledrainfallInCM.txt.Aforloopwillbeusedto
iteratethroughtheinputfile.Thistime,eachlineofthefilewillbesplitandtherainfallvalueinincheswill
beconvertedtotheequivalentvalueincentimeters(lines78).
Thewritestatementonline10doesalloftheworktocreateanewlineintheoutputfile.Notethatitcanadd
onlyasinglestringtothefileeachtimeitisused.Forthisreason,weneedtousestringconcatenationtobuild
upthelinepiecebypiece.Thefirstpiece,values[0],isthecitycode.Wethenaddablanktoseparatethecode
fromtherainfallamount.Next,thefloatingpointvaluecalledcmhastobeconvertedtoastringusingthestr
method.Finally,theentirelineiscompletedbyaddinganewlinecharacter.Session5.2showsthecontentsof
thenewlycreatedfile.

Listing5.2Creatingafilewithnewdata

$catrainfallInCM.txt
Akron65.5574
Albia95.631
Algona77.9526
Allison85.4456
Alton69.6722
AmesW86.5378
AmesSE86.233
Anamosa89.73819999999999
Ankeny84.7852
Atlantic88.31580000000001
Audubon84.86139999999999
Beaconsfield89.5858

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 4/23
8/23/2016 Hello Everyone

Bedford92.32900000000001
BellePlaine90.9574
Bellevue87.24900000000001
Blockton92.1512
Bloomfield96.5708
Boone92.202
Brighton85.3186
Britt80.1116
Buckeye85.4964
BurlingtonKBUR96.3676
Burlington93.82759999999999
Carroll84.6582
Cascade85.0392

Session5.2Showingthecontentsofanewtextfile

5.2.3StringFormatting
Asyoucanseeonline10ofListing5.2,convertingvaluestostringsandconcatenatingthesestringstogether
can be a tedious process. Fortunately, Python provides us with a better alternative: formatted stringsa
template in which words or spaces that will remain constant are combined, with room for variables to be
insertedintothestring.Forexample,thestatement

print(values[0],"had",values[1],"inchesofrain.")

containsthewordshadandinchesofraineverytime,butthecitynameandtheamountisdifferent.
Usingaformattedstring,wewritethepreviousstatementas

print("%shad%dinchesofrain"%(values[0],values[1]))

Thissimpleexampleillustratesanewstringexpression.The%operatorisastringoperatorcalledtheformat
operator. The left side of the expression holds the template or format string, and the right side holds a
collection of values that will be substituted into the format string. Note that the number of values in the
collectionontherightsidecorrespondswiththenumberof%charactersintheformatstring.Valuesaretaken
inorderfromthecollectionandinsertedintotheformatstring.
Lets look at both sides of this formatting expression in more detail. The format string may contain one or
moreconversionspecifications.Aconversioncharactertellstheformatoperatorwhattypeofvalueisgoing
tobeinsertedintothatpositioninthestring.Intheexampleabove,the%sspecifiesastring,whilethe%d
specifiesaninteger.Otherpossibletypespecificationsincludei,u,f,e,g,c,or%.Table5.2summarizesallof
thevarioustypespecifications.
In addition to the format character, you can also include a format modifier between the % and the format
character.Formatmodifiersmaybeusedtoleftjustify,orrightjustifiythevaluewithaspecifiedfieldwidth.
Modifiers can be used to specify the field width and a number of digits after the decimal point. Table 5.3
explainstheseadditionaloptions.
Therightsideoftheformatoperatorisacollectionofvaluesthatwillbeinsertedintotheformatstring.The
collectionwillbeeitheratupleoradictionary.Ifthecollectionisatuple,thevaluesareinsertedinorderof
position.Thatis,thefirstelementinthetuplecorrespondstothefirstformatcharacterintheformatstring.If
thecollectionisadictionary,thevaluesareinsertedaccordingtotheirkeys.Inthiscaseallformatcharacters
mustusethe(name)modifiertospecifythenameofthekey.

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 5/23
8/23/2016 Hello Everyone

Character OutputFormat
d,i Integerorlonginteger
u Unsignedinteger
f Floatingpointasm.ddddd
e Floatingpointasm.ddddde+/xx
E Floatingpointasm.dddddE+/xx
g Use%eforexponentslessthan4orgreaterthan+5,otherwiseuse%f
c Singlecharacter
s String,oranyPythondataobjectthatcanbeconvertedto
astringbyusingthestrfunction.
% Insertaliteral%character

Table5.2StringFormattingConversionCharacters

Modifier Example Description


number %20d Putthevalueinafieldwidthof20
%20d Putthevalueinafield20characterswide,leftjustified
+ %+20d Putthevalueinafield20characterswide,rightjustified
0 %020d Putthevalueinafield20characterswide,fillinwithleadingzeros.
%20.2f Putthevalueinafield20characterswidewith2characterstotherightofthedecimal
point.
(name) % Getthevaluefromthesupplieddictionaryusingnameasthekey.
(name)d

Table5.3Additionalformattingoptions

Session5.3showssomeexamplesofformattedstringsinuse.

>>>a=10
>>>b='apple'
>>>print("The%scosts%dcents"%(b,a))
Theapplecosts10cents
>>>myStr="The%+15scosts%4.1dcents"%(b,a)
>>>myStr
'Theapplecosts10cents'
>>>myStr="The%+15scosts%6.1fcents"%(b,a)
>>>myStr
'Theapplecosts10.0cents'
>>>myDict={'name':'apple','cost':10,'price':15}
>>>print("The%(name)scosts%(price)5.1fcents"%myDict)
Theapplecosts15.0cents

Session5.3Demonstratingstringformatting

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 6/23
8/23/2016 Hello Everyone

Exercises

5.1Writeaprogramtoreadtherainfall.txtfileandthenwriteoutanewfilecalledrainfallfmt.txt.Thenew
fileshouldformateachlinesothatthecityisrightjustifiedinafieldthatis25characterswide,and
therainfalldatashouldbeprintedinafieldthatis5characterswidewith1digittotherightofthe
decimalpoint.

5.2 Write a function that writes a temperature conversion table called tempconv.txt. The table should
includetemperaturesfrom300to212degreesFahrenheitandtheirCelsiusequivalents,presentedin
two columns with appropriate headings. Each column should be 10 characters wide, and each
temperatureshouldhave3digitstotherightofthedecimalpoint.

5.2.4AlternativeFileReadingMethods
In addition to the for loop, Python provides three methods to read data from the input file. The readline
methodreadsonelinefromthefileandreturnsitasastring.Thestringreturnedbyreadlinewillcontainthe
newlinecharacterattheend.Thismethodreturnstheemptystringwhenitreachestheendofthefile.The
readlinesmethodreturnsthecontentsoftheentirefileasalistofstrings,whereeachiteminthelistrepresents
one line of the file. It is also possible to read the entire file into a single string with read. Table 5.4
summarizesthesemethodsandSession5.4showstheminaction.
Note that we need to reopen the file before each read so that we start from the beginning. Each file has a
markerthatdenotesthecurrentreadpositioninthefile.Anytimeoneofthereadmethodsiscalled,themarker
ismovedtothecharacterimmediatelyfollowingthelastcharacterreturned.Inthecaseofreadline,thismoves
the marker to the first character of the next line in the file. In the case of read or readlines, the marker is
movedtotheendofthefile.

Method Use Explanation


Name
write filevar.write(astring) Addsastringtotheendofthefile.filevarmustrefertoafilethathasbeen
openedforwriting.
read(n) filevar.read() Reads and returns a string of n characters, or the entire file as a single
stringifnisnotprovided.
readline(n) filevar.readline() Returns the next line of the file with all text up to and including the
newlinecharacter.Ifnisprovidedasaparameter,thenonlyncharacters
willbereturnedifthelineislongerthann.
readlines(n) filevar.readlines() Returnsalistofnstrings,eachrepresentingasinglelineofthefile.Ifnis
notprovided,thenalllinesofthefilearereturned.

Table5.4ReadingandwritingmethodsforfilesinPython

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 7/23
8/23/2016 Hello Everyone

Session5.4Usingotherfileprocessingmethods

Exercises

5.3OpenafileduringaPythonsession.Callthereadlinemethodtwiceonthatfile,thencallthereadlines
method.Whatlinesdoesthelistreturnedbyreadlinesinclude?

5.4OpenthefileinExercise5.3again,butcallreadlinesimmediately.Comparethelinesreturnedinthis
exercisewiththepreviousone.

5.5Writeaprogramthatreadsinthecontentsofafileandwritesanewfilewhereallthecharactersarein
uppercase.

5.6Writeaprogramthatreadsinafileandthenprintsoutthenumberoflines,words,andcharactersin
thefile.

5.7Writeaprogramthatcreatesafilewithaconcordanceanindexthattellsyouwhichlineofthefile
eachwordappearson.Ifawordisonmorethanoneline,theconcordancewillshowyouallofthe
linescontainingthatword.Hint:Useadictionarykeyedbyeachwordtosolvethisproblem.

5.2.5StatisticswithRealData
WecompletethissectionbyusingourstatisticalfunctionsfromANestofSnakes(Chapter4) on a large
dataset.Whenworkingwithrealdata,itisoftennecessarytoperformsomebasicdataprocessingtogetthe
data into a format that works for our purposes. We will use a large data file that contains detailed data
https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 8/23
8/23/2016 Hello Everyone

pertainingtoearthquakeoccurrencesaroundtheworld.Thisfilewasalsothesourceofthesimplecountdata
usedinANestofSnakes(Chapter4).

Earthquakes

The U.S. Geological Survey (USGS) maintains a log of all significant earthquakes that occur around the
world. This log covers a sevenday period and includes data pertaining to the magnitude, date and time,
location,anddepthofthequake.Initsrawform,aportionofthelog(atextfile)looksasfollows:

2.82006/10/1902:02:1062.391149.75115.0CENTRALALASKA
2.52006/10/1900:31:1520.119156.2131.5MAUIREGION,HAWAII
5.02006/10/1821:15:514.82382.59237.3SOUTHOFPANAMA
2.62006/10/1821:12:2559.934147.90430.0GULFOFALASKA
3.42006/10/1820:59:2136.54089.6407.7SOUTHEASTERNMISSOURI
2.72006/10/1820:11:2261.023151.41860.0SOUTHERNALASKA
3.12006/10/1816:40:1520.282156.6114.7MAUIREGION,HAWAII
2.72006/10/1814:12:1959.808152.53850.0SOUTHERNALASKA
2.82006/10/1814:02:1260.686151.87190.0KENAIPENINSULA,ALASKA
4.92006/10/1812:10:011.758127.488127.0HALMAHERA,INDONESIA
6.22006/10/1810:45:3615.081167.243138.5VANUATU
2.82006/10/1810:45:1732.162115.8956.3BAJACALIFORNIA,MEXICO
3.32006/10/1810:08:4532.165115.8917.3BAJACALIFORNIA,MEXICO
2.82006/10/1808:22:2732.263115.2973.4BAJACALIFORNIA,MEXICO
3.72006/10/1805:34:1562.326151.22485.9CENTRALALASKA
4.62006/10/1803:25:0321.53866.593201.7POTOSI,BOLIVIA
3.72006/10/1802:32:2657.560137.1861.0OFFTHECOASTOFSOUTHEASTERNALASKA
4.92006/10/1802:01:271.35597.15725.8NIASREGION,INDONESIA
2.52006/10/1800:18:4219.801155.39110.6ISLANDOFHAWAII,HAWAII
3.12006/10/1722:59:0161.444150.52360.0SOUTHERNALASKA

Notethatthecolumnsofthedatafileareseparatedbyspaces.Thelastcolumnisactuallyasequenceof
wordsdenotingalocation.

EarthquakeStatistics

Forthisexample,wewillfocusonthefirstcolumnofearthquakedata,whichrepresentstheseverityofeach
earthquake.Seismologistsmeasureseverityinunitsofmagnitude.Thelargerthemagnitude,thegreaterthe
severityoftheearthquake.
Ourfirsttaskinanalyzingthisrealdatasourceistoprocessthedatafilebyextractingthepertinentdatafrom
the file and storing it in an appropriate collection. For our purposes here, we need to process the file and
createalistofmagnitudessothatourstatisticsfunctionscandotheirwork.
Assumeourdatafileiscalledearthquakes.txt.Wecanuseacombinationoffileprocessing,stringprocessing,
andlistmanipulationtoconstructourlist.Wewriteasmallfunction(Listing5.3)thatwillreturnthelistof
magnitudes.Line2opensthefilesothatwecanreadthedatalinebylineusingaforloop(line5).Notethat
line7convertsthemagnitudestringtoafloat,whichmeansthatthiswillbealistoffloatingpointnumbers
https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 9/23
8/23/2016 Hello Everyone

representingthemagnitudesofearthquakesthathaveoccurred.

Listing5.3Processingtheearthquakedatafile

Wecannowuseourbasicstatisticsfunctionstolearnabouttheearthquakesthathaveoccurredduringthis
sevenday period of time. Session 5.5 first calls the makeMagnitudeList function and then uses statistical
functionstocomputevaluesfortherange,mean,median,mode,andstandarddeviation.Wecanseethatthe
rangeofearthquakemagnitudesis4.2units,fromamaximumof6.7toaminimumof2.5.Thethreemeasures
of central tendency seem to show some inconsistency. The mean of 3.76 and the median of 3.5 are fairly
similar.However,themodeof2.5isquitesmall.

>>>magList=makeMagnitudeList()
>>>getRange(magList)
4.2
>>>max(magList)
6.7
>>>min(magList)
2.5
>>>mean(magList)
3.7671641791044794
>>>median(magList)
3.5
>>>mode(magList)
[2.5]
>>>standardDev(magList)
1.0650429183420023

Session5.5Statisticsfromtheearthquakedata

The reason for this difference between mean and mode becomes apparent when we look at our frequency
table(Session5.6).Althoughmostoftheearthquakesreportedduringthepastweekwererelativelysmallin
magnitudeaswewouldexpectithappensthatthereweresomeverystrongoneswithmagnitudesof6and
higher.Forthisperiodoftimethemeanisthusshiftedupbytheseinfrequentbutstrongevents.

>>>frequencyTable(magList)

ITEM FREQUENCY
2.5 18
2.6 15
2.7 14
2.8 12
https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 10/23
8/23/2016 Hello Everyone

2.9 11
3.0 4
3.1 10
3.2 6
3.3 7
3.4 3
3.5 3
3.6 1
3.7 7
3.9 1
4.0 3
4.1 3
4.2 5
4.3 6
4.4 6
4.5 6
4.6 8
4.7 6
4.8 9
4.9 9
5.0 4
5.1 6
5.2 3
5.3 4
5.4 1
5.5 1
5.7 2
5.8 1
6.0 1
6.2 1
6.3 1
6.4 1
6.6 1
6.7 1
>>>

Session5.6Frequencytableforearthquakemagnitudedata

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 11/23
8/23/2016 Hello Everyone

Figure5.1Frequencyhistogramofearthquakemagnitudes

Finally, we can use our histogram generator from A Nest of Snakes (Chapter 4) to construct a visual
representationofthemagnitudefrequency.Whenwelookatthedistributionofearthquakemagnitudes,itis
visually apparent that the majority of them were minor. Although the frequency table provides the same
information,itisoftenpreferabletoseetherelationships(Figure5.1).

Exercises
5.8Frequencytablesareoftencreatedbyplacingdataitemsinarange.Implementafunctionthatwill
group the earthquake data by the following criteria: Micro (03), Minor (33.9), Light (44.9),
Moderate(55.9),Major(66.9),Strong(77.9),andGreat(>=8)

5.9Usingthecriteriafromthepreviousexercise,usethefrequencychartfunctionfromChapter4todraw
avisualrepresentationofthedata.

5.10Writeafunctiontoprocesstheearthquakedatafileandcreatelistsofearthquakemagnitudes,onefor
eachdate.Yourfunctionshouldreturnalistofliststhatlookssomethinglikethis:

[[date1,magnitude1,magnitude2,magnitude3,],
[date2,magnitude1,magnitude2],
]

5.11Modifythefunctioninthepreviousexercisetoreturnadictionaryofkeyvaluepairswherethekeyis
thedateandthevalueisalistofmagnitudesfortheearthquakesthatoccurredonthatdate.

5.3ReadingDatafromtheInternet
Althoughreadingdatafromfilesisuseful,intodaysworldaccessingdataontheInternetisbecomingeven
https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 12/23
8/23/2016 Hello Everyone

moremeaningful.Fortunately,Pythonmakesiteasytogetonlinedatausingtheurllib.requestmodule.The
nameofthemodulecomesfromthefactthatthewebaddressofyourfavoritewebsiteisalsoknownasa
UniformResourceLocatororURL.
Usingtheurllib.requestmodule,wecanreaddatafromwebpagesjustaseasilyaswecanreaddatafroma
file.Infact,iterationandallofthemethodsdescribedinTableB.7inPythonQuickReference(Appendix
B)workonwebpagesaswellasfiles.Wecouldalsouseaforlooptoiteratethrougheachlineofawebpage,
as we did in Section 5.2. However, in this section we explore the use of the read, readline, and readlines
methods. Session 5.7 illustrates the use of the read method, which reads an entire web page into a string
variablecalledpageText.
Rather thanusing the openfunctionaswe did for files, we can create a URL object for reading bycalling
urllib.request.urlopen.Theurlopenfunctionhidesallofthenetworkingdetailsneededtocommunicatewith
thewebsiteandreturnsanobjectthatactsjustlikeafile.

>>>importurllib.request

>>>page=urllib.request.urlopen("http://knuth.luther.edu/~david/helloworld.html")
>>>pageText=page.read()
>>>pageText
b'\n
\n\n\n\n

HelloPython
\n\n\n'
>>>decodedPageText=pageText.decode("utf8")
>>>decodedPageText
'\n
\n\n\n\n

HelloPython
\n\n\n'
>>>print(decodedPageText)

HelloPython
>>>page=urllib.request.urlopen("http://knuth.luther.edu/~david/helloworld.html")
>>>pageText=page.readline()
>>>pageText
b'\n'
>>>
>>>page=urllib.request.urlopen("http://knuth.luther.edu/~david/helloworld.html")
>>>pageText=page.readlines()
>>>pageText
[b'\n',b'
\n',b'\n',
b'\n',b'
\n',b'
https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 13/23
8/23/2016 Hello Everyone

HelloPython
\n',b'\n',
b'\n']
>>>

Session5.7Capturingthecontentsofawebpagewithurllib.request

NoticethatwhenweevaluatepageText,wegetonelongstringthatisnotformattedverywell.However,if
youlookcloser,youwillseethatthisisnotreallyastringatall.ItisactuallywhatPythoncallsabytearray.
Bytearrayslooklikestringsbutcloserinspectionrevealsthatthereisasmallbprecedingthequotedelimited
contents. Unlike text files, which are simple collections of characters, web pages can be encoded in many
differentways.Tobesurethattheyarereadproperly,Pythonreturnsthecontentsasencodedbytesofdata.
Ifweknowhowthedatahasbeenencoded,thenitiseasytodecodeit(turnitbackintoastring)byusingthe
decodemethod.Notethattheline

>>>decodedPageText=pageText.decode("utf8")

assumesthatthetextisencodedusingutf8.ThevariabledecodedPageTextisnowastandardstringthatcan
bemanipulatedusinganystringmethodsandfunctionsdescribedearlier.
WhenweevaluatedecodedPageText,wegetonelongstringthatisstillnotformattedverywell.However,it
doescontainnewlinecharacters(\n).AswasmentionedinSection5.2,whenevaluatingastringwecansee
the\ncharactersembeddedinthesequenceofcharacters,butwhenprintingthestring,itappearsasitwouldif
wewereeditingthefile.
Thereadlinemethodmakesuseofthenewlinecharactertoreturnonlyonelineofthewebpageatatime.
Notethatitactuallyreturnsabytearrayasbefore.Similarly,thereadlinesmethodreturnsalistofbytearrays,
oneforeachlineofthewebpage.

5.3.1UsingawhileLooptoProcessData
Oftenyouwillwanttoprocessdatafromawebsiteorafilebutyoumaynotknowhowmuchdatayouwant
to read at the start. One approach you can take is to read a small amount of data and ask a question. The
answertothisquestionmaydeterminehowmuchmoredatatoreadorwhethertoreadanymoredataatall.
Thisapproachrequiresamoregeneralwayofiteratingoverthecharactersorlinesinafile.
Pythonprovidesuswithageneralloopmechanismcalledawhileloop.Itsstructureisasfollows:

while
:statement1
statement2

Asyoumayguessfromthetemplateshownabove,thewhileloopcontinuestoperformthestatementsinthe
bodyoftheloopaslongastheconditionremainstrue.Oncetheconditionevaluatestofalsethewhileloop
stopsperformingthestatementsinthebody.AconditioncanbeanyPythonBooleanexpression.Recallthat
BooleanexpressionsevaluatetoTrueorFalse.Althoughweintroducethewhileloopinthissection,youwill
findmoredetailsandexamplesofitsuseinSection7.4.4.
Supposethatwewanttocountthenumberoflinesintheheadingofawebpageandthenprintthosepartsof
thewebpagethatareinthebodysection.Theheadingisthepartofthewebpagethatstartswiththestring

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 14/23
8/23/2016 Hello Everyone

andendswiththestring.Thebodyisthepartthatbeginswiththestringandendswiththestring.Those
stringsthatbeginwith<andendwith>arecalledhypertextmarkuplanguage(HTML)tags.
Beforewecanstartcountingthelinesintheheadingofawebpage,wemustreadallofthelinesbeforethe
linecontainingthe
tag.InListing5.4wedothisonlines79.Noticethatbeforeweenterthewhileloopwedoaninitialreadline
calledaprimingread,aprocessthatallowstheconditionofthewhilelooptobeevaluatedforthefirsttime.
Withoutthiscalltoreadline,linewouldhavenovalue.
Nowthatwehavereadthelinethatcontainsthe
tag,wecanstartcountinglines.Sincethenumberoflinesintheheadingofawebpageisdifferentforeach
page,wecanuseawhilelooptoreadlinesuntilweencounterthetag.Aswereadeachline,wecansimply
incrementacountervariablenumHeadLines(seelines1114inListing5.4).
Itisimportanttorememberthatthelastthingyouwilldointhebodyofthewhileloopistoreadthenextline.
Ifyoudonotreadthenextline,youwillcontinuetoprocessthesamelineoverandoveragaininaninfinite
loop.Infiniteloopsarealmostnevergood,anditisyourresponsibilitytomakesurethatsomethingchanges
inthebodyofyourwhileloopsothattheconditionwilleventuallyfail.
Afterwehavereadthelinecontainingthe,wecontinuetoreadlinesuntilwefindthe
tag(seelines1618).Wethenreadandprintthelinesofthebody(seelines2023).Noticethatwhenwe
printthelinereadbyreadlineweusethestatementprintline[:1].Recallthatthesliceoperation[:1]returns
theentiresequenceexceptforthelastitem.Sincethelastiteminthelineisanewlinecharacter,weremoveit
orelsewegettwonewlinesduetothefactthatprintautomaticallyaddsanewlineforus.
The statement on line 21 of Listing 5.4 also demonstrates compound Boolean expressions. Recall that in a
compoundBooleanexpressionwecancombinemultipleBooleanexpressionsusingthelogicaloperatorsand,
or,andnot.ThefirstpartofthecompoundBooleanexpressionline!=""checksfortheendoffilecondition.
Recall that readline returns an empty string when it encounters the end of a file. The second part of the
expressionchecksfortheendofthebodysection,denotedbythetag.Theandoperatorallowsustoexitthe
loopwheneverwereachtheendofthefileortheendofthebodysection.

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 15/23
8/23/2016 Hello Everyone

Listing5.4Countinglinesintheheadandthebody

Exercises

5.12StartupaPythonsessionanduseurllib.requesttoreadthesourceforyourfavoritewebpage.

5.13WriteafunctioncalledsavePagethattakesastringrepresentingaURL,andafilenameasaparameter
andthensavesthecontentsofthewebpagetothefile.

5.14 Using the read method, write your own readline function that takes an opened URL object as a
parameter and returns a single line as a string. Hint: You will want to make use of the optional
parameterofthereadmethod.

5.15Usingthereadlinemethod,writeyourownreadlinesfunctionthattakesanopenedURLobjectasa
parameterandreturnsalistofthelines.

5.16Writeafunctionthatopensawebpageandreturnsadictionaryofallthelinksandtheirtextonthat
page.AlinkisdefinedbyanHTMLtagthatlookslike

linktext
Thelinkiseverythinginquotesafterthehref=,andthetextiseverythingbetweenthe>andthe.

5.17WriteaPythonfunctionthatcangenerateanoutlineforawebpage.Yourfunctionshouldprintthe
titleofthewebpageandthetextbetweenany

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 16/23
8/23/2016 Hello Everyone

,
,
,or

tags.Eachlevelofheadingtagshouldbeindentedproperlysothatall

tagsareindentedundertheir

tag,etc.
5.18Useawhilelooptoimplementtheforloopforiinrange(10).

5.19Useawhilelooptoimplementtheforloopforiinrange(10,1,1).

5.3.2StockMarketData
LetsreturntoInternetdatasourcesandstatistics.Foraslongastherehasbeenastockmarket,investorshave
triedtoanalyzethepriceofstocksinmanydifferentwaysinordertopredictwhetherthemarketwillgoupor
down.Lotsofseeminglycrazyindicatorshavebeendiscoveredovertheyearsthatsupposedlypredictstock
market trends: A Democrat in the White House means bad news for stocks short skirts indicate that the
marketwillgoup,andlongskirtsindicateadownwardtrendifanNFCteamwinstheSuperBowl,itwillbe
abullmarket,butanAFCteamindicatesabearmarket[Mal03].
These indicators were not just made up. They come from years of observation. In fact, for some of these
indicatorstherereallyisamathematicalcorrelation.However,therelationshipisnotcausal,justcoincidence.
Inthissectionwelookatonealgorithmfordeterminingcorrelationandapplyittostockmarketdatathatis
readilyavailableontheInternet.

AccessingStockData

TherearemanysourcesofstockmarketdataontheInternet.Forpurposesofthissectionandtheexercises
that follow, we will use http://finance.yahoo.com. Session 5.8 shows how easy it is to get some stock
information for Target corporation. First we use urllib.request to open the data source, then we use the
readlinesmethodandthesliceoperatortogetthefirsttenlinesofdata.

>>>url1=urllib.request.urlopen('http://ichart.yahoo.com/table.csv?s=TGT')
>>>data=url1.readlines()[:10]
>>>data
[b'Date,Open,High,Low,Close,Volume,AdjClose\n',
b'20120531,58.20,58.46,57.84,57.91,6856500,57.91\n',
b'20120530,57.80,57.84,57.00,57.79,7232100,57.79\n',
b'20120529,57.90,58.17,57.39,58.13,4792600,58.13\n',
b'20120525,57.43,57.71,57.17,57.62,4293900,57.62\n',
b'20120524,56.86,57.39,56.65,57.25,4460600,57.25\n',
b'20120523,56.24,56.89,55.94,56.78,3956300,56.78\n',
b'20120522,55.89,56.90,55.81,56.67,5777400,56.67\n',
b'20120521,55.43,55.90,54.93,55.83,4779400,55.83\n',
https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 17/23
8/23/2016 Hello Everyone

b'20120518,55.07,56.00,54.91,55.46,7144300,55.46\n']
>>>

Session5.8ExampledataforTarget(TGT)

Beforewegoanyfurther,itisimportanttounderstandabitmoreaboutthestructureofaURL.Thereisalot
of information packed into a URL, and if you want to get the most out of the urllib.request module it is
important for you to understand the components of a URL. Figure5.2 illustrates the four components of a
URL:(1)protocol,(2)hostname,(3)resource,and(4)parameters.
Theprotocolhttpinthiscasetellstheprogramthatanattemptisbeingmadetoloadawebpage.Other
possiblevaluesforprotocolincludeftpforfiletransferprotocol,orfileforafileonyourharddrive.Thehost
nametellstheprogramthenameofthewebserver.Thisisprobablythepartyouaremostfamiliarwith.The
resourcemayindicateaparticularHTMLfiletoload.Itmayalsobeaprogramthatyouwantthewebserver
torun.Iftheresourceisaprogram,thewebserverwillruntheprogramandtheresultsoftheprogramwillbe
sentbacktothewebbrowser.Theparametersarethelastcomponents,similartotheparametersyoupassto
afunctioncall.Inthiscase,theparametersarepassedalongtotheprogramonthewebserver.Noticethatthe
firstparameterisseparatedfromtheresourcebya?.

Figure5.2ThecomponentsofaURL

Parameters are always specified as keyvalue pairs. The first parameter in Figure 5.2 is s=AAPL. This is
important because it is the parameter that corresponds to the stock ticker for which we want a price quote.
Anyadditionalkeyvaluepairsareseparatedbyan&.Inthiscase,thekeysd,e,andf indicate the ending
month,day,andyearforthequote.Keysa,b,andccanalsobeusedtospecifyastartingmonth,day,and
year.YoumaywanttoexperimentwiththeseparametersbysimplytypingthisURLintoyourwebbrowserto
seewhatdatayoureceive.

CorrelatingStockData

Correlationmeasuresthestrengthanddirectionoftherelationshipbetweentwovariables.Expressedanother
way, correlation measures the tendency of two variables to increase or decrease at the same time. This
measureisoftencalledthecorrelationcoefficient.Returningtotheexamplescitedearlierinthissection,we
decide that one variable may be the value of the Dow Jones Industrial Average and the other the length of
womensskirts,orthewinneroftheSuperBowl.
In addition, we will look at variables representing the stock prices for two companies. So we will ask the
followingquestion:IsthestockpriceforcompanyAcorrelatedwiththestockpriceforcompanyB?For
example,ifthepricesarecorrelatedandonecompanyisonanupwardtrend,doesthatindicatethatitisalsoa
goodtimetoinvestintheothercompany?
Although there are several algorithms for calculating a correlation coefficient, we will use the Pearson
correlationcoefficient.Theformulaforcalculatingthiscoefficientis

wherexandyarethemeansofthetwovariablesxandy,andSx,Syarethestandarddeviations.
https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 18/23
8/23/2016 Hello Everyone

Figure5.3illustratessomedifferentcorrelationalvaluesbetweentwovariables.Thevaluesfromonevariable
areusedasthexcoordinateandthevaluesfromtheothervariableareusedastheycoordinate.Eachofthe
fourexamplesshows1,000pairsofvaluesofthex,yvariables.Whenthevariablesarehighlycorrelated,they
nearly form a line. When they are not correlated, the points form a cloud or wide band. It is important to
rememberthatavalueof1.0indicatesthatthetwovariablesarepositivelycorrelated,whileavalueof1.0
indicatesthatthetwovariablesarenegativelycorrelated.Avalueof0.0indicatesthatthereisnocorrelation
betweenthetwovalues.

Figure5.3Examplesofpointsandtheircorrelation

Listing 5.5 implements the Pearson correlation formula. We use the mean and standard deviation functions
fromANestofSnakes(Chapter4).Thetwoparametersxlistandylistrepresentthevaluesthatwewantto
correlate. The two lists have the same length, and they are parallel, which means that the x value at a
particularpositioninthelistandayvalueatthesamepositionrepresentasinglex,ydatapoint.Forexample,
usingourstockdata,thevaluesataparticularindexinxlistandylistrepresentthestockpriceforaparticular
day.

Listing5.5ThePearsoncorrelationfunction

LetsusethisfunctiontocomputethecorrelationbetweenthestockpricesforAppleComputer(AAPL)and
forCocaCola(Coke).Todothis,weneedtoaccessthestockdataforeachcorporationandextracttheclosing
priceforeachday.Usingurllib.request,wecanopenandreadtheresultsintoonebiglistusingthereadlines
method.Atthispointthedataisalistofstrings.Inordertoprocessthelist,weneedtospliteachstringinto

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 19/23
8/23/2016 Hello Everyone

thecomponentsDate,Open,High,Low,Close,Volume,andAdjClose.Sinceeachofthesecomponentsis
separatedbyacomma,wecanusethesplitmethodtocreatealistusingthe','astheparameterforthesplit
character.
Whenworkingwithrealdata,itisagoodideatomakeuseofPythonsinteractivecapabilitiestolearnabit
moreaboutyourdata.Youcanalsopracticeyourlistmanipulationskillsatthesametime.Session5.9shows
howtoreadthestockdataforAAPL,andhowwecanusethesplitmethodtoturnalistofstringsintoalistof
lists.Noticethatafterwehavesplitthelistintoitscomponentparts,theindividualitemsinthelistarestill
strings.Inordertousethedatainourcorrelationfunction,wewillneedtoconvertthestringsintofloating
pointnumbers.

>>>url1=urllib.request.urlopen('http://ichart.yahoo.com/table.csv?s=AAPL')
>>>t1Data=url1.readlines()
>>>t1Data[:10]
[b'Date,Open,High,Low,Close,Volume,AdjClose\n',
b'20120531,580.74,581.50,571.46,577.73,17559800,577.73\n',
b'20120530,569.20,579.99,566.56,579.17,18891600,579.17\n',
b'20120529,570.90,574.00,565.31,572.27,13576100,572.27\n',
b'20120525,564.59,565.85,558.47,562.29,11732400,562.29\n',
b'20120524,575.87,576.50,561.23,565.32,17722500,565.32\n',
b'20120523,557.50,572.80,553.23,570.56,20889200,570.56\n',
b'20120522,569.55,573.88,552.58,556.97,24816800,556.97\n',
b'20120521,534.50,561.54,534.05,561.28,22539500,561.28\n',
b'20120518,533.96,543.41,522.18,530.38,26153300,530.38\n']
>>>t1Data[0].decode("utf8").split(',')
['Date','Open','High','Low','Close','Volume','AdjClose\n']
>>>t1DataAlt=[line.decode("utf8").split(',')forlineint1Data[1:]]
>>>t1DataAlt[:3]
[['20120531','580.74','581.50','571.46','577.73','17559800','577.73\n'],
['20120530','569.20','579.99','566.56','579.17','18891600','579.17\n'],
['20120529','570.90','574.00','565.31','572.27','13576100','572.27\n']]
>>>

Session5.9Investigatingdatainteractively

Session5.9containsanothernewPythonprogrammingtrickforcreatinglists.Theline

t1Data=[line.decode("utf8").split(',')forlineint1Data[1:]]

uses a list comprehension to construct a list of lists. We will come back to this but first lets look at the
generalformofalistcomprehensionandsomesimplerexamplesofitsuse.
Thegeneralformofalistcomprehensionis

[forin
forin

if]

Anyoftheitemsthatcorrespondtoaloopvariableinoneoftheforloopscanbeusedintheexpression.You
needtohaveonlyoneforloopinthelistcomprehension,andtheifisoptional.
https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 20/23
8/23/2016 Hello Everyone

Listcomprehensionsallowyoutoeasilycreateonelistbasedondoingsomeprocessingorselectioncriteria
appliedtoanotherlist.Forexample,supposewewanttocreatealistofthecubesofthefirst10numbers.One
waytodothiswouldbeasfollows:

>>>cubes=[]
>>>forxinrange(1,11):
cubes.append(x*x*x)
>>>cubes
[1,8,27,64,125,216,343,512,729,1000]

Usinglistcomprehensions,wecandothisinasinglestep:

>>>cubes=[x*x*xforxinrange(1,11)]
>>>cubes
[1,8,27,64,125,216,343,512,729,1000]
>>>

Thevariablextakesonthevalues1through10asyoumightexpectfromtheforxinrange(1,11)clausein
thelistcomprehension.Eachtimethroughtheloop,theexpressionx*x*xiscomputedandaddedtothelist
thatisbeingconstructed.
Listcomprehensionscanalsobeusedwiththeifstatementtocreateashorterlistbykeepingonlythosevalues
fromalongerlistthatmeetacertaincriteria.Thisoperationisoftenreferredtoasfiltering.Forexample,
supposeyouwantedtocreatealistofalltheevencubes,startingfromthelistofcubeswecreatedpreviously:

>>>evenCubes=[xforxincubesifx%2==0]
>>>evenCubes
[8,64,216,512,1000]

Withabetterunderstandingoflistcomprehensions,wecannowreturntopreparingthedataforusewiththe
correlationalgorithm.Oncethedatahasbeensplitintothelistoflistsformat,wecanextracttheclosingprice
foreachday.Tomakesurethedataiscompatible,wewanttoextractonlytheclosingpricesforeachdaythat
both stocks were traded. For example, suppose we were comparing a relatively new Internet stock such as
Amazon(AMZN)againstanoldstocksuchasAT&T(T).WewouldhavedataonAmazongoingbackonlyto
1997whereasAT&Thasbeentradedsincetheearly1900s.
ThestockCorrelatefunctionisshowninListing5.6.MostoftheworkinstockCorrelateinvolvesprocessing
thestockdatafromtheInternettogetitintoaformwecanusewiththecorrelatefunction.Lines37readthe
data from the Yahoo website. Lines 89 move the raw data into a list of lists format. Notice that the
expressionusedinthelistcomprehensionisline[0:1].decode("utf8").split(',').Thisexpressionconstructsa
newlistforeachlineofdata.Finally,thetwolistsofclosingstockpricesareconstructedinthelooponlines
1215. Now that the two lists of closing prices have been created, all that is left is to return the result of
callingthecorrelationfunctiononthetwolists.

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 21/23
8/23/2016 Hello Everyone

Listing5.6CorrelatingdataretrievedfromYahoo

Exercises

5.20 Use the stockCorrelate function to find out if there is a correlation between your two favorite
companies.

5.21Writeafunctionthatwillgraphtheclosingpricesforastock.Yourfunctionshouldacceptabeginning
dateandanendingdatealongwiththetickersymbolforastock.

5.22 Writeafunctionthatwillgraph thehigh, low, and closing pricesfor a stock. Your function should
acceptabeginningdateandanendingdatealongwiththetickersymbolforastock.

5.23Writeafunctionthatacceptsacompanynameasaparameterandreturnsthetickersymbolforthe
company.YouwillneedtolookattheYahoowebsite.

5.24Findoutifthereisacorrelationbetweenvolumeandpriceforacompanyofyourchoice.

5.25Writeafunctionthatwilldeterminethecorrelationbetweentwostocksforagivendaterange.

5.26Writeafunctionthatwillacceptalistofstocksasaparameterandwillreturnthetwostocksfromthe
listthataremosthighlycorrelated.

5.4Summary
ThischapterusedexternaldatasourcesasinputintoPythonprograms.Thesedatasources,calledtextfiles,
canresideonalocalmachineormaybeavailableviatheInternet.Wealsoshowedhowtocreateformatted
outputbyusingformatstrings.Thisallowsforstructuredoutputaswellasformattednumericvalues,columns
ofdata,andjustification.Weusedourstatisticalfunctionstoanalyzerealearthquakedata.Finally,usingstock
marketdata,wedevelopedanotherstatisticalfunctioncalledcorrelation.

KeyTerms
FileTransferProtocol(FTP)
https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 22/23
8/23/2016 Hello Everyone

formatoperator
formattedstring
hostname
hypertextmarkuplanguage(HTML)
HypertextTransferProtocol(HTTP)
infiniteloop
listcomprehension
Pearsoncorrelationcoefficient
primingread
protocol
textfile
UniformResourceLocator(URL)

PythonKeywords
and
close
float
for
not
open
or
read
readline
readlines
urllib
urlopen
while
write

Bibliography
[Mal03]BurtonG.Malkiel.ARandomWalkDownWallStreet.W.W.Norton&Company,2003.

ProgrammingExercise

5.1 Imagine that you have removed the stock listing pages from the current issue of the Wall Street
Journal and then pinned them to a wall in your room. Now imagine that you take a step back and
throw20dartsatthewall.Youinvest$10,000inthestockofwhichevertickersymbolyourdartsland
on.Thequestionishowwealthywillyoubecome?Willyoudobetterorworsethaninvestinginthe
stocks listed on the Dow Jones Industrial Average? Will you do better or worse than professional
money managers who carefully research all of their trades? Write a program to investigate these
questions. You can find lists of stocks at the following URL:
http://www.dbc.com/cgibin/htx.exe/SYMBOLS/ASTOCK.html?SOURCE=core/dbc.

https://reader.yuzu.com/#/books/9781284089042/c/6/24!/4/498@0:16.8 23/23
8/23/2016 Yuzu: Python Programming in Context

CHAPTER6
PycturePerfectPrograms

6.1Objectives
Tounderstandpixelbasedimageprocessing
Tousenestediteration
Touseandunderstandtuples
Toimplementanumberofimageprocessingalgorithms
Tounderstandpassingfunctionsasparameters

6.2WhatIsDigitalImageProcessing?
Digitalphotographyisaverycommonwaytoproducephotographstoday.Itseemsthatalmosteveryonehasa
digitalcameraaswellassoftwarethatcanorganizeandmanipulatephotographs.Inthischapterweconsider
digitalimagesandmanyofthetechniquesthatcanbeusedtomodifythem.
Digitalimageprocessingreferstotheprocessofusingalgorithmstoeditandmanipulatedigitalimages.A
digitalimageisafinitecollectionofsmall,discretepictureelementscalledpixels.Thesepixelsareorganized
inatwodimensionalgridandrepresentthesmallestamountofpictureinformationthatisavailable.Ifyou
lookcloselyatanimage,pixelscansometimesappearassmalldots.Morepixelsinyourimagemeanmore
detailorresolution.
Digital cameras are often rated according to how much resolution they provide. Typically resolution is
expressed as a number of megapixels. One megapixel means that the picture you take is composed of 1
millionpixels.An8megapixelcameraiscapableoftakingapicturewithupto8millionpixels.

6.2.1TheRGBColorModel
Eachpixelinthedigitalimageislimitedtohavingasinglecolor.Thespecificcolordependsonaformulathat
mixesvariousamountsoftheprimarycolorsred,green,andblue.Viewingcolorsasacombinationofred,
green,andblueisoftenreferredtoastheRGBcolormodel.
The amount of each primary color component is referred to as its intensity. Intensities will range from a
minimumof0toamaximumof255.Forexample,acolorwith255redintensity,0greenintensity,and255
blue intensity will be purple (or magenta). Black will have zero intensity for all primary color components
andwhitewillhavefullcolorintensity,255,forall.Table6.1showssomecommoncombinations.
An interesting question arises when you consider how many colors there might be using the RGB color
model. Since each of the three colors has 256 intensity levels, there are 2563 = 16,777,216 different
combinations of red, green, and blue intensities. All of these colors make up the color palette for the RGB
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 1/35
8/23/2016 Yuzu: Python Programming in Context

colormodel.

6.2.2ThecImageModule
InordertomanipulateimageswewilluseagroupofobjectsfoundinourcImagemodule.(SeeInstallingthe
Required Software (Appendix A) for instructions on downloading and installing cImage.py). This module
containsobjectsthatallowustoconstructandmanipulatepixels.Wecanalsoconstructanimagefromafile
or create a blank image that we can fill in later. In addition, we can create windows where images can be
displayed.

Table6.1Red,green,andblueintensitiesforsomecommoncolors

MethodName ExampleUse Explanation


Pixel(r,g,b) p=Pixel(25,200,143) Createapixelwith25red,200green,and143blue.
getRed() r=p.getRed() Returntheredcomponentintensity.
getGreen() g=p.getGreen() Returnthegreencomponentintensity.
getBlue() g=p.getBlue() Returnthebluecomponentintensity.
setRed() p.setRed(100) Settheredcomponentintensityto100.
setGreen() p.setGreen(45) Setthegreencomponentintensityto45.
setBlue() p.setBlue(87) Setthebluecomponentintensityto87.

Table6.2Pixelobject

ThePixelObject

Imagesarecollectionsofpixels.Inordertorepresentapixel,weneedawaytocollecttogetherthered,green,
and blue components. The Pixel object provides a constructor and methods that allow us to create and
manipulatethecolorcomponentsofpixels.Table6.2showstheconstructorandmethodsprovidedbypixel
objects. Session 6.1 shows them in action. The constructor will require the three color components. It will
returnareferencetoaPixelobjectthatcanbeaccessedormodified.Wecanextractthecolorintensitiesusing
the getRed, getGreen, and getBlue methods. Similarly, we can modify the individual components within a
pixelusingsetRed,setGreen,andsetBlue.

>>>fromcImageimport*
>>>p=Pixel(200,100,150)
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 2/35
8/23/2016 Yuzu: Python Programming in Context

>>>p
(200,100,150)
>>>p.getRed()
200
>>>p.setBlue(20)
>>>p
(200,100,20)
>>>

Session6.1Creatingandusingapixel

TheImageWinObject

Before creating images, we will create a window that can be used to display our images. The ImageWin
object provides a constructor that produces a window with a title, width, and height. When a window is
constructed,itisimmediatelyshown.Thecodebelowproducesanemptywindowthatis600pixelswideand
400pixelshigh.Table6.3showsadditionalmethodsfortheImageWinobject.NotethatthegetMousemethod
returnsacoordinatepositionwithinthewindowitselfandisnotrelatedtoanyparticularimagethatmightbe
displayedwithinthewindow.

>>>fromcImageimport*
>>>myWin=ImageWin("ImageProcessing",600,400)
>>>

MethodName ExampleUse Explanation


ImageWin(title, ImageWin("Pictures", Createawindowtodisplayimagesthatare800pixelswide
width,height) 800,600) and600pixelshigh.
exitOnClick() myWin.exitOnClick() Close the image window and exit when the mouse is
clicked.
getMouse() pos = Return an (x, y) tuple representing the position of the
myWin.getMouse() mouseclickinthewindow.

Table6.3TheImageWinobject

MethodName ExampleUse Explanation


FileImage(filename) im=FileImage("pic.gif") Createanimageobjectfromafilenamedpic.gif.
EmptyImage(width, im = EmptyImage(300, Createanemptyimagethatis300pixelswideand200
height) 200) pixelshigh.
getWidth() w=im.getWidth() Returnthewidthoftheimageinpixels.
getHeight() h=im.getHeight() Returntheheightoftheimageinpixels.
getPixel(col,row) p=im.getPixel(150,100) ReturnthePixelfromrow100,column150.
setPixel(col, row, im.setPixel(150, 100, Setthepixelatrow100,column150tobewhite.
newp) Pixel(255,255,255))
setPosition(col, im.setPosition(20,20) Positionthetopleftcorneroftheimageat(col,row)in
row) thewindow.
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 3/35
8/23/2016 Yuzu: Python Programming in Context

draw(imagewin) im.draw(myWin) DrawtheimageiminthemyWinimagewindow.Itwill


defaulttotheupperleftcorner.
save(fileName) im.save(fileName) Save the image to a file. Use gif or ppm as the
extension.

Table6.4FileImageandEmptyImageObjects

Nowthatwehaveawindow,weneedtocreateimagestodisplay.ThecImagemoduleprovidestwokindsof
imageobjects:FileImageandEmptyImage.Theseobjectsallowustocreateandmanipulateimages,andthey
giveussimpleaccesstothepixelsineachimage.Table6.4showsthetwoconstructorsforcreatingimagesas
wellasothermethodsthatareprovidedbybothobjects.

TheFileImageObject

The FileImage object is an image that is constructed from files such as those that are created by digital
camerasorthatresideonwebpages.Forexample,thefilelutherBell.gifwastakenwithanordinarydigital
camera.Session6.2showsthattheFileImageconstructorneedsonlythenameoftheimagefile.Itconverts
theimagestoredinthatfiletoanimageobject.Inthiscase,bellisthereferencetothatobject.Finally,wecan
usethedrawmethodtoasktheimageobjecttoshowitselfinanimagewindow.Thedefaultpositioningisto
placetheimageintheupperleftcornerofthewindow.Figure6.1showstheresult.

>>>fromcImageimport*
>>>myWin=ImageWin("LutherBell",300,200)
>>>bell=FileImage("lutherBell.gif")
>>>bell.draw(myWin)
>>>

Session6.2Creatingandshowingafileimage

Figure6.1Drawinganimageinawindow

As we said earlier, an image is a twodimensional grid of pixel values. Each small square in Figure 6.2
representsapixelthatcantakeonanyoneofthemillionsofcolorsintheRGBcolormodel.Wecanaccess
informationaboutourspecificimagebyusingthegetWidthandgetHeightmethods(seeSession6.3).This
imageis300pixelsacrossand200pixelstoptobottom.Rowsarenumberedfrom0to1lessthantheheight
oftheimage.Columnsarenumberedfrom0to1lessthanthewidth.

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 4/35
8/23/2016 Yuzu: Python Programming in Context

Figure6.2Detailsofanimage

>>>bell.getWidth()
300
>>>bell.getHeight()
200
>>>bell.getPixel(124,165)
(117,123,123)
>>>

Session6.3Accessinginformationaboutanimage

We can access a particular pixel by using the getPixel method. To use getPixel, we need to provide the
location for the pixel of interest. The location will be a pair of values, one specifying the column and one
specifyingtherow.Eachuniquecolumnrowpairwillprovideaccesstoasinglepixel.
Intheexample,weareaccessingthepixellocatedatcolumn124,row165.Inotherwords,itisthepixelthat
is125pixelsfromtheleftand166pixelsfromthetop.Itisimportanttorememberthatwestartcountingat0.
The value that is returned in our example shows the red, green, and blue components of the Pixel at that
locationinourimage.

TheEmptyImageObject

We often want to build a new image pixel by pixel starting with a blank image. Using the EmptyImage
constructor,wecancreateanimagethathasaspecificwidthandheightbutwhereeachpixelisvoidofcolor.
ThismeansthateachPixelhasthevalue(0,0,0)orblack.ThestatementsinSession6.4createanempty
imagewithallblackpixels.

>>>myImWin=ImageWin("EmptyImage",300,300)
>>>emptyIm=EmptyImage(300,300)
>>>emptyIm.draw(myImWin)
>>>

Session6.4Creatinganddisplayinganemptyimage

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 5/35
8/23/2016 Yuzu: Python Programming in Context

Asanexampletoshowthebasicuseoftheimagemethods,wewillconstructanimagethatstartsoutempty
and is filled with white pixels at specific locations. Session 6.5 starts by creating a window and an empty
imagethatissizedtofitwithinthewindow.Inordertocreatealineofwhitepixels,weusealoopvariablei
and iterate over the range from 0 to 299. The setPixel method can be called using the value of i for both
columnandrowwithapixelcalledwhitePixelthathasbeencreatedwithacombinationofred,green,and
bluecorrespondingtothecolorwhite.WedrawtheimageinthewindowasshowninFigure6.3.Finally,we
savetheimagetoafileusingthesavemethod.

>>>fromcImageimport*
>>>myImWin=ImageWin("LineImage",300,300)
>>>lineImage=EmptyImage(300,300)
>>>whitePixel=Pixel(255,255,255)
>>>foriinrange(300):
lineImage.setPixel(i,i,whitePixel)

>>>lineImage.draw(myImWin)
>>>lineImage.save("lineImage.gif")
>>>

Session6.5UsingEmptyImage

Figure6.3Creatingawhitediagonalline

Exercises

6.1ModifySession6.5tocreatealinewithrandompixelcolors.

6.2 Write a function to create an image of a rectangle. Start with an EmptyImage. Challenge:Createa
filledrectangle.

6.3Writeafunctiontocreateanimageofacircle.

6.4DownloadanimagefromtheWeboryourpersonalimagecollectionanddisplayitinawindow.Note
theimagemustbeingifformat.

6.5Modifysomepixelsintheimagefromthepreviousquestionandsaveit.
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 6/35
8/23/2016 Yuzu: Python Programming in Context

6.6 Why is the number of color intensities limited to the range 0255? Do some research to find the
answertothisquestion.

6.3BasicImageProcessing
Wenowhaveallofthetoolsnecessarytodosomesimpleimageprocessing.Ourfirstexampleswillperform
color manipulations on an image. In other words, we want to take the existing pixels and modify them in
someway tochangetheappearance of theoriginal image.Thebasicideawillbetosystematicallyprocess
eachpixeloneatatimeandperformthefollowingoperations:
1.Extractthecolorcomponents.
2.Buildanewpixel.
3.Placethatpixelinanewimageatthesamelocationastheoriginal.
Onceweseethegeneralpattern,theoptionsareendless.Notethatallofthenewlyconstructedimagesinthis
sectionwillbethesamedimensionsastheimagetheyarebasedon.

6.3.1NegativeImages
Whenimagesareplacedonfilmandthendeveloped,asetofnegativesisproduced.Anegativeimageisalso
knownasacolorreversedimage.Inanegativeimage,redbecomescyanwherecyanisthemixtureofgreen
andblue.Likewise,yellowbecomesblueandbluebecomesyellow.Regionsthatarewhiteturnblack,black
turnswhite,lightturnsdarkanddarkturnslight.Thiscontinuesforallpossiblecolorcombinations.
Atthepixellevel,thenegativeoperationisjustamatterofreversingthered,green,andbluecomponentsin
thatpixel.Sincecolorintensityrangesfrom0to255,apixelwithalargeamountofaspecificcolorsay,red
willhaveasmallamountinthenegative.Atthemaximum,apixelwitharedintensityof255willhavea
redintensityof0inthenegative.Thissuggeststhatthewaytocreateanegativepixelistosubtracteachof
thered,green,andblueintensityvaluesfrom255.Theresultscanthenbeplacedinanewpixel.
Listing 6.1 shows a function that will take a Pixel as a parameter and return the negative pixel using the
suggestedprocessfromabove.NotethatthefunctionexpectstoreceiveanentirePixelandwilldecompose
thecolorcomponents,performthesubtractions,andthenbuildandreturnanewPixel.Wecaneasilytestthis
functionasshowninSession6.6.

Listing6.1Constructinganegativepixel

>>>apixel=Pixel(155,23,230)
>>>negativePixel(apixel)
(100,232,25)
>>>

Session6.6TestingthenegativePixelfunction

Inordertocreatethenegativeimage,wecallthenegativePixelfunctiononeachpixel.Weneedtocomeup
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 7/35
8/23/2016 Yuzu: Python Programming in Context

with a pattern that will allow us to process each pixel. To do this, we can think of the image as having a
specificnumberofrowsequaltotheheightoftheimage.Eachrowinturnhasanumberofcolumnsequalto
thewidthoftheimage.Withthisinmind,wecanbuildaniterationthatwillsystematicallymovethroughall
of the rows and within each, will move through all of the columns. This gives rise to the notion of nested
iterationthe placement of an iteration as the process inside of another iteration. In other words, for each
pass of the outer iteration, the inner iteration will run to completion. The inner iteration will run from
starttofinishforeachpassoftheouteriteration.
Asanexample,considerthecodefragmentinSession6.7usingforstatements.Theouteriterationismoving
overthelist[0,1,2],whichwasproducedbyrange(3).Foreachiteminthatlist,theinnerloopwilliterateover
thecharacters'c','a','t'.Thismeansthattheprintfunctionwillbecalledforeachcharacterinthestring"cat"
foreachnumberinthelist[0,1,2].

>>>fornuminrange(3):
forchin"cat":
print(num,ch)
0c
0a
0t
1c
1a
1t
2c
2a
2t
>>>

Session6.7Showingnestediterationwithlistsandstrings

Theresultingoutputshowsninelines.Eachgroupofthreerepresentsonepassoftheouterloop.Withineach
group,thevalueofnumstaysthesame.Foreachvalueofnum,theentireinnerloopcompletes,andtherefore
eachofthethreecharactersofthestringappears.
We can now apply this idea to the construction of a function to compute the negative of each pixel in an
image(seeListing6.2).Thefunctionwilltakeasingleparameterthatgivesthenameofafilecontainingan
image.Thefunctionwillnotreturnanythingbutwillsimplydisplayboththeoriginalandthenegativeimage.
Thefirststep(lines28),istocreateanimagewindow,openanoriginalimage,anddrawitinthewindow.
Wethenneedtocreateanemptyimagethathasthesamewidthandheightastheoriginal.Notethatwidthand
heightarethewidthandheightofboththeoriginalandnewimage.
Usingtheideaofnestediteration,wewillfirstiterateovertherows,startingwithrow0andextendingdown
toheight1.Foreachrow,wewillprocessallofthecolumnswithintherow.

forrowinrange(height):
forcolinrange(width):

Eachpixelisaccessedbyrowandcol.WecangettheoriginalcolortupleatthatlocationusingthegetPixel
method(line12).Oncewehavetheoriginalpixel,wecanusethenegativePixelfunctiontotransformitinto
thenegative.Finally,usingthesamerowandcol,wecanplacethenewnegativepixelinthenewimage(line
14) using the setPixel method. Once the iteration is complete for all pixels, the new image is drawn in the
window. Note that we use the setPosition method to place the new image next to the original. Figure 6.4
showstheresultingimages.
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 8/35
8/23/2016 Yuzu: Python Programming in Context

Figure6.4Anegativeimage

Listing6.2Constructinganegativeimage

6.3.2Grayscale
Another very common color manipulation is to convert an image to grayscale, where each pixel will be a
shadeofgray,rangingfromverydark(black)toverylight(white).Withgrayscale,eachofthered,green,and
blue color components will take on the same value. In other words there are 256 different grayscale color
values ranging from darkest (0, 0, 0) to lightest (255, 255, 255). The standard color known as gray is
typicallycodedas(128,128,128).
Our task then is to take each color pixel and convert it into a gray pixel. The easiest way to do this is to
considerthattheintensityofeachred,green,andbluecomponentneedstoplayapartintheintensityofthe
gray.Ifallofthecolorintensitiesareclosetozero,theresultingcolorisverydark,whichshouldinturnshow
asadarkshadeofgray.Ontheotherhand,ifallofthecolorintensitiesarecloserto255,theresultingcoloris
verylightandtheresultinggrayshouldbelightaswell.
This analysis gives rise to a simple but accurate formula for grayscale. We will simply take the average
intensityofthered,green,andbluecomponents.Thisaveragecanthenbeusedforallthreecolorcomponents
inanewpixelthatwillbeashadeofgray.Listing6.3showsafunction,similartothenegativePixelfunction
describedpreviously,thattakesaPixelandreturnsthegrayscaleequivalent.Session6.8showsthefunctionin
use.

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 9/35
8/23/2016 Yuzu: Python Programming in Context

Listing6.3Constructingagrayscalepixel

>>>grayPixel(Pixel(34,128,74))
(78,78,78)
>>>grayPixel(Pixel(200,234,165))
(199,199,199)
>>>grayPixel(Pixel(23,56,77))
(52,52,52)
>>>

Session6.8TestingthegrayPixelfunction

Now the process of creating a grayscale image can proceed in the same way as described for creating the
negative (see Listing 6.4). After opening and drawing the original image, we create a new, empty image.
Usingnestediteration,processeachpixel,thistimeconvertingeachtothecorrespondinggrayscalevalue(line
13).ThefinalimageisshowninFigure6.5.
Wedevelopedthepreviousexamplesbycontinuallybuildinguponaframeworkofsimpleideas.Westarted
withthepixel,thencreatedafunctiontotransformthecolorcomponentsofapixel,andfinallyappliedthat
functiontoallofthepixelsintheimage.Thisstepwiseconstructionisaverycommonmethodologyusedin
writing computer programs. Building upon those functions that already work in order to provide more
complex functionality that can again be used as a foundation allows programmers to be very efficient. We
takethisonestepfurtherinthenextsection.

Figure6.5Agrayscaleimage

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 10/35
8/23/2016 Yuzu: Python Programming in Context

Listing6.4Constructingagrayscaleimage

Exercises

6.7Writeafunctionthatremovesallredfromanimage.

6.8Writeafunctionthatenhancestheredintensityofeachpixelinanimage.

6.9Writeafunctionthatdiminishestheblueintensityofeachpixelinanimage.

6.10 Write a function that manipulates all three color intensities in a pixel using a strategy of your own
choice.

6.11Writeafunctionthattakesacolorimageanddisplaysablackandwhiteimagenexttoit.Hint:You
maywanttostartbyconvertingtheimagetograyscale.Anypixelwithagrayvaluelessthansome
thresholdwillbecomeblack.Allotherpixelswillbewhite.

6.3.3AGeneralSolution:ThePixelMapper
If you compare the Python listings for the makeGrayScale and makeNegative functions, you will note that
thereisquiteabitofredundancy.Infact,thesamestepswerefollowedwithonlyoneexceptionnamely,the
functionthatwascalledtomapeachoriginalpixelintoanewpixel.Thissimilaritycausesustothinkthatwe
couldfactoroutthecodethatisthesameandcreateamoregeneralPythonfunction.Thisisanotherexample
ofusingabstractiontosolveproblems.
Figure6.6showshowsuchafunctionmightbeconstructed.WewillcreateafunctioncalledpixelMapperthat
willtaketwoparameters,anoriginalimageandanRGBfunction.ThepixelMapperfunctiontransformsthe
originalimageintoanewimageusingtheRGBfunction.AfterapplyingtheRGBfunctiontoeverypixel,the
transformedimageisreturned.Inthiswaywehaveasinglefunctionthatiscapableoftransforminganimage
givenanyfunctionthatmanipulatesthecolorintensitiesofasinglepixel.
Inordertoimplementthisgeneralpixelmapper,weneedtobeabletopassafunctionasaparameter.Upto
thispoint,allofourparametershavebeendataobjectssuchasintegers,floatingpointnumbers,lists,tuples,
andimages.Thequestiontoconsideriswhetherthereisanydifferencebetweenafunctionandatypicaldata
object.

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 11/35
8/23/2016 Yuzu: Python Programming in Context

Theanswertothisquestionisthatthereisnodifference.Tounderstandwhy,wewillfirstlookatasimple
example.ThefunctionsquareIttakesanumberandreturnsthesquare.

defsquareIt(n):
returnn*n

We can invoke the squareIt function with the usual syntax (see Session 6.9), placing the actual value to be
squared as a parameter. However, if we evaluate the name of the function without invoking it (without the
parametersinparentheses),wecanseethattheresultisafunctiondefinition.ThenameofaPythonfunction
is a reference to a data objectin particular, a function definition (see Figure 6.7). Note that the strange
lookingnumber,0x1021730,isactuallytheaddresswherethefunctionisstoredinmemory.

Figure6.6Ageneralpixelmappingfunction

Figure6.7Afunctionisadataobject

Sinceafunctionissimplyanotherdatatype,youmightwonderwhatkindsofoperatorsyoucanusewithit.In
factthereareonlytwooperatorsthatyoucanusewithafunction.Theparenthesesareactuallyanoperator
thattellsPythontoapplythefunctiontothesuppliedparameters.Inaddition,sinceafunctionisanobject,
youcanusetheassignmentoperatortogiveafunctionanothername,asshowninSession6.9.Notethatnow
thevariablezisareferencetothesamedataobjectassquareItandcanbeusedwiththeparenthesisoperator.

>>>squareIt(3)
9
>>>squareIt(squareIt(3))
81
>>>squareIt
>>>z=squareIt
>>>z(3)
9
>>>z

Session6.9EvaluatingthesquareItfunction

SinceanyPythonobjectcanbepassedasaparameter,itiscertainlypossibletopassthefunctiondefinition
object.Wejustneedtobecarefulnottoinvokethefunctionpriortopassingit.Toshowthis(Session6.10),
wecreateasimplefunctioncalledtestthatexpectstwoparameters,afunctionobjectandanumber.Thebody
oftestwillinvokethefunctionobjectusingthenumberasaparameterandreturntheresult.

>>>deftest(funParm,n):

returnfunParm(n)
>>>
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 12/35
8/23/2016 Yuzu: Python Programming in Context

>>>test(squareIt,3)
9
>>>test(squareIt,5)
25
>>>test(squareIt(3),5)
Traceback(mostrecentcalllast):
File"",line1,in
File"",line2,intest
TypeError:'int'objectisnotcallable

Session6.10Usingafunctionpassedasaparameter

We can then use our test function by passing the squareIt function definition. In addition, we will pass the
integer 3. Remember, when we pass the function definition object, we do not include the parentheses pair.
Figure6.8showsthereferencesimmediatelyaftertesthasbeencalledandtheparametershavebeenreceived.
AcopyofthereferencetotheactualparametersquareItisreceivedbyfunParmandncontainsareferenceto
theobject3.

Figure6.8Passingthemethodandaninteger

ThestatementreturnfunParm(n)willcausethefunctionreferredtobyfunParmtobeinvokedonthevalueof
n.Inthiscase,sincefunParmisareferencetosquareIt,thesquareItfunctionwillbeinvokedwiththevalueof
3. The next example shows the result of passing 5 instead of 3. Note the error message at the end of the
sessionwhenwecallthesquareItfunctioninsteadofpassingthedefinition.
With these mechanics in place, it is now possible to create an implementation for our general pixelMapper
function(seeListing6.5).Recallthatthisfunctionwilltaketwoparameters,animageandanRGBfunction.
Lines35constructanewemptyimagethatisthesamesizeastheoriginal.Line10,whichisinsideofour
nestediteration,doesmostofthework.ThergbFunctionparameterisappliedtoeachpixelfromtheoriginal
imageandtheresultingpixelisplacedinthenewimage.Oncethenestediterationiscomplete,wewillreturn
thenewimage.

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 13/35
8/23/2016 Yuzu: Python Programming in Context

Listing6.5Ageneralpixelmappingmethod

WecancompleteourexamplebycallingthepixelMapperfunctionusingoneofourRGBfunctionsfromthe
previoussections.Listing6.6showsamainfunction,generalTransform,thatsetsuptheimagewindowand
loadstheoriginalimage.Line6invokespixelMapperusingthegrayPixelfunction.Theresultisidenticalto
thatshowninFigure6.5.

Listing6.6Callingthegeneralpixelmappingfunction

Exercises

6.12UseareferencediagramtoexplaintheerrormessageinSession6.10.

6.13 Use the generalTransform and pixelMapper functions to create a negative image using the
negativePixelfunctiondevelopedearlier.

6.14WriteanRGBfunctiontoremovetheredfromapixel.TestthisfunctionwithpixelMapper.

6.15WriteanRGBfunctiontoconvertapixeltoblackandwhite.TestthisfunctionwithpixelMapper.

6.16WriteanRGBfunctionofyourchoice.TestthisfunctionwithpixelMapper.

6.17Sepiatoneisabrownishcolorthatwasusedforphotographsintimespast.Theformulaforcreatinga
sepiatoneisasfollows:

WriteanRGBfunctiontoconvertapixeltosepiatone.Hint:RememberthatRGBvaluesmustbeintegers
between0and255.
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 14/35
8/23/2016 Yuzu: Python Programming in Context

6.4Parameters,ParameterPassing,andScope
Throughoutmanyofthepreviouschapters,wehaveusedfunctionstoimplementabstraction.Wehavebroken
problemsdownintosmaller,moremanageablepiecesandhaveimplementedfunctionsthatwecancallover
and over again. In the previous section we pushed this idea one step further by passing functions as
parameters to other functions. In this section we explore in more detail the underlying mechanics of how
functionsandparameterpassingwork.
Consider the function shown in Listing 6.7, which computes the hypotenuse of a right triangle. Using the
Pythagorean theorem, a2 + b2 = c2, this function needs the lengths of the two sides, called a and b, and
computesandreturnsthelengthofthelongside,calledc.Session6.11showsthefunctioninuse.

Listing6.7Asimplefunctiontocomputethehypotenuseofatriangle

>>>hypotenuse(3,4)
5.0
>>>
>>>side1=3
>>>side2=4
>>>hypotenuse(side1,side2)
5.0
>>>
>>>hypotenuse(side1*2,side2*2)
10.0
>>>
>>>hypotenuse

Session6.11Asimplemethod

In the first example, references to the objects 3 and 4 are passed to the function. These are known as the
actualparametersastheyrepresenttheactualdatathatthefunctionwillreceive.Aswehaveseenbefore,
the parameter list (a, b) receives these object references one at a time, in order, from left to right. So, a
receivesareferencetotheobject3andbreceivesareferencetotheobject4.Theseparameters,definedinthe
functionitself,areknownastheformalparameters.
In the second example shown, the actual parameters are not literal numbers but are instead names that are
referringtotheobjects3and4.Priortothefunctioncall,Pythonevaluatesthetwonamesside1andside2in
ordertofindtheobjects.Onceagain,areceivesareferencetotheobject3andbreceivesareferencetothe
object4.

6.4.1CallbyAssignmentParameterPassing
Ingeneral,theprocessbywhichafunctionsformalparameterreceivesanactualparametervalueisknownas
parameterpassing.Therearemanydifferentwaystopassparametersanddifferentprogramminglanguages
havechosentouseavarietyofthem.InPython,however,allparametersarepassedusingasinglemechanism
knownascallbyassignmentparameterpassing.

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 15/35
8/23/2016 Yuzu: Python Programming in Context

Callbyassignmentparameterpassingusesasimpletwostepprocesstopassdatawhenthefunctioniscalled.
Callingafunctionisalsoknownasinvocation.Thefirstthingthathappensisthattheactualparametersare
evaluated. This evaluation results in an object reference to the result. In the first case from Session 6.11,
evaluatingaliteralnumbersimplyreturnsareferencetothenumberitself.Inthesecondexample,evaluating
avariablenamereturnstheobjectreferencenamedbythatvariable.
Oncetheevaluationoftheactualparametersiscomplete,theobjectreferencesarepassedtoandreceivedby
the formal parameters in the function. The formal parameter becomes a new name for the reference that is
passed.Inasenseitisasifweexecutedtheassignmentstatementformalparameter=actualparameter.
As a final example, consider the third invocation shown in Session 6.11. Here the actual parameters are
expressionsthatdoublethelengthsoftheoriginalsides.Callbyassignmentparameterpassingevaluatesthese
expressionsfirstandthenassignsthereferencestotheresultingobjectstotheformalparametersaandb.The
hypotenusefunctionhasnoideawherethereferencescamefromorhowcomplicatedtheoriginalexpression
mighthavebeen.Thereferencesthatarereceivedaresimplytheresultsoftheevaluation.
Call by assignment parameter passing has some important ramifications that may not be obvious to you at
first.Changestotheformalparametermayormaynotresultinchangestotheactualparameterdependingon
whethertheactualparameterismutableorimmutable.Iftheactualparameterisimmutable,thenchangesto
theformalparameterwillhavenoeffectoutsidethefunction.Iftheactualparameterismutable,thenchanges
totheobjectreferencedbytheformalparameterwillbevisibleoutsidethefunction.
Forexample,iftheactualparameterisareferencetotheinteger3,thenassigningareferencetotheinteger5
totheformalparameterwouldnotbevisibleoutsidethefunction.Iftheactualparameterisareferencetoa
list, then any changes to the contents of the list including additions or deletions will be visible outside the
function.However,iftheformalparameterisassignedtoadifferentlist,thebehaviorisconsistentwiththe
behaviorforintegers.

6.4.2Namespaces
In Python, all of the names defined in a program, whether for data or for functions, are organized into
namespacescollectionsofallnamesthatareavailabletobeaccessedataparticularpointintimeduringthe
execution of a Python program. When we start Python, we create two namespaces. The first is called the
builtinnamespace(seeFigure6.9), and it includes all of the systemdefined names of functions and data
typesthatweregularlyuseinPython.Namessuchasrange,str,andfloatareincludedinthisnamespace.The
secondiscalledthemainnamespace,anditisinitiallyempty.Pythoncallsthesetwonamespaces__builtin_
_and__main__.
AswebegintocreateourownnamesinaPythonsession,theyareaddedtothemainnamespace.Asshownin
theexampleinSession6.11,thevariablenamesside1andside2arenowaddedtothemainnamespace.The
functionnamehypotenuseisalsoaddedtothemainnamespace.Notethatthenamesareaddedasaresultof
an assignment statement or a method definition. The objects referred to by the names are shown as well.
Figure6.10showsthelocationofthenamesandtheobjectstheyreference.Notethattheobjectsexistoutside
thenamespace.

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 16/35
8/23/2016 Yuzu: Python Programming in Context

Figure6.9Thebuiltinnamespace

At this point it is instructive to investigate the namespace idea interactively. Python provides us with the
function dir that allows us to see a list of the names in any namespace. Session 6.12 shows what happens
whenweinvokethedirfunctionafterdefiningthehypotenusefunctionandassigningvaluestotheside1and
side2variables.
In addition to the three names that we just created, there are four other names of interest defined in our
namespace.The__builtins__referstothebuiltinnamespacewereferredtoearlier.Itispossibleforyouto
findoutthenamesoftheobjectsinthebuiltinnamespacebycallingthefunctiondir(__builtins__).The_
_doc__nameisalwaysavailabletoholdareferencetoastringthatdescribesthenamespace.The__main__
namespacedoesnothavedocumentationbutothernamespacessuchasmathmay.__name__holdsthename
ofthecurrentnamespace.InSession6.12youcanseethatweevaluated__name__tofindthatweareinthe
__main__namespace.
The final name in the list from dir that we have yet to discuss is math. This name appears because we
importedthemathmoduleonline1ofthesession.Asyoucansee,thenamemathreferstoanobjectthatisa
module.Itisimportanttonotethatamoduledefinesitsownnamespace.Justaswiththebuiltinnamespace,
you can find out the names in the math namespace by evaluating dir(math). Note that the math namespace
alsohasitsown__name__and__doc__entries.

Figure6.10Themainnamespace

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 17/35
8/23/2016 Yuzu: Python Programming in Context

>>>importmath

>>>defhypotenuse(a,b):

c=math.sqrt(a**2+b**2)

returnc

>>>side1=3

>>>side2=4

>>>dir()

['__builtins__','__doc__','__name__','__package__','hypotenuse','math','side1','side2']

>>>__name__

'__main__'

>>>math

>>>dir(math)

['__doc__','__file__','__name__','__package__','acos','asin','atan','atan2','ceil','copysign','cos',
'cosh','degrees','e','exp','fabs','floor','fmod','frexp','hypot','isinf','isnan','ldexp','log','log10','modf','pi',
'pow','radians','sin','sinh','sqrt','tan','tanh','trunc']

>>>math.__doc__

'This module is always available. It provides access to the\nmathematical functions defined by the C
standard.'

>>>

Session6.12Exploringthemainnamespace

Wenowhaveallthetoolsneededtounderstandthedifferencebetweenthestatementsimportmathandfrom
mathimport*.Session6.13showstheresultofcallingthedirfunctionafterimportingthemathmoduleusing
thestatementfrommathimport*.Noticethatallthenamesfromthemathmodulenowappearaspartofthe
mainnamespace.Thisallowsustocallfunctionssuchassqrtdirectlywithoutprefacingthefunctionname
withthemodulename.

>>>frommathimport*

>>>dir()

['__builtins__','__doc__','__name__','__package__','acos','asin','atan','atan2','ceil','copysign','cos',
'cosh','degrees','e','exp','fabs','floor','fmod','frexp','hypot','isinf','isnan','ldexp','log','log10','modf','pi',
'pow','radians','sin','sinh','sqrt','tan','tanh','trunc']

>>>

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 18/35
8/23/2016 Yuzu: Python Programming in Context

Session6.13Importingthemathnamesintothemainnamespace

Exercises
6.18Trycallingthedirfunctiononthe__builtins__object.

6.19Importtheturtlemoduleandfindoutthenamesdefined.

6.20Lookatthe__doc__stringfortheturtlemodule.

6.21IfyouputastringatthebeginningofanyPythonfile,thatstringbecomesthe__doc__stringforthat
module.TryaddingastringtothebeginningofoneofyourPythonfiles.Canyouimportthatfileand
seethestringyouadded?Thehelpfunctionalsoreturnsthisstringaspartofthedocumentationfora
module.

6.4.3CallingFunctionsandFindingNames
Whenafunctionisinvoked,anewnamespaceknownasthelocalnamespaceiscreatedcorrespondingtothe
function itself. This namespace includes those names that are created inside the function. This includes the
formalparametersaswellasanynamesthatareusedonthelefthandsideofanassignmentstatementinthe
bodyofthefunction.Thesenamesarereferredtoaslocalvariablessincetheyhavebeencreatedwithinthe
functionandarepartofthelocalnamespace.Whenthefunctioniscompleted,eitherduetoareturnstatement
orsimplyduetorunningoutofcodestatements,thelocalnamespaceisdestroyed.Thismeansthatallofthe
locallydefinednamesarenolongeravailableforuse.
Itisimportanttonotetheplacementofthesenamespaceswithrespecttooneanother.Themainnamespaceis
placed inside the builtin namespace. Likewise, local namespaces are placed inside the namespace of the
modulewheretheyaredefined.Forprogramsthatyouwrite,thenamespacesforyourfunctionswillbeplaced
in the main namespace. The namespaces for functions from modules that you import will be placed in the
namespace of the imported module. Figure 6.11 shows the placement of the local namespace for the
hypotenuse function when it is called. This figure also provides an illustration of the call by assignment
mechanism.Notethattheformalparametersaandbarereferringtothesameobjectsastheactualparameters
side1andside2.

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 19/35
8/23/2016 Yuzu: Python Programming in Context

Figure6.11Alocalnamespace

When a name is used in a statement, Python needs a way to locate the particular occurrence of that name
amongallofthenamesthathavebeenintroduceduptothatpoint.Inordertofindthename,Pythonusesthe
followingsimplerules:
1.Wheneveranameisused,exceptonthelefthandsideofanassignmentstatement,Pythonsearches
throughthenamespacesinthefollowingorder:

(a)Thecurrentlocalnamespace,ifoneexists
(b)Themainormodulenamespace
(c)Thebuiltinnamespace

WhenPythonfindsthefirstoccurrenceofthename,thesearchends.LookingagainatFigure6.11,
youmayfindithelpfultothinkofthisassearchingfromtheinsideout.Ifthenameisnotfound,an
errorisreported.
2.Whenyouuseanameonthelefthandsideofanassignmentstatement,Pythonsearchesonlythe
currentnamespace.

(a)Ifthenameisnotfound,anewnameiscreatedinthecurrentnamespace.
(b)Ifthenameisfound,thentheoldreferencewillbereplacedwiththeobjectfromtherighthand
sideoftheassignmentstatement.

ThismeansthatthesamenamemayexistinmanydifferentnamespacesbutPythonwillalwaysuse
thenameasgovernedbyrule1.

To show these rules in action, consider the code shown in Session 6.14. Here, function test1 defines one
formalparametervaluecalleda.Itadds5toaandprintstheresult.Sinceaisaformalparameter,itbecomes
partofthelocalnamespaceforthefunctiontest1.
Next,anassignmentstatementcreatesavariablecalledaandsetsittorefertotheobject6.Thisoccurrenceof
thenameaisaddedtothemainnamespace.Whenweinvoketest1usingaastheactualparameter,callby
assignmentparameterpassingwillfirstevaluatea.Theresultisareferencetotheobject6,whichispassed
andreceivedbytheformalparameterainthelocalnamespace.Whentheassignmentstatementisperformed
inthefunctiontest1,Pythonmustsearchforthenamea.Theresultofthesearchisthattheafromthelocal
namespaceisusedinthestatementa=a+5.Theainthemainnamespaceisunaffectedandstillreferstothe
object6.

>>>deftest1(a):
a=a+5
print(a)

>>>a=6
>>>test1(a)
11
>>>a
6

>>>deftest2(b):
print(b)
print(a)

>>>test2(14)
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 20/35
8/23/2016 Yuzu: Python Programming in Context

14
6
>>>a
6
>>>b
Traceback(mostrecentcalllast):
File"",line1,in?
NameError:name'b'isnotdefined
>>>

Session6.14Showingnamelookuprulesatwork

InthesecondexampleinSession6.14,test2isdefinedwithaformalparametercalledb.Thisfunctionprints
b and then prints a. However, the name a is not defined in the local namespace for test2. When the print
statementsareexecuted,wewillusethepreviousrulestolocatethenames.Theresultofthesearchisthatbis
foundinthelocalnamespacebutaisfoundinthemainnamespace.
Inthisexample,thereferencetotheobject14isassignedtotheformalparameterbintest2sothatthefirst
printstatementsimplyprintsthevalue14.Thesecondprintstatementtriestofindavariablecalleda.Sinceit
cannotbefoundinthelocalscope,thesearchproceedsoutwardtothemainnamespacewhereaisfoundwith
avalueof6.Therefore,6isprinted.
Notethatafterthecalltotest2completes,astillhasthevalue6sinceitispartofthemainnamespace.Once
thefunctiontest2completes,thenamespaceisdestroyedandbisnolongerpresent.Sincebdoesnotexistin
themainnamespace,anerrorisreportedthatbisnotdefined.

6.4.4ModulesandNamespaces
Thehypotenusefunctiondefinedearlierusesthesqrtfunctionfromthemathlibrary.Inordertoaccessthat
function,weneededtoimportthemathmodule.InPython,thestatementimportmathcreatesanameinthe
currentnamespacewithareferencetoanewnamespaceforthemoduleitself.Inthiscase,thenamemathis
addedtothemainnamespace.Thenewnamespaceforthemathmoduleisplacedinthebuiltinnamespace,
asisshowninFigure6.12.Themathnamespacecontainsnamesoffunctionssuchassqrt.Notethatthename
sqrtreferstoafunctiondefinition.
Itisimportanttonotethatthenamespaceforthemathmodulewasplacedinthebuiltinnamespace,notinthe
mainnamespace.Thenamespacesforallimportedmoduleswillbeplacedatthesamelevelwithinthebuiltin
namespace.Theonlythingthatwillbeplaceddirectlyinthemainnamespaceisthenameofthemodulewith
thecorrespondingreferencetothemodulenamespace.

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 21/35
8/23/2016 Yuzu: Python Programming in Context

Figure6.12Namespacesincludingimportedmodule

Totakethisonestepfurther,considerthehypotenusefunctionatthepointwherethesqrtfunctionhasbeen
invoked. As before, the namespace for hypotenuse has been placed as a local namespace in the main
namespace.Whenthehypotenusefunctioncallsthesqrtfunction,anewlocalnamespaceforsqrtiscreated.
Eventhoughthecalltosqrtwasmadefromthehypotenusenamespace,thesqrtnamespaceisplacedinthe
mathnamespacesincethatiswherethesqrtfunctionwasdefined.
Thelocalnamespacecreatedwhenafunctioniscalledisalwayscreatedinthenamespaceforthemodulein
whichthefunctionwasdefined.Inourexample,thisistrueregardlessofhowthemathmoduleisimported.
Evenifwehadimportedmathusingfrommathimport*thenamespaceforsqrtwouldbeplacedinthemath
namespace.
Figure6.13showsallofthenamespacesuptothispoint.Accordingtothenamelookuprulesdefinedearlier,
searches for names used in the sqrt function will start in the local sqrt namespace, proceed outward to the
mathnamespace,andfinallytothebuiltinnamespace.

Figure6.13Invocationofsqrtfunctionfrommath

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 22/35
8/23/2016 Yuzu: Python Programming in Context

6.5AdvancedImageProcessing
Wenowturnourattentiontosomeimageprocessingalgorithmsthatrequirethemanipulationofmorethan
onepixeleitherintheoriginalorinthenewimage.Thesetechniqueswillrequirethatwelookforadditional
patternsinthewaythatweprocessthepixelsoftheimage.

6.5.1Resizing
One of the most common manipulations performed on images is resizingthe process of increasing or
decreasingthedimensions(widthandheight)ofanimage.Inthissectionwefocusonenlarginganimage.In
particular,weconsidertheprocessofcreatinganewimagethatistwicethesizeoftheoriginal.
Figure 6.14 shows the basic idea. The original image is three pixels wide by four pixels high. When we
enlarge the image by a factor of 2, the new image will be 6 pixels wide by 8 pixels high. This presents a
problemwithrespecttotheindividualpixelswithintheimage.
The original image has12individual pixels. No matterwhat wedo, we will notbe able tocreate any new
detailintheimage.Thismeansthatwhenweincreasethenumberofpixelsto48inthenewimage,36ofthe
pixels must use information that is already present in the original. Our problem is to decide systematically
howtospreadtheoriginaldetailoverthepixelsofthenewimage.
Figure6.15 shows one possible solution to this problem. Each pixel of the original will be mapped into 4
pixelsinthenewimage.Every1by1blockofpixelsintheoriginalimageismappedintoa2by2blockin
thenewimage.Thisresultsinaonetofourmappingthatwillbecarriedoutforalloftheoriginalpixels.Our
taskistodiscoverapatternformappingapixelfromtheoriginalimageontothenewimage.

Figure6.14Enlarginganimagebyafactorof2

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 23/35
8/23/2016 Yuzu: Python Programming in Context

Figure6.15Mappingeacholdpixelto4newpixels

Anexampleofthismappingprocessshowsthatpixel(0,0)willbemappedtopixels(0,0),(1,0),(0,1),(1,
1).Likewise,pixel(2,2)mapstopixels(4,4),(5,4),(4,5),(5,5).Extendingthispatterntothegeneralcase
ofapixelwithlocation(col,row)givesthefourpixels(2col,2row),(2col+1,2row),(2col,2
row+1),(2col+1,2row+1).Figure6.16illustratesthemappingequationsforaparticularpixel.You
shouldcheckyourunderstandingbyconsideringotherpixelsintheoriginalimage.
Listing6.8showsthecompletefunctionfordoublingthesizeofanimage.Sincethenewimagewillbetwice
thesizeoftheold,itwillbenecessarytocreateanemptyimagewithdimensionsthataredoublethoseofthe
original(seelines25).
Nowwecanusenestediterationtoprocesseachoriginalpixel.Asbefore,twoforloops,oneforthecolumns
and one for the rows, will allow us to systematically process each pixel. Using the color components from
eacholdpixel,wecopythemtothenewimage.Lines1114usethepatterndiscussedabovetoassigneach
pixelinthenewimage.Notethateachofthefourpixelsreceivesthesamecolortuple.Theresultcanbeseen
inFigure6.17.

Figure6.16Mappingforapixelatlocation(col,row)

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 24/35
8/23/2016 Yuzu: Python Programming in Context

Listing6.8Doublingthesizeofanimage

Figure6.17Enlargedimage

6.5.2Stretching:ADifferentPerspective
Intheprevioussectionwedevelopedanalgorithmforenlarginganimage.Thatsolutionrequiredustomap
each pixel in the original image to 4 pixels in the new image. In this section we consider an alternative
solution:constructinganewimagebymappingpixelsfromthenewimagetotheoriginal.Viewingproblems
frommanydifferentperspectivescanoftenprovidevaluableinsight.Ouralternativesolutiontakesadvantage
ofthisinsightandleadstoasimplersolution.
Figure6.18showsthesameimageasbefore.However,thistimethepixelmappingisdrawninthereverse
direction.Morespecifically,insteadoflookingattheproblemfromtheperspectiveoftheoriginalimage,we
areturningourfocustothepixelsofthenewimage.Asweprocessthepixelsofthenewimage,weneedto
figureoutwhichpixelintheoriginalimageshouldbeused.
Listing6.9showsthecompletedcodeforournewfunction,whichwilltakeanoriginalimageasaparameter
and return the new, enlarged image. Again, we will need a new empty image that is twice the size of the
original.Thistimewewriteouriterationtoprocesseachpixelinthenewimage.Thenestediterationideawill
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 25/35
8/23/2016 Yuzu: Python Programming in Context

stillworkbuttheboundswillneedtobeintermsofthenewimage,ascanbeseeninlines78.

Figure6.18Mappingeachof4newpixelsbackto1oldpixel

Listing6.9Doublingthesizeofanimage:Mappingnewbacktoold

Wenowneedtoperformthepixelmapping.Aswesawinthelastsection,thepixelsatlocations(4,4),(5,4),
(4,5),(5,5)willallmapbacktopixel(2,2)intheoriginal.Asanotherexample(seeagainFigure6.18,pixels
(4,0),(5,0),(4,1),(5,1)willallmapbacktopixel(2,0)).Ourtaskistofindthemappingpatternthatwill
allowustolocatetheappropriatepixelinthegeneralcase.
Onceagain,itmayappearthattherearefourcasessincefourpixelsinthenewimageassociatetoasingle
pixel in the original. However, upon further examination, that is not true. Since we are considering the
problemfromtheperspectiveofthenewimage,thereisonlyonepixelthatisofinterestintheoriginal.This
suggests that there may be a single operation that will map each of the new pixels back to the original.
Lookingattheexamplepixels,wecanseethatintegerdivisionwillperformtheoperationthatweneed(see
Figure6.19).
Morespecifically,intheexamplesgivenabove,weneedanoperationthatcanbedonetoboth4and5where
theresultwillbe2.Also,thesameoperationon0and1willneedtoyield0.Recallthatboth4//2and5//2
givearesultof2sincethe//operatorwhenworkingonintegersgivesanintegerresultwhilediscardingthe
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 26/35
8/23/2016 Yuzu: Python Programming in Context

remainder.Likewise,0//2and1//2bothgive0astheirresult.
We can now use this operation to complete the function. Lines 1011 extract the correct pixel from the
originalimagebyusingtheintegerdivisionoperatortocomputethecorrespondingcolumnandrow.Oncethe
pixelhasbeenchosen,wecanassignittothelocationinthenewimage.Ofcourse,theresultisthesameas
seeninthelastsection(seeagainFigure6.17).

Figure6.19Mappingbackusingintegerdivision

As we stated earlier, enlarging an image provides no new detail. The result can therefore look grainy or
blocky due to the fact that we are mapping one pixel to many locations in the new image. Although we
cannotcreateanynewdetailtoaddtotheimage,itispossibletosmoothoutsomeofthehardedgesby
processingeachpixelwithrespecttothosearoundit.

Exercises

6.22Writeafunctiontoquadruplethesizeofanimage.

6.23Writeageneralfunctionforenlarginganimagethatacceptsascaleparameterforenlarginginthex
directionandanotherparameterforenlargingintheydirection.

6.24Writeafunctionforreducingthesizeofanimage.

6.25Writeafunctionthatwillsmooththeenlargedimage.Hint:Youwillwanttoreplaceeachpixelinthe
enlargedimagewiththeaverageofitselfanditsneighbors.

6.26 Write a function to remove noise from an image. You can do this by replacing each pixel with the
medianofitselfanditsneighbors.

6.5.3FlippinganImage
We now consider manipulations that physically transform an image by moving pixels to new locations. In
particular,weconsideraprocessknownasflipping.Creatingaflipimagerequiresthatwedecidewherethe
flipwilloccur.Forourpurposesinthissection,wewillassumethatflippinghappenswithrespecttoaline
thatwewillcalltheflipaxis.Thebasicideaistoplacepixelsthatappearononesideoftheflipaxisinanew
locationontheoppositesideofthataxis,keepingthedistancefromtheaxisthesame.
Asanexample,considerthesimpleimagewith16pixelsinFigure6.20andaflipaxisplacedverticallyatthe
centeroftheimage.Becauseweareflippingontheverticalaxis,eachrowwillmaintainitspositionrelative
toeveryotherrow.However,withineachrow,thepixelswillmovetotheoppositesideoftheaxis,asshown
bythearrows.Thefirstpixelwillbelastandthelastpixelwillbefirst.
Thestructureofthisfunctionissimilartothosewehavewrittenthusfar.Webuildournestediterationsuch
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 27/35
8/23/2016 Yuzu: Python Programming in Context

that the outer iteration will process the rows and the inner iteration will process each pixel within the row.
Listing 6.10 shows the completed function. Note that the new image is the same height and width as the
original.

Figure6.20Flippinganimageontheverticalaxis

Listing6.10Creatingtheverticalflipofanimage

Weneedtodiscoverapatterntomapeachpixelfromitsoriginallocationintoanewlocationwithrespectto
theflipaxis.ReferringtoFigure6.20wecanseethatthefollowingassociationsareneededinanimagethatis
4pixelswide:column0willmaptocolumn3,column1willmaptocolumn2,column2willmaptocolumn
1,andcolumn3willmaptocolumn0.Ingeneral,smallvaluesmaptolargevaluesandlargevaluesmapto
small.

Figure6.21Aflippedimage

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 28/35
8/23/2016 Yuzu: Python Programming in Context

Thefirstthingwemighttryistousethewidthandsimplysubtracttheoriginalcolumntogetthenewcolumn.
Ifwetrythiswithcolumn0,weimmediatelyseethatthereisaproblemsince40is4,whichisoutsidethe
range of valid column values. The cause of this error is that we start counting the columns (as well as the
rows)withzero.
Tofixthis,wecanbasethesubtractionontheactualmaximumpixelpositioninsteadofthewidth.Sincethe
pixelsinthisexamplearenamedwithcolumn0thoughcolumn3(widthof4),wecanuse3asourbasefor
thesubtraction.Inthiscase,thegeneralmappingequationwillbe(width1)column.Notethatwidth1isa
constant,whichmeansthatwecanperformthiscalculationjustonce,outsidetheloopaswedoonline7.
Since we are performing a flip using a vertical flip axis, the pixels stay in the same row. Line 11 uses the
calculationabovetoextracttheproperpixelfromtheoriginalimageandline13placesitinitsnewposition
inthenewimage.NotethatrowisusedinbothgetPixelandsetPixel.Figure6.21showstheresultingimage.

Exercises

6.27WritethefunctionhorizontalFliptoflipanimageonthehorizontalaxis.

6.28RewritetheverticalFlipfunctionsothatitflipsanimageinplace.

6.29Mirroringisamanipulationsimilartoflipping.Whenproducingamirror,thepixelsononesideofthe
mirror axis are reflected back on the other side. In a mirror operation half of the pixels are lost.
Implementamirrorontheverticalaxis.

6.30Implementamirroronthehorizontalaxis.

6.31Implementamirrorataspecificcolumnorrow.Note:Thisoperationwillchangetheimagesize.

6.32 Write a function rotateImage90 that takes an image as a parameter and rotates the image by 90
degrees.

6.33WriteafunctionrotateImage180thattakesanimageasaparameterandrotatesitby180degrees.

6.34Writeafunctionrotatethattakesanimageandanumberofdegreestorotatetheimage.Notethatthis
rotation may leave some empty pixels. You will also need to size the new image so it can hold the
entirerotatedimage.

6.5.4EdgeDetection
Ourfinalimageprocessingalgorithminthischapteriscallededgedetectionanimageprocessingtechnique
thattriestoextractfeatureinformationfromanimagebyfindingplacesintheimagethathaveverydramatic
changesincolorintensityvalues.Forexample,assumethatwehaveanimagecontainingtwoapples,onered
and one green, that are placed next to one another. The border between a block of red pixels from the red
appleandablockofgreenpixelsfromthegreenapplemightconstituteanedgerepresentingthedistinction
betweenthetwoobjects.
Asanotherexample,considerthegrayscaleimage(actuallyblackandwhite)showninFigure6.22.Intheleft
imagetherearethreeobjects:awhitesquare,acloud,andastar.Therightimageshowstheedgesthatexist.
Eachblackpixelintheedgeimagedenotesapointwherethereisadistinctdifferenceintheintensityofthe
originalgrayscalepixels.Findingtheseedgeshelpstodifferentiatebetweenanyfeaturesthatmayexistinthe
originalimage.
Edgedetectionhasbeenstudiedingreatdetail.Therearemanydifferentapproachesthatcanbeusedtofind
edgeswithinanimage.Inthissectionwedescribeoneoftheclassicalgorithmsforproducingtheedges.The

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 29/35
8/23/2016 Yuzu: Python Programming in Context

mathematicsusedtoderivethealgorithmarebeyondthescopeofthisbook.However,wecaneasilydevelop
theideasandtechniquesnecessarytoimplementthealgorithm.
Inordertofindanedge,itisnecessarytoevaluateeachpixelinrelationtothosethatappeararoundit.Since
wearelookingforplaceswhereintensityononesideofthepixelisgreatlydifferentfromtheintensityonthe
other, it will help us to simplify the pixel values. Our first step in discovering edges will be to convert the
image to grayscale. This means that the intensity of the pixel can be thought of as the common color
componentintensity.

Figure6.22Asimpleedgedetection

(Recallthatshadesofgrayaremadefrompixelswithequalquantitiesofred,green,andblue.)Eachpixelcan
thenbethoughttohaveoneof256intensityvalues.
Asameansoflookingfortheseintensitydifferences,weusetheideaofakernel,alsoknownasafilterora
mask.Thesekernelswillbeusedtoweighttheintensitiesofthesurroundingpixels.Forexample,considerthe
3 by 3 kernels shown in Figure6.23. These grids of integer weights are known as the Sobel operators,
namedafterIrwinSobelwhodevelopedthemforuseinedgedetection.
Theleftmask,labeledXMask,willbeusedtolookforintensitydifferencesinthelefttorightdirectionofthe
image. You can see that the leftmost column of values is negative and the rightmost column is positive.
Likewise,theYMaskwilllookforintensitydifferencesintheupanddowndirectionascanbeseenbythe
locationofthepositiveandnegativeweights.

Figure6.23Kernelmasksforconvolvingpixels

The kernels will be used during convolutiona process in which pixels in the original image will be
mathematicallycombinedwitheachmask.Theresultwillthenbeusedtodecidewhetherthatpixelrepresents
anedge.
Convolutionwillrequireamaskandaspecificpixel.Themaskwillbecenteredonthepixelofinterest,as
showninFigure6.24. Each weight value in the mask now associates with one of the nine pixel intensities
underthemask.Theconvolutionprocesswillsimplycomputethesumofnineproductswhereeachproduct
istheweightmultipliedbytheintensityoftheassociatedpixel.Ifyouhavealargeintensityontheleftside
and a small intensity on the right (indicating an edge), you will get a weighted sum with a large negative
value.Ifyouhaveasmallintensityontheleftandalargeintensityontheright,youwillgetalargepositive
weighted sum. Either way, a large absolute value of the weighted sum will indicate an edge. This same
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 30/35
8/23/2016 Yuzu: Python Programming in Context

argumentappliesforthetoptobottomsplitoftheYMask.
Toimplementconvolution,wewillfirstneedtoconsiderawaytostorethekernels.Sincekernelslookvery
similartoimages,rowsandcolumnsofweights,itmakessensetotakeadvantageofthisstructure.Wewill
use a list of lists implementation for the kernel. For example, the XMask discussed earlier will be
implemented as the list [ [1,0,1],[2,0,2], [1,0,1] ]. The outer list contains three items, each of which
representsarowinthekernel.Eachrowhasthreeitems,oneforeachcolumn.Similarly,theYMaskwillbe[
[1,2,1],[0,0,0],[1,2,1]].

Figure6.24UsingtheXMasktoconvolvethepixelatc,r

Accessingaspecificweightwithinakernelwillrequiretwoindexvalues,onefortheouterlistandoneforthe
innerlist.Sinceweareimplementingtheouterlisttobealistofthreerows,thefirstindexwillbetherow
value.Onceweselectarowlist,thesecondindexwillbeusedtogetthespecificcolumn.
For example, XMask[1][2] will access the item in XMask indexed by 1, which is the middle row of the
XMask. The 2 indexes the last item in the list that corresponds to the last column. This access is for the
weightstoredinthemiddlerowandlastcolumnofXMask.
Wecannowconstructtheconvolvefunction.Wesaidearlierthatthisprocessrequiresanimage,aspecific
pixelwithintheimage,andakernel.Thetrickypartofthisfunctionistoalignthekernelandtheunderlying
image. An easy way to do this is to think about a mapping. The kernel row indices will run from 0 to 2.
Likewiseforthecolumnindices.Forapixelintheimagewithindex(column,row),therowindicesforthe
underlyingpixelswillrunfromrow1torow+1andforthecolumnsitwillbecolumn1tocolumn+1.
Wewilldefinethebaseindextobethestartingindexforthe33gridofunderlyingimagepixels.Thebase
index for the columns will be column1 and the base index for the rows will be row1. As we process the
pixelsoftheimage,thedifferencebetweenthecurrentimagerowvalueandthebaseindexfortherowswill
beequaltotherowindexneededtoaccessthecorrectrowinthekernel.Likewise,wecandothesamething
forthecolumns.
Oncewehavecomputedtheindexintothekernelwecanuseittocomputetheproductoftheweightandthe
pixel intensity. We will first access the pixel and then extract the red component that will be its grayscale
intensity.Sincewehavealreadyconvertedtheimagetograyscale,wecanuseanyoneofthered,green,or
blue components for the intensity. Finally, that product can be added to a running sum of products for all
underlying pixels. The complete convolve function is shown in Listing 6.11. Note that the final step is to
returnthevalueofthesum.
Nowthatwecanperformtheconvolutionoperationforaspecificpixelwithakernel,wecancompletethe
edgedetectionalgorithm.Thestepsoftheprocessareasfollows:
1.Converttheoriginalimagetograyscale.
2.Createanemptyimagewiththesamedimensionastheoriginal.
https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 31/35
8/23/2016 Yuzu: Python Programming in Context

3.Processeachinnerpixeloftheoriginalimagebyperformingthefollowing:

(a)ConvolvethepixelwiththeXMaskcalltheresultgX.
(b)ConvolvethepixelwiththeYMaskcalltheresultgY.
(c)ComputethesquarerootofthesumofsquaresofgXandgYcalltheresultg.

Listing6.11Convolutionforaspecificpixel

(d)Basedonthevalueofg,assignthecorrespondingpixelinthenewimagetobeeitherblackor
white.

Listing6.12showsthePythoncodethatimplementsthestepsoutlinedpreviously.Webeginbyconvertingthe
originalimagetograyscaleusingthepixelMapper()functiondevelopedearlierinthechapter.Thiswillallow
forsimpleintensitylevelswithineachpixel.Wewillalsoneedanemptyimagethatisthesamesizeasthe
original.Itwillalsobeusefultodefineafewdataobjectsforuselater.Sinceeachpixelintheedgedetection
result will be either black or white, we will create black and white tuples that can be assigned later in the
process.Also,wewillneedthelistoflistsimplementationofthetwokernels.Theseinitializationsaredone
onlines38.
Nowitistimetoprocesstheoriginalpixelslookingforanedge.Sinceeachpixelisrequiredtohaveeight
surroundingpixelsfortheconvolutionoperation,wewillnotprocessthefirstandlastpixeloneachrowand
column.Thismeansthatournestediterationwillstartatone,notzero,anditwillcontinuethroughheight2
andwidth2asshownonlines1011.
Each pixel will now participate in the convolution process using both kernels. The resulting sums will be
squaredandsummedtogether,andinthefinalstepwewilltakethesquareroot(seelines1214).
Thevalueofthissquareroot,calledg,representsameasureofhowmuchdifferenceexistsbetweenthepixel
andthosearoundit.Thedecisionastowhetherthepixelshouldbelabeledasanedgeismadebycomparingg
toathresholdvalue.Itturnsoutthatwhenusingthesekernels,175isagoodthresholdvalueforconsidering
whetheryouhavefoundanedge.Usingsimpleselection,wewilljustchecktoseeifthevalueisgreaterthan
175.Ifitis,wewillcolorthepixelblackotherwisewewillmakeitwhite.Figure6.25showstheresultof
executingthisfunction.

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 32/35
8/23/2016 Yuzu: Python Programming in Context

Listing6.12Theedgedetectionmethod

Figure6.25Runningtheedgedetectionalgorithm

Exercises

6.35TryseveraldifferentthresholdvaluesinedgeDetect.Whateffectdoeschangingthethresholdhaveon
theimage?Does175workbestforallimages?Whatwouldbeawaytoautomaticallyselectagood
thresholdforanimage?

6.36 Modify the convolve function so that it applies the kernel to the red, green, and blue components
separatelyandreturnsatupleofvaluesasaresult.

6.37Convolutionhasmanyuses.Forexample,asimpleconvolutionkernelistheblurringkernel,which
lookslikethis:

In this case we simply apply the kernel and return the weighted average without doing any
thresholding.Writeablurfunctionthatusesthenewconvolvefunctiontobluranimage.

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 33/35
8/23/2016 Yuzu: Python Programming in Context

6.38Thesharpenkernellookslikethis:

Youcansharpenapixelbyemphasizingitsvalueanddeemphasizingthepixelsaroundit.Sharpening
istheoppositeofblurring.Usethesharpenkerneltosharpenanimage.

6.39 Write a general function that can take an image and a kernel and then return an image with the
convolutionkernelappliedtoeachpixel.

6.40Researchconvolutionkernelsandfindanewonetotry.

6.6Summary
InthischapterwefocusedoncImageanewmodulethatcontainsanumberofdatatypesthatcanbeusedto
manipulatedigitalimages.Inparticular,cImageincludesthefollowing:

ImageWin
EmptyImage
FileImage
Pixel
Toprocessthepixelsofanimage,weusedapatterncallednestediterationthatis,iterationinsideiteration.
Nestediterationallowedustoprocessallofthepixelsinagivenrow,columnbycolumn,beforemovingon
tothenextrow.Wealsointroducedthenotionofnamespacescollectionsofnamesavailableataparticular
point in time. These namespaces are organized to allow us to look up names when they are used, thereby
makingsurethattherearenoambiguities.Thischapterconcludedwithamoredetailedconsiderationofthe
mechanicsofparameterpassing.

KeyTerms
actualparameter
builtinnamespace
callbyassignment
convolution
digitalimage
edgedetection
flipaxis
flipimage
flipping
formalparameter
grayscale
imageprocessing
invocation
kernel
listoflists
localnamespace

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 34/35
8/23/2016 Yuzu: Python Programming in Context

mainnamespace
namespace
negative
nestediteration
parameterpassing
pixel
Pythagoreantheorem
resizing
RGBcolormodel
Sobeloperators

PythonKeywords
dir
EmptyImage
FileImage
for
ImageWin
import
math
Pixel
range
return

Bibliography

[Par96] J.R.Parker.AlgorithmsforImageProcessingandComputerVision.Wiley,1996.

https://reader.yuzu.com/#/books/9781284089042/c/6/26!/4/726@0:67.3 35/35
8/23/2016 Yuzu: Python Programming in Context

CHAPTER7
DataMining:ClusterAnalysis

7.1Objectives
TousePythonlistsasameansofstoringdata
Toimplementanontrivialdataminingapplication
Tounderstandandimplementclusteranalysis
Tousevisualizationasameansofdisplayingpatterns

7.2WhatIsDataMining?
We have already considered ways that statistical techniques can help us to process and summarize large
amounts of data. By computing statistical measures such as the range, mean, standard deviation, and
frequency,wecanbegintomakestatementsaboutourdata.Inthischapterwewillexplorethisideafurther.
Large amounts of data can be overwhelming. Consider once again the data set from Earthquakes, Floods,
and Other Natural Disasters (Chapter 5) that represents all of the earthquakes that took place around the
worldinatwoweekperiod.Aportionofthislargedatafileisagainshownhere:

2.82006/10/1902:02:1062.391149.75115.0CENTRALALASKA
2.52006/10/1900:31:1520.119156.2131.5MAUIREGION,HAWAII
5.02006/10/1821:15:514.82382.59237.3SOUTHOFPANAMA
2.62006/10/1821:12:2559.934147.90430.0GULFOFALASKA
3.42006/10/1820:59:2136.54089.6407.7SOUTHEASTERNMISSOURI
2.72006/10/1820:11:2261.023151.41860.0SOUTHERNALASKA
3.12006/10/1816:40:1520.282156.6114.7MAUIREGION,HAWAII
2.72006/10/1814:12:1959.808152.53850.0SOUTHERNALASKA
2.82006/10/1814:02:1260.686151.87190.0KENAIPENINSULA,ALASKA
4.92006/10/1812:10:011.758127.488127.0HALMAHERA,INDONESIA
6.22006/10/1810:45:3615.081167.243138.5VANUATU
2.82006/10/1810:45:1732.162115.8956.3BAJACALIFORNIA,MEXICO
3.32006/10/1810:08:4532.165115.8917.3BAJACALIFORNIA,MEXICO
2.82006/10/1808:22:2732.263115.2973.4BAJACALIFORNIA,MEXICO
3.72006/10/1805:34:1562.326151.22485.9CENTRALALASKA
4.62006/10/1803:25:0321.53866.593201.7POTOSI,BOLIVIA
3.72006/10/1802:32:2657.560137.1861.0OFFTHECOASTOFSOUTHEASTERNALASKA
4.92006/10/1802:01:271.35597.15725.8NIASREGION,INDONESIA
2.52006/10/1800:18:4219.801155.39110.6ISLANDOFHAWAII,HAWAII
3.12006/10/1722:59:0161.444150.52360.0SOUTHERNALASKA

https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 1/19
8/23/2016 Yuzu: Python Programming in Context

Recallthateachlineofdatarepresentsoneearthquake.Eachlineconsistsofsevenfields.Eachoftheseven
fields describes a significant attribute related to that earthquake. These attributes are, in order from left to
right,magnitude,date,timeofday,latitude,longitude,depth,andlocation.
Althoughitmaynotseempossible,itisverylikelythatthereareimportantpiecesofinformationhiddenaway
withinthisdatathatarenotobviousfromthesimpletypesofdescriptivestatisticsusedinANestofSnakes
(Chapter4).Thisiswherewecanusedataminingtheapplicationofautomatedtechniquesthatattemptto
discoverunderlyingpatterns.Thesetechniquescanbeappliedtoanynumberofdatadomains.Forexample,
inbusiness,dataminingisoftenusedformarketingpurposestofindpatternsexhibitedbyconsumers.Once
thesepatternsareidentified,theycanbeusedtorecommendtheproductsthatacustomermightpurchase.In
addition,therearemanyapplicationsinscienceandmedicinewherefindingpatternsinlargeamountsofdata
isrequired.
Clusteranalysis is a data mining technique that attempts to divide the data into meaningful groups called
clusters.Theseclustersrepresentdatavaluesthatshowsomekindofsimilaritytoeachotherwhileexhibiting
adissimilarrelationshiptodatavaluesoutsideofthecluster.
Inthischapterwefocusourattentiononclusteranalysisasawaytofindhiddeninformationinacollectionof
data.Ourgoalistoimplementoneversionofclusteranalysisalongwithtoolsthatallowustoseetheresults.

7.3ClusterAnalysis:ASimpleExample
Asasimpleexampleofwhatcanbelearnedthroughclusteranalysis,considerthedatashowninFigure7.1.
Thisdatarepresentstheperformanceofaclassofmathematicsstudents.Thexaxisgivesthehomeworktotal
forthesemesterasapercentageoftotalpointsavailabletheyaxisshowstheperformanceonexams,againas
apercentageoftotalpoints.
Atfirstglanceitmaynotbeapparentthattherearepatternswithinthedata.Infact,ifwewerelookingatthe
raw data scores and there were hundreds of students in the class, it is very unlikely that we would notice
anything.However,ifwerunthedatathroughaclusteranalysisroutine,wemaydiscoverthatsomeofthe
points tend to group together as shown in Figure 7.2. Our analysis technique has identified three clusters,
labeled A, B, and C. All of the data points in cluster A are said to have some underlying characteristic in
commonwitheachotherbasedonsomemeasureofsimilarity.ThesamecanbesaidforclustersBandC.The
points that appear in different clusters have characteristics that make them similar to each other but
distinguishthemfromotherclusters.
Itisimportanttonotethatsomeofthepointsdonotappearinanyofthethreeclusters.Thesepointshave
characteristicsthataredifferentfromthecharacteristicsthatdefinetheotherclusters.Inaddition,thesepoints
donothaveenoughsimilaritytoformtheirowncluster.Someclusteranalysistechniqueswillallowforthis
andsomewillrequirethatallpointseventuallybeplacedinacluster.
If we analyze our results further, we might be able to infer that these clusters are actually identifying
performancecluesforourstudents.Forexample,clusterAwouldseemtosuggestthatthereareanumberof
students who do well on both exams and homework. This could suggest a causal relationship and it is
supportedbyclusterBwherestudentsdidpoorlyonbothmeasures.ClusterCcouldleadustothinkthatthese
arethestudentswhohavetestanxietysincetheydidwellwiththehomeworkbutstillcouldnotperformon
theexams.

https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 2/19
8/23/2016 Yuzu: Python Programming in Context

Figure7.1Datarepresentingexamscoresandhomeworktotals

Figure7.2Possibleclusterswithinthedata

Inanycase,itisclearthattherearesomenaturalrelationshipsthatappearwithinourdataset.Findingthese
relationshipscanbecriticaltounderstandingunderlyingprocessesthatareinherentinthedata.Usingthese
clusterstodescribe,predict,orotherwisequantifyrelationshipswithindatacanbeaverypowerfultool.

7.4ImplementingClusterAnalysisonSimpleData
Wenowturnourattentiontodevelopingthefundamentalstepsoftheclusteranalysistechnique.Inorderto
concentrateonthealgorithmweuseaverysimpledataset.Later,wewillapplythesesameideastoourmore
complexearthquakedata.Ourexampledatainthissectionconsistsofexamscoresforagroupof21students
takinganintroductiontocomputersciencecourse.Thesescoresrepresentthepercentageofcorrectanswers.
Therangeofscoresisbetween0and100.
AsimplelistingofthescoresisshowninTable7.1.Ourinitialobservationdoesnotrevealanypatterns.We
could certainly use our previous work to compute descriptive statistics that might yield a bit more
information, but in this case we are interested in knowing if there are any patterns of similarity among the
studentscores.Clusteranalysiscouldhelpustoanswerthatquestion.

Table7.1Tableofscores

7.4.1DistanceBetweenTwoPoints
Oneofthemostimportantstepsintheclusteranalysisalgorithmistoclassifydatapointsastotheirsimilarity
to other data points. In order to measure this similarity, we need some way to suggest that two points are

https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 3/19
8/23/2016 Yuzu: Python Programming in Context

closetooneanother.Thiscanbedonebycomputingavaluethatwewillcallthedistancebetweentwo
datapoints.
There are many ways to measure the distance between two data points. For our purposes here, we use a
simplemeasureofdistanceknownasEuclideandistance.Considerthetwodatapoints,AandB,shownin
Figure7.3.IfweassumethatpointAhaslocationX1andpointBhaslocationX2,thenthedistancebetween
them,d,willbethesimpledifferencebetweenthetwolocationvaluesd=X2X1.However,sincewedonot
knowwhetherthisdifferencewillbepositiveornegative,theabsolutevalueshouldbeused.
An alternative way to eliminate negative values is to square them since the squaring operation will always
result in a positive number. To get the original value back, we can then take the square root. Using this
approachyieldstheequation

Althoughthismayseemlikeextrawork,thebenefitcanbeseenifweextendourexampletocomputingthe
distance between two points in more than one dimension. Figure 7.4 shows our two points. This time the
location of point A is given by the pair (X1, Y1). Likewise, point B has location (X2, Y2). To compute the
distance d between the two points now requires that we use the twodimensional version of the previous
equation,whichisalsoknownasthePythagoreantheorem:

Ingeneral,thiscanbeextendedtohandleanynumberofdimensions.Thedistancebetweentwodatapointsin
n dimensions, can be computed as the square root of the squares of the differences between each of the n
coordinates.Forexample,considerapointAinfivedimensionalspacewithlocation(23,44,12,76,34).If
pointBhaslocation(67,55,85,23,24),thedistancebetweenpointAandpointBisgivenby

Figure7.3Simpledistancebetweentwopoints

Figure7.4Twodimensionaldistancebetweentwopoints

Listing7.1showsafunctiontocomputethedistancebetweentwopoints.Eachpointisdefinedasalistofn
coordinatevalues.Startingwithsuminitializedto0,weiteratethroughthecoordinatelist.Ifweassumethat
each point has the same dimensionality, the index range for point1 will be the same as the index range for
point2.Line4computesthesquareofthedifferenceandline5addsthatvaluetotherunningtotal.Finally,
thesquarerootiscomputedandreturned.

https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 4/19
8/23/2016 Yuzu: Python Programming in Context

Listing7.1ComputingtheEuclideandistancebetweentwopoints

7.4.2ClustersandCentroids
Clustersofdatapointsexhibitthecharacteristicthatallofthepointsintheclusteraresimilartooneanother.
Anotherwaytothinkaboutthissimilarityistosuggestthatallofthedatapointsintheclusterareassociated
withsomenotionofacenterpoint.Thiscenterpointcanbeusedtoidentifyaparticularcluster.
A centroid is defined as the mean of a collection of data points. Each cluster will have a centroid that
representsthecenterofthecluster.Itisimportanttonotethatthecentroiddoesnotneedtobeanactualpoint
inthecluster.Itissimplythepointthattendstobeinthecenterofallothers.
Tocomputethecentroidforasetofpoints,wecalculatethemeanvalueforeachdimension.Forexample,if
wehavetwopoints,intwodimensions,(3,7)and(1,5),thecentroidwillbe or(2,6).This
canbeextendedtoincludeanynumberofpointswithanynumberofdimensions.
Figure7.5showsthreepoints,(2,4),(5,8),(8,6),eachwithtwodimensions.Thecentroid(5,6),shownby
thestar,representsthenotionofcenterwithrespecttothethreepoints.Ifthesepointswereconsideredtobea
cluster,wecouldusethecentroidasitsidentity.

7.4.3TheKMeansClusterAnalysisAlgorithm
Therearemanydifferentclusteringalgorithms.Wepresenthereoneoftheoldestandeasiesttounderstand
techniquestheKmeansalgorithm.Thebasicstepsofthissimpletodescribeandimplementalgorithmare
asfollows:

Figure7.5Thecentroidofthreepointsintwodimensions

1.Decidehowmanyclustersyouwouldliketocreatecallthatnumberk.
2.Randomlychoosekofthedatapointstoserveastheinitialcentroidsforthekclusters.
3.Repeatthefollowingsteps:

https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 5/19
8/23/2016 Yuzu: Python Programming in Context

(a)Assigneachdatapointtoaclustercorrespondingtothecentroiditisclosestto.
(b)Recomputethecentroidsforeachofthekclusters.
4.Showtheclusters.

ThenumberofiterationsperformedinStep3canvary.Sometimes,asimplemaximumiterationvalueisused.
Alternatively,thestepcanberepeateduntiltheclustersbecomestable.Clustersareconsideredtobestable
whenthecentroidsnolongerchangefromiterationtoiteration.Undersomecircumstances,thismaynever
occurasthepointswilltendtooscillatebackandforthbetweendifferentclusters.
Weshouldalsonotethatchoosingtheinitialcentroidsrandomlycanleadtoemptyclustersandresultsthat
differfromruntorun.WewillmentionmoreaboutsomeoftheshortcomingsinSection7.6.

7.4.4ImplementationofKMeans
Wenowimplementtheclusteranalysisalgorithmontheexamdatasetandassumethatthedataisstoredina
filenamedcs150exams.txtwithonescoreperline.Thereisnoorderoradditionalidentifyinginformationin
thedatafile.Weneedtoprocessthefilelinebylineandextracttheexamscores.
Sinceweneedtokeeptheindividualscoresseparate,weassignanidentificationkeytoeachscorestarting
with1andproceedingthroughn,wherenisthenumberofscores.Inotherwords,wecanidentifyeachscore
by using the line number from the file. In this way we can then associate the key with any additional
informationthatmaybepresentinthedatafile.(Thiswillbeveryusefulinlaterexamples.)
Listing 7.2 shows a function that takes a file name as a parameter and returns the dictionary as described
above.Wefirstopenthefile,createanemptydictionary,andthenstartaccessingeachlineofthefile.Since
theonlydataonthelineisthetestscore,wecansimplyusetheintfunctiontoconvertittoaninteger.Line10
entersthescoreinthedictionaryassociatedwiththekey.Itisimportanttonotethatthescoreisplacedina
listbeforeitisaddedtothedictionary.Recallthateachdatapointcanbemultidimensional.Eventhoughitis
notthecasehere,theeuclidDfunctionwillexpectalldatapointstobelistsofvalues.

Listing7.2Processingtheexamscoredatafile

Thenextstepistochoosethenumberofclusters,referredtoaskonthepreviouspage.Oftenthenumberof
clustersmaybebasedonsomepreconceivednotionaboutthedatavalues.Inthiscase,wecreatefiveclusters,
assumingthatonepossiblereasonforclusteringthedataistolookforgroupingsthatrepresentoneoffive
gradingcategories,sayA,B,C,D,orF.
Nowweneedtopickkrandomdatavaluestobeourinitialcentroids.Toimplementthis,weusetherandint
function from the random module. This random number generator will pick an integer in the range [a, b]
includingtheendpoints.Forexample,randint(2,5)willreturnarandomintegerbetween2and5inclusive.
Itisimportantthatwechoosekrandomvaluesthatareunique.Ifweweretosimplyiteratektimes,thereis
https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 6/19
8/23/2016 Yuzu: Python Programming in Context

noguaranteethatthekvalueswillnotcontainaduplicate.Inordertosatisfythisrequirement,weneedtouse
awhileloop.

IndefiniteIteration

Mostoftheiterationperformeduptothispointhasusedtheforloop.Recallthattheforloopallowsagroup
ofstatementstoberepeated,onceforeachvalueinasequence.Forexample,inthePythonstatementtheprint
function,whichmakesupthebody,willbeexecuted(repeated)fivetimes.Eachtimethroughtheloop,the
loopvariablenumwilltakeonasubsequentvaluefromthelist.Thetotalnumberofiterationsisstrictlygiven
bythenumberofitemsinthesequenceprovided.

fornumin[1,2,3,4,5]:
print("hello")

Thistypeofiterationisoftenreferredtoasdefiniteiterationsincethenumberofrepetitionsis(definitely)
known based on the size of the iteration sequence. Whenever we know the number of times we want to
iterate,theforloopistheappropriateconstructtouse.However,inmanycases,wedonotknowhowmany
iterations will be necessary. We know that we want to repeat a process, and we know that we want to
eventuallystoptherepetition,buttheactualnumberofrepetitionsisuncertain.Inthesecases,weneedtoturn
toaformofiterationknownasindefiniteiteration,aprocesswherewestateaconditionthatwillbeusedto
decidewhethertocontinuewiththeiterationprocess.Iftheconditionissatisfied,wewillperformtheprocess
again and then recheck the condition. When the condition no longer holds, the process will no longer be
repeated.

Thewhileloop

InordertoimplementindefiniteiterationinPython,weusethewhileloop,whichissimilartotheforloop
and the if statement in that it will control a body of statements (see Listing 7.3). As with other structured
statements,thestatementsinthebodywillbeindented.

Listing7.3Thewhilelooptemplate

Figure7.6 shows the logical flow of control created by the while loop. The condition can be any Boolean
expressionthat is, any expression that evaluates to True or False. The statements in the body will be
executed repeatedly until the condition evaluates to False. It is important to note that if the Boolean
expressionisFalseinitially,thestatementswillneverbeexecuted.Inotherwords,thestatementsinthebody
willbeexecutedzeroormoretimes,dependingonthevalueofthecondition.
As an example, consider the familiar code fragment shown in Listing 7.4. Here we are using a for loop to
computethesumofthefirst10integers.Line1initializesanaccumulatorvariable,total,thatwillbeusedto
keeptherunningsum.Theforloop(line2)willautomaticallyiteratethroughthesequenceproducedbythe
rangefunction,[1,2,3,4,5,6,7,8,9,10],assigningeachsubsequentvaluetotheloopvariableanum.The
body of the iteration (line 3) adds the value of anum to the accumulator. Finally, the value of the total is
printed.

https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 7/19
8/23/2016 Yuzu: Python Programming in Context

Figure7.6Thewhileloop

Listing7.4Summationfrom1to10usingaforloop

Wecanrewritethesummationfragmentusingawhileloop,asshowninListing7.5.Thealgorithmremains
thesame.Wewilliterateoverthevaluesfrom1to10andaddeachtoanaccumulatorvariablecalledtotal.As
before, line 1 initializes the accumulator variable and line 4 performs the addition to compute the running
sum.Lines2,3,and5takentogetherformtheiterationcontrol.
It is important to note that the iteration process is not simply the while : but includes a number of other
statementsaswell.Inordertowriteacorrectiterativeprocessusingawhileloop,itisnecessarytoinclude
threeparts:theinitialization,thecondition,andthechangeofstate.Allofthesepartsmustworktogether
fortheiterationtosucceed.

Listing7.5Summationfrom1to10usingawhileloop

Line3providestheconditionuponwhichtheiterationwillcontinue.Inthisexample,aslongasthevalueof
thevariableanumislessthanorequalto10,thebodystatementswillbeexecuted.Inordertoevaluatethis
condition, it is necessary to provide an initial value for anum. Recall that the while loop evaluates the
conditionfirst,beforeperformingthebody.Anyvariablesbeingusedintheconditionmusthavetheirinitial
valuespriortoenteringthewhileloop.Sincethegoalofthisiterationistocountfrom1to10,itmakessense
tostartthevalueofanumat1.
Iftheconditionsucceeds,controlproceedstothebody.Aftercompletionofthebody,theconditionwillbe
rechecked.Theonlywayfortheiterationtostopisfortheconditiontofail.Thisrequiresthatachangeoccur

https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 8/19
8/23/2016 Yuzu: Python Programming in Context

insidethebodythatwilldirectlyimpacttheresultofthecondition.Thischangeofstateisacriticalpartofthe
iteration. Without it, the condition that was True originally would remain True and therefore the while
statementwouldneverstop.Inthisexample,line5providesthischangeofstate.Eachtimethroughthebody,
the value of anum is incremented by 1. Eventually, the value of anum will reach 11, which will cause the
conditiontofail.
It is common for beginning programmers to forget one of these three parts or to include them but have
inconsistencies. For example, consider Listing 7.6. Here, the initialization and condition are correct but the
changeofstateismissing.ThismeansthatoncetheconditionisfoundtobeTrue,nothingexistsinthebody
tochangetheconditiontoFalse.Theresultwillbeawhileloopthatneverstops.Thisisknownasaninfinite
loop.Theresultwillbeawhileloopthatneverstops.

Listing7.6Infiniteloop

Listing 7.7 shows a function that will process the data file using an indefinite iteration. Line 7 uses the
readlinemethodtogetthenextlineofthefile.Thismethodwillreturntheemptystring,"",whenthereareno
morelinesinthefile.Theconditionwillcheckfortheemptystringandallowexecutionofthebodyonlyif
therewasalineofdatapresentinthefile.
Afterthelinehasbeenprocessed,itisnecessarytorecheckthecondition.Beforethiscanhappen,achangeof
statemustoccur.Forthisexample,thechangeofstateistoreadthenextlineofdatafromthefile.Eithera
validlineortheemptystringwillbereturnedandtheconditioncanbereevaluated.

Listing7.7Processingtheexamscoredatafileusingindefiniteiteration

Exercises

7.1Writeafunctionthatprintsthenumbersfrom0to50countingby5.Thisfunctionmustuseawhile
loop.

https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 9/19
8/23/2016 Yuzu: Python Programming in Context

7.2Writeafunctionthattakesastringasaparameterandreturnsthenumberofspacesinthestring.This
functionmustuseawhileloop.

7.3Writeafunctionthataskstheusertoenterexamscoresoneatatimeuntilthewordstopisentered.
Whenstopisentered,theprogramshouldcomputetheaverageofthescores.

7.4WriteafunctionthattakesastringasaparameterandreturnsTrueifthestringisapalindrome,and
Falseotherwise.Thisfunctionshoulduseawhileloop.Hint:Apalindromeisawordthatisspelled
thesameforwardandbackward.

7.4.5ImplementationofKMeansContinued
ThefunctionshowninListing7.8implementsthecentroidselectionprocess.Ittakesthenumberofcentroids
(calledk)andthedatadictionaryasparameters.Line2createsanemptylistofcentroids.Ourtaskistofill
thislistwithkrandomlyselecteddatapoints.Sincewedonotwanttouseaspecifickeytwice,wewillstorea
listoftheselectedkeysandcheckeachrandomlyselectedkeyagainstthislist.Ifthekeyisalreadyinthelist,
we do not include its data point as a centroid and instead continue on to pick another key randomly. This
process will continue until k centroids have been selected. Since we do not know how many random
selectionswillberequired,awhileloopisused.

Listing7.8Choosingkrandomcentroids

We keep track of the number of valid centroids that have been found with centroidCount. As long as that
numberislessthank,wecontinuetogeneraterandomkeys.Ifanunusedkeyisfound,itisaddedtothelistin
line10andthecountisincremented(line11).
Listing7.9implementsafunctiontoactuallycreatetheclusters.createClusterstakesthenumberofclusters
(again called k), the previously created centroids, the data dictionary, and the number of repetitions as
parameters and returns a list of the clusters. Each cluster will be represented by a list. Since we have a
collectionofclusters,alistofthoseclusterlistswillbetheappropriatewaytostorethem.Lines46create
thislistofkemptyclusters.

https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 10/19
8/23/2016 Yuzu: Python Programming in Context

Listing7.9Creatingtheclusters

Wenowgothrougheachiteminourdatadictionaryandassignittothepropercluster.Recallthatwehavea
list of centroids, one for each cluster. We want to assign each data point to the cluster with the closest
centroid.Wecandothisbyusingourdistancefunction,euclidD,describedearlier.
Since there are k clusters, each data point will have k distances associated with it, one from each centroid.
Line9createsanemptylistofdistances,andlines1012computethedistancebetweenthedatapointand
eachcentroid.Thesedistancesareplacedinthedistancelist.Thedistances,centroids,andclusterslistsare
parallel to each other. This means that for any particular index number i, i will refer to data for the same
clusterinbothlists.Figure7.7showsthethreeparallellistsandtherelationshipbetweenthem.
Oncewehaveallofthedistancescomputed,wecanlookfortheclosestcentroid.RecallthatPythonprovides
afunctioncalledminthatreturnsthesmallestvalueinalist(line14).Onceweknowthesmallestvalue,we
canusethelistmethodindextofindwheretheminimumoccurredinthedistancelist(line15).Theindexof
theminimumtellsuswhichclusterthedatapointshouldbelongto.Byusingthatindex,wecanaccessthelist
ofclustersandappendthekeytothepropercluster(line17).Again,itisimportanttorememberthatweare
storingthekeysinsteadoftheactualdatapoints.

https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 11/19
8/23/2016 Yuzu: Python Programming in Context

Figure7.7Parallellists

ThefinalstepintheKmeansalgorithmrequiresthatwerecomputethecentroidsforeachcluster.Sincethe
centroidofaclusterissimplytheaverageofalldatapointsinthecluster,wecaniteratethroughthepoints,
createarunningsum,andthendividebythenumberofpoints.
Lines 1920 implement the centroid recalculation. Line 19 is important since our data points can be
multidimensional. dimensions is the number of dimensions within the data point. For our exam score
example,thiswillbeone.Recallthattocomputethenewcentroidwemusttaketheaverageofthecoordinate
valueineachdimension.
Alistofrunningsums,representedbysumswillincludeasumforeachdimensionofthedatapoint.Each
sumcomponentisinitializedto0.Lines2425calculatetherunningsumsofthecomponentsandlines2629
computetheaverage.Thelaststatement(line31)assignstheaveragetotheproperpositioninthecentroids
list.
Recallthattheclusteringprocessisrepeatedanumberoftimes.Inthisfunction,theparameterrepeatswill
allowtheusertodecidehowmanyiterationstoperform.Listing7.9alsoincludesasmallfragmentofcode
(lines3337)thatprintsoutthecontentsoftheclustersaftereachpass.Finally,Listing7.10showsafunction
thatwillperformtheclusteranalysisonthecs150exams.txtdatafile.Itwillmakethreepassesthroughthe
datainordertoproducefiveclusters.

Listing7.10Clusteranalysisfortheexamdataset

Whenweruntheprogram,theoutputinSession7.1isproduced.Inthefirstpass(pass0),theexamscoresare
spreadoverthefiveclusters.Pass1showsthatsomeofthescoreshavemovedduetothenewcentroidvalues
thatwerecomputedafterpass0.Finally,thelastpassshowsafinalmodificationoftheclusters.Ifwewere
using this analysis to assign grades, we might suggest that the first cluster would be the As, the second
would be the Fs, and so on. It is important to note that running the program again could give different
https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 12/19
8/23/2016 Yuzu: Python Programming in Context

resultsduetotherandomselectionofinitialcentroids.

****PASS0****
CLUSTER
[98][98][90][89]
CLUSTER
[34][12][44][45][25][34][12][45][22][11]
CLUSTER
[56][76][76][77]
CLUSTER
[87]
CLUSTER
[78][78]
****PASS1****
CLUSTER
[98][98]
CLUSTER
[34][12][44][45][25][34][12][45][22][11]
CLUSTER
[56]
CLUSTER
[87][90][89]
CLUSTER
[76][76][78][78][77]
****PASS2****
CLUSTER
[98][98]
CLUSTER
[34][12][25][34][12][22][11]
CLUSTER
[56][44][45][45]
CLUSTER
[87][90][89]
CLUSTER
[76][76][78][78][77]

Session7.1Clustersofexamscores

Exercises

7.5 Load and run the clusterAnalysis function using the exam score data. Compare your clusters with
thoseshowninSession7.1.

7.6RunclusterAnalysisagainbuttrytousedifferentnumbersofclustersandpasses.

7.7ModifycreateClusterssothattheouterloopusesindefiniteiteration.Theloopshouldexitwhenthe
clustersnolongerchange.

7.8 The previous exercise could create an infinite loop if the clusters are oscillating. Add another
conditiontothelooptomakesurethatnomorethanmaxRepeatsnumberofiterationsoccur.

7.9Implementadifferentdistancemethodanduseittoclustertheexamdata.Doyouseeanychangesin
https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 13/19
8/23/2016 Yuzu: Python Programming in Context

theclusters?

7.5ImplementingClusterAnalysis:Earthquakes
We started this chapter by considering real data describing earthquakes that occurred during a twoweek
period.Giventherawdata,itmightbedifficulttoseeanytypeofpatternorsimilarity.However,ifweextend
ourclusteranalysistechniquefromtheprevioussection,wemightdiscoversomeinterestingresults.

7.5.1FileProcessing
Ourfirstproblemwillbetofindawaytoprocessandstorethedatacontainedinthedatafilesothatwecan
useitinourclusteringalgorithm.Recallthateachlineofthefilelookslikethefollowing:

3.72006/10/1805:34:1562.326151.22485.9CENTRALALASKA

Thisentrydescribesanearthquakeofmagnitude3.7thatoccurredatadepthof85.9kilometersinthegeneral
regionofCentralAlaska.Theexactlongitudeandlatitudeaswellastimearealsoprovided.
Forthisexamplewedeployourclusteranalysisalgorithmusingthelocationdata.Inotherwords,wewould
liketoseeifthereareclustersofearthquakesthatoccurincloseproximitytooneanother.Todothis,weneed
tounderstandhowthelocationdataisstoredinthefileandwhatitmeans.

Figure7.8Longitudeandlatitudemeasurementsontheglobe

Each earthquake is described by providing its exact location as a pair of values called the longitude and
latitude. Figure 7.8 shows how these values map to locations on the globe. The latitude values run north
southwithzerolatitudelocatedattheequator.Thenorthpoleoftheglobeis+90andthesouthpoleis90.
Likewise,thelongitudevaluesrunwesteast,withthezerobeingtheprimemeridian,animaginarylinethat
runsnorthsouththroughGreenwich,England.Forlatitudesinthefarwestthemeasurementis180andthe
fareastmeasurementis+180sincetheglobeisassumedtobeafull360degreecircleontheequator.
Notethatourdatapointsnowhavetwodimensionsnamely,alatitudeandalongitude.Recallhoweverthat
ourpreviousdescriptionofthealgorithmwillstillworksincewewenttothetroubleofmakingtheeuclidD
functionworkwithmultidimensionaldatapoints.
Wecannowworkonextractingthenecessarydatafromthefile.Lookingattheexamplelineagainshowsus
thattherearesevendistinctpiecesofdataforeachearthquake.Also,afterlookingcloser,wecanseethateach
item is separated by a blank with the exception of the name, which can have many words. We can easily
extract the data values we want by splitting the input line into a list of items and then index the items
https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 14/19
8/23/2016 Yuzu: Python Programming in Context

appropriately,asshowninSession7.2.

>>>aline
'3.72006/10/1805:34:1562.326151.22485.9CENTRALALASKA'
>>>items=aline.split()
>>>items
['3.7','2006/10/18','05:34:15','62.326','151.224','85.9',
'CENTRAL','ALASKA']
>>>items[3]
'62.326'
>>>items[4]
'151.224'
>>>items[6:]
['CENTRAL','ALASKA']
>>>

Session7.2Fileprocessingforearthquakedata

We can build the Python code using the framework from Session 7.2. After opening the file, we iterate
through the lines, split each one, and then extract the data. Listing 7.11 shows a new implementation of
readFile,whichcreatesadatadictionaryoftwodimensionaldatapoints.

Listing7.11Processingtheearthquakedatafile

Wearestillusingthekeyasawaytoreferuniquelytoeachlineofthefile.Inthiscase,thedatadictdictionary
associatesthekeywiththelongitudelatitudedatapoint.Lines910extractthelongitudeandlatitudedata.
Notethatweneedtoconvertthesevaluesintofloatingpointnumbers.
Once we have our data points, we can use our clusterAnalysis function as follows:
clusterAnalysis("earthquakes.txt").Rememberthatweneedtomakedecisionsabouthowmanyclusterswe
wanttocreateandhowmanyiterationsshouldbeused.Unfortunately,whenwerunourprogram,theoutput,
afragmentofwhichisshownbelow,isdifficulttounderstand.Thereasonforthisisthatoursimpleoutput
mechanismdisplaysthecontentsofeachclusterbyshowingthedatapoints.

CLUSTER
[177.81800000000001,30.312999999999999]
[178.768,20.655999999999999]
https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 15/19
8/23/2016 Yuzu: Python Programming in Context

[177.83099999999999,20.097999999999999]
[176.92599999999999,22.847000000000001]
[121.491,56.020000000000003]
[176.048,19.716000000000001]
[175.98599999999999,23.420000000000002]
[175.851,23.574999999999999]
[175.869,23.436]
[176.03999999999999,23.495000000000001]

7.5.2Visualization
There is one more modification that will make our results much more interesting. Instead of printing the
longitudesandlatitudesinlonglists,wewillusevisualizationtoplotthepositionsoftheearthquakesona
mapoftheworldandshowtheclustersaspointsonthatmap.Thisprocessofvisualizingthedatacanbe
quiteuseful,especiallyifwearelookingforhardtoseerelationshipsthatmaynotbereadilyapparentfrom
longlistsofdata.Itiscommontotrytofindsomewaytovisualizetheclusters.
Asanexampleofwhatwearedescribing,considerFigure7.9,wheretheearthquakedatahasbeenprocessed
using six clusters. Each earthquake is shown as a point on the map. In addition, the clusters are colored to
distinguish one from the other. It can be readily seen that there are definite relationships as to where these
earthquakesaretakingplace(althoughyoulikelyknewthisalready).
Wecaneasilybuildthisvisualizationbyusingtheturtlemodule.Thebasicideaistousetheturtletoplota
coloreddotateachearthquakelocationgivenbythelongitudeandlatitude.Thecolorwillbedependenton
theclustertowhichtheearthquakebelongs.Thechallengingpartissettingupthedrawingwindowsothatthe
mapandthecoordinatesareconsistentwithourlongitudeandlatitudevalues.

Figure7.9Plottingearthquakestoshowclusters.CourtesyofScientificVisualizationStudio/NASA.

Listing7.12showsafunctiontogenerateclustersandvisualizethem.Tostart,wemakeuseofthereadFile,
createCentroids,andcreateClustersfunctionsaswedidpreviously.Oncetheclustershavebeencomputedwe
createaTurtlecalledquakeTusingtheturtleconstructor.LookingagainatFigure7.9,youcanseethatthe
backgroundoftheturtledrawingwindowcontainsanimageoftheworld.Thisimage,storedinafilenamed
worldmap.gif,is448pixelswideby266pixelshigh.Byusingthebgpicmethod(line8),wecansettheimage
tobethebackgroundpictureforthedrawingwindow.Sincewewantthedrawingwindowtoincludeonlythe
areaofthemap,wecanthenusethescreensizemethod(line9)toresetthewidthandheightofthedrawing
window.
From our previous discussion about longitude and latitude, we know that the lowerleft corner of the map
https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 16/19
8/23/2016 Yuzu: Python Programming in Context

shouldbelocation(180,90)andtheupperrightcornershouldbe(180,90).Wecanremapourplotting
byrealizingthatthecurrentlowerleftcorneris(224,133)andtheupperrightcorneris(224,133).That
meanswesimplyneedtocomputemultiplicationfactorsforthewidthandtheheight(wFactorandhFactorin
lines11and12)andusethemwhenweplotthelongitudeandlatitude(line24).
Thenextlinesturnoffthelocationalmarkerfortheturtleandraisethetailsothatlinesarenotdrawnasthe
turtleismovedfromlocationtolocation.Finally,line17createsalistofcolorsthatwillbeusedtodistinguish
eachclusterredforthefirstcluster,greenforthesecond,andsoon.
We can now show the contents of each cluster by iterating through the clusters and processing each
earthquakeinthecluster.Line20setsthetailcolorbyusingclusterIndexasanindexintothecolorlist.For
eachearthquakeinthecluster,weextractthelongitudeandlatitudedatafromthedatadictandusethosetwo
valuesascoordinatesfortheturtle.Oncetheturtlehasbeendirectedtotheproperlocation,thedotmethod
willplotapointusingthecurrenttailcolor.Itshouldbenotedthatthenumberofcolorsinthecolorlistmust
beatleastasbigasthenumberofclustersbeingcreatedsothateachclustergetsauniquecolor.

Listing7.12Visualizingearthquakeclusters

Exercises

7.10LoadandrunthecodeforthevisualizeQuakesfunction.Howdoyourresultscomparewiththosein
thebook?

7.11Trytochangethenumberofclusters.Besuretoaddmorecolorsifyouaddmoreclusters.

7.12Gotohttp://earthquake.usgs.govandcopythelatest7daysofearthquakedata.Youmayhavetoedit
whatyoucopyslightlyinordertorunvisualizeQuakesorchangethereadDatafunctiontomatch.

7.13Usingtheearthquakedata,trytoclusterthequakesbytheirdepthinsteadoflatitudeandlongitude.
https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 17/19
8/23/2016 Yuzu: Python Programming in Context

Nowvisualizethedataonthemapusingthisnewclustering.

7.6ClusterAnalysisShortcomingsandSolutions
The basic Kmeans cluster analysis algorithm is easy to implement. However, there can be a number of
problemsthatoccur.Webrieflydescribeafewofthesehereandleavethesolutionsforyouasexercises.
Step2ofouralgorithmrequiredthatwepickkdatapointstoserveastheinitialcentroids.Oursolutiontothis
was to use a random selection. This meant that two runs of the program could produce different results. It
seems likely that by choosing the centroids in a more intentional manner we can impact the way that the
clustersareultimatelyconstructed.Thiscouldbebasedeitheronuserinputorondataanalysis.
Itispossiblethatclusterscanbecomeemptyastheiterationprocesscontinues.Inourimplementation,oncea
cluster becomes empty there is no way for it to be repopulated since it no longer has a centroid. When a
clusterbecomesempty,somemethodmightbeemployedtocreateanewcentroidsothatdatapointscanbe
added in the next iteration. Of course, it is always possible to leave the cluster empty and produce fewer
clustersthanoriginallyspecifiedaswouldhappeninourimplementation.
Sometimesaclustercangettoolargeorcanencompassdatapointsthatareseeminglynotrelated.Thiscan
happenwhentherearedatapointsinthedatasetthatareclearlydifferentfromtherest(sometimesreferredto
asoutliers).Whenanoutlierisfound,itmaybepossibletoprovidesomespecialprocessingsoastocreatean
additionalcluster,ortoexcludeitfromanyclustertherebynullifyingtheimpacttheoutliermighthaveonthe
centroidcalculations.

Exercises

7.14Implementasolutiontotheemptyclusterproblem.

7.15Implementasolutionforduplicatedatavalues.Thiscouldhappenifyouhavetwoexamscoresthat
areidenticalbutbelongtodifferentpeople.

7.16Implementanalternativetorandomcentroidselectionwheretheuserhassomesayintheprocess.

7.17Implementanalternativetorandomcentroidselectionwheresomesimpledataanalysisisdonetotry
andpickthebestcentroidvalues.

7.18Implementafunctiontofindandeliminateoutliersinthedataset.

7.7Summary
In this chapter we implemented the Kmeans algorithma simple and easy to implement cluster analysis
technique that is vulnerable to problems in some cases. It can be applied to a wide variety of application
domainsandperformsinafairlyefficientmanner.Theredoexistothertechniquesforclusteranalysis,andwe
urgeyoutoconsidersomeofthemforcomparisonpurposes.
Aspartoftheimplementation,werevisitedthenotionofiterationandpresentedamuchmoredetailedview
of the while statement. We utilized listsmore specifically parallel listsand dictionaries as a means of
organizingourdata.Finally,wecreatedavisualizationforourclusteranalysisresults.

KeyTerms
centroid
https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 18/19
8/23/2016 Yuzu: Python Programming in Context

changeofstate
clusteranalysis
clusters
condition
datamining
definiteiteration
Euclidiandistance
indefiniteiteration
infiniteloop
initialization
Kmeansalgorithm
visualization

PythonKeywords
for
open
range
readline
while

Bibliography

[qua] U.S.GeologicalSurvey.2008.Retrievedfrom:http://earthquake.usgs.gov/.
[TSK06] PangNing Tan, Michael Steinbach, and Vipin Kumar. Introduction to Data Mining. Addison
Wesley,2006.

https://reader.yuzu.com/#/books/9781284089042/c/6/28!/4/444@0:34.6 19/19
8/23/2016 Yuzu: Python Programming in Context

CHAPTER8
Cryptanalysis

8.1Objectives
TounderstandmoreadvancedexamplesofusingadictionaryinPython
TounderstandmoreadvancedexamplesofusinglistsinPython
Tousepatternmatchingwithregularexpressions
Tolearnhowsimpleprogramscanhelpyousolvemoreadvancedproblems

8.2Introduction
InCodesandOtherSecrets(Chapter3)westudiedseveralalgorithmsforencryptingsecretmessages.One
ofthemostsecurealgorithmsforencryptiontodayiscalledRSA,namedaftertheauthorsRonRivest,Adi
Shamir, and Leonard Adleman. RSA is used by your web browser when you make a secure connection to
your bank and when you shop online at your favorite store. Obviously, any encryption technique that is
widelyusedforsuchsensitivedatawillbethetargetofmanyattacks.IfsomeonelearnedhowtobreakRSA,
theycouldeasilystealmanybankaccountnumbersandpasswords.
In this chapter we look at the topic of cryptanalysisthe field of code breaking. Of course, the study of
breaking codes is as old as the field of making codes. In fact, some of the first computer scientists were
deeply involved in cryptanalysis. Charles Babbage, the designer of the difference engine, was working on
breakingtheVignrecipherwecodedinCodesandOtherSecrets(Chapter3).AlanTuring,creatorofthe
TuringTestforartificialintelligenceandtheTuringMachine,isresponsibleforbreakingtheGermanEnigma
cipherduringWorldWarII.
Breakingadvancedcodesbybruteforceisaninterestingexerciseforpeoplewithaccesstosupercomputersor
whowanttoinvestigatedistributedcomputing.TherailfencecipherintroducedinCodesandOtherSecrets
(Chapter3)canbeeasilybrokenusingabruteforceapproachonanycomputer.Bruteforcesimplymeansto
try all possibilities. However, many beginning computer science students are reluctant to try a brute force
approachtoanything,convincingthemselvesthattheremustbeabetterway.Infact,manytimesabrute
forcesolutionisappropriatesimplybecauseittakesadvantageofthecomputersabilitytomakemillionsof
calculationsveryquickly.
As a problemsolving challenge, we will see that there are more interesting ways to attack the codes we
developedinCodesandOtherSecrets(Chapter3).Inparticular,wewillseeinthischaptersomeexamples
of short Python programs that can enhance our human abilities for problem solving. Just as we use a
calculatortosolvecomplexmathproblems,shortPythonprogramscanhelpussolveothercomplexproblems.
We start this chapter with a simple brute force attack on the rail fence cipher. Next we show how to use
frequencyanalysistocrackthesubstitutioncipher.Finally,weseethatfrequencyanalysiscanalsobeusedto
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 1/29
8/23/2016 Yuzu: Python Programming in Context

cracktheVignrecipher.

8.3CrackingtheRailFence
One of the programming problems in Codes and Other Secrets (Chapter 3) was to write a generalized
versionofthetranspositioncipherwedescribedinSection3.4,calledtherailfencecipher.Inthissectionwe
willlookatabruteforcetechniquetobreakthiscipher.Letsbeginbyreviewingtheprocedurefortherail
fence.
Thekeytotherailfencecipheristhenumberofrailsused.Whenyouknowthenumberofrails,youencrypt
themessagebyfillingtherailstoptobottomandlefttoright.Forexample,letssupposewewanttoencrypt
themessagenewipodcomingtomorrowusingthreerails.

Oncewehaveconstructedtherails,wecreateanewstringforeachrailbyconcatenatingthelettersintherail
from left to right, then we concatenate the rails from top to bottom. This gives us the message n oci
mreidontoowpmgorw.

Cryptanalystsinterceptingthismessagemayknowthatthemessagewasencryptedusingtherailfencecipher,
but they will not know how many rails were used. A brute force way to decrypt this message is to try
decryptingthemessageusing2rails,3rails,4railsandsoonuntilwefindamessagethatmakessense.

AssumingthatwehavewrittenthefunctionrailDecrypt(seesection8.3.3),wemightuseasimpleforloopto
tryallpossibledifferentnumbersofrails.Session8.1showsourfirstattemptatabruteforcedecryption.

foriinrange(1,len(cipherText)+1):
print(railDecrypt(cipherText,i))
['n','oci','mreidontoowp','mgorw']
['nn','toocoiw','pm','rmegiodrow']
['new','ipod','coming','tomorrow']
['nmn','rtmoeogciooidwr','opw']
['nienw','itpomdo','croom']
['nienwg','itpoomdo','rcroomw']
['ncmino','irdtwmo','eoopg']
['ncmino','o','irdtwmro','eoopgw']
['noimednow','c','riotop']
['noimednow','c','riotopm']
['noimednow','g','c','riotopmo']
['noimednow','gr','c','riotopmow']
['n','oci','mreidon']
['n','oci','mreidont']
['n','oci','mreidonto']
['n','oci','mreidontoo']
['n','oci','mreidontoow']
['n','oci','mreidontoowp']
['n','oci','mreidontoowp']
['n','oci','mreidontoowp','m']
['n','oci','mreidontoowp','mg']
['n','oci','mreidontoowp','mgo']
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 2/29
8/23/2016 Yuzu: Python Programming in Context

['n','oci','mreidontoowp','mgor']
['n','oci','mreidontoowp','mgorw']

Session8.1Bruteforcedecryptionsoftherailfencecipher

8.3.1CheckingOurWorkwithaDictionary
AsyoucanseefromSession8.1theproblemwiththebruteforceapproachisthatiftheciphertextisquite
longtheremaybemanylinesofgibberishtoscanthroughbeforefindingtheonemessagethatiscorrect.One
waywecanimprovethissituationistouseadictionarybothliterallyandinthePythonsense.Youcansee
thatourrailDecryptfunctionreturnsalistofstrings.Supposethatwecheckedeachstringagainstadictionary
toseeifitwasgibberishorarealword.Alistofstringswithahighpercentagethatappearinthedictionaryis
morelikelytorepresentarealmessagethanalistofstringswithalowpercentage.
Inourcasewedonotneedadictionarythatincludesdefinitions.Allweneedisafilethatcontainsnumerous
real words. Many of these lists of words are freely available on the Internet. We will refer to one such list
storedinafilecalledwordlist.txt.
Thewordlistfilecontains41,238Englishwords,withonewordoneachline.Tomakelookingupwordseasy,
wewillloadthisfileintoaPythondictionary.Listing8.1isanexampleofafunctionthatreadsafileofwords
andreturnsaPythondictionarycontainingallthewordsinthefile.
Since we are not storing a useful value along with the key, one question you might ask is why use a
dictionary?Theanswerisspeed.SupposeyourewrotethecreateWordDictfunctiontobecreateWordList,so
thatcreateWordListreturnedalistofwordsratherthanadictionary.Wecanstillcheckwhetherawordwasin
ourwordlistwiththePythonstatementifwinwordList:.IfyoucheckedtheamountoftimePythonneededto
decidewhetherawordwasinthelist,youwouldseethat,onaverage,usingadictionaryis2,500timesfaster
thanusingalist.Onedisadvantagetousingthedictionaryisthatthereissomewastedspacesincewedonot
have any use for the value that goes with each key. Python actually provides a data type called a set that
behaveslikeadictionarybutstoresonlythekeys.

Listing8.1Loadingwordsfromafileintoadictionary

Exercises

8.1WriteacreateWordListfunctiontocreatealistofwordsfromthewordlistfile.

8.2WriteacreateWordSetfunctiontocreateasetofwordsfromthewordlistfile.

8.3 Devise an experiment to measure performance of createWordList, createWordSet, and


createWordDictionary. You can use the time module with the function time.time to get the current
clocktime,whichmaybeaccuratetoseveralmilliseconds.Gettingtheclocktimebeforeyoustarta
taskandafteryoucompleteataskallowsyoutoestimatehowlongthetasktakes.

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 3/29
8/23/2016 Yuzu: Python Programming in Context

8.3.2ABruteForceSolution
Lets return to the main theme of using brute force to crack the rail fence cipher. We can now count the
numberofdecryptedwordsthatarefoundinthedictionary.Wecanimproveouralgorithmbyremembering
whichrailsizehadthelargestnumberofrecognizablewords.Afterwehavetriedoutallpossiblerailsizes,
wecansimplylookatthemessagewiththemostcorrectwords.Notethatweshouldrecognizethatnolistof
wordsislikelytocontaineverywordinourmessage.
Listing 8.2 shows a complete solution for breaking the rail fence cipher. On line 9 we begin a loop that
iterates over all the words returned by railDecrypt. If a word is in the dictionary of known words, we
increment the goodCount by one. By now you will recognize this pattern as the accumulator pattern. On
lines1214 we use another common pattern. We might call this the minmax pattern. When the current
decrypted word list contains more known words than any of the previous decryptions, we remember it by
setting maxGoodSoFar to the number of real words. The if statement on line 12 checks to see if the latest
goodCountisgreaterthanthelargestknowngoodCount,calledmaxGoodSoFar.Inaddition,werememberthe
bestversionofthemessagesofarbyassigningitthenamebestGuess.
Theassignmentstatementonline14isagoodonetolookatclosely.Theexpression"".join(words)isavery
usefulcombinationofstringsandlists.Infact,youcanthinkofitastheoppositeofthesplitfunction.Inthis
example,thejoinmethodgluestogetherallofthestringsinthelistwordsseparatingthemusingthespace
character.Notethatyoucanjointhewordstogetherusingany(andasmany)charactersasyouwant.Ifyou
wantedtoseparatethewordswithtwodashes,youwouldsimplychangethecallto"".join(words).

Listing8.2Abruteforcealgorithmforbreakingtherailfencecipher

Exercises

8.4 Given the list of words ['the', 'quick', 'brown', 'fox'] use the join method on the following separator
strings:"",':',",","".

8.5UsingtherailDecryptfunctionfromListing8.3, run the railBreak function from Listing8.2 on the


ciphertextnocimreidontoowpmgorw.

8.6Usetherailfenceencryptionfunctiontoencryptyourownmessage.Givetheciphertexttoapartnerto
decrypt.

8.7FindadifferentwordlistontheInternettousewiththerailBreakfunction.Comparethevaluesof

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 4/29
8/23/2016 Yuzu: Python Programming in Context

maxGoodSoFarfordifferentwordlistsonthesameciphertextmessage.

8.3.3ARailFenceDecryptionAlgorithm
ItisnowtimetowritetherailDecryptfunctionwehavebeenusingintheprevioussections.Thekeytothe
raildecryptalgorithmistorememberhowthemessagewasputtogetherinthefirstplace.Withthatsetof
stepsinmind,therailDecryptfunctionwillsimplyundotheoriginaloperation.Letsgobacktoouroriginal
exampleandexaminetheencryptedstring(nocimreidontoowpmgorw)abitmorecarefully.Weknowthat
inthiscaseweusedthreerails.Wealsoknowthatbecausethestringis24characterslongeachrailwas24
3 or 8 characters. Using the fact that we have three rails and each rail is 8 characters long, we know that
characters0,0+8,and0+2*8arethefirstcharactersofeachrowandthereforearethefirstthreelettersofthe
decryptedmessage.AsyoucanseeinTable8.1,Character0is'n',character8is'e',andcharacter16is'w'.To
determine the next three characters of the message we shift each of those three positions 1 to the right.
Shiftingtotherightgivesus1,1+8,and1+2*8.Thisgivesusthenextthreecharactersof'','i',and'p'.

Table8.1Ciphertextwithcharacterindices.

Themethodweusedforfindingthefirstsixcharactersinthedecryptedmessageisanexampleofsomething
thecomputerdoesforusallthetime.Thatis,ittranslatesthecoordinatesfordatastoredintabularforminto
linear form. The memory in the computer is a onedimensional structure. So, if we want to store a two
dimensional table in the computers memory, we need to map from the row, column coordinates
correspondingtoacellinthetabletoalocationinmemory.Infacttherearetwowaystodothemapping.One
wayiscalledrowmajorstorageandthesecondiscalledcolumnmajorstorage.Rowmajorstorageismost
oftenused.
Table 8.2 illustrates how 18 contiguous memory locations would be organized using both row major and
columnmajorstorage.
Rowmajorstoragetakesthefirstrowofthetableandstoresallthevaluesonthatrowoneaftertheother,
followedbythesecondrowandthethirdandsoon.Columnmajorstoragetakesthefirstcolumnandstores
allthevaluesinthatcolumnoneafteranother,followedbythesecondcolumnandthethirdcolumnandso
on.Thequestionishowdoyoucomputethepositioninmemoryforanyonerowcolumnvalue?

Table8.2Examplesofrowandcolumnmajorstorage

For row major storage, the answer is a generalization of the pattern we started when figuring out how to
decodeoursecretmessage.Foragivenrowandcolumn,wefindthevaluebyusingtheformula

position=column+rowrowLength

InListing8.3youcanseethisformulainuseonline6aspartoftherailDecryptfunction.
LetslookattheentirerailDecryptfunction.Thefirstthingtonoticeisthattheciphertextandthenumberof
railsarebothparameterspassedtotherailDecryptfunction.Thenestedforloopsinlines47actasifyouare
accessingthecharactersofthemessageintheoriginaltwodimensionaltableformat.Thecalculationonline7
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 5/29
8/23/2016 Yuzu: Python Programming in Context

allowsustofigureouttheindexintotheonedimensionalstring.

Listing8.3Decryptingtherailfencecipher

As a final note, if you have not already written the railEncrypt function as described in Codes and Other
Secrets(Chapter3),youmightthinkoftherailencryptfunctionasatransformationoftheoriginalplaintext
string, using a column major storage scheme. The equation for finding the memory location in a column
majorstorageschemeis

position=row+columnnumRows

Thecolumnmajorcalculationallowsyoutotreattheplaintextstringasifitwereatableofcharactersstored
incolumnmajorformat.Iteratingoverthestringcolumnbycolumnandusingtheaccumulatorpatterngives
youtheciphertextstringyouhavejustdecoded.

Exercises

8.8WritetherailEncryptfunctionusingthecolumnmajoraccesspatternjustdescribed.

8.9NowthatyouhaveseenthedetailsoftherailDecryptfunctionyoucanmakeitsmarterintwoways:

(a)Youdonotneedtocheckcaseswherethenumberofrailsisgreaterthanthemessagelength
dividedbytwo.Canyouexplainwhy?
(b)Youonlyneedtocheckcaseswherethenumberofrailsevenlydividesthetotalmessagelength.
Canyouexplainwhy?

8.4CrackingtheSubstitutionCipher
Thesubstitutioncipherismuchhardertocrackthantherailfencecipher.Aswementionedpreviously,there
are 26! or roughly 4 1026 different possible arrangements of the alphabet that could be used as a key.
Clearly the brute force strategy for finding the key would take an extremely long time. However, the
substitutioncipherdoeshaveafatalflawthatwecanexploit.
TheflawisthatthesubstitutioncipherallowsustotakeadvantageofpatternsintheEnglishlanguagethat
helpusdeducelettersinthekey.ThefirstpatternwewillexploitisthatsomelettersintheEnglishlanguage
aremuchmorepopularthanothers.Ifwecountthenumberoftimeseachletteroccursinadocumentsuchas
thisbook,oranyotherEnglishlanguagebook,wewouldfindthattheletterse,t,a,o,andiaremuchmore
commonthanmanyotherlettersinthealphabet.

8.4.1LetterFrequency
Wecanwriteourownprogramtocalculatethefrequencyofletters.Therearetwowayswecouldreporton

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 6/29
8/23/2016 Yuzu: Python Programming in Context

letterfrequency.Onesimplewaywouldbetocountthetotalnumberofoccurrencesofeachletter.However,
thetotalwouldchangedependingonthesizeofthedocument.Abetterwayistoreportthepercentage.That
isthetotalcountforeachletterdividedbythetotalnumberofletters.InANestofSnakes(Chapter4),you
used Python to explore other interesting statistics in detail. We will keep our analysis very simple in this
chapter.Figure8.1showsagraphoftheresultswearelookingforinourletterfrequencyprogram.

Figure8.1Relativeletterfrequencies

TogeneratethedataforthegraphinFigure8.1,wewillwriteaPythonfunctionthattakesastring,text,asa
parameter.Foreachletterinthealphabet,thefunctionprintsoutthenumberoftimeseachletteroccurredin
text and the percentage of the total characters in text that the count represents. Counting the letters is easy
sincewecanusethecountmethodofthestringclass.Thetrickierpartofthisexerciseisthatinordertoget
an accurate percentage we need to ignore nonletter characters like spaces, and punctuation marks and
numbers.
Oneeffectivewaytoignorenonlettercharactersistoremovethemfromthestringbeforewestartcounting.
Weneedawaytoremoveallthenonlettercharactersfromtext.Earlier,wewroteafunctionthatcanremove
charactersfromastringcalledremoveMatches.Theproblemiswedonothaveahandystringfullofallthe
nonlettercharacters,butwecancreateone.Session8.2 shows how we can make a string of nonLetters by
firstremovingallthelettersfromthestringwewanttocount.Thetextstringattheendofthesessionisready
foranalysisbyourcountingfunction.

>>>text='Arethere25,26,or27nonletterstoremove?'
>>>text=text.lower()
>>>text
'arethere25,26,or27nonletterstoremove?'
>>>nonLetters=removeMatches(text,'abcdefghijklmnopqrstuvwxyz')
>>>nonLetters
'25,26,27?'
>>>text=removeMatches(text,nonLetters)
>>>text
'arethereornonletterstoremove'
>>>
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 7/29
8/23/2016 Yuzu: Python Programming in Context

Session8.2DemonstratingremoveMatches

To count all the letters of the alphabet, we will use the dictionary technique we introduced in A Nest of
Snakes(Chapter4)tokeeptrackofthefrequencyofnumbers.Thekeyforeachentryinthedictionaryisthe
letter. To build our dictionary, we will simply iterate over all the characters in a string. The dictionary will
initiallykeeptrackofthenumberoftimeseachcharacteroccursinthestring.Afterallthecountshavebeen
accumulated, we will make a second pass through the dictionary and convert the counts into percentages.
Listing8.4showsthePythoncodeforthelettercountingfunction.

Listing8.4Countingthefrequencyofeachletterinthealphabet

Lets look at a session where we calculate the text frequencies of the letters in H. G. Wellss War of the
Worlds, a novel that is free from copyright and freely downloadable from project Gutenberg
(http://www.gutenberg.org).Wewillusethereadmethodtoreadintheentirefilefromdiskasonestring,then
useourletterFrequencyfunctiontocomputetherelativefrequenciesofeachletter.Session8.3demonstrates
theuseofletterFrequency.Ofcourse,itwouldbemuchmoreinterestingtoseetheinformationsortedfrom
most frequent letter to least frequent. Unfortunately, dictionaries are not guaranteed to be ordered in any
particularway,sowewillhavetoextracttheinformationfromthedictionaryintoalistandthensortthelist.
Wecanextractallthekeyvaluepairsfromadictionaryusingtheitemsmethodalongwiththelistfunction.
For example, the first three elements in the list returned by items on our letter frequency dictionary are as
follows:

>>>wells=open('wells.txt')
>>>text=wells.read()
>>>lf=letterFrequency(text)
>>>lf
{'a':0.083323852323795439,
'b':0.015227686444052563,
'c':0.024048580692872176,
'd':0.047549633084930883,
'e':0.12521332271460264,
'f':0.02288241651971102,
'g':0.024187240457363899,
'h':0.060299220661015986,
'i':0.067993059901018255,
'j':0.00097417373001877237,
'k':0.0076582854542351668,
'l':0.037996330849308833,
'm':0.025634279538085215,
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 8/29
8/23/2016 Yuzu: Python Programming in Context

'n':0.072031969964161779,
'o':0.070976022526878665,
'p':0.018196427555606121,
'q':0.00067552192957506113,
'r':0.059904573639001084,
's':0.060178337789407815,
't':0.096688520393651514,
'u':0.02678977757551624,
'v':0.0087817850844757947,
'w':0.022974856362705499,
'x':0.0013865976449172308,
'y':0.018047101655384264,
'z':0.00038042550770806075}

Session8.3UnsortedletterfrequencyforWaroftheWorlds

>>>list(lf.items())[:3]
[('a',0.083323852323795439),
('b',0.015227686444052563),
('c',0.024048580692872176)]
ThesortfunctionprovidedbyPythonisextremelypowerful.Letsseehowwecanuseittosortthislistfrom
the most frequently used letter to the least. The first question you might ask is how does the sort function
knowwhatvaluetousewhenitsortsthislist?Remember,alistcancontainanyPythonobject,andsothesort
functionmustbeabletosortanykindofobject.Inthiscasetheelementsonourlistaretuples,sothesort
function defaults to using the first element of the tuple. Fortunately, the sort function takes some optional
parametersthatmakeitmoreflexible.
There are two optional parameters for the sort method: key and reverse. If we call sort with the optional
parameterreversesettoTrue,thesortfunctionwillsortthelistfromlargesttosmallestinsteadoftheother
way around. You should try this for yourself on a simple list of integers such as x = [3,7,4,9]. Then call
x.sort(reverse=True).Youwillseethatxisnowsortedfromhighesttolowest.
Thesecondoptionalparameter,keywillallowustowriteafunctionthattakesoneobjectasaparameterand
returnsavaluefromthatobjectthatweshoulduseasthesortkeyforthatobject.Forexample,supposewe
hadalistofturtlesandwewantedtosortthelistaccordingtothexcoordinateofeachturtle.Inthecaseof
ourdictionary,wejustwanttowriteafunctionthatcanreturnthefrequencynumberfromatuple.Thisisa
veryshorttwolinefunction,asshowninListing8.5.

Listing8.5Returningthesecondvalueinatuple

UsingthegetFreqmethodalongwiththekeyandreverseparameters,wecannowsortourletterfrequency
dictionaryjusthowwewantit(seeSession8.4).Itisworthrepeatingherethatthesortmethodundergoesan
inplacesortthatis,itmodifiesthelistitissorting.Itdoesnotcreateanewlist,nordoesthesortfunction
return any value. A common mistake is to write a statement such as myList = myList.sort(). Try this for
yourself,andyouwillseethatunfortunatelymyListhasbeensettoNoneratherthansorted.

>>>lfList=lf.items()
>>>lfList.sort(key=getFreq,reverse=True)
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 9/29
8/23/2016 Yuzu: Python Programming in Context

>>>forentryinlfList:
....print("%s%5.3f"%entry)
e0.125
t0.097
a0.083
n0.072
o0.071
i0.068
h0.060
s0.060
r0.060
d0.048
l0.038
u0.027
m0.026
g0.024
c0.024
w0.023
f0.023
p0.018
y0.018
b0.015
v0.009
k0.008
x0.001
j0.001
q0.001
z0.000

Session8.4SortedletterfrequencyfromWaroftheWorlds

Exercises

8.10Evaluatethefollowingstatements:

l1=[4,5,3,9,1,6,7]
l1=l1.sort()
Whatisthevalueofl1whenyouaredone?

8.11 Write a function called extractLetters that simply keeps all the letter characters in a string. This
functioncouldbeusedinplaceofthetwocallstoremoveMatches.

8.4.2CiphertextFrequencyAnalysis
Letterpopularityiscriticalforbreakingasubstitutioncipher.Becauseasubstitutionciphermakesaonefor
onelettersubstitutionfromtheplaintexttotheciphertext,themostpopularletterintheplaintextwillalsobe
themostpopularletterintheciphertext.Thissmallamountofinformationisenoughtobeginbreakingthe
encryptionthroughfrequencyanalysis.
Letsdoabitofcryptanalysisonthefollowingparagraph.

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 10/29
8/23/2016 Yuzu: Python Programming in Context

ul ilahvble jkhbevbt hk kul letl cskul dvk kuhk kul kuvbt uhe ahel sci vkjlzs jkhivbt hk vkj jkihbtl
hddlhihbwl hjkcbvjule wuvlszq hk vkj mbmjmhz juhdl hbe wczcmi hbe evazq dliwlvnvbt lnlb kulb
jcal lnvelbwl cseljvtb vb vkj hiivnhz kul lhizq acibvbt ohj ocbelismzzq jkvzz hbe kul jmb xmjk
wzlhivbt kul dvbl killj kcohiej olqgivetl ohj hzilheq ohia ul eve bck ilalagli ulhivbt hbq gviej kuhk
acibvbtkulilohjwlikhvbzqbcgillrljkviivbthbekulcbzqjcmbejolilkulshvbkacnlalbkjsicaovkuvb
kulwvbeliqwqzvbeliulohjhzzhzcblcbkulwcaacb

Thefirststepinbreakingthiscodeistoapplyourletterfrequencyanalysistotheencryptedtext.Theresults
ofthisareshowninTable8.3.
Oneapproachwouldbetohopethatthereisanexactmatchinthefrequenciesoflettersbetweentheplaintext
sampleandtheciphertext.Ifthiswerethecase,wecouldfigureoutthekeybymatchingthelettersinorderof
theirpopularity:e l, t b, a k, n h, and so on. Using these letter substitutions, the first line of
ciphertextwouldbedecryptedasfollows:seieunoterhantrotmnaaseermedfaseyoaasnaaseasotmsnr
unrefdioahelfhaniotmnaoahhaintme.Clearlyablindmappingisnottheanswer.

Table8.3Relativefrequencyoflettersintheciphertext

8.4.3LetterPairAnalysis
Becausethereisboundtobesomevariationinthefrequencyoflettersparticularlyinsmallersampleswe
havetotrytomatchtheletterswithabitmoreflexibility.Letsbeginbyassumingthatthefourmostpopular
plaintextletterswillmatchthefourmostpopularciphertextlettersinsomeorder.Forexample,aciphertext'l'
willbeeither'e','t','n',or'a'.Likewisetheciphertextletters'b','k',and'h'willmatchoneof'e','t','n',or'a'.
Noticethatofthefourletterswearetryingtoidentify,twoarevowelsandtwoareconsonants.Onewaythat
we can statistically tell the difference between a vowel and a consonant is to notice that vowels frequently
appearbeforeorafteralmostanyotherletterinthealphabet.Consonantstypicallyhaveasmallnumberof
lettersthattheyappearnextto.Thissuggeststhatwecanwriteavariationofourletterfrequencyfunctionto
figureouthowfrequentlyeachletterappearsnexttoanother.
Althoughthismayseemlikeadauntingtask,itisimportanttounderstandthatifwestartwithourbasicletter
frequencyfunction,wearealreadyalongwaytowardourgoal.Letslookattheadditionalproblemsweneed
tosolve:

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 11/29
8/23/2016 Yuzu: Python Programming in Context

1.Ratherthancountthenumberoftimeseachletteroccurs,wewanttokeepalistofneighboringletters.
Thelistwillcontainallthelettersthatappearbeforeoraftertheletterinquestion.
2.Theoutputofthefunctionwillbeadictionarythatcontainsakeyforeachletterofthealphabetanda
listofneighboringletters.
3.Weneedtobecarefulabouthowweaccountfornonlettersinourcounting.Ifwesimplydelete
nonlettersaswedidpreviously,wewillendupcreatingfalseneighbors.

Onceagainwewanttokeeptrackofsomeinformationthatisindexedbyaletter.Thissuggeststhatwewill
use a dictionary again. The difference is that we need to keep track of something more complicated than a
simplenumber.ThegoodnewsisthatdictionariescanstoreanyPythonobjectasavalue.Tokeeptrackofa
charactersneighbors,weusethatcharacterasthekeyandstorealistofneighboringcharactersasthevalue.
Forexample,ifourdictionaryiscalledmyNeighbors,thenmyNeighbors['q']willevaluatetothelistofletters
thatoccurnextto'q'.Forexampleinonelargetexttheletter'q'appearsnexttotheletters['u','e','o','s','n','d',
'i','h','a','c'].
Onceweknowthatwewillusealistforkeepingtrackoftheneighbors,thenextproblemiseasytosolve.We
canusethelenfunctiontofindthelengthofourlistofneighborsafterwehaveprocessedallthecharactersin
thestring.
Anoutlineofournewfunctionisasfollows:

1.CreateanemptydictionarynbDict.
2.Loopoverallthecharactersinastring.

(a)IfthecurrentcharacterisnotinnbDict,addanemptylist.Otherwise,retrievethelistof
neighborsalreadystored.
(b)Addthenextcharacterasaneighborofthecurrentcharacter.
(c)IfthenextcharacterisnotinnbDict,addanemptylist.Otherwiseretrievethelistofneighbors
alreadystored.
(d)Addthecurrentcharacterasaneighborofthenextcharacter.
3.Loopoverallthekeysofthedictionaryandreplacethelistwiththelengthofthelist.
4.Returnthedictionary

Thereareacoupleofideasintheoverviewworthconsidering.First,noticethatwhenwearelookingatthe
characteratindexiinthecharacterstringweaddthecharacteratindexi+1asaneighbor.Atthesametime,
weaddthecharacteratindexiasaneighborofthecharacteratindexi+1.Inthiswaywecanensurethatas
we move through the list we are getting the neighboring characters that occur both before and after each
character.
Second,weneedtobecarefulaboutwhichcharactersweaddtothelistofneighbors.Ifaletterisalreadyon
the neighbor list, we do not want to add it again. In addition, since we do not wish to count spaces,
punctuationmarks,numbers,oranyothernonletterasneighbors,wewillignorethosecharacters.Theeasiest
waytomakesurethatwearecountingonlylettersistocheckifthecharacterisoneofthe26lettersofthe
alphabet.Todothis,wewritealittlefunctionthattakestwoparameters:acharacterandalist.Letscallthis
functionmaybeAdd.Listing8.6showsthecompletefunctiontoaddacharactertoalistundertheconditions
describedabove.

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 12/29
8/23/2016 Yuzu: Python Programming in Context

Listing8.6Conditionallyaddingacharactertoalist

ThemaybeAddfunctionisagoodexampleofafunctionthatmodifiesoneofitsparameters.Noticethatwe
arepassingalistasaparameterbutwearenotreturningalist.Session8.5showsthemaybeAddfunctionin
action.

>>>myList=[]
>>>maybeAdd('a',myList)
>>>myList
['a']
>>>maybeAdd('',myList)
>>>myList
['a']
>>>maybeAdd('b',myList)
>>>myList
['a','b']
>>>maybeAdd('a',myList)
>>>myList
['a','b']

Session8.5TestingthemaybeAddfunction

Listing8.7Creatingadictionaryofneighbors

Listing8.7showsthecompleteneighborCountfunction.Thereareseveralinterestingthingstothinkaboutin
theneighborCountfunction.First,weusethestatementforiin range(len(text)1) rather than iterating over
eachcharacterinthestringbecauseweneedtoindexthecurrentcharacteraswellasthenextcharacter.We
uselen(text)1becauseweconsiderthecurrentcharacterandthenextcharacter.Ifwesimplyusedlen(text),
wewouldeventuallygetanindexoutofrangeerror.
Another statement to look at carefully is nbList = nbDict.setdefault(text[i],[]). The setdefault method first
checkstoseeifthekeyisinthedictionary.Ifitisnot,itaddsadefaultvalueandreturnsareferencetothe
defaultvalue.ThisstatementisashortcutforthePythoncodeshowninListing8.8.

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 13/29
8/23/2016 Yuzu: Python Programming in Context

Listing8.8Addinganewdefaultvaluetoadictionary

Thispatternisquitecommonwhenusingdictionaries.Usingthesetdefaultmethodisagoodideabecauseit
reducestheamountofcodeyouneedtowriteandreducesyourchancesofmakinganerror.
NowwewillusetheneighborCountfunctiontocontinueouranalysisoftheciphertext.Session8.6showsthe
neighborCount function in action. After building the dictionary freqDict we will print the number of
neighborsforthecommonletterse,n,andt.

>>>freqDict=neighborCount(text)

>>>foriin'ent':
....print(i,freqDict[i])
....
e26
n25
t21

Session8.6Numberofneighborsfore,n,andt

Thissessionshowsusthateappearsnextto26differentletters,nappearsnextto25differentletters,andt
appears next to 21 different letters. We were hoping for a result that would allow us to easily differentiate
betweenvowelsandconsonants.Unfortunately,thisisnotit.
In order for these counts to be more useful, we need a bit more detail. For example e probably appears
frequentlynexttomanydifferentletters,whereasnortappearnexttosomelettersveryfrequentlyandother
lettersinfrequently.Weneedtoseethefrequencywithwhichdifferentlettersappearnexttoeachother.What
weneedtobuildisatablelikethatshowninTable8.4.
LetsimproveneighborCountsowecanproduceatableofletterpairfrequencycounts.Thismayallowusto
learn enough to make better guesses about which letters are vowels and which are consonants. The most
importantchangewewillmakeistoreplacethelistofletterswithadictionarysothatwecankeepacountfor
eachneighboringletter.Thiswillcreateadictionaryofdictionaries.Forexampled['a']['x']tellsusthenumber
oftimestheletters'a'and'x'appearnexttoeachother.

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 14/29
8/23/2016 Yuzu: Python Programming in Context

Table8.4Letterpairfrequencyanalysisforoneparagraph

Thenecessarymodificationsareactuallyquitesimple.IntheneighborCountfunctionweneedtochangeonly
thecallstosetdefaulttocreateadictionaryinsteadofanemptylist.
Wedothisbychanging[]to{}
ThechangeoccursinthemaybeAddfunction.Thisfunctionisdifferentbecauseitisnowupdatingacountof
eachneighboringletterinadictionaryratherthansimplyappendingalettertoalist.Listing8.9 shows the
newversionofmaybeAdd.Itisworthnotingthatline3isanotherexampleoftheaccumulatorpatternthat
you have seen in so many different places already. Once again the setdefault function simplifies the
implementationofthispattern.

Listing8.9NewaddMaybefunctionforadictionaryofdictionaries
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 15/29
8/23/2016 Yuzu: Python Programming in Context

Session8.7showstheresultofcallingtheneighborCountfunctionontheentiretext.Letslookatwhatwe
canlearnabouttheletterse,n,andt.Thisdatashowsusthateappearsmorefrequentlynexttoaverylarge
numberofletters.Inaddition,althoughbothnandtappearnexttomanydifferentletters,theydosomuch
lessfrequently.Alsonotethateappearsleastfrequentlynexttoothervowelsandacoupleofconsonantslike
zandj.

>>>d=neighborCount(text)
>>>printd['e']
{'a':1924,'c':1655,'b':1261,'e':2616,'d':4815,'g':1007,'f':656,
'i':903,'h':8586,'k':704,'j':143,'m':2552,'l':2898,'o':230,
'n':4547,'q':26,'p':1328,'s':4134,'r':8258,'u':167,'t':3149,
'w':1436,'v':2094,'y':798,'x':331,'z':70}

>>>printd['n']
{'a':5198,'c':577,'b':108,'e':4547,'d':4030,'g':3181,'f':100,
'i':6671,'h':32,'k':341,'j':10,'m':33,'l':225,'o':3995,
'n':278,'q':7,'p':12,'s':912,'r':375,'u':1246,'t':1739,
'w':291,'v':65,'y':204,'x':6}

>>>printd['t']
{'a':3821,'c':697,'b':34,'e':3149,'d':2,'g':29,'f':281,
'i':4506,'h':10393,'m':80,'l':587,'o':3144,'n':1739,'p':228,
's':3082,'r':1877,'u':1705,'t':880,'w':217,'y':298,'x':84}

Session8.7TestingtheneighborCountfunction

Figure8.2Letterpairfrequenciesfora,e,n,andt.

The data, as we have shown it in Session8.7, is difficult to interpret. Although we can present the data as
showninTable8.4,abetterwaytogetanideaofhowtheletterpairsworkoutistocreateahistogram.Figure
8.2showsahistogramthatcomparesthefrequencieswithwhichlettersa,e,n,andtoccurwithalltheother
letters of the alphabet. This plot was produced with the matplotlib module of Python (see Exercise 1 in

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 16/29
8/23/2016 Yuzu: Python Programming in Context

Chapter4).
ComparedwiththeoutputfromSession8.5,thehistograminFigure8.2showsusthateandaappearmore
frequentlywithmoreotherletters.(Exercise8.13willaskyoutocreateyourowndictionaryfortheciphertext
paragraph.Whenyoudo,youwillgetfrequencypairsforthelettersl,b,k,andhthatarethesameasthose
showninTable8.5.)
ThehistograminFigure8.3correspondstoTable8.5.Youcanseethatlandharevowelssincetheyappear
morefrequentlywithmoreletters.Theletterskandbareconsonants.Butwhichoftheseciphertextcharacters
isaandwhichise?
Wehavethreepiecesofevidencetomakeagoodguessabouttheletterl.First,itwasthemostcommonletter
intheciphertextparagraph.Second,ourpairanalysisindicatesthatlisavowelbecauseitappearsrelatively
frequentlywithmanyotherletters.Finally,wecanseethattheletterlappearsnexttoitselffourtimesbutthe
letterhneverappearsnexttoitself.Sinceitismuchmorecommonforetoappearnexttoitselfthantheletter
a(unlessyouarewritingathesisaboutaardvarks),wecanprobablyconcludethatlmapstotheeandhmaps
toa.

Figure8.3Letterfrequencyintheciphertext

Nowthatwehavetwolettermappingsinplaceletsplugthemintotheciphertextandseeifthatgivesusany
cluesthatwillhelpdecryptthelettersbandk.WecanusethePythonstringreplacemethodtohelpusreplace
the ciphertext letters with the plaintext letters we have figured out. Session 8.8 shows how to replace the
ciphertextlwithEandhwithA.Fromnowonwewilluseuppercaseforthedecodedplaintextletterstomake
itclearwhichletterswehavedecodedandwhichwestillneedtoworkon.

Table8.5Frequencypairsforthefourmostcommonciphertextletters
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 17/29
8/23/2016 Yuzu: Python Programming in Context

>>>ciphertext=ciphertext.replace('l','E')

>>>ciphertext=ciphertext.replace('h','A')

>>>ciphertext
'uEiEaAvbEejkAbevbtAkkuEEetEcskuEdvkkuAkkuEkuvbt
uAeaAeEscivkjEzsjkAivbtAkvkjjkiAbtEAddEAiAbwEAjkcbvjuEe
wuvEszqAkvkjmbmjmAzjuAdEAbewczcmiAbeevazqdEiwEvnvbtEnEbkuEb
jcaEEnveEbwEcseEjvtbvbvkjAiivnAzkuEEAizqacibvbtoAj
ocbeEismzzqjkvzzAbekuEjmbxmjkwzEAivbtkuEdvbEkiEEjkcoAiej
oEqgivetEoAjAziEAeqoAiauEevebckiEaEagEiuEAivbtAbqgviejkuAk
acibvbtkuEiEoAjwEikAvbzqbcgiEErEjkviivbtAbekuEcbzqjcmbej
oEiEkuEsAvbkacnEaEbkjsicaovkuvbkuEwvbeEiqwqzvbeEiuEoAjAzz
AzcbEcbkuEwcaacb'

Session8.8Firstcharacterreplacement

Exercises
8.12Createafrequencyhistogramfortheciphertextparagraph.

8.13ProduceatablesimilartoTable8.4.

8.4.4WordFrequencyAnalysis
We have made some progress, but we still have a long way to go. Lets employ another cryptanalysis
techniquetohelpwiththenextstep.Thistechniquewilllookattheshortwordsnamely,thewordsthatare
one,two,orthreeletterslong.
Aneasywaytodothisistoreorderthewordsintheciphertextandsortthembytheirlength.Rememberthat
wesaidthesortfunctioncanacceptakeyfunctionasaparameter.Listing8.10showsaPythonfunctionthat
accepts one word as a parameter and returns the length of the word. Session 8.9 shows how to use our
sortByLenfunctionandprovidesuswithsomeinterestingnewclues.

Listing8.10Comparingtwowordsbylength

>>>cipherwords.sort(key=sortByLen)
>>>cipherWords
['uE','Ak','cs','Ak','Ak','cs','vb','uE','bc','uE','cb',
'kuE','kuE','dvk','kuE','uAe','sci','vkj','vkj','Abe','Abe',
'vkj','kuE','oAj','Abe','kuE','jmb','kuE','oAj','eve','bck',
'Abq','oAj','Abe','kuE','kuE','kuE','oAj','Azz','kuE']

Session8.9Sortingciphertextwordsbylength

ThenewcluesfromSession8.9arethatthewordAkappearsthreetimes,uEappearstwice,Abeappearsfour

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 18/29
8/23/2016 Yuzu: Python Programming in Context

times,andkuEappears10.Sowemightaskthesequestions:

1.Whataresomepopularthreeletterwords?
2.Whatthreeletterwordsendine?
3.Whatthreeletterwordsbeginwitha?

Exercise8.15attheendofthissectionasksyoutowriteafunctionthatreadsastringandprintsoutalistof
wordsthatareofagivenlength,orderedfrommostpopulartoleast.Ifyourunsuchafunctiononthetextof
WaroftheWorldsyouwouldproduceTable8.6.Nowthechoicesfortheletterskandubecomeclear.Since
themostpopularthreeletterwordistheanditistheonlyoneinthetop10thatendsine,itisaprettysafe
assumptionthatkuEcorrespondstothewordTHE.Inaddition,sinceandisthesecondmostpopularthree
letterword,wecanalsomaketheassumptionthatbmapstoNandemapstoD.Session8.10 shows what
happenswhenwemakethesefouradditionalsubstitutions.

Count
Word
the 4959
and 2555
was 851
had 579
for 369
but 295
his 250
out 231
all 225
not 212

Table8.6Threeletterwordfrequencycounts

>>>ciphertext=ciphertext.replace('b','N')

>>>ciphertext=ciphertext.replace('e','D')

>>>ciphertext=ciphertext.replace('k','T')

>>>ciphertext=ciphertext.replace('u','H')

>>>ciphertext
'HEiEaAvNEDjTANDvNtATTHEEDtEcsTHEdvTTHAT
THETHvNtHADaADEscivTjEzsjTAivNtATvTjjTiANtEAddEAiANwE
AjTcNvjHEDwHvEszqATvTjmNmjmAzjHAdEANDwczcmiANDDvazq
dEiwEvnvNtEnENTHENjcaEEnvDENwEcsDEjvtNvNvTjAiivnAzTHE
EAizqaciNvNtoAjocNDEismzzqjTvzzANDTHEjmNxmjTwzEAivNtTHE
dvNETiEEjTcoAiDjoEqgivDtEoAjAziEADqoAiaHEDvDNcTiEaEagEi
HEAivNtANqgviDjTHATaciNvNtTHEiEoAjwEiTAvNzqNcgiEErE
jTviivNtANDTHEcNzqjcmNDjoEiETHEsAvNTacnEaENTjsicaovTHvN
THEwvNDEiqwqzvNDEiHEoAjAzzAzcNEcNTHEwcaacN'

Session8.10Secondreplacement,usingwordfrequency

Inadditiontoandandtheotherwordssuchasthat,at,had,andthenalsoappear.Consideringthatwehave
mappedonlysixletters,thisresultisverypromising.Infactyoucanprobablymakesomeadditionalguesses

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 19/29
8/23/2016 Yuzu: Python Programming in Context

yourselfbasedonwhatyousee.
ReferringbacktoTable8.6,letstrytodecryptafewmoreletters.First,wemightnoticethatanotherpopular
threeletterwordappearinginthepartiallydecryptedciphertextisthewordoAj.Lookingatourlistofpopular
threeletter words, we see that was is the third most popular word. In addition was has an a in the middle
position.Scanningthroughtherestoftheciphertext,wecanseethattherearemanywordsthatendinj.Of
course,sisaverypopularletterattheendofwordsbecauseitmakesawordplural.Onceagainyoucanwrite
afunctiontofindoutwhatlettersappearmostfrequentlyattheendofaword.Itisverylikelyyouwouldfind
outitiss.Ifweusethemappings,omapstoWandjmapstoS,wenowfindthatourciphertextlookslikethe
following:

HEiEaAvNEDSTANDvNtATTHEEDtEcsTHEdvTTHATTHETHvNtHADaADEscivTSEzs
STAivNt AT vTS STiANtE AddEAiANwE ASTcNvSHED wHvEszq AT vTS mNmSmAz SHAdE
ANDwczcmiANDDvazqdEiwEvnvNtEnENTHENScaEEnvDENwEcsDESvtNvNvTSAiivnAz
THE EAizq aciNvNt WAS WcNDEismzzq STvzz AND THE SmN xmST wzEAivNt THE dvNE
TiEES TcWAiDS WEqgivDtE WAS AziEADq WAiaHE DvD NcT iEaEagEi HEAivNt ANq gviDS
THAT aciNvNt THEiE WAS wEiTAvNzq Nc giEErE STviivNt AND THE cNzq ScmNDS WEiE
THEsAvNTacnEaENTSsicaWvTHvNTHEwvNDEiqwqzvNDEiHEWASAzzAzcNEcNTHE
wcaacN
Wecangettwomoreletterswithanotherobservation.Lookatthelastthreelettersofeachwordandnotice
thatthereareseveralthatendinvNt.ThereareninewordsthatendinthepatternvNt. A common English
languagesuffixforwordsising.Thisgivesusthevoweli,whichmeansthatwenowknowthemappingsfor
thevowelsa,e,andi.
Atthispointwecanalsomakeaneducatedguessthatthewordcsintheciphertextisprobablyof.Thepair
frequencyprofileofcfitsthatofavowel,andthewordofmakesthemostsenseinthecontextofthephrase
STANDINGATTHEEDGEcsTHEdIT.
Finally, lets make one more substitution before looking at the whole text again. There is another common
letterintheciphertextthatwehavenotdecodedyet:itisz.Noticethatwehavethepartiallydecodedthree
letterwordAzz.Theonlythreeletterwordinourtoptenthathasadoubleletteristhewordall,soitisagood
betthatzmapstoL.

HE iEaAINED STANDING AT THE EDGE OF THE dIT THAT THE THING HAD aADE FOi
ITSELFSTAiINGATITSSTiANGEAddEAiANwEASTONISHEDwHIEFLqATITSmNmSmAL
SHAdEANDwOLOmiANDDIaLqdEiwEInINGEnENTHENSOaEEnIDENwEOFDESIGNIN
ITS AiiInAL THE EAiLq aOiNING WAS WONDEiFmLLq STILL AND THE SmN xmST
wLEAiING THE dINE TiEES TOWAiDS WEqgiIDGE WAS ALiEADq WAiaHE DID NOT
iEaEagEi HEAiING ANq gIiDS THAT aOiNING THEiE WAS wEiTAINLq NO giEErE STIiiING
AND THE ONLq SOmNDS WEiE THE FAINT aOnEaENTS FiOa WITHIN THE wINDEiq
wqLINDEiHEWASALLALONEONTHEwOaaON

Wehavemadegoodprogress,findingthemappingsfor12letterswithonly14moreletterstofillin.Table8.7
showstheletterswehaveidentifiedandthosethatwehaveyettofigureout.Wecouldcontinuetomakesome
deductionsaboutwhatlettersgowhere.ButletslookatanotherwaywecanusePythontohelpusbydoing
someautomatedpatternmatchingaverycommonapplicationforcomputers.

Table8.7Progressonmappingtheletters

Exercises
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 20/29
8/23/2016 Yuzu: Python Programming in Context

8.14WriteafunctionwordPopthatacceptsatextandalengthNandreturnsthelistofallthewordsthatare
Nletterslong,sortedbytheirlength.

8.15Writeafunctionthatreturnsthemostpopularendingletterforwords.

8.16Writeafunctionthatfindsthemostpopularsuffixesforwords.Youmaywanttotrythisfunctionfor
twoandthreelettersuffixes.

8.4.5PatternMatchingwithPartialWords
Whatwewouldliketodoisselectawordfromthepartiallydecodedciphertextandasktheprogramtofinda
wordfromthedictionarythatisthesamelengthandmatchesthelettersthatwehavedecodedsofar.Itturns
outthathumansareprettygoodatthetaskofpatternmatching.Forexample,givenaADEyoucanprobably
comeupwiththewordsMADE,FADE,andWADE.Someofthosewouldworkforusbutotherswouldnot.
Forexample,FADEwouldnotworkforusbecauseFhasalreadybeendecoded.Ideallywewouldliketogive
thefunctionthesetoflettersthatarelegaltousewhenmatchingthecodedpartsofthewords.Ifthecomputer
comesbackwithonlyonematch,wecanthenbeprettysurethatwehavedecodedsomeadditionalletters.
Most modern programming languages today provide a patternmatching library that uses regular
expressions. In Python, regular expressions are available through the re module. Regular expressions are
extremelyusefulandpowerfulprogrammingtools.Wejustbarelyscratchthesurfaceinthissection,butwe
willseehowevenasimpleuseofregularexpressionscanmakethefinaldecryptionstepeasier.
Regularexpressionsallowustoseeiftwostringsmatch,muchlikethe==,exceptthatinregularexpressions
we can use wild card characters as part of the match. For example, we can test to see if the string. ADE
matchedFADE.Whenusingregularexpressions,the.characterisawildcardthatmatchesanycharacter.The
regularexpressionfunctionthatweusetotestiftwostringsmatchiscalledmatch.Thematchfunctiontakes
twoparameters:aregularexpressionandthestringwewanttomatchagainst.Session8.11illustratestheuse
ofthematchfunctionaswellassomesimplepatternmatching.

>>>re.match('.ADE','FADE')
<_sre.sre_matchobjectat>

>>>re.match('.ADE','FADER')
<_sre.sre_matchobjectat>

>>>re.match('.ADE','ADE')

>>>re.match('.ADE','FUDE')

Session8.11Tryingregularexpressionmatches

Iftwostringsmatch,thematchfunctionreturnsamatchobject.Atthispointwecanjustthinkofthematch
objectasbeingourindicationofsuccess.Asyoulearntousemoreadvancedregularexpressions,youwilluse
the match object for more interesting purposes. If the two strings do not match, the function returns None.
Thisallowsustousere.matchasaconditioninanifstatement.
Nowsincewewanttobemorespecificinourmatches,letslearnsomeadditionalregularexpressionsyntax.
Withinaregularexpression,thesquarebrackets([])allowustodefineasetofcharacters,anyoneofwhich
maymatch.Forexample,[abc]willmatchaorborc,butnotanyothercharacter.Inaddition,wecanusethe
+charactertomatchmultipleinstancesofacharacter.Forexample,[abc]+willmatchaorborcorabcor
aaaaaaaorbbbboraaabbbcccoranyothercombinationofthelettersabc.Youreadthe+charactertomean
one or more, so we say that the regular expression [abc]+ means match one or more instances of the

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 21/29
8/23/2016 Yuzu: Python Programming in Context

charactersa,b,orc.
You may also have noticed that '.ADE' matched 'FADER'. This is because the regular expression matcher
starts at the beginning and tries to match the whole regular expression. Once the end of the expression is
foundthematcherdoesnotcarewhatcomesafter.Ifwewanttoindicatetothematcherthatwedonotwant
anythingtocomeafterthepatternweprovide,wemustendourpatternwitha$.
Table8.7showedthatwehavenotdecodedB,C,J,K,M,P,Q,R,U,V,W,X,Y,orZ.Whenwecheckfor
matchesinthedictionary,wecanusetheregularexpression[BCJKMPQRUVWXYZ]tolimitourchoicesfor
aparticularletterpositiontomatchonlythosecharacters.Thecalltomatchfortheexamplewehavebeen
usingwouldbere.match('[BCJKMPQRUVWXYZ]ADE','FADE').SinceFisnotoneofthecharactersinside
thesquarebrackets,thiscallwouldfailandreturnNone.However,re.match('[BCJKMPQRUVWXYZ]ADE',
'MADE')wouldbesuccessfulandreturnamatchobjectsinceMisoneofthecharactersinthebrackets.
LetswriteaPythonfunctionthatusestheregularexpressionmoduletocreatealistofallthewordsinthe
dictionaryfilethatmatchagivenregularexpression.Thebasicformofourfunctionisreallyquitesimple.We
use a loop to read through all the words in the dictionary, trying to match each word against the regular
expression.Ifawordmatches,weaddittothelist.Iftheworddoesnotmatch,weignoreit.Whenwehave
tested all the words, we return the list that we constructed. The code for this matching function, called
checkWord,isshowninListing8.11.

Listing8.11Matchingwordsfromthedictionaryagainstpatterns

Session8.12usesthecheckWordfunctiontoseeifwecanidentifysomemoreletters.Inourpartiallydecoded
messagewehavethestringaOiNING.Usingthepattern'.o.ning'thecheckWordfunctionmatchestheword
morningfromthedictionary.Thesecondexampleusesapatternthatexplicitlyrestrictsthepossiblematches
toourlistofunmappedcharacters.TherestofSession8.12findsmatchesforsomeadditionalwords.

>>>checkWord('.o.ning')
['morning']

>>>checkWord('[bcjkmpqruvwxyz]o[bcjkmpqruvwxyz]ning')
['morning']

>>>checkWord('a..i.al')
['admiral',
'admiralty',
'ambivalence',
'ambivalent',
'ambivalently',
'antimalarial',
'arrival']

>>>checkWord('a..i.al$')
['admiral','arrival']

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 22/29
8/23/2016 Yuzu: Python Programming in Context

>>>checkWord('a[bcjkmpqruvwxyz][bcjkmpqruvwxyz]i[bcjkmpqruvwxyz]al$')
['arrival']

Session8.12UsingcheckWordtofindmatches

ConstructingthepatternstringforthecheckWordparameterisquitetedious.Wecanreducetheamountof
workwemustdobymakingthecheckWordfunctionmoreintelligent.Todoso,letspasstwoparametersto
thecheckWordfunction:astringofunusedlettersandthewordfromthepartiallydecodedciphertext.This
meansthatletterstokeepinplacewill(initially)becapitalizedandlettersthatwecanmatchfromtheunused
groupwillbeinlowercase.
Wecanusetheremodulesreplacefunction(calledre.sub)tosubstituteallofthelowercasecharacterswith
thegroupofletterswewanttomatch.Line5ofListing8.12showstheuseofthere.subfunction.Noticethat
wecanuseasimplifiedpatterntorepresentallofthelowercasecharacters.Thepattern[az]isthesameas
typing[abcdefghijklmnopqrstuvwxyz].
Thesyntaxforthere.subfunctionisasfollowsre.sub(pattern,replacementstring,targetstring).There.sub
function finds each instance of the pattern in the target string and replaces it with replacement string. We
haveaddedaprintstatementtothecheckWordfunctionsothatyoucanseethefinalversionoftheregular
expressionconstructedbycheckWord.

Listing8.12checkWordconstructionofaregularexpression

>>>checkWord('bcjkmpqruvwxyz','WONDEiFmLLq')
matchingwonde[bcjkmpqruvwxyz]f[bcjkmpqruvwxyz]ll[bcjkmpqruvwxyz]$
['wonderfully']
>>>checkWord('bcjkmpqruvwxyz','AiiInAL')
matchinga[bcjkmpqruvwxyz][bcjkmpqruvwxyz]i[bcjkmpqruvwxyz]al$
['arrival']
>>>checkWord('bcjkmpqruvwxyz','mNmSmAL')
matching[bcjkmpqruvwxyz]n[bcjkmpqruvwxyz]s[bcjkmpqruvwxyz]al$
['unusual']

Session8.13UsingthenewcheckWordtofindmatches

Session 8.13 illustrates the new checkWord function in action. But as you can see, there is one more
improvementwecouldmake.ItwouldbeniceifcheckWordcouldtellustheciphertexttoplaintextmapping
forthelettersthatcompletethematch.Infact,regularexpressionsallowustodothisverynicelybyusing
match objects and capturegroups, which allow us to find out what characters in the target word matched
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 23/29
8/23/2016 Yuzu: Python Programming in Context

lettersindifferentpartsofourpattern.Youcreateacapturegroupbysurroundingsomepartoftheregular
expression with left and right parentheses. Here is a simple example: 'F (..)L (..)$'. This regularexpression
willmatchanywordthatstartswithanFfollowedbyanytwoletters,followedbyanLfollowedbytwomore
letters.So,wordslikefueledorfoiledorfoolerwouldallmatch.
Session8.14illustratestheuseofcapturegroupsinregularexpressions.Letssupposethatwematchedthe
wordFOILED.Thefirstcapturegroupcorrespondstothefirstsetofparentheses,sothelettersinthecapture
groupwouldbeOI.ThesecondcapturegroupwouldcontainthelettersED.Thematchobjectallowsustoget
capturegroupsbynumber,usingthegroupmethod,ortogetallthecapturegroupsasalistusingthegroups
method.

>>>cg=re.match('F(..)L(..)','FOILED')

>>>cg
<_sre.sre_matchobjectat>

>>>cg.group(1)
'OI'

>>>cg.group(2)
'ED'

>>>cg=re.match('F(..)L(..)','FOOLER')

>>>cg.groups()
('OO','ER')

Session8.14Demonstratingcapturegroups

Nowthatyouhavesomeideaofhowcapturegroupsworkwithregularexpressionswewillusethemtocreate
a list of character maps that show us which characters we can substitute for which. We begin with the
checkWordfunctionandaddthisnewfeature.Therearethreekeydifferencesbetweenthenewversionand
theoldone:

1.Weneedtocreatealistofthelowercaseciphertextlettersinouroriginalpattern.
2.Weneedtoaddcapturegroupstotheregularexpression.
3.Whenwegetamatchingword,wemustsavethematchinglettersfromthecapturegroupsalongwith
theirciphertextequivalents.

Listing8.13showstheentirenewversionofcheckWordnowcalledfindLetters.Letslookatthelineswhere
therearesignificantdifferencesfromtheoldcheckWordfunction.Thefirstthingwewanttodoiscreatealist
ofallthelowercaseciphertextlettersinthepartiallydecryptedword(pattern).Youcouldwritealooptodo
thisandbuildthislistyourselfbycheckingoneletteratatime.However,theremodulehasafunctioncalled
findallthatdoesexactlythisjobforus.Itreturnsallthesubstringsofastringthatmatchaparticularregular
expression.Session8.15demonstratesthefindallfunctioninaction.Line4usesthefindallfunctiontocreate
thelistofciphertextletters.

>>>re.findall('[123]','1,234')
['1','2','3']

>>>re.findall('[1234]+','1,234')
['1','234']
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 24/29
8/23/2016 Yuzu: Python Programming in Context

>>>re.findall('[AZ]','HelloWorld')
['H','W']

Session8.15Findingalloccurrencesofaregularexpression

Wenextmakeaveryminorchangetoline6toaddparenthesesaroundourregularexpressionthatmatches
theundecodedciphertextletters.Thisensuresthateachinstanceofalowercaseletterinthepatternwillend
upinitsowncapturegroup.
Finally,lines1117makeuseofthematchobjectandthegroupsfunction.Whenamatchismadeagainsta
word from the word list file, the call myMatch.groups() gives us the letters that were used to make the
matchingword.Atthispointwehavetwoliststhatcontainimportantinformation.ThectLetterslistcontains
theoriginalciphertextlettersandthematchingLetterslistcontainstheplaintextlettersthatcompletetheword.
WemustnowmatchthelettersfromctLetterswiththecorrespondinglettersfrommatchingLetters.Itiseasy
toknowwhichletterfromctLetterscorrespondstoaletterinmatchingLettersbecausetheyareparallellists.
The first letter in ctLetters corresponds to the first letter in matchingLetters and so on. We could write our
ownfunctionthatacceptstwolistsandputstheletterstogetherintoanewlistmovingthroughthelistsone
itematatime,butPythonhasahandyfunctionthatdoesthisforus.Itisthezipfunction,whichtakestwo
lists and zips them together, matching the first item from list1 with the first item from list2 and so on.
Session8.16illustrateshowzipworks.

Listing8.13Showingthematchingletters

>>>zip([1,2,3],[4,5,6])
[(1,4),(2,5),(3,6)]

>>>zip(['a','b','c'],['Z','Y','X'])
[('a','Z'),('b','Y'),('c','X')]

>>>zip(['a','b','c'],['Z','Y','X'],[1,2,3])
[('a','Z',1),('b','Y',2),('c','X',3)]

Session8.16Usingthezipfunctiontocreatealistoftuples
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 25/29
8/23/2016 Yuzu: Python Programming in Context

Session8.17showshowthefindLettersfunctioncanhelpusfinishdecodingtheciphertext.Onthefirstline
findLetter searches for the mappings that use the partially decrypted word 'AiiInAL'. We discover that
'AiiInAL'isagoodmatchfortheword'arrival'.Thethirdlineofthesessionshowsusthattheciphertextletter
imapstotheplaintextletterRandnmapstotheplaintextletterV.
AdditionalcallstofindLettersgiveusthemappingsform,g,i,r,q,andn.Nowamessageisreallystartingto
show.Withjustafewmoresubstitutionswewillhavetheentiremessagedecoded.

>>>findLetters('bcjkmpqruvwxyz','AiiInAL')
['i','i','n']
['arrival',[('i','R'),('i','R'),('n','V')]]

>>>findLetters('bcjkmpqruvwxyz','ALiEADq')
['i','q']
['already',[('i','R'),('q','Y')]]

>>>findLetters('bcjkmpqruvwxyz','giEErE')
['g','i','r']
['breeze',[('g','B'),('i','R'),('r','Z')]]

>>>findLetters('bcjkmpqruvwxyz','mNmSmAL')
['m','m','m']
['unusual',[('m','U'),('m','U'),('m','U')]]

>>>ciphertext=ciphertext.replace('m','U')

>>>ciphertext=ciphertext.replace('g','B')

>>>ciphertext=ciphertext.replace('i','R')

>>>ciphertext=ciphertext.replace('r','Z')

>>>ciphertext=ciphertext.replace('q','Y')

>>>ciphertext=ciphertext.replace('n','V')

Session8.17DemonstratingthefindLettersfunction

HE REaAINED STANDING AT THE EDGE OF THE dIT THAT THE THING HAD aADE FOR
ITSELF STARING AT ITS STRANGE AddEARANwE ASTONISHED wHIEFLY AT ITS
UNUSUALSHAdEANDwOLOURANDDIaLYdERwEIVINGEVENTHENSOaEEVIDENwE
OFDESIGNINITSARRIVALTHEEARLYaORNINGWASWONDERFULLYSTILLANDTHE
SUNxUSTwLEARINGTHEdINETREESTOWARDSWEYBRIDGEWASALREADYWARaHE
DIDNOTREaEaBERHEARINGANYBIRDSTHATaORNING THERE WAS wERTAINLY NO
BREEZESTIRRINGANDTHEONLYSOUNDSWERETHEFAINTaOVEaENTSFROaWITHIN
THEwINDERYwYLINDERHEWASALLALONEONTHEwOaaON

Fromhereweneedonlyacouplemoresubstitutionstocompletethemessage,asshowninSession8.18.

>>>findLetters('cjkmpqwx','wOaaON')
['w','a','a']

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 26/29
8/23/2016 Yuzu: Python Programming in Context

['common',
[('w','C'),('a','M'),('a','M')],
'pompon',
[('w','P'),('a','M'),('a','P')]]
>>>ciphertext=ciphertext.replace('w','C')

>>>ciphertext=ciphertext.replace('a','M')
>>>findLetters('jkpqwx','dIT')
['d']

['kit',
[('d','K')],
'pit',
[('d','P')],
'wit',
[('d','W')]]

>>>findLetters('jkpqwx','AddEARANCE')
['d','d']
['appearance',[('d','P'),('d','P')]]

>>>ciphertext=ciphertext.replace('d','P')
findLetters('jkpqwx','xUST')
['x']
['just',[('x','J')]]

Session8.18Thelastsubstitutions

Table8.8Finalmappingoftheletters

HEREMAINEDSTANDINGATTHEEDGEOFTHEPITTHATTHETHINGHADMADEFOR
ITSELF STARING AT ITS STRANGE APPEARANCE ASTONISHED CHIEFLY AT ITS
UNUSUALSHAPEANDCOLOURANDDIMLYPERCEIVINGEVENTHENSOMEEVIDENCE
OF DESIGN IN ITS ARRIVAL THE EARLY MORNING WAS WONDERFULLY STILL AND
THE SUN JUST CLEARING THE PINE TREES TOWARDS WEYBRIDGE WAS ALREADY
WARM HE DID NOT REMEMBER HEARING ANY BIRDS THAT MORNING THERE WAS
CERTAINLY NO BREEZE STIRRING AND THE ONLY SOUNDS WERE THE FAINT
MOVEMENTS FROM WITHIN THE CINDERY CYLINDER HE WAS ALL ALONE ON THE
COMMON

Table8.8showsthefinalkeythatmapstheciphertextletterstotheplaintextletters.NoticethatthekeyGen
functionwewroteearlierwasusedtocreatethekeyfromthepasswordhgwells.

8.4.6RegularExpressionSummary
We conclude this section with a summary of the regular expression syntax and the functions we have used
fromtheremodule.ThesearesummarizedinTables8.9and8.10.

. Matchanycharacter.

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 27/29
8/23/2016 Yuzu: Python Programming in Context

[abc] Matchaorborc.
[^abc] Matchanycharactersotherthanaorborc.
[abc]+ Matchoneormoreoccurrencesofthecharactersabcforexample,borabaorccba.
[abc]* Matchzeroormoreoccurrencesofthecharactersabcforexample,borabaorccba.
(regex) Createacapturegroup.

Table8.9Simpleregularexpressionsyntax

Method Use Explanation


match re.match('[abc]XY.') Matchesanystringthatstartswitha,b,or
c followed by XY followed by any
character. Returns a match object on
successorNone.
sub re.sub('[tv]','X','vxyzbgtt') Returns'XxyzbgXX'.Likereplaceexcept
touseregularexpressionmatching.
findall re.findall('[bc]+','abcdefedcba') Returns ['bc','cb']. Returns a list of all
substrings matching the regular
expression.
groups matchObj.groups() Returns a list of all capture groups
matched.matchObjiscreatedbyacallto
match.
group matchObj.group(2) Returnsasinglecapturegroup.matchObj
iscreatedbyacalltomatch.

Table8.10Regularexpressionmodulefunctions

Exercises
8.17Writearegularexpressionpatterntomatchallwordsendingins.

8.18Writearegularexpressionpatterntomatchallwordsendingining.

8.19Writearegularexpressionpatterntomatchallwordswithssanywhereinthestring.

8.20Writearegularexpressionpatterntomatchallwordsbeginningandendingwiththelettera.

8.21Writearegularexpressionpatterntomatchallthewordsthatstartwithst.

8.22Writearegularexpressiontomatchallthefourletterwordswherethemiddletwolettersarevowels.

8.23WriteafunctionthatcanextractthehostnamefromaURL.ThehostnameisthepartoftheURLthat
comesafterhttp://butbeforethenext/.

8.24Writeafunctionthatcantakeafilenameandreturnthenameandthesuffixastwoseparatestrings.
Note:Inordertomatchthe.characterinaregularexpression,youmustuse\..

8.5Summary
https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 28/29
8/23/2016 Yuzu: Python Programming in Context

Inthischapterweexploredcryptanalysisbyimplementingtechniquesforreadingencryptedtext.Weutilized
many past features of Python, including lists and dictionaries. However, in this chapter we extended these
ideas by using lists and dictionaries as containers for other lists and dictionaries. We used a brute force
solutiontosystematicallysolveahardproblem.Partofthatsolutionintroducedapatterncalledminmaxfor
systematicallyfindingabetteranswergivenabestanswersofar.Finally,weintroducedthenotionofa
regularexpressionandshowedhowthispowerfulpatternmatchingtoolcanhelpustolocatemissinglettersin
partiallydecryptedtext.

KeyTerms
bruteforcemethod
capturegroup
columnmajorstorage
cryptanalysis
frequencyanalysis
minmaxpattern
patternmatching
railfencecipher
regularexpression
rowmajorstorage
substitutioncipher

PythonKeywords
cmp
for
if
join
None
sort

Bibliography

[Sch94] Bruce Schneier. Applied Cryptography: Protocols, Algorithms, and Source Code in C. Wiley,
1994.
[Sin00] Simon Singh. The Code Book: The Science of Secrecy from Ancient Egypt to Quantum
Cryptography.Anchor,2000.
[wor] Translatum.Retrievedfromhttp://www.translatum.gr/dictionaries/downloadenglish.htm.

https://reader.yuzu.com/#/books/9781284089042/c/6/30!/4/540@0:45.8 29/29
8/23/2016 Yuzu: Python Programming in Context

CHAPTER9
Fractals:TheGeometryofNature

9.1Objectives
Tointroducefunctionalprogrammingstyle
Topracticewritingrecursivefunctions
Tointroducegrammarsandproductionrules

9.2Introduction
TakealookatthetreeinFigure9.1.Noticethatyoucouldchooseanyofthemajorbranchesofthattree,cutit
off,anditwouldbearastrongresemblancetotheentiretree.Furthermore,youcouldcutoneofthebranches
offofthatbranchanditwouldresembletheentiretree.Infact,youcouldcontinuetoprunethetreedownto
thesizeofsomesmalltwigsandyouwouldseethateventhetwigshavethesamebasicshapeandstructureas
theentiretree.Thisselfsimilarityatsmallerandsmallerscalesistheessenceofafractal.
Now take a look at the tree in Figure 9.2. Not only is it reminiscent of the real tree, but it is even more
obviouslyselfsimilar.Infact,thistreewasdrawnusingaturtletreedrawingprogramthatusesthefollowing
simpleinstructions:

1.Drawatrunkthatisnunitslong.
2.Turntotheright30degreesanddrawanothertreewithatrunkthatisn15unitslong.
3.Turntotheleft60degreesanddrawanothertreewithatrunkthatisn15unitslong.

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 1/19
8/23/2016 Yuzu: Python Programming in Context

Figure9.1Atree

Figure9.2Afractaltreedrawnwithturtle

AtfirstglanceyoumaythinkthatallthiswoulddoisdrawaY.Butrememberthateachtimewedrawatree
wefollowthesamesetofsteps.Thosestepsincludedrawingatrunkandthenasmallertreetotherightanda
smallertreetotheleft.Whatwehavejustdescribedisarecursiveprocessfordrawingatree.

9.3RecursivePrograms
Before we look at the program used to draw the tree we must first learn a little more about recursion. In
computer science and mathematics a recursive function is a function that calls itself. A very simple, but
erroneous,illustrationofarecursivefunctionisshowninListing9.1.

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 2/19
8/23/2016 Yuzu: Python Programming in Context

Listing9.1Anerroneousrecursivefunction

The hello function prints the message Hello World and then calls itself again. If you ran this function in
Python, the program would continue to call itself and print Hello World until Python simply crashed. A
moreusefulwayofthinkingaboutrecursivefunctionsisasfollows:

1.Arewedoneyet?Havewefoundaproblemthatissmallenoughtosolvetrivially?Ifso,solveit
withoutanyfurtherworkandreturntheanswer.
2.Ifnot,simplifytheproblem,andsolvethesimplerproblem.Combinetheresultofsolvingthe
simplifiedproblemwithwhatyouknowtosolvetheoriginal.Returnthecombinedresult.

Thefirststepiscalledthebasecase.Everyrecursiveprogrammusthaveabasecaseinordertoknowwhen
tostop.ThisispreciselythethingthatismissingfromtheprograminListing9.1.
Thesecondstepisoftencalledtherecursivestep.Therecursivestepinvolvessimplifyingtheproblemina
way that moves it closer to the base case. Sometimes you will need to combine the result returned by the
recursive call with some data you saved when you simplified the problem. In other cases the problem is
solvedjustbymakingthesimplifyingstep.

9.3.1RecursiveSquares
LetslookataproblemofdrawingthenestedboxesshowninFigure9.3.

Figure9.3Nestedboxes

Inthiscaseweneedtodrawaseriesofsquares.Weknowhowtodrawasinglesquare.Buthowdowedraw
theentirepicture?Letsstartbyidentifyingthebasecase.Sincedrawingasquarethathasasidelengthof1
unit is the smallest possible square, we will identify that as the base case. The simplification step is to
recursivelydrawsmallerandsmallersquaresuntilwegettothepointwherethesidelengthis1.
Listing9.2showsthecodeforthenestedboxes.ThedrawSquarefunctionshouldlookfamiliartoyou.The
nestedboxfunctionisarecursiveprocessthatfollowsthetwoimportantstepsoutlinedpreviously.First,line
7checksforthebasecase.Theexpressionside>=1willevaluatetoFalseassoonasthesideparameteris
lessthan1.Becausethereisnoelseclause,noticethatassoonasthesideislessthan1,thenestedBoxsimply
https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 3/19
8/23/2016 Yuzu: Python Programming in Context

returnswithoutdoinganything.
Whenthevalueofsideisgreaterthan1,wedrawasquarethathasasidelengthofside.Afterthesquareis
drawn,thenextstepistocallnestedBoxrecursively,reducingthesidelengthby5.Byreducingthevalueof
sideby5wearemovingtowardthebasecase.

Listing9.2Recursivelydrawingboxes

Exercises

9.1RunthenestedBoxfunctionwithaninitialsideof200,thenmodifynestedBoxbyreversinglines8
and9.Canyouexplainthedifferentbehavior?

9.2 Write a recursive function to draw concentric nested boxes where each box is centered around the
samepoint.YoumayfindithelpfultowriteahelperfunctioncalleddrawCenteredSquarethataccepts
parametersforaturtle,thecenterxandypositions,andthelengthoftheside.

9.3.2ClassicRecursiveFunctions
Itisalsooftenpossibletouserecursiontoexpressmathematicalfunctionsinanelegantway.Thefollowing
exercisesallowyoutoexploresimplerecursionbywritingsomenongraphicalfunctions.

Exercises

9.3Thefactorialfunctionisdefinedasfollows:

Writearecursivefunctionfactthatcomputesthefactorialofanypositiveinteger.

9.4Writearecursivefunctiontocomputethesumofallthenumbersinalist.

9.5Writearecursivefunctiontofindtheminimumnumberinalist.

9.6Writearecursivefunctiontofindthemaximumnumberinalist.

9.7Writearecursivefunctiontoreversethecharactersinastring.

9.8Writearecursivefunctiontodecideifagivenstringisapalindrome.

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 4/19
8/23/2016 Yuzu: Python Programming in Context

9.3.3DrawingaRecursiveTree
LetsreturntotheproblemofdrawingthetreeinFigure9.1.Recallthattheinstructionsfordrawingthetree
areasfollows:

1.Drawatrunkthatisnunitslong.

2.Turntotheright30degreesanddrawanothertreewithatrunkthatisn15unitslong.

3.Turntotheleft60degreesanddrawanothertreewithatrunkthatisn15unitslong.

Nowletsapplytherulesforrecursiontoimplementaprogramthatfollowsthetreedrawinginstructions.
First,wemustidentifythebasecase.Whendrawingatree,thebasecaseisatreewherethetrunkislessthan
somepredefinedvalue.Wecouldsaythatatrunklengthof1isthesmallestpossibletreewecoulddraw,but
infactwecanpickanynumbertouseasthesmallesttrunklength.Ifthetrunklengthislessthanorequalto
ourpredefinedminimum,wecansimplystopdrawingnewtreesandreturn.
Wehavealreadyseentherecursivestepindrawingthetree.Infact,therearetwoofthem.Steps2and3both
specifythatweneedtodrawanothertree.Itisalsoimportanttonoticethatnotonlyarewedrawinganother
tree(recursively),butwearealsomakingprogresstowardthebasecasebydrawingatrunkthatissmaller
thanthecurrenttrunklength.Listing9.3showsourrecursivePythonfunction.
The amazing thing about the tree function is its length. You will see that recursion often offers us the
capabilityofelegantlycapturingacomplexprocess.Letslookatthecodecarefullyandseehowitcompares
withtheruleswedefined.First,noticethatwecheckforthebasecaseonline2.Wehavewrittentheprogram
slightlylongerthannecessarytomakeitclearthatwearecheckingforthebasecase.Theexplicitreturnand
theelseclausecouldberemovediftheconditionalwaschangedtotrunkLength>=5.

Listing9.3Arecursivetreefunction

Ifwehavenotreachedthebasecase,theturtlemovesforwardandturnstotheright30degrees.Thenonline
7wemakethefirstrecursivecalltodrawasmallertreeontherightsideofthetrunk.Whenthissideofthe
trunkisdone,thenthefunctionreturnsandturns60degreestotheleft,andtheleftsideofthetrunkisdrawn.
Runthisprogramandcarefullywatchtheorderthatthebranchesaredrawnin.Youwillnoticethatnoleft
sidebranchesaredrawnuntilwehavereachedourfirstinstanceofabasecase.Youwillalsonoticethatthe
turtleneedstobeturned90degreestotheleftbeforemakingtheinitialcalltothefunctionsincewewantthe
treetogrowup.

Exercises

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 5/19
8/23/2016 Yuzu: Python Programming in Context

9.9RewritethetreefunctionusingtheconditionaltrunkLength>=5tocheckforthebasecase.

9.10Swaptherulesforthetreesothatitdrawstheleftsideofthetreebeforetheright.

9.11Youcancreateamoreinterestingandrealisticlookingtreebyrandomizingtheangletheturtleturns.
Ratherthanalwaysusinga30degreeangle,selectananglebetween15and45degrees.

9.12Youcanaddadditionalrealismbyrandomizinghowmuchthebranchesshrinkeachtimeyoumakea
recursivecall.Insteadofalwayssubtracting15,trysubtractingarandomamountbetween5and25.

9.13 Finally, you can add color to the tree by making the large branches brown, and the small branches
green.Chooseathresholdvalueforthelengthofthetrunkandsetthecoloraccordingly.

9.3.4TheSierpinskiTriangle
LetslookatanothersimplefractalcalledtheSierpinskitriangle.Imaginethatyouhavethreetrianglesthat
areallthesamesize.Ifyoutakethefirsttwotrianglesandputthemsidebysideandthenbalancethethird
triangle on the peaks of the first two triangles, you will get a new, larger triangle composed of those three
triangles. Further suppose that you constructed each of the three original triangles from three smaller
triangles,andeachofthethreesmallertriangleswiththreesmallertrianglesstill.Figure9.4givesyouanidea
about what is involved. The idea of selfsimilarity at smaller and smaller scales is key to the concept of a
fractal,anditiskeytounderstandingrecursion.
Writing a program to draw the Sierpinski triangle is more complex than the fractal tree, but it is still
surprisinglysimple.Wewillinventasimplebasecaseforthisprogramcalledthedepth,whichrepresentsthe
numberoftimeswesubdividetheoriginaltriangle.Whenwereachasufficientdepth,wethendrawatriangle
of the appropriate size for that depth. In fact, we start the depth with a positive number, and each time we
recursively divide a triangle we subtract one from the depth. When we reach a depth of 0, we then finally
drawaverysmalltriangle.
The Sierpinski triangle shown in Figure9.4 shows a triangle of depth 2. Notice that there are nine shaded
triangles,whichrepresentthetrianglesdrawnbythebasecase.Thetrianglesnumbered1,5,and9werenot
drawnbutareartifactsofthethreesurroundingtriangles.Figure9.5showsaSierpinskitriangleofdepth5.
The sierpinski function will accept three points as parameters. These points define the three corners of the
triangle.Tosubdividealargetriangleintothreesmallertriangles,weusethefollowingrule:Foreachcorner
ofthelargetriangle,createasmalltriangleusingthegivencornerandthepointsthatarehalfwaybetweenthe
given corner and the other two corners. For example, consider triangle ABC in Figure 9.6. The three new
triangleswecanconstructaretrianglesA13,B21,andC32.

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 6/19
8/23/2016 Yuzu: Python Programming in Context

Figure9.4TheSierpinskitriangle

Figure9.5ASierpinskitriangleofdepth5

Figure9.6Splittingatriangleequally

Points 1, 2, and 3 are calculated by finding the midpoint of line segments AB, BC, and CA, respectively.
Recall that we can calculate the midpoint of a line segment (mx, my) using the equation

Drawingthesmallertrianglesistherecursivestepinourfunctionandwillinvolvethreerecursivecallstothe
sierpinskifunctionusingtheverticescalculatedbythemethodjustdescribed.
Thefunctionwewillwriterequirestwosimplehelperfunctions:adrawtrianglefunctionandafunctionto
calculate the coordinates of a point halfway between two other points. The drawTriangle function takes a
turtle and three points as parameters. The goto method is used to simply connect the three points. The
midPointfunctionsimplyencodestheequationfromaboveandreturnsatuplegivingthexandycoordinates
ofthemidpoint.

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 7/19
8/23/2016 Yuzu: Python Programming in Context

Listing9.4DrawingaSierpinskitriangle

ThesierpinskifunctionandthetwohelperfunctionsareshowninListing9.4.Notethatthefinalversionof
the function has a parameter for the turtle as well as a parameter for the depth. The first line of sierpinski
checks to see whether the depth is greater than zero. If that is the case, we make three recursive calls to
sierpinski.Eachrecursivecallrepresentsoneofthethreesubtriangles.Whenwemaketherecursivecall,we
subtract1fromthedepthtomovetowardthebasecase.Whenthefunctionreachesadepthofzero,nomore
recursionisrequired.Atthispointatriangleisdrawnandthefunctionsimplyreturns.Notethatthelargerthe
depthspecifiedoriginally,thesmallerthistrianglewillbe.

Exercises

9.14Trycallingsierpinskiwithdifferentshapedtriangles.

9.15ModifythesierpinskianddrawTrianglefunctionstoaddcolor.Hint:Makeeachrecursivecalldrawin
adifferentcolor.

9.16Modifythesierpinskifunctionsothatitunconditionallydrawsatriangleateachdepthratherthanjust
atadepthof0.

9.17Changetheorderoftherecursivecallsinsidethesierpinskifunction.

9.3.5CallTreeforaSierpinskiTriangle
Hopefullybynowyouhavetriedtorunthesierpinskifunctiononyourownafewtimes,anditshouldnowbe
clearthatthetriangleisdrawninthirds.Afterathirdofthelargertrianglehasbeencompleted,thenthenext
thirdwillbedrawnandfinallythelastthirdwillbedrawn.Withineachsmallerthird,thesamebehaviorholds
true.
Wecangraphthisbehaviorusingacalltree,whichenablesustoordertherecursivecallstothesierpinski
functiontoexplainwhythetriangleisdrawnintheorderitis.ThecalltreeforaSierpinskitriangleofdepth2
isshowninFigure9.7.Noticethatthelabelsonthearrowsonthecalltreematchthetrianglenumbersshown
inFigure9.4.

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 8/19
8/23/2016 Yuzu: Python Programming in Context

Figure9.7ThecalltreeforaSierpinskitriangleofdepth2

Exercises

9.18MakeacalltreeforaSierpinskitriangleofdepth3.

9.19YoucangenerateaSierpinskisquarebysubdividingasquarerecursively.Takeasquare,divideitinto
3by3squares.Applythedivisionalgorithmtoeverysquareexceptthecenterone.Whenyougettoa
sufficientlysmallsquare,stopsubdividinganddrawthesquare.

9.4Snowflakes,Lindenmayer,andGrammars
HereinnorthernIowawegetalotofsnow.Itissaidthatnotwosnowflakesareexactlyalike.Snowflakesare
another item in nature that can be reproduced by using fractal techniques. The photo in Figure 9.8 is of a
snowflake.ThesnowflakeinFigure9.9wasproducedusingtheKochcurve,afractalalgorithmdeveloped
byHelgevonKochin1904.
A Koch snowflake is constructed from several Koch curves. Lets look at Kochs algorithm for drawing a
curveinmoredetail.Thesimplestpossiblecurveisactuallyastraightlineoflengthn,thebasecase.Thenext
easiestcurvecanbedescribedintermsofthefollowingsetofturtleinstructions:

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 9/19
8/23/2016 Yuzu: Python Programming in Context

Figure9.8Photoofasnowflake.CourtesyofNOAA.

Figure9.9AsnowflakeproducedusingafractalalgorithmdevelopedbyHelgevonKochin1904.

forward(n)
left(60)
forward(n)
right(60)
right(60)
forward(n)
left(60)
forward(n)

Ifyouthinkrecursivelyaboutthecurve,youcanimaginethateachstraightlinecanbereplacedbyasetof
shorterlinesfollowingtheinstructionsabove.Figure9.10illustratestheKochcurveatseverallevelsofdetail.
AtthispointyoucouldwriteasimplerecursivefunctiontoplotaKochcurve.(Infact,Exercise9.23asksyou
todoso.)Butletslookatadifferentwayofthinkingaboutfractalsandrecursion.

Figure9.10FourlevelsofaKochcurve

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 10/19
8/23/2016 Yuzu: Python Programming in Context

9.4.1LSystems
In 1968 Astrid Lindenmayer, a biologist, invented the Lsystem,aformalmathematicaltheorydesignedto
modelthegrowthofbiologicalsystems.YoucanthinkofLsystemsascontainingtheinstructionsforhowa
simple cell can grow into a complex organism. Lsystems can be used to specify the rules for all kinds of
fractals.
Asitturnsout,Lsystemsarebasedonaformalideafromcomputersciencecalledagrammar,whichmay
beusedformanypurposes,includingthespecificationofprogramminglanguages.Agrammarconsistsofa
setofsymbolsandoneormoreproductionrules.Theserulesspecifyhowonesymbolcanbereplacedby
oneormoreothersymbols.Aproductionrulehastwoparts:(1)theleftside,whichspecifiesasymbol,and
(2) the right side, which specifies the symbols that can replace the one on the left. Lets look at a simple
exampleofagrammar:

Axiom A
Rules AB
BAB

We can interpret this grammar as follows, beginning with the axiom, the starting point. Axiom A is the
simpleststringyoucanproduceinthegrammar.Youmayalsothinkoftheaxiomintermsofabasecasefor
thegrammar.Therulestellushowwecanconstructmorecomplicatedstringsthatarepartofthegrammar.
Forexample,rule1tellsusthatwecansubstitutethestringBforA,andrule2tellsusthatwecanchangethe
stringBintothestringAB.
Whenyouapplytherulestoastring,youmustworkfromlefttoright,applyingtherulewheretheleftside
matchesthecurrentsymbol.Ifarulematches,thenyoumustapplytherule.Letslookatanexample:

A Axiom
B (applyrule1toA)
AB (applyrule2toB)
BAB (applyrule1toA,thenapplyrule2toB)
ABBAB (applyrule2toB,thenrule1toA,thenrule2toB)
BABABBAB (rulesapplied:1,2,2,1,2)

Trytoapplytherulesandproduceafewmorelinesonyourown.Ifyouareapplyingtherulescorrectly,you
willnoticethatthelengthofeachstringfollowstheFibonaccisequenceofnumbers:1,1,2,3,5,8,13,21,
.
To make the transition from these simple Lsystems to an Lsystem that a turtle can use to draw pictures,
consider the interpretation of the symbols used to define the Lsystem. Suppose that rather than using the
symbolsAandBweusedthesymbolsF,B,+,and.Findicatesthattheturtleshouldmoveforwardand
drawaline.Bindicatestheturtleshouldmovebackwardwhiledrawingaline.The+symbolindicatesthat
theturtleshouldturnrightandindicatesthattheturtleshouldturnleft.
Lets return to the Koch curve and consider the set of steps we outlined to draw a simple curve. We can
specifythefollowingsetofactionsasanLsystem:

Axiom F
Rule FFF++FF
https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 11/19
8/23/2016 Yuzu: Python Programming in Context

Notice that the axiom gives us a straight line, which is the simplest Koch curve. But if we apply the
productionruleonetime,wegetthestringFF++FF.ThisgivesusthenextsimplestKochcurve.Applying
theproductionruletothestringFF++FFgivesusthestringFF++FFFF++FF++FF++FFFF++F
F.
Givenastringofinstructions,itisstraightforwardtowriteaPythonfunctionthatcouldinterpretthestring
andmaketheappropriatecallstoturtlemethods.TosuccessfullydrawaKochcurve,wewouldneedtoknow
thedistancetohavetheturtlegoforward,alongwiththeanglefortheturtletoturn.
Listing9.5showsthecodeforthefunctiondrawLS.ThedrawLSfunctionsimplyiteratesovereachcharacter
in the instruction string. When an F or B is encountered, the turtle is instructed to move forward or
backward distance units. When a + or is encountered, the turtle is instructed to turn right or left by
angledegrees.Ifanyothercharacterisencountered,anerrormessageisprinted.

Listing9.5AsimplefunctiontofollowanLsystemstring

Exercises
9.20 Use the drawLS function to draw the string FF++FFFF++FF++FF++FFFF++FF. Use an
angleof60degreesandadistanceof20.

9.21ApplytheproductionrulefortheKochcurveonemoretimeanddrawtheresultsusingdrawLS.

9.22ExperimentwiththedrawLSfunctionusingdifferentanglesanddistances.

9.23WriteasimplerecursivefunctiontodrawaKochcurve.

9.24UsingthefunctionforaKochcurve,drawasnowflakewithsixsides.

9.25OneofLindenmayersveryfirstLsystemswasasetofrulestomodelthegrowthofalgae.Therules
areasfollows:

Axiom A
Rules AAB
BA

Apply these rules to construct five new strings. The length of each string represents the number of
algaecells.

9.26Hereisanothersimplesetofrulesthatgeneratecantordust.

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 12/19
8/23/2016 Yuzu: Python Programming in Context

Axiom A
Rules AABA
BBBB
Applytherulestoproducefivenewstrings.

9.4.2AutomaticallyExpandingProductionRules
ApplyingtheproductionrulesinanLsystemcanbeverytedious,soletswriteafunctiontoautomatethe
process.WefirstneedtodeterminehowproductionrulescanberepresentedinPython.Infact,thisproblem
issimilartothetranslationprobleminChapter4.ThesolutionistouseaPythondictionary.
Adictionarycanrepresentanynumberofproductionruleswheretheleftsideoftheruleisthekeyandthe
right side of the rule is the value. For example, the set of production rules that generates the Fibonacci
numbersisasfollows:

Axiom A
Rules AB
BAB

ThePythondictionarythatcorrespondstotheserulescanbestoredas

Now lets write a Python function to take an initial string and apply a set of production rules a specified
numberoftimes.Listing9.6showsthePythonfunctionapplyProduction.

Listing9.6Automaticallyapplyingproductionrules

Everything in the applyProduction function should look familiar. The outer loop allows us to apply the
production rules to the string n times. The inner loop is a simple accumulator pattern that allows us to
construct a new string by applying the production rules one character at a time. Notice the statement
rules.get(ch,ch)shownonline5.Thisstatementelegantlyhandlesthecasewherethereisacharacterbutno
productionruleforexpandingthecharacterfurther.Recallthatthegetmethodallowsustospecifyadefault
value for the case that a key is not found in the dictionary. In this instance when character ch has no
productionrule,wesimplyleavethecharacterinplace.
Session9.1showstheapplyProductionfunctioninactionusingtheFibonacciproductionrules.

>>>axiom='A'
>>>myRules={'A':'B','B':'AB'}
>>>foriinrange(10):
res=applyProduction(axiom,myRules,i)
print("%3d%s"%(len(res),res))

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 13/19
8/23/2016 Yuzu: Python Programming in Context

1A

1B
2AB
3BAB
5ABBAB
8BABABBAB
13ABBABBABABBAB
21BABABBABABBABBABABBAB
34ABBABBABABBABBABABBABABBABBABABBAB
55BABABBABABBABBABABBABABBABBABABBABBABABBABABBABBABABBAB
>>>

Session9.1Usingtheproductionruleexpander

Exercises
9.27UsetheapplyProductionanddrawLSfunctionstodrawaKochcurveoflevel5.

9.28WecaneasilydrawaKochsnowflakeusingLsystemrulesbystartingwithamorecomplexaxiom.
UsetheapplyProductionanddrawLSfunctionstodrawafractalusingtheaxiomF++F++Fandthe
productionruleFFF++FF.

9.29 Using an angle of 90 degrees and the following Lsystem, draw the resulting figure for several
differentlevelsofexpansion.

Axiom FFFF
Rules FFF+F+FFFF+F
ThisfigureisknownasthequadricKochisland.

9.4.3MoreAdvancedLSystems
InordertouseLsystemstomodelplantgrowthweneedtoaddonemorefeaturetotheLsystemsgrammar:
thecharacters"["and"]".Theleftsquarebracketandrightsquarebracketcharactersrepresentoperationsto
saveandrestorethestateoftheturtle.Thatiswhenevera"["isencounteredinthestring,thepositionand
heading of the turtle is saved. When a "]" is encountered, the position and heading from the last save are
restored.
Usingthesenewcharacters,wecannowuseanLsystemtospecifythetreewedrewusingListing9.3.The
grammarforthisnewLsystemisasfollows:

Axiom X
Rules XF[X]+X
FFF

Startingwiththeaxiom"X"andapplyingtheproductionrulestwicegivesusthestringFF[F[X]+X]+F[
X]+X.Supposethatweuseanangleof60degreesandadistanceof20units.Thisstringinstructstheturtle
totakethefollowingactions:

1.Goforwardatotalof40units.
https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 14/19
8/23/2016 Yuzu: Python Programming in Context

2.SavethepositionandheadingcallthisstateW.
3.Turnleft60degrees.
4.Goforward20units.
5.SavethepositionandheadingcallthisstateZ.
6.Turnleft60degrees.
7.Donothing.Xisnotarecognizedcommand.
8.RestoretheturtlepositiontothesavedstatecalledZ.
9.Turnright60degrees.

10.Donothing.

11.RestorethepositionandheadingtothesavedstatecalledW.

12.Turnright60degrees.

13.Goforward20units.

AtthispointwehavesucceededinsimplydrawingaY.Butnoticethatthedifferencebetweenthisapproach
andthecodeweusedinListing9.3isthatwenolongerneedtohavetheturtlebackup.Byusingthesaveand
restoreoperations,wecansimplyjumptheturtlebacktoanypreviousposition.
Lets modify the drawLS function to support the save and restore operations. Listing 9.7 shows the new
function.Noticethatinthisversionwehaveremovedthefinalelsestatementwiththecorrespondingerror
message.Thisallowsthefunctiontosilentlyignoreanycommandsitdoesnotrecognize.ThestateSaverlist
allowsustoappendnewstatesthatwewanttosavetotheendofthelist.Usingthepopmethodallowsusto
restorethemostrecentlysavedstate.
Finally, lets write a simple function that combines all of our work into a single function that applies the
productionrulesanddrawstheresultingLsystem.Thelsystemfunctiontakessevenparameters:

axiom Theinitialaxiom
rules Adictionaryspecifyingtheproductionrules
depth The number of times to expand the
productionrules
initialPosition Theinitialpositionfortheturtle
heading Theinitialheadingfortheturtle

angle The angle to turn for the + or


operations
length The distance to move forward or backward
foranForB.

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 15/19
8/23/2016 Yuzu: Python Programming in Context

Listing9.7AnimprovedLsystemdrawingfunction

ThecodeforthelsystemfunctionisshowninListing9.8.

Listing9.8AfunctiontoexpandanddrawanLsystem

Session9.2showstheuseofthisfunctiontodrawasimpletree.

>>>myRules={'X':'F[X]+X','F':'FF'}
>>>axiom='X'
>>>lsystem(axiom,myRules,7,(0,200),90,30,2)

Session9.2CreatingandrunningasingleLsystem

Herearetherulesfordrawingsomethingthatlookslikeasprigofrosemary.

Axiom H
Rules HHFX[+H][H]
XX[FFF][+FFF]FX

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 16/19
8/23/2016 Yuzu: Python Programming in Context

Usinganangleof25.7degrees,wegettheimageshowninFigure9.11.
From here on you are on your own to explore the world of Lsystems. The following exercises have some
examplestogetyoustarted,buttherearemanymoreavailableifyoudosomeresearch.

Figure9.11Asprigofrosemarydrawnbyaturtle

Exercises
9.30 Modify the drawLS function to support the G operation, which allows the turtle to go forward
withoutdrawingaline.

9.31UsingyourmodifieddrawLSfunction,trythefollowingLsystem:

Axiom FFF
Rules FFFFGG
GGG

9.32ImplementthefollowingLsystemusinganangleof25:

Axiom F
Rules FF[F]F[+F]F

9.33ImplementthefollowingLsystemusinganangleof25:

Axiom F
Rules FFF+[+FFF][F+F+F]

9.34ImplementthefollowingLsystemusinganangleof45:

Axiom FX
Rules F
Y+FXFY+

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 17/19
8/23/2016 Yuzu: Python Programming in Context

XFX++FY

9.5Summary
Inthischapterweintroducedrecursion,apowerfulprogrammingtechniquethatallowsustoelegantlysolve
problems by taking advantage of selfreference. Fractals are graphically selfreferentialthat is, they
containsmallerversionsofthemselves.WecanimplementrecursionbysimplylettingaPythonfunctioncall
itself.Identificationofthenonrecursivecase,calledthebasecase,isimportanttomakesurethattherecursive
callsworkcorrectly.Finally,weimplementthenotionofLsystems,whichformallydenotefractalbehavior
usingasetofgrammarrules.Astringofsymbolscanbeproducedbyapplyingtheserulesoverandover.By
interpretingthisresultstringasturtlecommands,thefractalcanbedrawn.

KeyTerms
axiom
basecase
calltree
erroneous
fractal
grammar
Kochcurve
Lsystem
productionrule
recursion
recursivestep
symbol

PythonKeywords
def
elif
for
if

Bibliography

[Fla02] GaryWilliamFlake.TheComputationalBeautyofNature.MITPress,2002.
[Lin68] A. Lindenmayer. Mathematical models for cellular interactions in development. Journal of
TheoreticalBiology,18:280315,1968.
[Man82] BenoitB.Mandelbrot.TheFractalGeometryofNature.Freeman,1982.

ProgrammingExercises

9.1ModifytheLsystemfunctionsothatitrecursivelyexpandstherules.Thiswillallowyoutoadda
verticalbar("|")operatorthatmeansgoforwardbutthedistancetogoforwardisscaledaccordingto

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 18/19
8/23/2016 Yuzu: Python Programming in Context

thedepthoftherecursion.

9.2ResearchLsystemsandfindadifferentsetofproductionrulestoimplement.

https://reader.yuzu.com/#/books/9781284089042/c/6/32!/4/408@0:0 19/19
8/23/2016 Yuzu: Python Programming in Context

CHAPTER10
Astronomy

10.1Objectives
Toexploreclassesandobjectsfurther
Tounderstandhowtoconstructaclass
Towriteconstructors,accessormethods,andmutatormethods
Tounderstandtheconceptofself
Toexploreinstancedata
Toimplementagraphicalsimulationusingobjects

10.2Introduction
Haveyoueverwitnessedalunareclipse?Theresultingimagesofashadowedmoonwithchangesincolor
andbrightnesscauseustoaskquestionsastothedetailsofthisuncommonoccurrence.Welearnedinscience
classthatthisfascinatingcelestialeventiscausedbytherelativemotionoftheearth,sun,andmoonasthey
orbit around one another. Early astronomers such as Ptolemy and Copernicus built elaborate models of the
planets to show their relative positions or sizes. These models were constructed so that the motion of the
planetsinthemodelmatchedthemotionoftheplanetsinthenightsky.
In this chapter we take up the task of building a different type of planetary model, a software model. Our
modelwillconsistofplanets,suns,andmoonsanditwillalsobecapableofshowingrelativemovement.We
examinewaysthatmodernprogramminglanguagesandtechniquesallowrealsituationstoberepresentedand
manipulatedasprogrammingconstructs.

10.2.1Programming
Many people assume that computer science is all about programming. As we have seen thus far, computer
science is much more. It is the study of problems, the problemsolving process, and the solutions to those
problems.Thesesolutions,alsoknownasalgorithms,provideastartingpointforprogramming.
Programming is the process of taking an algorithm and encoding it into a notation that the computer can
understandandexecute.Thesenotationsarereferredtoasprogramminglanguages.Asyouarealreadyaware,
inthistextweareusingtheprogramminglanguagePythontorepresentoursolutions.
Programmingisanimportantpartofwhatacomputerscientistdoes.Itisthroughprogrammingthatwecreate
arepresentationofoursolution.However,thesolutionsthatweachieveareoftenimpactedbytheprocessand
languagethatwechoose.
Algorithmsdescribethesolutiontoaproblemintermsofthedataneededtorepresenttheprobleminstance
https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 1/30
8/23/2016 Yuzu: Python Programming in Context

andasetofstepsnecessarytoproducetheintendedresult.Programminglanguagesmustprovideawayto
representboththeprocessandthedatarequiredbythesolution.Inotherwords,programminglanguageslike
Pythonmustprovidecontrolstructuresanddatatypes.
We have already seen some of these basic requirements in action. We know that our programs process
statements in sequence. In addition, special statements such as if and for enable the flow of control
mechanismstoallowselectionanditerationofgroupsofstatements.Wecanalsouseabstractiontobundle
statements together into functions that when invoked can be passed parameters to allow them to do their
work.Insomecases,aresultisreturned.
Inthischapter,wefocusondata.Ofcoursewewillstillneedcontrolstructurestorepresentouralgorithms.
However,welearnthatspecificrequirementsforsolvingaproblemcanoftenbeincludedaspartofthedata
modelthatwebuild.

10.2.2ObjectOrientedProgramming
Pythonsupportstheobjectorientedprogrammingparadigm.ThismeansthatPythonconsidersdatatobe
veryimportantintheproblemsolvingprocess.Infact,inmanywaysdatabecomesthefocusofwhatwedo
whenconfrontedwithanewproblemtosolve.Objectorientedprogrammingisaboutobjectstheindividual
dataitemsthataremanipulatedtosolveaproblem.InPython,everydataitemisanobject.
Inordertounderstandobjects,youmustalsounderstandtheideaofaclass.Eachobjectmustbeaninstance
ofaclass.Classesareusedinobjectorientedprogrammingtodescribewhatanobjectwilllooklike,what
anobjectknowsaboutitself(itsinstancedata),andwhatanobjectcando(itsmethods).Classesareoften
described as templates for objects in that each object that belongs to a specific class will have the same
instancedataandmethods.However,sincethespecificvaluesoftheseinstancedataitemswillbedifferent,
theobjectswilllikelybehavedifferentlywhenaskedtoperformtheirmethods.
YouarealreadyfamiliarwithsomeoftheseideasfromyourworkwiththeTurtleclass.Whenwecreatea
turtleusingthestatementt=Turtle(),wearecreatinganinstanceoftheTurtleclass.Youcancreatemore
thanoneturtle,andeachturtlehasitsowninstancedata.Forexample,eachturtlehasaposition,aheading,a
color,andsoon.Eachturtlehasanumberofmethodsforexample,forward,left,andup.Thebehaviorofa
specificturtleisdependentuponthevaluesofitsowninstancedata.Soifoneturtlehasaredtail,itwilldraw
a red line when the forward method is called, but a turtle with a blue tail will draw a blue line when its
forwardmethodiscalled.

10.2.3PythonClasses
WehavealreadyexploredanumberofclassesthatareprovidedbyPython.Someofthese,suchasint,bool,
and float, are called primitive classes because they represent only a single value. For example, the integer
(object)5isaninstanceoftheclassint.Likewise,theobjectTrueisaninstanceoftheclassbool.
Similarly,wehaveexploredclassessuchasstrandlistthatdescribestringandlistobjects.Thesesocalled
collectionclassesprovideastructurethatallowsobjectstobegroupedtogether.Forexample,Session10.1
showsthatthelist[23,66,True]isacollectionofthreeobjects:twointegersandaBoolean.Theliststructure
providesanumberofmethodsthatwecanusetomanipulatethesecollections.Indexingallowsustoaskthe
listforoneofitsobjectsusingthelinearposition,alsoknownastheindex.Thereversemethodreversesthe
order of the items in the list. Note that we used the familiar dot operator to have the list object invoke the
reversemethod.

>>>mylist=[23,66,True]
>>>mylist[1]
66
>>>mylist.reverse()

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 2/30
8/23/2016 Yuzu: Python Programming in Context

>>>mylist
[True,66,23]

Session10.1Listsandlistmethods

Althoughitmaynotbeobvious,evenintegersusemethodstoperformbasicarithmetic.Session10.2shows
anintegervariablecalledcountwithaninitialvalueof1.Thenextstatementperformsanadditionusingthe
accumulatorpatternthatwehaveseenmanytimesbefore.Aswewouldexpect,thevalueofcountisnow2.
Thenextstatementlooksabitstrange.Thisistheaccumulatorpatternonceagain.However,therightsideof
theassignmentisactuallyusingaspecialmethodcalled__add__()toperformtheaddition.Thismethodis
definedintheintclassandreturnstheresultofaddingthevalueoftheobjecttothevalueoftheparameter,in
this case 1. It is worth noting that the indexing operator shown above is also a special method called
__getitem__().NoticethatmyList.__getitem__(1)isequivalenttomyList[1].

>>>count=1
>>>count=count+1
>>>count
2
>>>count=count.__add__(1)
>>>count
3
>>>
>>>
>>>mylist.__getitem__(1)
66
>>>

Session10.2Additionandindexingmethods

10.3DesigningandImplementingaPlanetClass
We now turn our attention to solving the problem of building a model of the solar system. To do so will
require that we consider the data that will be present. However, even with the rich set of builtin classes
provided by Python, it is often preferable to describe our problem and solution in terms of classes that are
specifically designed to represent the objects present in the problem. We begin by building a simple
representationofaplanetandthenwewilldesignandimplementaPlanetclass.
Todesignaclasstorepresenttheideaofaplanet,itisnecessarytoconsiderthedatatheplanetobjectswill
needtoknowaboutthemselves.Thevaluesoftheinstancedatawillhelptodifferentiatetheindividualplanet
objects.Weassumethateachplanethasaname.Eachplanetalsohassizeinformationsuchastheradiusand
themass.Wealsowanteachplanettoknowhowfaritisfromthesun.
Inadditiontodata,theclasswillprovidemethodsthataplanetcanperform.Someofthesemightbesimple,
such as returning the name of the planet. Other methods may require more computation. In Python, the
general format for defining a class begins with the keyword class followed by the name of the class and a
colon.Themethodsfortheclassareindentedundertheclassheading,andlookjustlikefunctiondefinitions.

classclassname:
defmethod1()

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 3/30
8/23/2016 Yuzu: Python Programming in Context

defmethod2()

10.3.1ConstructorMethod
The first method that all classes should provide is the constructor, which can be defined as the way data
objectsarecreated.TocreateaPlanetobject,wewillneedthefourpiecesofinformationlistedpreviouslyas
parameters: (1) name, (2) radius, (3) mass, and (4) distance. The constructor will then create instance
variablestoholdthesevalues.Eachinstancevariableholdsareferencetoanobject.
In Python, the constructor is always called __init__ . (Note that there are two underscores before and after
init.)Methodsinclassdefinitionsarewritteninthesamewayasotherfunctionsseenthusfar.Weprovidethe
nameofthemethodandthenacollectionofparametersthatdescribetheinitialstateofthenewobject.These
parameterswillbefilledinwithvalueswhenthemethodisinvoked.
Listing10.1showstheconstructorforthePlanetclass.Noticethateventhoughwestatedthatfourpiecesof
informationwouldbenecessarytoconstructaplanet,therearefiveformalparameters.Theextraparameter,
self,isaspecialparameterthatwillalwaysrefertotheobjectthatisbeingconstructed.Itmustalwaysbethe
firstparameterinthelist.Pythonautomaticallyaddsanactualparametercorrespondingtoselfwhenyoucall
theconstructor.Thismeansyoushouldnotexplicitlypassaparametercorrespondingtoself.
Line3definesaninstancevariablecalledname,whichisattachedtothespecialparameterselfbyadot.This
notation, self.name, appearing on the left side of an assignment statement in the constructor defines an
instancevariable.Sinceselfreferstotheobjectbeingconstructed,self.namereferstoavariableintheobject.
We call these variables instance variables. Lines 46 introduce the other three instance variables described
previously.

Listing10.1Planetclasswithaconstructor

We use the constructor to create an instance of the class by using the name of the class (we do not call
__init__ directly). Just as __add__ is called automatically when the + operator is used, __init__ is called
automatically when the class name is followed by the ( ) function call operator. Session 10.3 shows the
familiarassignmentstatement(similartocreatinganinstanceoftheTurtleclass).Asstatedabove,thecallto
the Planet constructor requires only four parameters even though it is defined to have five. The first
parameter, self, never receives an explicit value as it always refers implicitly back to the object being
constructed.NotethatevaluatingthereferencemyplanetshowsthatitisaninstanceofthePlanetclass.The
value0x58530istheactualaddressinmemorywheretheobjectisstored.

>>>myplanet=Planet("X25",45,198,1000)
>>>myplanet
<__main__.planetinstanceat>
>>>

Session10.3Usingtheconstructor

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 4/30
8/23/2016 Yuzu: Python Programming in Context

ThenewlycreatedobjectmyplanetisaninstanceofthePlanetclasswithanameof"X25",aradiusof45,a
mass of 198, and a distance of 1000. Figure 10.1 shows a logical view of this object. Note that we have
separated the object into two distinct layers. The inner layer, which we call the state, contains the instance
variable names. The outer layer contains the names of the methods. In both cases the names are simply
referencestotheactualobjects.

Figure10.1APlanetobject

Wedrawobjectsinthisfashiontosuggestthatthereisastrongrelationshipbetweenthemethodsofanobject
anditsinstancedata.

10.3.2AccessorMethods
Thenextmethodsthatwewillwritearecommonlycalledaccessormethodsastheyallowustoaccessthe
instancevariablesoftheobject.Thesearesometimesalsoreferredtoasgettermethodssincethewordget
oftenappearsinthename.Itistypicalthateachinstancevariablemighthaveanassociatedaccessormethod.
For example, Listing 10.2 shows the four accessor methods associated with the instance variables name,
radius,mass,anddistancefromthePlanetclass.
Session10.4showsafewofthesemethodsbeingusedwiththePlanetobjectnamedmyplanetcreatedearlier.
ThegetNamemethodreturnsthestringreferencedbythenameinstancevariable.
InthegetNamemethodwereferredtothenameinstancevariableasself.name,whereselfisasynonymfor
myplanet(selfandmyplanetbothreferencethesameobject).Inordertoseethismoreclearly,youcanaccess
theinstancevariabledirectlyusinganexpressionsuchasmyplanet.name.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 5/30
8/23/2016 Yuzu: Python Programming in Context

Listing10.2SimpleaccessormethodsinthePlanetclass

Althougheitherofthesetwotechniqueswillallowustoaccessinstancevariableswithintheobject,atthis
point it is preferable to use the accessor methods. Using the accessor methods provides a more formal and
controlledaccesstotheobject.Onalargesoftwareproject,itiscommonfortheinternalrepresentationofan
instance variable to change. An accessor method hides those internal changes from the user. The common
termforthispracticeisinformationhiding.

>>>myplanet.getName()
'X25'
>>>myplanet.getMass()
198
>>>myplanet.name
'X25'

Session10.4Usingaccessormethods

It is also possible to create accessor methods that return computed results based upon values of instance
variables. For example, if we assume that a planet is a sphere, we can write an accessor method that will
returnthevolumeoftheplanetsincetheradiusisalreadyaninstancevariable.Wecanalsoincludemethods
thatreturnthesurfaceareaaswellasthedensityoftheplanet.Listing10.3showsthesemethods.Eachof
theseattributes(volume,surface,anddensity)arequantities,notinstancevariables,thatcanbeascertained
through some computation or manipulation involving instance data. Session 10.5 shows these methods in
action.

Listing10.3AdditionalaccessormethodsinthePlanetclass

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 6/30
8/23/2016 Yuzu: Python Programming in Context

>>>myplanet.getVolume()
381703.50741115981
>>>myplanet.getSurfaceArea()
25446.900494077323
>>>myplanet.getDensity()
0.0005187272219291404
>>>

Session10.5Usingotheraccessormethods

Therearetwoadditionaldetailsthatweshouldlookatbeforemovingon.Inthemethodstocomputevolume
andsurfacearea,weusedthevalueofpifromthemathmodule.Thisrequiresthatweimportmathatthestart
ofthePlanetclass(beforetheclassdefinitionitself).Also,thecomputationfordensity,theresultofdividing
the mass by the volume, uses the getVolume method. To call the getVolume method, we must use
self.getVolumesinceselfisareferencetothePlanetobject.Notethatselfisareferencetotheobjectinvoking
thegetDensitymethod,whichinturncanbeaskedtoinvokethegetVolumemethod.Ifwehadsimplytriedto
call getVolume directly, we would get an error since there is no method to return volume other than the
methoddefinedinthePlanetclass.

Exercises

10.1ModifythePlanetclassconstructortoaddanewinstancevariablecallednumMoons.

10.2WriteanewaccessormethodtoreturnthenumberofmoonsthatreferencesthenumMoonsinstance
variablecreatedinExercise10.1.

10.3WriteanewaccessormethodforPlanetcalledgetCircumference.

10.4CreateanewclasscalledSentence.Theconstructorshouldacceptasingleparameterthatisastring.
Create an instance variable that stores the sentence as a string. Assume the sentence has no
punctuation.

10.5WritethefollowingaccessormethodsforthesentenceclasscreatedinExercise10.4

(a)getSentence:Returnthesentenceasastring.
(b)getWords:Returnthelistofwordsinthesentence.
(c)getLength:Returnthenumberofcharactersinthesentence.
(d)getNumWords:Returnthenumberofwordsinthesentence.

10.6CreateavariationoftheSentenceclass,againcalledSentence.Theconstructorshouldacceptasingle
parameter that is a string. This time create an instance variable that stores the sentence as a list of
words.

10.7WritethefollowingaccessormethodsforthenewclasscreatedinExercise10.6

(a)getSentence:Returnthesentenceasastring.
(b)getWords:Returnthelistofwordsinthesentence.
(c)getLength:Returnthenumberofcharactersinthesentence.
(d)getNumWords:Returnthenumberofwordsinthesentence.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 7/30
8/23/2016 Yuzu: Python Programming in Context

10.3.3MutatorMethods
Mutatormethodsareproceduresthatmutateorchangeanobjectinsomeway.Changestotheobjectinvolve
changestooneormoreoftheinstancevariables.Recallthateachobjectfromaparticularclasshasthesame
instance variables but the values of those variables are different, therefore allowing the object to behave
differentlywhenaskedtoperformmethods.Inordertochangethosevariablevalues,weprovidemethods.
A mutator method will allow us to change the name of a planet in our example. Instead of using a cryptic
name,suchasX25,wecanchangethenametosomethingmoremeaningful.Todothis,weneedamethod
that takes the new name as a parameter and modifies the value of the name instance variable. Listing 10.4
showsthesetNamemethod.Asbefore,thefirstparameterisself.Inaddition,asecondparameterthatwill
receivethenewname(newname)isdefined.Thevalueofnewnameisthenassignedtotheinstancevariable
nameinthebodyofthemethod.Itisimportanttonotethatnothingisreturnedfromthismethod.Infact,that
isatypicalpatterntorecognize.Mutatormethodsmodifythestateofanobjectbutdonotreturnanyvalueto
thecaller.

Listing10.4MutatormethodsinthePlanetclass

Whenweusethismethod,asshowninSession10.6,weprovideoneparametervalue.Althoughthemethod
definitionhastwoparameters,thefirst,self,willreceiveareferencetotheobjectimplicitly.Sincethestateof
theobjecthasnowchanged,itrespondsdifferentlywhenaskedtoperformthegetNamemethod.

>>>myplanet.getName()
'X25'
>>>myplanet.setName("GammaHydra")
>>>myplanet.getName()
'GammaHydra'
>>>

Session10.6Usingamutatormethod

Exercises

10.8WriteamutatormethodcalledsetMoonsthatchangesthenumberofmoonsaroundourplanet.

10.9AddaninstancevariablemoonListandacorrespondingmutatormethodcalledaddMoonthataddsthe
nameofamoontothemoonList.

10.10WriteamutatormethodfortheSentenceclassthatallowsyoutocapitalizeallthewordsinasentence.

10.11WriteamutatormethodfortheSentenceclassthatallowsyoutoaddapunctuationmarktotheendof
thesentence.

10.12Writeanaccessormethodthatallowsyoutotranslateasentenceintopirateseoranyotherlanguage.
Noteyoucouldaddadictionaryasaninstancevariableoftheclass.RefertoChapter4fordetailson
translatingEnglishtopirate.

10.3.4SpecialMethods
https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 8/30
8/23/2016 Yuzu: Python Programming in Context

We mentioned earlier the existence of special methods that were related to system functionality such as
integeradditionanditemselectioninalist.Thereareanumberofotherspecialmethodsthatwecandefine
withinaclass.Oneofthese,__str__,isusedtoprovideastringrepresentationforanobject.
Session10.7showstheresultofprintingaPlanetobject.Theprintfunctionautomaticallytriestoconvertthe
object into a string representation. Unfortunately, the Planet object does not know how to respond to this
request.Theresultisthedefaultrepresentation,whichiscertainlynotveryhelpful.

>>>print(myplanet)
<__main__.planetobjectat>
>>>

Session10.7PrintingaPlanetobject

There are two ways that we can provide better printing capability for the Planet class. One is to define a
method called show that will allow any Planet object to show itself in a form by printing the individual
instancevariableitems.Forthisexample,wewillsimplyprintthenameoftheplanet.Wecanimplementthis
methodasshowninListing10.5.

Listing10.5Theshow()method

NowifwecreateaPlanetobject,wecanaskittoshowitself.Theobjectwillrespondbyprintingitsname
(seeSession10.8).However,westillcannotprinttheobjectdirectly.Inordertoprovideabettersolution,we
needtoimplementamethodthatallowstheobjecttoconvertitselftoastring.Thisiswhattheprintfunction
needstodoitsjob.

>>>myhome=Planet("Earth",6371,5.97e24,152097701)
>>>myhome.show()
Earth
>>>print(myhome)
<__main__.planetobjectat>
>>>

Session10.8Usingtheshow()method

In Python, the special method __str__ provides this functionality. We have already seen that the default
implementationfor__str__returnsthestringwiththeclassnameandinstanceaddress.Whatweneedtodois
provideanalternativeimplementationforthe__str__methodthatprovidesmoreuserfriendlyinformation.In
thiscase,__str__willsimplyreturnthenameoftheplanet.
Todothis,wedefineamethodwiththename__str__andgiveanewimplementation,asshowninListing
10.6.Themethoddoesnotrequireanyotheradditionalinformationotherthantheselfparameter.Themethod
inturnreturnsthestringthatwehavechosentorepresenttheobject.Session10.9showstheresultofprinting
theobject.Italsodemonstratesthatwhenyoucallthestrfunctiononanobjectitautomaticallyinvokesthe
__str__ method for that object. Figure 10.2 shows the complete reference diagram for the myhome planet
object.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 9/30
8/23/2016 Yuzu: Python Programming in Context

Listing10.6The__str__Method

>>>myhome=Planet("Earth",6371,5.97e24,152097701)
>>>print(myhome)
Earth
>>>myhome.__str__()
'Earth'
>>>str(myhome)
'Earth'

Session10.9Usingthe__str__method

Figure10.2LogicalviewofaPlanetobject

Exercises

10.13Adda__str__methodtotheSentenceclassyoustartedinExercise10.4or10.6.

10.14Implementthe__getitem__methodfortheSentenceclass.Thismethodshouldallowyoutousethe
indexoperatornotationtoaccessawordinthesentence.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 10/30
8/23/2016 Yuzu: Python Programming in Context

10.15Implementthe__len__methodsothatyoucanusethelenoperator.

10.16Implementthe__contains__methodsothatyoucanaskifawordisinasentence.

10.17Implementthe__add__methodtoallowyoutoconcatenatetwosentencestogether.Makesureyour
__add__methodreturnsanewinstanceofSentence.

10.18Designaclasstorepresentadie.Theconstructorshouldallowyoutospecifyhowmanysidesthedie
has.Addarollmethodthatsimulatesrollingadie.Implementthe__str__method.

10.19Implementthe__ge__,__gt__,__le__,__lt__,__eq__,and__ne__methodsfortheDieclass.These
methodsallowyoutocomparethevaluesofthecurrentrolloftwodice.

10.20Designaclasstoimplementaplayingcard.Thecardshouldrememberitsrankandsuit.Implementthe
__ge__,__gt__,__le__,__lt__,__eq__,and__ne__methodsforthecardusingtherulethatthe2is
thelowestcardinasuitandtheaceisthehighest.Whencomparingcardswithequalrankfromtwo
differentsuits,usetherulethatspadesarethelowestsuitfollowedbyclubs,diamonds,andhearts.

10.21Designaclasstorepresentadeckofcards.TheDeckclassshouldprovidemethodsforshufflingand
drawing.

10.22UsingtheDeckclass,implementasimpleblackjackgame.

10.23 Design a class to represent a bank account. Provide methods for checking the balance, and for
depositingandwithdrawingmoney.TheconstructorshouldallowyoutoprovideacustomerIDforthe
account.Thebeginningbalanceshouldbeinitializedto0.

10.24Extendthebankaccountclassinthepreviousexercisesothatithasatransfermethod.Thetransfer
methodshouldtakeanamountandaninstanceofanaccounttotransferthemoneyto.

10.25Designaclasstorepresentafraction.Theconstructorshouldaccepttwoparameters:anumeratoranda
denominator. Implement the special methods for addition, subtraction, multiplication, division,
comparison,andconversiontostringformat.

10.26ExtendtheFractionclasssothatfractionsarealwayssimplifiedtotheirlowestterms.

10.3.5Methodsandself
Whenmethodsareinvoked,theywillcreatelocalnamespacesinthenamespacewheretheyaredefined.The
differencebetweenfunctionsandmethodsisthateachlocalnamespacewillalsohaveareferencetotheobject
thatinvokedthemethod.Thenamewehavebeenusingforthisreferenceisself.
ConsideragainthePlanetclassdescribedearlier.Whenwecreateanewinstance(seeSession10.10),weare
addingthevariablemyplanettothemainnamespacewithareferencetotheobjectbeingconstructed.Now
whenweinvokeamethodsuchasgetName,alocalnamespaceiscreatedandplacedinthemainnamespace.
Theformalparameter,self,implicitlyreceivesareferencetotheobjectthatmadetheinvocation(inthiscase
myplanet). The result can be seen in Figure 10.3. Although it is not a requirement that the first formal
parameterbecalledself,thatisacommonconventionthatwewillcontinuetouse.

>>>myplanet=Planet("X25",45,198,1000)
>>>myplanet
<__main__.planetobjectat>
>>>
>>>myplanet.getName()
'X25'
https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 11/30
8/23/2016 Yuzu: Python Programming in Context

Session10.10Showingscopewithmethods

When we look at the getName method, the only statement is return self.name. When Python evaluates
self.name, it first evaluates the name self, which returns a reference to the object that invoked the method.
Once we have the reference to the object, we continue to search for name in the instance variables of the
object.Sinceselfreferstothemyplanetobject,itisthevalueofthenameinstancevariableinmyplanetthatis
returned. It is for this reason that each object, when invoking the getName method, will behave differently
sincetheseobjectswilllikelyhaveadifferentvalueforthenameinstancevariable.

Figure10.3Alocalnamespaceforamethod

Exercises
10.27 Turtles have many instance variables including x, y, color, and heading. Draw a reference diagram
showingthenamespacesforthefollowingsessionatthepointimmediatelyaftertheforwardmethod
hasbeeninvoked.

>>>importturtle
>>>myTurtle=turtle.Turtle()
>>>myTurtle.forward(10)

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 12/30
8/23/2016 Yuzu: Python Programming in Context

10.3.6DetailsofMethodStorageandLookup
Beforemovingon,itisimportanttounderstandthedifferencebetweenthedescriptiveviewofobjectsthatwe
areusingandtheactualstorageandlookupmechanismthatPythonuses.Wehavebeenrepresentingobjects
as containing both instance variables and methods. This is an accurate representation for the instance
variablessinceeachobjectneedsitsowncopies.However,itturnsoutthatmethodsarestoredinaslightly
differentfashionsincetheyaresharedbyallinstancesofaclass.
Inordertoseethis,considerFigure10.4.Whenaclassisdefined,thenameoftheclassisaddedtothecurrent
namespacewithareferencetoaclassdefinitionobject.Inthiscase,wehaveaddedthenamePlanettothe
main namespace with a reference to a corresponding class definition object. Class definition objects store
methodnamesthatrefertomethoddefinitionsthatareimplementedbytheclass.Inthiscaseweareshowing
onlytwoofthemanymethodsthathavebeenimplementedbyPlanet.
WhenaninstanceofthePlanetclassiscreated,aswasdoneusingmyplanet=Planet("X25",45,198,1000),
thePlanetreferenceisfollowedtogainaccesstothe__init__methoddefinition.Thisconstructorisexecuted
and an object is created with the given instance variable values. This object is referenced from the main
namespaceusingthenamemyplanet.
You should note that there is one additional instance variable being shown in Figure 10.4. The __class__
instance variable is used to provide each object with a reference to the class definition object to which it
belongs.Now,whenamethodiscalled,

myplanet.getName()
thelookupsequencecanproceedafollows.First,myplanetisdereferencedtogainaccesstotheobject.Next,
the __class__ reference is followed to find the class definition object, then the method name, getName, is
used to access the method definition. When this method is invoked, a namespace is created and placed
followingthesamestepsasexplainedintheprevioussections.
Itisimportanttonotethatonlyonecopyofthemethoddefinitionisneedednomatterhowmanyobjects
existforaparticularclass.Sinceeachobjectwillhavehavea__class__reference,themethoddefinitionscan
always be located from the object. Furthermore, since each method has a formal parameter, self, that will
referencebacktotheobjectmakingtheinvocation,therecanbenoconfusionwhendifferentobjectscallthe
samemethod.Eachwillgetitsownlocalnamespacewithanappropriatevalueforself.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 13/30
8/23/2016 Yuzu: Python Programming in Context

Figure10.4Actualstorageofmethods

10.4DesigningandImplementingaSunClass
Ourtaskinthischapteristoconstructasoftwaremodelofaplanetarysystem.Thismeansthatweneedto
consider not only the planets that might be present but also the most important member, the sun. For this
examplewewillconsiderthesuntobesimilartoaplanet,inthatitisalarge,round,celestialbody.Itwill
certainlyhavesomeofthesamecharacteristicsasplanets,includingname,mass,andradius.However,since
thesunisatthecenterofthesolarsystem,itwillnothaveanydistancemeasure.Inaddition,thesunprovides
heatandlightthatcanbecharacterizedbythetemperatureonthesurface.
Giventhatdescription,wecancreateaSunclassusingthesamepatternsthatwefollowedforthePlanetclass.
Ourconstructorwillrequirevaluesforthename,mass,radius,andtemperature.Listing10.7showsapartial
implementationoftheclass,whichincludesthe__init__,getMass,and__str__methods.Weleavetherestof
themethodsasanexercise.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 14/30
8/23/2016 Yuzu: Python Programming in Context

Listing10.7TheSunclass

Exercises

10.28CompletetheimplementationoftheSunclassbywritingthefollowingmethods:
(a)getRadius
(b)getTemperature
(c)getVolume
(d)getSurfaceArea
(e)getDensity
(f)setName
(g)setRadius

10.29Writeafunctiontoprintatableofradius,volume,andsurfaceareaforvaluesofradiusbetween10and
500usingincrementsof10.

10.5DesigningandImplementingaSolarSystem
Now we are ready to build our solar system, which will consist of a sun and a collection of planets, each
definedtobesomedistanceawayfromthesun.Wewillassumethatthesunresidesatthecenterofthesolar
system.TheSolarSystemclasswillbeimplementedinthesamewayastheotherclassesseensofar.Weneed
toprovideaconstructorthatwillberesponsiblefordefiningtheinstancevariables.Wealsodefineappropriate
accessorandmutatormethods.
ThecompleteSolarSystemclassisshowninListing10.8.Ourconstructor(lines24)willassumethatabasic
SolarSystemobjectmusthaveaSunobjectatitscenter.Thismeansthattheconstructorwillexpecttoreceive
a Sun object as a parameter but will assume an empty collection of planets. We will implement the planet
collectionasalist.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 15/30
8/23/2016 Yuzu: Python Programming in Context

Listing10.8TheSolarSystemclass

InordertoaddaPlanettotheSolarSystem,weincludeamutatormethodcalledaddPlanetthatcanmodifythe
collectionofplanets.Thismethod(lines67)receivesaPlanetobjectasaparameterandaddstheobjectto
thecollectionofplanets.Sincethecollectionisalist,themodificationwillsimplyusetheappendmethod.
Finally, a simple accessor method called showPlanets (lines 911) will show all of the planets in the solar
system.Thiscanbeimplementedbyiteratingthroughthelistofplanetsandprintingeachoneofthem.Recall
thatthePlanetclassimplementsthe__str__methodthatreturnsthenameoftheplanet.
Inordertousethethreeclasseswehaveimplemented,wemustsavethemasPythonfiles.TheclassesSun,
Planet,andSolarSystemwillbesavedinthefilessun.py,planet.py,andsolarsystem.py.Whenwestoreaclass
in a file like this, we have created a module that can be used by other programs. This is no different than
turtle,math, orcImage.Touse our Planet, Sun, or SolarSystem classes, we simply import the module that
containsthem.
Session10.11showsstatementsthatimportthemodules,anditcreatesasolarsystemwithfourplanets.Note
thatthemodulenameisthesameasthefilenamebutwithoutthe.py.Afterimportingthemodules,wecreate
aSunobjectcalledsunandaSolarSystemobjectcalledssusingsunasitscenter.NextwecreateanewPlanet
object p with planet name MERCURY and add it as the first planet in the solar system. Similarly, three
otherplanetsarecreatedandadded.Finally,wecalltheshowPlanetsmethod,whichprintsthenamesofthe
planetsasdescribedabove.Figure10.5showsalloftheobjectsandreferencespresentatthispoint.

>>>fromsunimport*
>>>fromplanetimport*
>>>fromsolarsystemimport*
>>>
>>>sun=Sun("SUN",5000,1000,5800)
>>>ss=SolarSystem(sun)
>>>
>>>p=Planet("MERCURY",19,10,25)
>>>ss.addPlanet(p)
>>>
>>>p=Planet("EARTH",50,60,30)
>>>ss.addPlanet(p)
>>>
>>>p=Planet("MARS",47,50,35)
>>>ss.addPlanet(p)
>>>
>>>m=Planet("JUPITER",75,100,50)
>>>ss.addPlanet(p)
>>>
https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 16/30
8/23/2016 Yuzu: Python Programming in Context

>>>ss.showPlanets()
MERCURY
EARTH
MARS
JUPITER
>>>

Session10.11Creatingandshowingasolarsystemwithfourplanets

Figure10.5Asolarsystemwithfourplanets

Exercises
10.30WriteamethodnumPlanetsthatreturnsthenumberofplanetsinthesolarsystem.

10.31WriteamethodtotalMassthatreturnsthetotalmassofthesolarsystem.Thetotalmassshouldinclude
alltheplanets,includingthesun.

10.32GiventherecentcontroversyoverPluto,writearemovePlanetmethodfortheSolarSystemclass.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 17/30
8/23/2016 Yuzu: Python Programming in Context

10.33 Write two methods, called getNearest and getFarthest, that will return the planet closest or farthest
fromthesunrespectively.

10.34Writea__str__methodthatprintsthenameofthesunandthenamesofalltheplanets,inorder,from
closesttofarthestfromthesun.

10.6AnimatingtheSolarSystem
Nowthatwecancreateasolarsystem,itisanaturalnextsteptoconsiderwhetherwecandrawitandthen
placeitinmotion.InthissectionweconstructananimationbasedonthelawsofmotiondiscoveredbySir
IsaacNewton.Althoughwewillnotattempttotakeintoaccounteverypossiblevariablethatexistsinnatures
complexinteractionofplanetaryobjects,theresultingsimulationwillstillrelyonthebasiclawsofphysics.
Understanding planetary movement requires an understanding of the basic laws of motion as described by
Newton.Inparticular,itisnecessarytounderstandhowobjectsthatareinmotioninteractwithoneanother
and how those interactions affect the movements. Since this is not a course in physics, we will resist the
temptation to describe all of the derivation required. However, it turns out that by using a few simple
equations,itispossibletocreateafairlyaccuratemodel.

10.6.1UsingTurtles
InordertodrawourSolarSystem,Sun,andPlanetobjects,wewilluseourTurtleclass.RecallthataTurtleis
agraphicalobjectthathasanumberofinstancevariables,includingpositionandcolor.Inaddition,aturtle
candefineacoordinatesystem.Theturtlecanbemovedtoanypositionwithinthatcoordinatewindow,andif
itstailisinthedownposition,alinewillbedrawn.Inthisapplication,wewillalsotakeadvantageofthefact
thatourturtlemoduleallowsmanysuchturtlestoexistsimultaneouslyinthesamewindow.Thismeansthat
wecanprovideeachplanetwithaTurtleobjectthatcanbeusedtographicallyrepresenttheplanet.
OurfirstmodificationwillbetotheSolarSystemclass.Sincethesolarsystemrepresentstheboundsofour
animation,wewilluseaTurtletoprovideauserdefinedplanetarycoordinatesystem.Whenasolarsystemis
created,wewillprovideawidthandaheightthatcaninturnbeusedtodefinetheupperandlowerboundson
thexandyaxeswithinthatcoordinatesystem.
Listing10.9 showsthenewSolarSystem class.Theconstructornowhas two parameters, width and height.
Note however that the sun is no longer a parameter as it will be added later. Instead, the instance variable
thesun will be initially set to None. By doing things in this order, we allow the planets and sun to take
advantageofthegraphicssetupworkthatwillbedonebytheSolarSystem.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 18/30
8/23/2016 Yuzu: Python Programming in Context

Listing10.9TheSolarSystemclasswithahiddenturtle

Lines58createinstancevariablescalledssturtleandssscreenthatwillprovidegraphicalfunctionalitytothe
solarsystem.Sincetheturtlewillnotactuallydrawanything,wewillhideitsothattheshapecannotbeseen.
Using the width and height, we use the setworldcoordinates method to create a coordinate system that is
equallydistributedaroundtheposition(0,0).

Two additional methods have been added to the SolarSystem class. Lines 1415 define a method called
addSunthatwillallowustoaddasuntothesolarsystem.Thefreezemethod(lines2122)simplyletsthe
userfreezethescreenaftertheanimationhasbeencompleted.Thiskeepsthewindowfromautomatically
closing.
We next modify the Sun class (shown in Listing 10.10) by creating two new instance variables, self.x and
self.y, which will keep track of the coordinate position for the sun. Since we assume that the sun is at the
centerofthesolarsystem,initializingthesevaluestozeroprovidesagoodstartingpoint.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 19/30
8/23/2016 Yuzu: Python Programming in Context

Listing10.10TheSunclasswithvisualization

Byaddingself.sturtleasathirdinstancevariableoftheSunclass,eachSunobjectwillcontainaTurtleobject
todothegraphicalwork.SincethecoloroftheTurtlecanbeuserdefined,wesetittoyellow.Inaddition,
wechangetheshapeoftheturtlefromthedefaulttriangletoacircle.ThemajorityoftheSunclassmethods
remainunchangedinthisnewimplementation.Twonewaccessormethods,getXPosandgetYPos,willallow
ustoretrievethexandycoordinatesofthesun.
Finally,thePlanetclasswillalsobemodifiedsothateachinstanceofPlanetwillcontainaTurtle(seeListing
10.11).LiketheSunclass,eachplanetwillneedtokeeptrackofitspositionusinganxandaycoordinate
value.Theinitialxvaluewillbethedistancefromthesun.Theinitialyvaluewillsimplybe0.Thismeans
thatalloftheplanetsareinitiallylineduponthexaxis.Weusethecoloroftheturtletoindividualizeeach
Planet.WhenaPlanetiscreated,acolorwillbeprovidedthroughtheconstructor.Theshapeoftheturtlewill
alsobesettocircle.
Lines1618movetheplanetintoitsinitialposition.Notethatinordertomovetheplanet,thetailoftheturtle
israisedbeforeinvokingthegotomethodsoasnottoleavealine.Ofcourse,weneedtoloweritagainonce
theplanetarrivesatitsdestination.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 20/30
8/23/2016 Yuzu: Python Programming in Context

Listing10.11ThePlanetclasswithvisualization

Session10.12showshowtheseclassescanbeusedtocreateaninitialvisualrepresentationofasolarsystem.
First,aninstanceofSolarSystemiscreatedwithawidthandheightoftwounits.NextaSunobjectiscreated
andaddedtothesolarsystem.Finally,fourplanetswithdifferentnames,colors,anddistancesfromthesun
areadded.SincethesunandtheplanetshaveassociatedTurtlesintheirconstructors,theyareautomatically
shownwhentheobjectsarecreated.TheresultingimageisshowninFigure10.6.

>>>ss=SolarSystem(2,2)
>>>
>>>sun=Sun("SUN",5000,10,5800)
>>>ss.addSun(sun)
>>>
>>>m=Planet("MERCURY",19.5,1000,.25,"blue")
>>>ss.addPlanet(m)
>>>
>>>m=Planet("EARTH",47.5,5000,0.3,"green")
>>>ss.addPlanet(m)
>>>
>>>m=Planet("MARS",50,9000,0.5,"red")
>>>ss.addPlanet(m)
>>>
>>>m=Planet("JUPITER",100,49000,0.7,"black")
>>>ss.addPlanet(m)
>>>
>>>ss.freeze()
https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 21/30
8/23/2016 Yuzu: Python Programming in Context

Session10.12Creatingandshowingasunandfourplanets

Figure10.6Visualizingthesunandplanets

Exercises
10.35LoadandrunthecodeshowninSession10.12.

10.36AddaplanetcalledSATURNtothesolarsystemcreatedinExercise10.36.

10.37ModifythePlanetclasssothateachplanetcanbeadifferentsize.Hint:Youwillwanttolookupthe
resizemodeandshapesizemethodsintheturtledocumentation.

10.6.2PlanetaryOrbits
Planetaryorbitsareshapedbyanumberofvariablesandrelatedequations.Webeginbydefiningsomeofthe
necessary vocabulary and then move on to describe the equations that will be used to calculate the actual
orbits. To simplify our work, we will continue to assume that our universe exists in only two dimensions.
However,youshouldnote,aswegothroughtheexplanations,thatextendingtothreedimensionswouldnot
bedifficult.

DistanceBetweenObjects

Tostart,assumethataplanetislocatedatsomepositioninthesolarsystem,callit(x,y).Thebehaviorofthe
planet is dependent upon its distance from other objects in the solar system. Since we are working in two
dimensions,thedistancewillonceagainbecomputedusingthenotionofEuclidiandistance,aswasdescribed
inChapter7.
To review, for an object located at position (a, b), the distance between it and the planet described above
wouldbe Ifweassumethatthesunisatposition(0,0),thenthedistancebetweenthe
planetandthesunissimply Thisdistanceisoftenreferredtoasr.Forplanetinteraction,thecloser
twoobjectsaretooneanother(smallerr),themoretheyinteract.Twoobjectsthatareseparatedbyalarge
distancewillhaveverylittleinteractionwitheachother.
https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 22/30
8/23/2016 Yuzu: Python Programming in Context

Velocity

Ourplanetisnotstationaryinthesolarsystem.Itismoving.Thebasicdescriptionofhowitmovesisgiven
byitsvelocity.Velocityisdefinedasthechangeindistancedividedbythechangeintime.Forexample,ifwe
arewalkinginastraightlinewithaconstantvelocityof5kmperhour,wecansaythat1hourfromnowwe
willbe5kmfromourcurrentlocation,andafter2hourswewillbe10kmfromhere,andsoon.

Figure10.7Understandingvelocityintwodimensions

Velocityintwodimensionsisslightlymorecomplexthanwalkinginastraightline.Figure10.7showsthat
velocity,V,isactuallyacombinationoftwocomponents.Thexcomponent,calledVx,isthechangeinthex
directionforthatgivenamountoftime.Similarly,theycomponent,calledVy,isthechangeintheydirection.
Thisidea,knownasavectorinphysics,willbeimportanttousforcalculatingplanetarymovementintwo
dimensions.

Acceleration

The velocity of our planet will not be constant. This means that the velocity is being changed over some
period of time. This change is known as acceleration. Acceleration in two dimensions, like velocity, is
definedbyavectorwithtwocomponents,AxandAy.Accelerationcancauseanobjecttochangedirectionby
alteringoneorbothofthevelocitycomponents.
Forthisexample,theaccelerationoftheplanetwillbechangingduetointeractionswithotherobjectsinthe
solarsystem.Thiswillinturncausethevelocitytochange,thereforemovingtheplanettoanewpositionin
thetwodimensionalspace.Wedescribetheseinteractionsinalatersection.

CalculatingDistancefromVelocityandVelocityfromAcceleration

Thetwoprevioussectionsproviderelationshipsforvelocityandaccelerationthatusetimeinthedenominator.
Ifweknowthatacertainamountoftimehaselapsed,thenabitofalgebraallowsustocalculateimportant
valuesrelatedtothemovementofanobject.Forexample,sincevelocityisthedistancetraveleddividedby
the time, it follows that the distance traveled can be computed from the velocity and the time
Ifanobjectistravelingatacertainvelocityforaperiodoftime,thisallowsusto
knowhowfarithasmoved.
We said earlier that the velocity is actually a vector with both an xcomponent and a ycomponent. This
simply means that the calculation above must be done twice, once for each component. So, the distance
movedinthexdirection,Dx,isthexcomponentofthevelocity,Vx,multipliedbythetime.Likewiseforthe
movementintheydirection.
Sinceaccelerationisthechangeinvelocityoveragivenperiodoftime thesameanalysiscanbedone
https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 23/30
8/23/2016 Yuzu: Python Programming in Context

tocomputethevelocitycomponentsVx=AxtandVy=Ayt.

MassandGravity

We said earlier that the distance between two objects affects the amount of interaction that occurs between
them.Inadditiontodistance,thereareotherfactorsthatneedtobeconsidered.First,themassoftheplanet
will dictate how much effectithas onotherobjects inits vicinity. The mass ofan objectdescribes a basic
notionofhowmuchmatterispresentintheobject.Aplanetwithalargemasswillexertmoreforceonother
planetsofsmallermassatthesamedistance.
Gravityisanideathatweareallfamiliarwiththepullthatkeepsusontheearth,whatcausesustocome
backdownifwejumpintheair.Ingeneral,gravityisaforcethatverymassiveobjectsexertonotherobjects.
Thegravitythattheearthexertsonapersonstandingonthesurfaceiswhatweusuallyconsidertobeour
weight.Sinceobjectswithmoremasswillbepulledtoagreaterextentgiventhesamegravity,theideaof
weightandmasscanbethoughtofassimilarideas.

InteractionofthePlanets

In order to write a program that will plot the path of planets, we need to use some basic equations that
compute the affect of planet interaction. The first important equation, known as Newtons law of universal
gravitation,statesthattheforceorattraction,F,betweentwoobjectscanbecomputedas where
Gisaconstant,m1andm2arethemassesofthetwoobjects,andristhedistancebetweenthetwoobjects.G
isoftenreferredtoasthegravitationalconstant.
Another famous equation is F=ma,whereF is the force exerted by an object, m is its mass, and a is its
acceleration.Bysettingthesetwoequationsequaltooneanotherandsolvingforacceleration,itispossibleto
createanequationtocalculatetheaccelerationofoneobjectasaresultoftheforceexertedonitbyanother,
Inotherwords,theaccelerationexperiencedbythefirstobjectduetotheforceofgravityfrom
thesecondobjectcanbecalculatedbymultiplyingthegravitationalconstantbythemassofthesecondobject
anddividingbythesquareofthedistancebetweenthetwoobjects.
Figure10.8canbeusedtocomputetheindividualaccelerationcomponentsforaplanetthatisexperiencing
aninteractionwiththesun.Noticethattherearetwosimilartriangles.Thelargeonehassidesx,y,andr.The
smaller,representingtheforcethatispullingontheplanethassidesFx,Fy,andF.Sincethetwotriangles
aresocalledsimilartriangles,theratiosofcorrespondingsidesmustbeequal.Fromthiswecanderivethe
followingsequenceofequationsforthexcomponentofacceleration:

Asimilarderivationcanbeusedtogettheycomponent.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 24/30
8/23/2016 Yuzu: Python Programming in Context

Figure10.8Usingsimilartrianglestocomputetheaccelerationcomponents

10.6.3Implementation
It is now time to implement these equations so that we can compute the positions of the planets as they
revolvearoundthesun.Thebasicalgorithmcalculatestheamountofplanetmovementthattakesplaceovera
fixedperiodoftime.Italsocalculatestheplanetschangeinvelocityoverthatsameperiodoftime.Eachtime
theplanetmoves,thegravitationalforcechangessincethedistancebetweenitandthesunwillhavechanged.
Thechangeinforcewillcauseanewvelocity,whichinturnwillcausethenextchangeindistance.These
stepswillberepeatedforsomenumberoftimeperiods.
For each time step, do the following (assume that the value of the time period and the value of the
gravitationalconstanthavebeenset):

1.Usingthecurrentvelocityandelapsedtime,movetheplanettoanewlocationbycomputingthex
distanceandtheydistancefromthevelocitycomponents.
2.Computethenewdistancefromtheplanettothesun.
3.UseNewtonsgravitationalequationstocomputethenewaccelerationcomponentsfortheplanet.
4.Usethisnewaccelerationtoadjustthevelocitycomponents.

Inordertoimplementthesesteps,wefirstneedtomodifyourPlanetclasssothateachplanetcanremember
additionalinformationthatwillberequired.Eachplanetwillnowneedtoknowitscurrentvelocityandtobe
given an initial velocity as part of the constructor. Since velocity is a vector, we will keep track of the x
componentandtheycomponentseparately.Listing10.12showsthemodificationsthatneedtobemadetothe
Planetclass.Theonlyparametersthathavebeenaddedareforthetwovelocitycomponents.

Listing10.12ThePlanetclasswithanimation

Sincewehaveaddedsomeadditionalvelocityattributes,wewillneedtoprovidecorrespondingaccessorand
mutatormethods(seeListing10.13).Inaddition,wehaveaddedamovetomethodthatwillmodifythexand
ylocationoftheplanet.Itwillalsomovetheturtletothatlocationleavingalinebehind(tracingouttheorbit).

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 25/30
8/23/2016 Yuzu: Python Programming in Context

Listing10.13AnimationmethodsinthePlanetclass

MostoftheanimationwilloccuraspartoftheSolarSystemclass.RecallthataSolarSystemconsistsofasun
and a collection of planets. Each instance of Planet now has the information necessary to allow the
SolarSystem object to compute its movement and position according to the equations described earlier.
Listing 10.14 shows the specific method, called movePlanets, that is used to move each planet in the solar
systemonetimestep.Eachtimethismethodiscalled,alloftheplanetswillmovetotheirnextposition.
Lets take a look at each line of this method so that we can understand how the equations have been
implementedinPython.First,lines34setthetwoimportantconstantsthatareneededtoruntheanimation.
GisNewtonsgravitationalconstant.Forsimplicityinourexample,wewilluse.1,butintheexercisesatthe
endofthesectionyouwillexploreusingtheactualvalue.Weusedtforthetimestep,andinthiscasewewill
computethemovementthattakesplacein.001seconds.
Line 6 sets up the iteration that will process each planet in the solar system. The steps that need to be
performedtocomputethemovementforeachplanethavealreadybeendescribed.Lines78movetheplanet
basedonitscurrentvelocity.Notethatthenewxlocationiscomputedfromthexcomponentofthevelocity
andthenewylocationiscomputedfromtheycomponent.Oncetheplanethasbeenmovedtoanewlocation,
itisnecessarytorecomputethedistancebetweentheplanetandthesun(lines1012).

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 26/30
8/23/2016 Yuzu: Python Programming in Context

Listing10.14AnimationmethodintheSolarSystemclass

Lines1415cannowcomputethenewaccelerationcomponentsbasedonthisnewdistance.Theacceleration
isnotstoredaspartofthePlanetobject.Itissimplycomputedandthenusedtomodifythevelocity(lines17
18). The two velocity components are set individually based on the old velocity and the change due to the
acceleration. Be sure to go back to the earlier equations and make sure that you understand how each
calculationisdone.
Nowwecanfinishtheanimationbycreatingasolarsystemandallowingittomovetheplanets.Listing10.15
showsafunctionthatcreatesasimplesolarsystemwithasunandfourplanets.Althoughwehaveusedthe
namesofrealplanets,thevaluesformass,distance,andinitialvelocitiesarenotaccurate.However,theywill
worknicelytocreateastablesystemofplanetaryorbits.
Aseachplanetiscreated,itisgivenaninitialvelocity,allintheycomponent,andthenaddedtothesolar
system.Therealworkoftheanimationisdonebylines1921.ThenumTimePeriodsconstantsimplysetsthe
number of individual positions that will be calculated for each planet. Recall that each time period will
actuallyrepresent.001ofasecond.
Figure 10.9 shows the result of executing this function. Note that each planet travels through an elliptical
orbit.Thespecificshapeisdictatedbythestartingposition,themass,andtheinitialvelocityoftheplanet.
Noticethatthetimeintervalissufficienttoallowtheinnertwoplanetstomakecompleteorbits.However,the
outer two planets would need more time to complete a single orbit. It is also important to note that this
animationmodeltakesintoaccountonlytheinteractionbetweenaplanetandthesun.Itdoesnotmodelthe
interactionofplanetswitheachother.Inreality,theaccelerationsmustbecomputedbytakingintoaccountall
oftheotherplanets.Weleavethismodificationasanexercise.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 27/30
8/23/2016 Yuzu: Python Programming in Context

Listing10.15Creatingandmovingasolarsystemwithfourplanets

Figure10.9Runningtheanimationfortwoseconds

Exercises

10.38LoadtheSun,Planet,andSolarSystemclasses,aswellasthecreateSSandAnimatefunction.Runthe
solarsystemsimulation.

10.39ModifythenumTimePeriodsvariableofcreateSSandAnimatetomakethesimulationrunlonger.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 28/30
8/23/2016 Yuzu: Python Programming in Context

10.40Modifytheinitialvelocityofoneplanetandobservethechangeinitsorbit.

10.41 Modify the mass of the sun and observe the change in the orbit of each planet. Make sure you try
increasinganddecreasingthemass.

10.42Createanewplanetandaddittothesolarsystem.Hint:Makesurethatyourplanetislocatedinside
theinitialboundariesofthesolarsystem,orchangetheinitialboundaries.

10.43TherealvalueforNewtonsgravitationalconstantisG=6.673e11.UsingthisvalueforGandatime
stepof1800,lookuptherealvaluesforthemass,distance,andvelocityofthesunandoneormore
planets,andcreatearealmodelofthesolarsystem.

10.44 Modify the movePlanets method so that planets interact with each other. Hint: The accx and accy
variableswillaccumulateforthesunandallotherplanets.Tryrunningtheinitialsetofplanetswith
thisimprovedmodel.Youmayneedtochangetheirmassesorinitialvelocities.

10.45Addamoontotheearthoranyoftheotherplanets.

10.7Summary
Inthischapterweexploredtheimplementationofourowndatatypesbycreatingclasses.Thisabilityallows
the programmer to create solutions using a data model that closely resembles the problem domain. Classes
provideadescriptionofobjects(instancesofclasses)bydefiningdataandmethods.Objectsarecreatedby
calling the constructor method. Other methods, both accessor and mutator, allow the programmer to
communicate with individual objects. We also introduced a number of special methods that are defined by
Python,insomecasesboundtooperators.Finally,weusetheTurtletoimplementagraphicalsimulationof
planetarymovement.

KeyTerms
acceleration
accessormethod
class
classdefinitionobject
constructor
datamodel
dotoperator
gravitationalconstant
gravity
indexoperator
informationhiding
instancedata
instancevariable
mass
method
mutatormethod
object
objectorientedprogramming
state

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 29/30
8/23/2016 Yuzu: Python Programming in Context

velocity

PythonKeywords
__init__
__str__
bool
class
float
int
list
self
str

Bibliography

[Fey70] RichardFeynman.FeynmanLecturesonPhysics.AddisonWesley,1970.
[TM04] PaulA.TiplerandGeneMosca.PhysicsforScientistsandEngineers.W.H.Freeman,5thedition,
2004.

ProgrammingExercises

10.1ModifytheSolarSystemclasstosupportmorethanoneSun.

10.2ModifythePlanetclasssothattheplanetdoesnothavetheinstancevariablesforxandybutsimply
usesthexandylocationstoredintheturtle.

10.3Researchnbodysimulations.Usingtheideasfromthischapter,implementyourownsimplenbody
simulation.

https://reader.yuzu.com/#/books/9781284089042/c/6/34!/4/574@0:0 30/30
8/23/2016 Yuzu: Python Programming in Context

CHAPTER11
Bears,Fish,andPlants,OhMy!

11.1Objectives
Toexploreclassesandobjectsfurther
Todesignalargemulticlassapplication
Toimplementagraphicalsimulationusingobjects

11.2BearsandFish
YellowstoneNationalParkisoneofthemostfamousnaturalplacesintheUnitedStates.Peoplehavebeen
visitingtheparkformanyyearshopingtoseetheOldFaithfulgeyser,theYellowstoneRiverwaterfalls,and,
ofcourse,thebears.
For years it was common to see bears along roads and in park campgrounds. Unfortunately, many tourists
failedtorealizethatthesegreatcreatureswereactuallywildandthereforecertainlydangerous.Evenwithnew
lawstopreventcurioushumansfromfeedingthebears,manyofthebearshadtoberelocated.Thehopewas
to reacquaint bears with their natural food sources and at the same time reduce the number of injuries to
humans.
Oneimportantfoodsourceforbearsisfish,whichbearsliketoeat.Bearsdependonfishtoprovideastaple
partoftheirdiet,especiallyinthespringandsummerwhenbearsareleavinghibernationandbeginningto
care for young cubs. However, it turns out that the relationship between bears and fish can be much more
complicated.
Considerwhathappensifthenumberofbearsdecreasestooquickly.Sincetherearenowfewerbears,fish
populationscanincrease.But,morefishwillplaceastrainontheplantsthattheyfeedon.Theseplantsalso
provideoxygenforotherwaterdwellingcreatures.Changingthepopulationofonelifeformcanhavedrastic
effects on others. In this chapter, we explore ways that computers can help us understand this potential
impact.

11.3WhatIsaSimulation?
Earlier,weusedadartboardsimulationtocalculateanapproximationforthevalueofpi.Thatsimulation
usedarandomnumbergeneratortoplacedartsonadartboardasifthedartswerebeingthrownbyaplayer.
Althoughthelocationsofthedartschangedeachtimeweranthesimulation,wewereabletoconsistentlyuse
therelativedartlocationsincalculatingavalueforpi.
A computer simulation is a computer program that is designed to model some specific aspects of a real

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 1/20
8/23/2016 Yuzu: Python Programming in Context

situationorsystem.Computersimulationswilloftenutilizerandomnumbersasawayofintroducingsome
realisticvariabilityintotheunderlyingmodel.Theresultscanthenbeusedtogaininformationabouttheway
a real system behaves. The complexity of the computer simulation is dependent on the complexity of the
underlyingrealityaswellasthespecificcharacteristicsweareinterestedinrepresenting.
Oneofthemostcommontypesofsimulationisoneinwhicharealworldrelationshipismodeled,typically
between two or more lifeforms that must coexist and depend on one another in some fashion. These
relationships, often referred to as predatorprey relationships, suggest that one lifeform (the predator)
preysonanother(theprey)inordertosurvive.Asthenumberofpredatorsincreasesordecreases,itmayin
turncausethenumberofpreytoincreaseordecrease.Examplessuchasfoxesandrabbits,bigfishandlittle
fish, ladybugs and aphids, and whales and plankton are common. In each of these cases one lifeform
consumesanotherandtheexistenceofonedependsontheother.
Inthischapter,wewillconstructacomputersimulationthatwillgraphicallyshowaworldthatisinhabitedby
bothpredatorandprey.Inparticular,wewillmodelbearsandfish.Therewillberulesthateachmustliveby
and as time progresses, individuals will live, breed, die, and move about. We will be able to observe the
impactofinitialconditionsaswellastheinteractionofonelifeformwithanotherasthesimulationplaysout.

11.4RulesoftheGame
Ourcomputersimulationmodelsaworldthatcontainstwotypesofindividuallifeforms:bearsandfish.We
canthinkoftheworldasatwodimensionalgridwithafixedsizeforeachdimension.Lifeformswillonly
beabletoliveatspecificlocationswithinthegrid.
Eachlifeformwillbedescribedbyasetofrulesthatgovernshowitlives.Tostart,agroupoflifeformswill
beplacedintheworldatrandomlocations.Thesimulationwillprogressbyallowingoneofthelifeformsto
liveforonetimeunit.Duringthisunitoftime,allotherlifeformsareinsuspendedanimation.Thismeans
thatatanygiventimethelifeformwillbeinoneoftwostates:(1)aliveor(2)suspended.Theparticularlife
formwillbechosenatrandomfromthecollectionofallpossiblelifeforms.Eachtimealifeformisinthe
alivestateitmustreevaluateitssurroundingsbecauseitislikelythattherestoftheworldwillhavechanged
duringtheprevioustimeunits.Itisinthiswaythatthesimulationtakesonasenseofreality.
Fish will be allowed to breed, move, and die. Once a fish has been in the alive state twelve times, it may
attempttobreed.Todoso,itwillrandomlypickanadjacentlocation.Ifthatlocationisempty,anewfishwill
appear.Ifthelocationisoccupied,thefishwillhavetowaituntilnexttimeandtryagain.
Regardlessofwhetherafishbreeds,itwillnexttrytomove.Whenafishmoves,itwillrandomlypickan
adjacentlocation.Ifthatlocationisempty,thefishwillmovetothisnewlocation.Ifthelocationisoccupied,
thefishwillremaininitscurrentlocation.
Oneadditionalenvironmentalcharacteristicthatwillaffectfishisovercrowding.Ifafishdiscoversthatthere
aretwoormoreotherfishlivingadjacent,thenthefishwilldie.Thismeansthateveninthecompleteabsence
of bears, fish will selfregulate to some extent. In addition, note that in this version of the simulation fish
neverneedtoeat.
Bearswillbreed,move,eat,anddie.Breedingwilltakeplaceinmuchthesamefashionasdescribedabove
forfish.Theonlydifferencewillbethatbearsneedtobeinthealivestateeighttimesbeforetheycanstartthe
breedingprocess.Bearswillmoveinexactlythesamemannerasfish.
Bears will not be impacted by overcrowding but they will need to eat. In order to eat, a bear will need to
determinewhethertherearefishlivinginanadjacentlocation.Ifthereare,thenthebearwillrandomlypick
oneofthefishandeatit.Inordertoconsumethefish,thebearwillmovetothelocationcurrentlyoccupied
bythechosenfish.Iftherearenoadjacentfish,thebearwillbegintostarve.Anybearthathasbeeninthe
alivestateandstarvingtentimesinarowwilldie.

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 2/20
8/23/2016 Yuzu: Python Programming in Context

11.5Design
Wewillbeginbycomingupwithabasicdesignforoursimulationbasedonthepreviousdescription.The
firststepistoidentifythosepartsoftheproblemthatcorrespondtoobjects.Oneoftheeasiestwaystostart
theprocess ofobject identificationis to consider the prominent nounsthatappearinthedescriptionofthe
problem.Inthiscase,wordslikebear,fish,andworldseemlikegoodchoices.Thenounsthatyouidentify
nowwilllikelybecomePythonclasseswhenweimplementthedesign.
Next,weneedtoanalyzeeachnounandcomeupwithalistofthingsthatitshouldknowandalistofactions
thatitcanperform.Thelistofthingsthatanobjectshouldknowwillbecomeinstancevariables.Thelistof
thingsthatanobjectcandowillbecomemethods.Thisprocesswillbeveryhelpfulindecidinghowobjects
willinteractwithoneanother.Youmaynotidentifyalltheinstancevariablesandmethodsimmediately,but
youcanalwaysgobackandaddmorelaterasyoudiscoveradditionalinformationabouttheproblem.
The following lists provide a first attempt to identify the characteristic properties for the nouns previously
identified:
Aworldshouldknow
ThemaximumXandYdimensions.
Acollectionofalllifeformspresent.
Agridwiththelocationsofeachspecificlifeform.
Aworldshouldbeableto
Returnthedimensionsoftheworld.
Addanewlifeformataspecificlocation.
Deletealifeformwhereveritis.
Movealifeformfromoneworldlocationtoanother.
Checkaworldlocationtoseeifitisempty.
Returnthelifeformataspecificlocation.
Allowalifeformtoliveforonetimeunit.
Drawitself.
Abearshouldknow
Theworlditbelongsto.
Itsspecificworldlocationgivenas(x,y).
Howlongithasgonewithoutfood.
Howlongithasgonewithoutbreeding.
Abearshouldbeableto
Returnitslocation,boththexandyvalue.
Setitslocation,boththexandyvalue.
Settheworldthatitbelongsto.
Appearifitisnewlyborn.
Hideifitdies.
Movefromonelocationtoanother.
Liveforonetimeunit(includesbreeding,eating,moving,anddying).
Afishshouldknow
Theworlditbelongsto.
Itsspecificlocationgivenas(x,y).
Howlongithasgonewithoutbreeding.
Afishshouldbeableto
Returnitslocation,boththexandyvalue.
Setitslocation,boththexandyvalue.
https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 3/20
8/23/2016 Yuzu: Python Programming in Context

Settheworldthatitbelongsto.
Appearifitisnewlyborn.
Hideifitdies.
Movefromonelocationtoanother.
Liveforonetimeunit(includesbreeding,moving,anddying).
Figure11.1showsasummaryoftheseresultsinatabularform.Eachtableconsistsofthenameoftheclass,a
listofinstancevariables,andalistofmethods.Nameshavebeenchosenthatareconsistentwiththeprevious
descriptions.Notethatwehaveincludedanextrainstancevariable,aturtle,ineachclass.Sincewewantthis
tobeagraphicalsimulation,itwillbenecessarytoincludeinstancesoftheTurtleinanyclassthatwillhave
sometypeofdrawingcapability.

Figure11.1Listinginstancevariablesandmethodsforeachclass

11.6Implementation
We will create our simulation in Python by first implementing the instance variables and methods of the
classesasdescribedpreviously.Oncewehavetheclassesinplace,wecancompletethesimulationbysimply
creating a world, adding lifeforms to it, and then allowing the lifeforms to live. The longer we let the
simulationrun,themoreinterestingtheresultsmaybecome.

11.6.1TheWorldClass
WewillbeginbyimplementingtheWorldclass.RecallthattheWorldclasswillmaintaindimensionsaswell
asalistofthelifeformsthatarepresent.Itwillalsohaveagridtotrackthelifeformpositions.ATurtlewill
beusedtosettheinitialcoordinatesfortheworldandtodrawthegrid.
Implementation of the grid will require some additional thought. The grid will maintain the exact two
dimensional position of each lifeform. Each entry in the grid may contain a reference to a lifeform. For
https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 4/20
8/23/2016 Yuzu: Python Programming in Context

thosegridpositionsthatdonotcontainalifeform,wewillusetheNonevalue.
Figure11.2showsanexample12by6gridwiththreelifeformspresent.Eachpositionofthegridwillbe
addressedbyaunique(x,y)pair.Therearebearslivingatpositions(2,4)and(11,5),andafishiscurrentlyat
position(5,0).
One of the easiest ways to implement a collection in Python is to use a list. However, since lists are one
dimensional,weneedtobecreativeinordertorepresentthegridstwodimensionalstructure.Thesolution
willbetoimplementtherowsandcolumnsofthegridusingalistoflistsstructure.Inordertodothis,we
needtomakeadecisionwhetherthegridisactuallyacollectionofrowsoracollectionofcolumns.Oncewe
makethisdecision,thecodethatwewritewillsimplyneedtobeconsistentwiththatdecision.
Ourchoicewillbetothinkofthegridasacollectionofrows(listofrows).Incomputersciencethisisknown
asrowmajorstorage.Eachrowwillinturnbealistofitems,oneforeachcolumn.Theentrieswilleitherbe
areferencetoalifeformorNone.Figure11.3showsaPythonvariablecalledgthatisassignedtoalistofsix
lists.ThislistoflistsisdesignedtorepresenttheinformationfromFigure11.2.

Figure11.2Agridwiththreeitems

Figure11.3Alistoflistsrepresentationfortheexamplegrid

Eachiteminthelistgcorrespondstoarowinthegrid.Row0canbeaccessedbyg[0],row1byg[1],andso
on. Within each row there are 12 items. To access a specific item simply requires another list index. For
example,thefishisshowninposition(5,0).Toaccessthisposition,wewoulduseg[0]toaccessrow0and
thenuse[5]toaccessthesixthiteminthelist.Puttingthisalltogetherwouldgiveg[0][5]asthecomplete
accesstothatitem.
Generalizingthispattern,ifwewanttoaccesstheitematany(x,y)location,thelistoflistsgridlocationwill
begivenbyg[y][x].Itisimportanttonotethatsincewedecidedthatourrowswouldbestoredtogether,the
firstindexwillbetheroworyvalueandthesecondindexwillbethecolumnorxvalue.Thetwobearscan
https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 5/20
8/23/2016 Yuzu: Python Programming in Context

thereforebeaccessedbyg[4][2]andg[5][11].
Listing11.1showstheconstructorfortheWorldclass.Itcreatessixinstancevariables:(1)maxX,(2)maxY,
(3)thingList,(4)wturtle,(5)wscreen,and(6)grid.Initiallygridisanemptylist.Lines711showanested
iteration used to create the list of lists implementation. Each row is first created by repeatedly appending
Nonetoanemptylist.Thatentirelististhenappendedtothelistoflists(self.grid).Recallfromadiscussion
inChapter4thatsimplyusingtherepetitionoperatorwillresultinreferencestothesamelist,obviouslynot
whatwewantorneedhere.

Listing11.1TheWorldclassconstructor

Theremainderoftheconstructor(lines1318)modifiesthecoordinatesystemandaddstwonewshapesthat
will be used later as icons for fish and bears. The wturtle is hidden so that the default triangle shape is no
longerpresent.
The draw method (Listing 11.2) will use wturtle to draw the grid system assuming the maximum x and y
dimensions.Itfirstdrawstheouterboundariesofthegridandthengoesbackandfillsinthehorizontaland
verticallines.
TheotherWorldmethodsareshowninListing11.3.Thesewillallowustoaccessorchangesomeaspectof
the simulation world. The simple accessor methods getMaxX and getMaxY will return the maximum
dimensions, emptyLocation will return True or False depending on whether there is a lifeform at that
particularlocationinthegrid,andlookAtLocationwillreturnthevalueataparticulargridlocation.

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 6/20
8/23/2016 Yuzu: Python Programming in Context

Listing11.2Thedrawmethod

ThefreezeWorldmethodisusedattheendofthesimulation.Itcallstheexitonclickmethodofwscreento
allowthegraphicswindowtoremaindrawnafterthesimulationhasended.Clickingonthewindowwillthen
exitthesimulation.
AnewlifeformisaddedtotheworldbyaddThing,lines410.Itneedsthelifeformandthe(x,y)position
wherethelifeformshouldbeplaced.Someofthemethodsthatarecalledbelongtothelifeformitselfand
havenotyetbeendescribed.Wewillcomebacktotheseatalatertime.Fornow,line7addsthelifeformto
thegridandline9appendsthelifeformtothelistoflifeformsmaintainedbytheworld.
Removingalifeformfromthesimulationworldrequiresthatitbetakenoffthegridandalsoremovedfrom
thelifeformlist.ThisisdoneinthedelThingmethod.Line14setstheappropriategridreferencetoNone.
Line15usesthelistmethodremovetodeletethefishorbearfromthelistoflifeformsmaintainedbythe
world.

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 7/20
8/23/2016 Yuzu: Python Programming in Context

Listing11.3TherestoftheWorldclass

TheliveALittlemethod,lines2731,doesthemajorityofthesimulationwork.Aslongastherearestilllife
formsremaining,theworldwillpickarandomlifeformfromthelistthatitmaintains(lines2930).Oncea
randomlifeformhasbeenchosen,itisallowedtoliveaswasdescribedearlier.Thedetailsaredependenton
the type of lifeform that was chosen. Note that the world lets only one lifeform live during every call to
liveALittle.

Exercises

11.1ImplementtheWorldusingcolumnmajorstorageforthegridoflocations.Inotherwords,storethe
columnstogetherinlistsinsteadoftherows.

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 8/20
8/23/2016 Yuzu: Python Programming in Context

11.2Addtwoadditionalinstancevariables,bearCountandfishCount,totheWorldclass.Alsoaddmethods
getNumBears,getNumFish,incBears,decBears,incFish,anddecFishtoincrementanddecrementthe
appropriatecountersbyone.

11.3AddamethodshowCountstoshowthecountofbearsandfishyouaddedinExercise11.2.Youmay
wanttousethewritemethodofwturtleforthistask.

11.6.2TheFishClass
Inthissimulation,fisharetheprey.Theywilltrytobreed,movearound,andinevitablymayendupinthe
vicinityofabearwhowilllikelyeatthemfordinner.Fishneedtoknowwheretheyareandhowlongithas
been since they have given birth. In addition, since this is a graphical simulation, each fish will have an
instanceofTurtlesothatitcanbedisplayed.
InListing11.4,whichshowstheconstructorfortheFishclass,self.xposandself.yposareinitializedto(0,0)
butwillbesetlaterwhenthefishisplacedintheworld.Likewise,eachfishwillhaveareferencebacktothe
worldthattheylivein.Line10setsthisreferencetoNone.Thiswillalsobesetwhenthefishareaddedtothe
world.
ThenextgroupofmethodsfortheFishclasswillbethesimpleaccessorandmutatormethodsusedbyother
objectsastheyinteractwithFishobjects(seeListing11.5).Theyallowgettingandsettingofvaluesas
wellassomebasicTurtlefunction.Theappearandmovemethodsneedabitofexplanation.WhenaFishis
firstcreated,itwillbeaddedtotheworldataspecificlocationandthentoldtoappear.Theappearmethod
willtakecareofmovingtheunderlyingTurtleobjectandthenshowingtheiconinthesimulationdisplay.

Listing11.4TheconstructorfortheFishclass

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 9/20
8/23/2016 Yuzu: Python Programming in Context

Listing11.5ThesimpleaccessorandmutatormethodsoftheFishclass

Once the Fish object is in the simulation, it can be moved to a new location using the move method. This
methodnotonlymovestheunderlyingTurtlebutalsochangesthexandypositionsoftheFishobject.From
theFishobjectsperspective,thesearethetwothingsthatmusthappenforittomove.Itisimportanttonote
thattheworldmustalsomovetheobjectinitsunderlyingrepresentation.
ThemostimportantmethodintheFishclassisliveALittle.Thismethodisresponsibleforwhathappenstoa
Fisheachtimeitisallowedtoliveforatimeunit.Recallfromourdescriptionthatfishcandieiftheyare
crowdedbytoomanyotherfish.Ifthatisnotthecase,thentheywilltrytobreedandthenfinallytrytomove.
Ofcourse,ifafishdiesduetoovercrowding,itwillnotattempttobreedormove.
ThefirstthingthismethodneedstodoiscountthenumberofFishthatareatadjacentlocationsinorderto
decideifovercrowdingisoccurring.Thiswillrequirethatwedesignaprocessthatallowsustofindallofthe
locationsthatareadjacenttoagivenlocation.Forthistask,wewillusealistofoffsets.Eachoffsetisatuple
containinganamounttoadjustthexcoordinateandanamounttoadjusttheycoordinate.Figure11.4shows
the(x,y)valuesforanylocationandtheeightlocationsthatoccuraroundit.Notethatfromtheoriginal(x,y)
itispossibletocomputethecoordinatesofalladjacentlocations.
Figure11.5showshowwecantakethecoordinates,reducethemtoanoffsettuple,andthensimplycreatea
listoftheseoffsettuples.NowitisjustamatterofiteratingthroughtheoffsetListandadjustingtheoriginal
(x,y)positionbytheappropriatetuplecomponent.
Listing 11.6 shows the liveALittle method. Line 2 creates the list of offset tuples and line 5 initializes a
variabletocountthenumberofneighboringfish.Lines713areperformedforeachoffsetinoffsetList.First,
newxandyvaluesarecomputedfromthecurrent(x,y)locationandthecurrentoffsettuple.Line10checks
toseeifthisnewlocationisactuallyalegallocation.Itispossiblethatwecangenerateanillegallocationif
https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 10/20
8/23/2016 Yuzu: Python Programming in Context

theoriginal(x,y)wasonaboundary.

Figure11.4Coordinatesoftheeightadjacentlocationsaroundaspecific(x,y)

Figure11.5Coordinatesoftheeightadjacentlocationsaroundaspecific(x,y)

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 11/20
8/23/2016 Yuzu: Python Programming in Context

Listing11.6TheliveALittlemethod

Ifthelocationislegal,thenitcanbecheckedforalifeform.Finally,weusethebuiltinisinstancefunctionto
askifanobjectisaninstanceofaparticularclass.Inthiscase,wewanttocountonlythoseobjectsthatare
instancesoftheFishclass.Line13incrementstheadjfishcounterifthisisthecase.
OncewehavecountedtheadjacentFish,line15checkstoseeifthereisovercrowdingaccordingtotherules
describedearlier.Ifso,worldistoldtodeletethefish(self).Sinceselfisareferencetotheactualfish,world
canusethereferencetolocateandremovetheobjectfromthingList.Thismaylookoddtoyousincewehave
alwaysusedselfasaformalparameterratherthanasanactualparameter.However,ifyoutracethecallto
delThinginListing11.3,youwillseethatselfisassignedtotheformalparameterathing.
Ifnoovercrowdingistakingplace,thenextactivityistocheckwhetherthefishhasbeenactiveforenough
timeunitstotrytobreed.Accordingtotherules,itmustwait12timeunitsbeforetryingtobreed.Regardless
ofwhetherthefishissuccessfulinbreeding,itthentriestomovetoanewlocation.
IfitisdeterminedthattheFishcantrytobreed,thetryToBreedmethodiscalled(Listing11.7).Accordingto
therules,thismethodmustfirstpickarandomadjacentlocation.Todothis,wecanusethesameoffsetlist
techniquethatweusedearlier.Inthiscase,insteadofiteratingthroughalloftheoffsets,wewillsimplypick
one.Inordertochoosearandomelementofthelist,wewillfirstchoosearandomintegerintherangeof
indexvaluesusingthelengthoftheoffsetListastheupperbound.Oncewecomputethisnew(x,y)position,
wemustbesurethatitisintheactualrangeoflegalcoordinates.Ifitisnot,wemusttryagain.Thewhile
loop(line9)continuestochooserandomoffsetpairsuntilalegalresultisobtained.
Onceanewrandomlocationhasbeendetermined,therulesstatethatitmustbeemptyinorderforbreeding
totakeplace.Line16checksthelocationtoseeifanylifeformsarepresent.Iftheyarenot,anewFishis
created and added to the world at that location. Recall that the addThing method sets the (x, y) and world
valuesandmakesthefishappearinitsinitialpositioninthegraphicaldisplay.
ThetryToMovemethod(Listing11.8)hasmuchofthesamefunctionalityastryToBreed.Itmustfirstpicka
randomadjacentlocationandcheckifitisempty.Ifitis,thefishisallowedtomoveusingthemovemethod.

Listing11.7ThetryToBreedmethod

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 12/20
8/23/2016 Yuzu: Python Programming in Context

Listing11.8ThetryToMovemethod

Exercises
11.4MaketheoffsetListaninstancevariableoftheFishclass.

11.5 Modify the liveALittle method to use instance variables for the number of fish that constitute
overcrowding,andthenumberoftimeunitsthatmustpassbeforebreedingisallowed.Alsomodify
theFishconstructortohaveparameterstoinitializethenewinstancevariables.

11.6Addgetterandsettermethodsfortheinstancevariablesyoucreatedinthepreviousexercise.

11.7ModifythetryToMovemethodtoallowthefishtotryuptofourrandomlocationsforitsmove.

11.6.3TheBearClass
The Bear class is implemented in much the same way as the Fish class. In fact, many of the methods are
identical.Wewillfocusonthosemethodsthatareeitherdifferentorarenew.Theobviousplacetostartis
with the liveALittle method and the constructor. As described earlier, bears will not be impacted by
overcrowding.Thefirstthingtheywilldoistrytobreed.Theotherimportantdifferenceisthatbearswilleat
fishiffishareavailable.However,anybearthatdoesnotgetenoughfoodwillstarveanddie.Thismeansthat
theconstructorfortheBearclasswillneedtoincludeonemoreinstancevariablethatwillkeeptrackofthe
numberoftimeunitssincethebearslastmeal.
Listings11.9and11.10 show these modifications. The obvious difference between this constructor and the
Fish constructor is the graphics icon used as a turtle shape and the inclusion of the starveTick instance
variable.ThefirstthreelinesoftheliveALittlemethodsuggestthatbearsmustwaitforeighttimeunitsbefore
attemptingtobreed(recallthatfishwaitedfor12).ImplementationofthetryToBreedmethodisidenticalto
theFishclass.
ThenextstepintheliveALittlemethodallowsabeartotrytoeatafish(line6).ThetryToEatmethodshown
in Listing 11.11 is new. Recall that bears will check in the adjacent locations to see if there are any fish
present.Ifso,thebearwillpickoneofthefishatrandomandeatit.Thismeansthatthebearmovestothe
location currently occupied by the fish and the fish dies. If there are no fish present, the bear starves a bit
more.
https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 13/20
8/23/2016 Yuzu: Python Programming in Context

TheconstructormethodintheBearclass

Listing11.10TheliveALittlemethodintheBearclass

Asbefore,wewillusetheoffsetlisttechniquetochecktheadjacentlocationsforFish.However,insteadof
just counting the number of Fish that are present, we will create a list of adjacent fish. Line 5 creates an
initiallyemptylistandline13addstheneighboringFishtothelist.Thedetailsforcomputingtheadjacent(x,
y)locationsandcheckingtheinstancetypearethesameasweusedtolookforneighboringfishinthetestfor
overcrowding.
Line15checkstoseeifthereareanyFishadjacenttothebear.Ifnot,thenstarveTickisincremented(line
24).Iftherearefishpresent,oneischosenatrandombyonceagainpickingarandomindexvalue(line16).
Sincethebearmustmovetothelocationofthechosenfish(inordertoeatit),wemustgetthecurrent(x,y)
locationoftherandomlychosenfishbyusinggetXandgetY.Lines2021telltheworldtodeletethefishand
tell the bear to move to the location formerly occupied by the fish. Since the bear has now eaten, it is no
longerstarvingandstarveTickcanberesettozero(line22).

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 14/20
8/23/2016 Yuzu: Python Programming in Context

Listing11.11ThetryToEatmethod

Bearsmoveinexactlythesamewayasfish.ThetryToMovemethodisidenticalinbothclasses.Remember
thattheycheckadjacentlocationsandcanmoveonlyifoneisavailable.Itisalsoimportanttonotethatinthe
liveALittlemethodforBear,thetryToMovemethodisdoneregardlessofwhetherthebeareats.Thismeans
thatitispossibletohavethebearmovetwiceduringatimeunit,oncetoeatandthenagainwhenitmoves
normally.

Exercises
11.8MaketheoffsetListaninstancevariableoftheBearclass.

11.9ModifytheliveALittlemethodtouseinstancevariablesforthenumberoftimeunitsthatmustpass
beforeabearstarves,andthenumberoftimeunitsthatmustpassbeforebreedingisallowed.Also
modifytheBearconstructortohaveparameterstoinitializethenewinstancevariables.

11.10Addgetterandsettermethodsfortheinstancevariablesyoucreatedinthepreviousexercise.

11.11ModifythetryToMovemethodtoallowthebeartobemoreintelligentaboutthedirectionitmoves.
Perhapsthebearcanlookfartherouttoseeifthereareanyfishinsomedirection.

11.6.4MainSimulation
We can now turn our attention to implementing a function that will be responsible for setting up the entire
simulationandstartingit.Recallthatonceitisinexecution,thesimulationitselfdictateshowitprogresses
duetotherandomnumbersthataregenerated.Thismainfunctionwillneedtodofourthings:

1.Setinitialconstantspertainingtothenumberofbearsandfish,thesizeoftheworld,andthelengthof
thesimulation.

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 15/20
8/23/2016 Yuzu: Python Programming in Context

2.CreateaninstanceoftheWorld.
3.Createthespecifiednumberofbearsandfishandplacethematrandomlocationsintheworld.
4.Lettheworldliveforthespecifiednumberoftimeunits.

Listing11.12showsthecompletemainSimulationfunction.Inthiscaseweinitializetheworldwithtenbears
andtenfish.Line8createsaWorld,calledmyworldwithdimensionsasdefined.Thegridformyworldisthen
drawn(line9).
Lines 1118 will create and place the Fish objects in the world. Note that once a fish has been created, a
random(x,y)locationmustbegenerated.Sincewedonotwantmorethanonelifeformlivingataspecific
location, we must check to see if the location is empty before using it. If it is not empty, we continue to
randomlygeneraterandom(x,y)locations(line24).Forourpurposeshere,weassumethattherewillalways
beenoughspacetofindanemptylocation.
Bearsarecreatedandplacedinthesameway(lines2027).Figure11.6showstheonepossiblewaythatthe
lifeforms can be placed in the world. Of course, due to the random nature of the initial location selection,
subsequentrunsofthesimulationwilllikelylookdifferentatthestart.

Listing11.12ThemainSimulationfunction

Mostoftheworkisthenaccomplishedbylines2930.Foreachtimeunit,myWorldsliveALittlemethodis
https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 16/20
8/23/2016 Yuzu: Python Programming in Context

called.Recallthatthismeansthatarandomlifeformwillbechosentoliveforthattimeunit.Thefinallineof
thefunctionsimplyputstheworldintoawaitmodesothatwecanobservethefinalstateofthesimulation.
Whenyouclickinthesimulationwindow,itwillcloseandthefunctionwillexit.

Figure11.6Theinitialsimulation

Exercises

11.12 Modify addThing and delThing to automatically update the fish and bear counters you added in an
earlierexercise.

11.13Runthesimulationwithdifferentnumbersofbearsandfish.Whatdoyouobserveaboutthenumberof
bearsandfishovertime?

11.14ModifymainSimulationtocreatebearsandfishusingdifferentconstantsforthetimetobreedandtime
tostarve.Thiswillalloweachfishtobehaveslightlydifferently.

11.15 Modify the simulation so that it will automatically stop if the number of bears or fish go above or
belowsomethresholdsthatyoudefine.

11.16ModifytheliveALittlemethodoftheWorldsothatallbearsandfishgetachancetoliveeachtime
unit.Tomakeitfair,youshouldrandomlyshufflethingListeachtimeunit.

11.17ModifythemainSimulationfunctiontocreatetwolists.Onelistwillkeeptrackofthenumberoffish
thatarealiveineachtimeunit,theotherwillkeeptrackofthenumberofbears.Whenthesimulationis
done,thisdatashouldbewrittentoafile.Thefileshouldhavethreecolumns:onecolumnforthetime,
oneforthenumberoffish,andoneforthenumberofbears.

11.18 Write a function to read the file produced by the previous exercise. This function should produce a
graphthatshowshowthenumberofbearsandfishchangeovertime.

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 17/20
8/23/2016 Yuzu: Python Programming in Context

11.7GrowingPlants
Inourinitialdescriptionofthebearsandfishmodel,wementionedthatfishcouldalsobepredatorsinthat
theycouldeatplantsthatexistinthewater.Iftherearenotenoughplantsavailable,thefishwouldstarveand
die.Itisrelativelyeasytoincludeanotherclasstorepresenttheplantsbytakingcodethatalreadyexistsand
simplyrepackagingitasrequiredforaplant.
We can begin by realizing that plants will not be able to move. In addition, we will not have them eat
anything.So,theonlyactionthattheyreallycarryoutwhentheyliveisbreeding.Listing11.13showsthe
liveALittlemethodforthePlantclass.Notethatweareassumingthatplantscanattempttobreedafterfive
timeunits.

Listing11.13TheliveALittlemethodinthePlantclass

TheonlyotherchangethatweneedtomakeistheinclusionofatryToEatmethodaspartoftheFishclass.
ThismethodwouldbeidenticaltothetryToEatmethodoftheBearclassexceptfortheonemodificationthat
fisheatplants.Listing11.14showsthemodifiedlineofcodethatwouldbeused.

Listing11.14ThemodifiedliveALittlemethodintheFishclass

Figure11.7Theinitialsimulationincludingplants

Finally, we need to modify the main function so that plants are also added to the simulation at random
locations.Figure11.7showsapossibleinitialscenariothatincludesplantsaswellasthebearsandfishfrom
https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 18/20
8/23/2016 Yuzu: Python Programming in Context

before.

Exercises

11.19CreateaPlantclassthatissimilartotheFishclassusingthemodificationssuggestedinSection11.7.

11.20ModifytheliveALittlemethodoftheFishclasssothatfishcantrytoeatplants.Youwillneedtoadda
starveTickcounterforthefish.

11.21ModifythemainsimulationtocreatesomeinitialnumberofplantsandplacethemintheWorld.

11.22ModifythePlantclasstoimplementsomekindofovercrowdingmechanism.

11.8ANoteonInheritance
AsyouconsidertheimplementationfortheFish,Bear,andPlantclasses,youhaveprobablynoticedthatthere
areanumberofduplications.Eachclassincludesmethodsthatareidentical.Forexample,getX,getY,setX,
setY,appear,andhideareallimplementedthesamewayregardlessoftheclass.

Thisobservationleadsustothinkaboutthesimilaritiesamongalllifeformsinoursimulation.Itwouldmake
senseifwecouldsomehowtakeallofthisduplication,putitinoneplace,andhaveallofthelifeformsshare
it.Thisnewimplementationwouldallowustoefficientlyaddandmodifyfunctionality.

Thesolutionthatweareproposingiscalledinheritanceanobjectorientedprogrammingtechniquethatis
supportedbymanyprogramminglanguagesincludingPython.Weintroducethisnewtechniqueinthenext
chapterandwillaskyoutoreconsiderthesimulationasaprogrammingexercise.

11.9Summary
Inthischapterwedesignedandimplementedalarge,multiclassgraphicalsimulation.Tostart,weconsidered
adescriptionoftheproblem.Wepickedouttheimportantsubjects(nouns)andbegantoimplementclasses,
one for each. In order to design a class, we described those things that instances of a class would need to
know about themselves (the instance variables) and those things that instances of a class would need to be
abletodo(themethods).Aftercreatingeachclass,weimplementedafunctionthatestablishedmanyobjects
andleteachoneperformmethodsasdefinedbytheinitialrulesofthesimulation.Byusingrandomnumbers,
wewereabletoprovideadegreeofuncertaintysothatnotworunsofoursimulationwouldlookthesame.

KeyTerms
class
columnmajorstorage
computersimulation
inheritance
instancevariables
methods
nouns
object
predatorpreyrelationships
randomnumbergenerator
https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 19/20
8/23/2016 Yuzu: Python Programming in Context

rowmajorstorage

PythonKeywords
class
def
for
if
isinstance
None
random
randrange
range
self
while

Bibliography

[Fla02] GaryWilliamFlake.TheComputationalBeautyofNature.MITPress,2002.
[KE85] RichardKnightandL.L.Eberhardt.Populationdynamicsofyellowstonegrizzlybears.Ecology,
1985.

ProgrammingExercises

11.1Modifythesimulationsothatworldconsistsoftwokindsoflocations,waterandland.Fishandplants
canliveonlyinthewaterandbearscangointhewaterbutonlytoeatfish.

11.2Modifythesimulationtogivebearsandfishanenergyinstancevariable.Eachtimeabearorfisheats,
itincreasesitsenergylevel.Iftheenergyleveldropstozero,thebearorfishdies.Iftheenergylevelis
sufficientlyhigh,thenthebearorfishcanbreed.Bearsandfishshouldloseenergyastheymoveand
breed.

11.3AddaBerryclasssothatthebearshaveanotherfoodsource.Thisclassshouldbehavemuchlikethe
Plantclass.

11.4Chooseanotherrealworldpredatorpreyecosystemandimplementasimulation.

https://reader.yuzu.com/#/books/9781284089042/c/6/36!/4/428@0:22.4 20/20
8/23/2016 Yuzu: Python Programming in Context

CHAPTER12
YourFatherWasaRectangle

12.1Objectives
Tointroducetheconceptofinheritance
Tocreateaworkingobjectorientedgraphicspackage
Toprovideanotherexampleofobjectorienteddesign

12.2Introduction
So far in this book we have relied on our trusty turtle for any graphics we want to add to our programs.
However,mostcomputergraphicsprogramsdonotuseaturtle.Commondrawingprogramsthatyoumaybe
familiarwith,includingVisioorOmniGraffle,allowuserstoselecttheshapestheywanttoincludeintheir
drawing from a palette of available shapes. For simple drawing programs, this palette may include lines,
circles, squares, triangles, and other simple shapes. For more advanced drawing programs, such as
architecture programs or computeraided design (CAD) programs, the palette may include sophisticated
shapes like cabinets, bathroom fixtures, or even specific parts needed for a car. These kinds of drawing
programsaretypicallycalledobjectorienteddrawingprograms.
Objectoriented drawing programs are easy to use. If your drawing calls for a square, you simply drag a
squarefromthepaletteofshapesanddropitintoyourdrawingintheappropriateplace.Ifyourdrawingcalls
foracircle,oranyothershape,youdragthecircleintoposition.Onceashapeisplacedinthedrawing,the
usercanchangeattributesoftheshape,suchasitssize,color,outline,orposition.
Our goal for this chapter is to design an objectoriented graphics module for Python that will allow you to
write graphics programs by creating simple graphical shapes and placing them on a drawing canvas. The
shapeswillbeexactlythesameastheshapesyouwoulduseifyouweremakingadrawingprogramsuchas
theonesdescribedpreviously,exceptthatwewillnotgosofarastowritethecodethatwouldletauserdrag
theobjectsfromapalette.
Tobeclearaboutwherewearegoinginthischapter,considerthesimplePythonfunction,drawHouse,shown
inListing12.1.Thecodedrawsabluefilledrectangleasthemainpartofahouse,alongwithabrowndoor
andapeakedroof.Inaddition,itdrawsayellowsunthatslowlymovesacrossthesky.Thepicturedrawnby
thecodeinListing12.1isshowninFigure12.1.

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 1/23
8/23/2016 Yuzu: Python Programming in Context

Listing12.1Usinggeometricobjectstodrawahouse

Intherestofthischapterwewilldesignandimplementasetofclassesthatenableustowritethefunctionin
Listing12.1.Alongthewaywewilllearnaboutapowerfulprogrammingconceptcalledinheritance.

Figure12.1Usingobjectorientedgraphicstodrawasimplescene

12.3FirstDesign
Whendesigningaprojectlikeourgraphicsmodule,itisagoodideatobeginbymakingalistofthedifferent
kindsofobjectsinvolvedandtherelationshipsbetweenthoseobjects.Aswemakethelistofobjects,wewill
trytoidentifytwoimportantkindsofrelationshipsbetweentheobjects.Therelationshipswearelookingfor
areISAandHASA.
TheISArelationshipdescribes twoobjects where one objectisa more specific instanceof the other. For
example, a square isa more specific instance of a rectangle, and a circle isa more specific instance of an
ellipse.AsweidentifytheseISArelationships,wealsolookforfunctionalitythateachoftheinstanceshave
incommon.
AHASArelationshipdescribestwoobjectswhereoneobjectusesanotherobject.Forexample,ifyouthink
about a circle, each circlehasa centerpoint.A rectanglehasa lowerleftcorner point and an upperright
https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 2/23
8/23/2016 Yuzu: Python Programming in Context

cornerpoint.
With these definitions in mind let us now create a list of objects for our graphics module. An initial set of
objectsmightlooklikethefollowing:
Square
Circle
Oval
Rectangle
Triangle
Polygon
Line
Point
Canvas

Asyoubegintothinkaboutthewordsinthelist,youwillrecognizethatthereareISArelationshipsbetween
severalofthem.ARectangleisaPolygon.ASquareisaRectangle.ATriangleisaPolygon.ACircleisan
Oval.Youmayalsorecognizethatallofthewordswehavejustmentionedareshapes.Thewordshapesisnot
onourlistofobjects,butitmaybeagoodideatoadditbecauseitisanabstractionofmanyofthewordswe
havelisted.Asyoubegintodesignanapplication,thiswilloftenbethecase.Asyoustartthedesignprocess,
andasyoubegintoorganizetheobjectsyouhaveidentified,youwillveryoftenuncovernew,moreabstract
objects.
ThenextstepistoarrangeourwordsintoadiagramthatmakestheISArelationshipsexplicit.Figure12.2
illustratestherelationshipsbetweentheobjectswehavediscussedsofar.Thisdiagramisoftenreferredtoasa
hierarchicaldiagrambecauseoftheverticalrelationshipsbetweentheobjects.Themostabstractorgeneral
objectisatthetopofthehierarchyandthemostspecificobjectsareatthebottom.
When you look at the diagram in Figure 12.2 and think about the words on our list, you may have some
questions:WhataboutPoint,Line,andCanvas?Wheredotheseobjectsbelong?ShouldLinesandPointsbe
includedintheshapeshierarchy?IsthereanotherabstractionthatwecouldusetorelateLinesandPointsto
theothers?
WemightdeterminethatalthoughLinesandPointsarenotshapes,theyaregeometricobjects.Inaddition,
shapesarealsogeometricobjectsandsowemightcreateanotherabstractioncalledGeometricObject.
Andanotherquestionmightbeasked:WhatabouttheCanvas?TheCanvasisnotaGeometricObject,ora
Shape. However the Canvas is the place where we will draw our lines, points, and other shapes. The
relationship between Canvas and GeometricObject is a HASA relationship. A Canvas may contain many
geometricobjects.ThisnewhierarchycanbeseeninFigure12.3.
With this hierarchy of objects complete, we can begin to think about implementing the objects as Python
classes. The difference between this implementation and the implementations we have seen in previous
chaptersisthatinthiscasewewillusethehierarchicalrelationshipstoavoidtheduplicationofcodethatwe
sawinBears,Fish,andPlants,OhMy!(Chapter11).

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 3/23
8/23/2016 Yuzu: Python Programming in Context

Figure12.2Asimpleobjecthierarchy

Inobjectorientedprogramming,theISAlinksinFigure12.3defineaninheritancehierarchy.Inheritanceis
theideathatamoregeneralclassisaparentclassofamorespecificclass(achildclass).Theparentclass
may have methods that can be shared with any child. This idea of shared methods is how we avoid
duplicatingcode.
Inourexample,theRectangleclassisachildoftheShapeclass.Inobjectorientedprogrammingterms,this
meansthatanymethodsthatwewritefortheShapeclassareinheritedbytheRectangleclassandtherestof
theclassesthatarebelowShapeintheinheritancehierarchy.WhentwoclassesareconnectedbyanISAlink,
wecallaclassthatisdirectlyaboveanotherclassintheinheritancehierarchyasuperclass.Theclasswhere
theISAlinkoriginatesiscalledthesubclass.
Inthenextstageofourdesignweneedtoaskthefollowingquestions:

Whatthingsshouldeachobjectknow?
Whatthingsshouldeachobjectbeabletodo?

Thethingswewanteachobjecttoknowwillleadustothesetofinstancevariables.Thethingsthateach
objectcandowillgiveusthesetofmethodsweneedtowrite.

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 4/23
8/23/2016 Yuzu: Python Programming in Context

Figure12.3Anexpandedobjecthierarchy

Thereareseveralthingswewantourobjectstorememberaboutthemselves:

Fillcolor
Outlinecolor
Positiononthecanvas
Linewidth

IfyoulookbackatthedrawHousefunctioninListing12.1,youwillquicklycomeupwithalistofthingsthat
ourshapesshouldbeabletodo:

Setorchangetheoutlinecolor:setOutline(color).
Setorchangethefillcolor:setFill(color).
Movetoanewpositiononthecanvas:move(dx,dy).
Setorchangethewidthofalineoroutline:setWidth(w).
Setorchangethecolorofalineorpoint:setColor(color).

Wearenowatthepointwherewecantalkabouttheadvantagesofusinginheritance.Sinceallshapeshave
thesamesetofattributes,wecancreatetheinstancevariables,andwritethemethodstomodifythoseinstance
variablesintheShapeclass.Wecandothisbecausewheneveramethodisdefinedinasuperclassthemethod
canalsobecalledbyaninstanceofthesubclass,orevenanyofthesubclassesofthesubclass.Thiscansave
usanenormousamountofworkandisoneofthereasonsthatspendingtimeupfrontdoingagooddesignis
soimportant.
Itisgoodprogrammingpracticetolearnhowtodevelopalargeprogramlikethisonestepatatime.Tohelp
uswiththistask,wewillwriteasimpletestprogramthatusesjustasmallpartoftheoverallfunctionalitywe
wanttocreate.Oncewegetthissimpleprogramworking,wecanmoveontosomethingmorecomplicated.
Writingprogramsthiswayismucheasierandmorefunthantryingtowriteallthecodeforalargeprogram
firstandthentryingtofigureoutwherethingswentwrong.

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 5/23
8/23/2016 Yuzu: Python Programming in Context

Oneoftheeasiestprogramstowritethatmovesusalongwaytowardourendgoalistosimplydrawalineon
thescreen.ThisprogramisshowninListing12.2.Althoughtheprogramlookssimple,itactuallyforcesusto
dosomemoredesignontheclassesPoint,Line,GeometricObject,andCanvas.

Listing12.2Drawingaline

TheCanvasclassisresponsibleinsomewayfordrawingaGeometricObject.HowwillCanvasdothis?The
answer is by using a turtle. That is the only tool we have right now for drawing anything. In addition, the
turtle comes with a builtin window for drawing. With all of the turtles functionality, implementing the
Canvasclassshouldbeprettyeasy.ButitdoesmeanthatoneoftheinstancevariablesoftheCanvaswillbea
Turtle.WewillalsohavetowriteadrawmethodfortheCanvasthatcantakeanyshapewegiveitanddrawit
usingtheturtle.
The GeometricObject class has instance variables for lineColor and lineWidth. We also know that the
GeometricObject class will need methods for getting and setting the lineColor and lineWidth instance
variables.Althoughwewillnotchangethecolororwidthinoursimpletestprogram,wewillstillimplement
themethodsnow.
The Point class is an abstraction for a geometric point in our twodimensional coordinate system. The two
instancevariablesthatareuniquetoapointarethexcoordinateandtheycoordinate.Inaddition,thePoint
classinheritsthelineColorandlineWidthinstancevariablesfromGeometricObject.ThismeansthataPoint
has the following instance variables: x, y, lineColor, and lineWidth. The Point class provides three simple
methods: getX returns the x coordinate, getY returns the y coordinate, and getCoord returns the x and y
coordinatesasatuple.
TheLineclasshastwoinstancevariables:abeginningpointandanendingpoint.LikePoint,Lineinheritsthe
instancevariableslineColorandlineWidthfromGeometricObject,sowedonothavetoexplicitlyimplement
themaspartoftheLineclass.
Figure12.4showsourinheritancehierarchywiththedetailsofinstancevariablesandmethodsforeachclass
addedtothediagram.

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 6/23
8/23/2016 Yuzu: Python Programming in Context

Figure12.4Instancevariablesandmethods

12.4BasicImplementation
Lets begin with a topdown implementation of the portion of the class hierarchy needed to implement the
programinListing12.2.WewillstartwiththeCanvasclass(showninListing12.3),whichservesthesame
purposeforourgraphicsmoduleasitdoesfortheturtle.ItisaplaceforustodrawGeometricObjects.Infact
sinceweareusingtheturtletodoallofourbehindthescenesdrawing,wewillsimplymakeuseofthesame
window and canvas as the turtle. This decision saves us a lot of work but also has some important
ramificationsforourprogram.Forexample,itmeansthatwewillusethesamecoordinatesystemastheturtle
does.

12.4.1Canvas
ThemostdifficulttaskinwritingourCanvasclassiswritingtheconstructor.Asyouhavepreviouslylearned,
the constructor method is always named __init__. The job of the constructor for Canvas is to create an
instance of a Turtle and store a reference to it in an instance variable called turtle. We will also create an
instancevariablecalledscreenthatwillallowustoconfigurethecanvas.
Oncewehaveinitializedtheinstancevariablesfortheclass,wesetthewidthandheightoftheCanvasusing
thescreenmethodsetup.Finally,becausethepeoplewhowillbeusingourgraphicsclassesmaynotknow
anythingaboutturtlegraphics,wewillhidetheturtleusingthehideturtlemethod.Wedonotwantthemto
knowthataturtleisactuallydoingthework.

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 7/23
8/23/2016 Yuzu: Python Programming in Context

Listing12.3Thecanvasclass

ThefinalmethodofourCanvasclassisthedrawmethod,whichissimpletoimplementbutexposesoneof
themostpowerfulandimportantpartsofobjectorientedprogramming.Noticethatdrawacceptsageometric
object gObject as a parameter. After putting the turtle into a state with its tail up, and tracing off, the only
thingthedrawmethoddoesiscallthe_drawmethodonthegObject,passingourturtleasaparameter!Itis
important to realize that the draw method belongs to the Canvas, while the _draw method belongs to the
GeometricObject.Although_drawmaylookstrangetoyou,itisaperfectlylegalnameforafunction.Weuse
_drawinthiscasetodifferentiateitfromthedrawmethod,andtopreventprogrammersfrominadvertently
callingthedrawmethoddirectlyonaGeometricObject.
Ifyouthinkaboutitforawhile,youmayrealizethattheformalparameterofthedrawmethodcanbeany
GeometricObject.ThismeansthattheCanvasdoesnotknowwhetherithastodrawaPoint,aLine,orany
otherShape.Wecansolvethisproblembyusingthefollowingcode:

ifisinstance(gObject,Point):
#codetodrawapoint
elifisinstance(gObject,Line):
#codetodrawaline
elifisinstance(gObject,Circle):
#codetodrawacircle


else:
print('unknowngeometricobject')

While using isinstance to check the type of gObject would certainly work, objectoriented programming
provides us with polymorphisma much more powerful way to handle exactly this kind of problem that
allowsustowritea_drawmethodforeachofourspecificgeometricobjects.Pythonissmartenoughtocall
therightmethod,dependingonthetypeofobjectthatisreferencedbythegObjectvariable.Asyouwillsoon
see, we will implement a _draw method for the classes Point, Line, and GeometricObject. After we have
implemented these methods, we discuss the underlying mechanism that Python uses to implement
polymorphism.

12.4.2GeometricObject
Lets continue our implementation by writing the GeometricObject class. GeometricObject and Canvas are
peersatthetoplevelofourinheritancehierarchy.Themostimportantthingtopointoutintheimplementation
https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 8/23
8/23/2016 Yuzu: Python Programming in Context

ofGeometricObject,asshowninListing12.4,isthe_drawmethod.Allthe_drawmethoddoesisprintan
error message. The reason for this is because we should never actually create an instance of the
GeometricObjectclass.
TheGeometricObjectclassiscalledanabstractclass.Wecreatedthisclassbecauseitisanabstractionofits
child classes: Point, Line, Rectangle, and others. An abstract class provides us with one place to define
instancevariablesandmethodsthatareusedbyallofthechildclasses.TheGeometricObjectclasscontains
two instance variables: lineColor and lineWidth. Because the instance variables are initialized by the
GeometricObjectconstructor,theydonotneedtobeinitializedbyanyofthechildclasses.

Listing12.4ThegeometricObjectclass

12.4.3Point
ThePointclassisimportantbecauseitisusedbyallotherGeometricObjectsforpositioning.Forexample,the
LineclassusestwoPointobjectstoanchortheendsoftheLine.TheRectangleclassusestwoPointobjectsto
positiontheopposingcornersoftheRectangle,andsoon.
The code forthe Pointclass isgiven inListing 12.5. ThePoint class is a child ofGeometricObject, so the
class statement designates GeometricObject as the parent. The syntax we use to designate one class as the
parent of another is to place the parent in parentheses after the child. You can see this on line 1 of Listing
12.5.
AnotherwaythatwecanspeakabouttheparentchildrelationshipbetweenclassesistosaythatPointextends
GeometricObject. This can be a helpful way to think about the relationship because Point begins with the
instancevariablesandmethodsprovidedbyGeometricObjectandaddsnewinstancevariablesandmethodsof
itsown.
TherearetwokeyideastounderstandinthePointclass.First,theline

super().__init__()

makessurethatthe__init__methodintheparentclassiscalledandthattheinstancevariablesinheritedfrom
https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 9/23
8/23/2016 Yuzu: Python Programming in Context

GeometricObjecthavetheirproperinitialvalues.Thesuperfunctionreturnsaspecialsuperobjectthatknows
howtoproperlycallthe__init__methodfortheparentclass.
The _draw method implements the drawing of a Point, using the turtle that is passed as a parameter. The
importantthingtoseehereisthattheturtleismovedtotherightplaceonthecanvaswithitstailupusingthe
gotomethod.Theparameterspassedtogotoaretheinstancevariablesself.xandself.y.Oncetheturtleisin
position,adotisdrawnusingtheinstancevariablesself.lineWidthandself.lineColor.

12.4.4Line
TheLineclassisverysimilartothePointclasswiththeexceptionthatLinehastwoinstancevariablesthat
arePoints.Theotherdifferenceisthat_drawdrawsalineratherthanasimplepoint.Noticethatweneedto
make two calls to the turtle to set the line color and the line width. This is an important step because we
cannot make any assumptionsaboutwhatcoloror linewidth the turtle is set to use. The code for the Line
classisgiveninListing12.6.

12.4.5TestingOurImplementation
With our first four classes written, lets try our first test program and see what we can learn about our
implementation. Assume that all of the classes we have defined so far are stored in draw.py. Session 12.1
creates a Canvas and a Line, and it draws the line. If everything works, you should see a window with a
diagonallineinit.

Listing12.5ThePointclass

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 10/23
8/23/2016 Yuzu: Python Programming in Context

Listing12.6TheLineclass

>>>fromdrawimport*
>>>myCanvas=Canvas(800,600)
>>>myLine=Line(Point(100,100),Point(100,100))
>>>myCanvas.draw(myLine)

Session12.1Firsttestofgraphicsclasses

Letscontinuetoexplorethisexampleandseewhatwecanlearnabouttheobjectswehavecreated.Session
12.2illustratestheuseofsomeofthemethodswehavejustdefined.First,letsverifythatmyLinehasthe
default color and width. Next, lets get one of the Points used to create the line and check its x and y
coordinates,alongwithitswidthandcolor.

>>>myLine
>>>myCanvas
>>>isinstance(myLine,Line)
True
>>>myLine.getColor()
'black'
>>>myLine.getWidth()
1
>>>p=myLine.getP1()
>>>p
>>>p.getX()
100
>>>p.getY()
100
>>>p.getWidth()
1
>>>p.getColor()
'black'

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 11/23
8/23/2016 Yuzu: Python Programming in Context

Session12.2Investigatingobjects

Exercises

12.1Modifythetestprogramtochangethelinecolorandwidth.

12.2RewritethePointclass,butratherthanstoringthecoordinateinformationasseparatenumbers,store
thecoordinatesasatuple.Whenyouaredone,useyourPointclassinplaceofthePointclasswehave
writtenandmakesurethetestprogramstillworks.

12.3ModifytheLineclasssothatalinecanbeeithersolidordashed.

12.4WriteaprogramthatplotsasinewaveusinginstancesofthePointclass.

12.5CreateaninstanceofaGraphicsObjectandthentrytodrawit.Doyougetanerrormessage?

12.5UnderstandingInheritance
To better understand how inheritance works, we can look behind the scenes at how Python keeps track of
instancevariablesandmethods.Eachclassandinstanceofaclasshassomespecialinstancevariableswecan
usetoseehowPythonfindstherightmethodtocallortherightinstancevariable.Thefirstoftheseisthe
__bases__instance variable. This variable tells us the names of the parent of any class. For example, in
Session12.3youcanseethattheparentclassofLineisGeometricObject.TheparentofGeometricObjectis
object,andobjecthasnoparent.ThissuccessionofchildtoparentlinksisveryimportantforPythonwhena
methodiscalledthatwasdefinedinaparentclass.Bydefault,Pythonusesthespecialclassobjectatthetop
oftheinheritancehierarchy.
The name __bases__comes from the term base class, which is an alternative way of thinking about the
parentchild relationship. Some programmers say that the parent class is the base class, and the child class
extendsthebaseclass.Inourhierarchy,GeometricObjectisthebaseclassofLine.TheideaisthatLinehas
allthefunctionalityofGeometricObjectplusnewfunctionalitythatextendsthebase.

>>>Line.__bases__
(,)
>>>GeometricObject.__bases__
(,)
>>>object.__bases__
()
>>>Line.__dict__.keys()
['getP1','__module__','getP2','_draw','__doc__','__init__']
>>>GeometricObject.__dict__.keys()
['__module__','setColor','getColor','_draw','getWidth',
'__weakref__','__dict__','setWidth','__doc__','__init__']
>>>Line.__dict__['getP1']
>>>myLine.__class__
>>>myLine.__dict__.keys()
['p2','lineWidth','p1','lineColor']

Session12.3Investigatingtheinheritancehierarchy

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 12/23
8/23/2016 Yuzu: Python Programming in Context

Another instance variable that each class maintains is the __dict__variable. This instance variable is a
dictionarythatkeepstrackofthemethodsdefinedfortheclass.Forexample,inSession12.3youcanseethat
thedictionaryofmethodsfortheLineclasshaskeysthatcorrespondtothenameofeverymethodwedefined.
InstancesofPythonobjectsalsohavesomespecialinstancevariables.Everyobjecthasaninstancevariable
__class__,whichcontainsareferencetotheclassthattheobjectisaninstanceof.InSession12.3,youcan
see that myLine.__class__refers to the class Line. In addition, the __dict__variable for instances of classes
containsadictionaryofuserdefinedinstancevariables.Forexample,Session12.3showsthataninstanceof
theLineclassalsohasa__dict__instancevariablethatcontainskeysforp2,lineWidth,p1,andlineColor.

Putting all of these special variables together, we can see in more detail how inheritance works in Python.
Figure 12.5 shows a reference diagram to illustrate the chain of events in the Python interpreter for the
expressionmyLine.getWidth().
WhenPythonevaluatestheexpressionmyLine.getWidth(),thefirststepistodereferencethenamemyLine.
PythonnexttriestodereferencethenamegetWidthusingthefollowingchainoflookups:

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 13/23
8/23/2016 Yuzu: Python Programming in Context

Figure12.5Areferencediagramforfollowinganinheritancechain.Thenumberedarrowsindicatetheorder
ofthesearchoperation

1.If__dict__inmyLinecontainsgetWidthasakey,stopsearchinganduseit.
2.Otherwise,followthe__class__linkif__dict__intheclassobjectcontainsgetWidthasakey,stop
searchinganduseit.
3.Otherwise,followthe__bases__linktotheparentclass(GeometricObject).If__dict__contains
getWidthasakey,stopsearchinganduseit.Thislaststepcanberepeatedindefinitelyuntilallofthe
classeslistedin__bases__havebeentried.
4.IfallbaseclassesareexhaustedandgetWidthisnotfound,thengenerateanerror.

OncethenamegetWidthisdereferenced,thenextstepistoapplythefunctioncalloperators().Sinceweare
applying the call operators to a method of a class, we take the value of myLine and pass it as the first
parametertothegetWidthfunction.InthegetWidthfunction,myLineiscalledself.Figure12.6illustratesthe
binding of myLine as an actual parameter to the formal parameter self. The main thing to notice is that
myLineandselfbothreferencethesameinstanceofaLine.

Figure12.6Theselfparameterwithaninheritedmethod

Exercises

12.6 Using the reference diagram in Figure 12.5, trace the series of reference links that Python uses to
evaluatetheexpressionself.lineWidth(5).

12.7 Using the reference diagram in Figure 12.5, trace the series of reference links that Python uses to
https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 14/23
8/23/2016 Yuzu: Python Programming in Context

evaluatetheexpressionmyLine._draw().

12.8 Draw a reference diagram illustrating the sequence of lookups required for the statement
myLine.getP1().getX().

12.9InthePythonshell,followthesequenceoflookupstofindthelocationinmemoryofmyLine.p1.

12.6Limitations
Now that you have a better understanding of how inheritance works, lets try a slightly more difficult test
program.Listing12.7containsasimplefunctioncalledtest2todrawtwothicklinesthatcross.Oncethelines
aredrawn,thecolorofthefirstlineischangedtored.Whatdoyouthinkthefinalpictureshouldlooklike?

Listing12.7Drawingtwolinesthatcross

Ifyouthinkthattheresultshouldbetwocrossingblacklines,youarecorrect.WhydidntthecalltosetColor
actuallychangethecoloroftheline?CallingsetColoronlymodifiesthelineColorinstancevariableitdoes
notchangethewaythelineappearsonthescreen.Inordertochangetheappearanceofthelineonthescreen,
weneedtoredrawit.AlthoughtheobviousfixforthissmallprogramistomovethesetColorcallinfrontof
thecalltodraw,thatisnotanacceptablegeneralsolution.
Anothersolutionisthateverytimewechangethecolororthicknessofthelinewehavetotellthecanvasto
drawthelineagain.Thissolutioncanproduceunintendedconsequences.Forexample,wecansimplyaddone
morelinetotheprograminListing12.7tocallmyCanvas.draw(myLine).Nowthelineisred,butitisinfront
oftheotherline.Ifwewanttheblacklinetobeinfrontoftheredline,weneedtoredrawtheblacklineas
well.Ifthisisacomplicateddrawing,thiscanturnintoaverycomplicatedsetofdependencies.
Thebehaviorthatwereallywantcanbespecifiedasfollows:
1.Iftheobjectisnotdrawnyet,simplychangethevalueoftheinstancevariable.
2.Iftheobjectisalreadyvisible,changetheattributeandredrawalltheobjectsonthescreeninthesame
orderastheywereoriginallydrawn.

Onesolutiontothisproblemwouldbetoaddacallto_drawinthesetColorandsetWidthmethods.However,
thatgivesusthesameproblemwiththeorderofdrawingwejustmentioned.Thereisanotherproblemwith
thisapproachaswell.Inordertodrawtheline,weneedaturtle.Asyoumayremember,theturtleispassedas
aparametertothe_drawmethod.However,whenwecallthesetWidthmethod,wedonot(andshouldnot!)
haveareferencetoaturtleobject.Afinalproblemisthatrequirement1stipulatesthatifthelinehasnotyet
beendrawntheonlythingthatsetColorshoulddoischangethevalueofthecolorinstancevariable.

12.7AnImprovedImplementation

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 15/23
8/23/2016 Yuzu: Python Programming in Context

Thesolutiontotheproblemoutlinedpreviouslyisvaluabletounderstandbecauseitwillhelpyourecognize
the principles behind many professional graphics systems, including the TKinter graphics system used by
Python.ItalsohelpsyouunderstandwhatishappeninginthegraphicssystemusedintheJavaprogramming
language.Anotherbenefitofthesolutionisthatonceyouhavethemechanisminplacetosolvethecurrent
problem,youwillbeabletoimplementamovemethodforallgraphicsobjectsveryeasily.
Thesolutiontotheproblembeginswiththreeadditionalinstancevariables.Onevariablewillallowustokeep
trackofwhethertheobjecthasbeendrawn.Wemightcallthisinstancevariablevisible.Thesecondinstance
variablewillbeusedtokeeptrackoftheCanvasonwhichtheobjectisdrawn.Thethirdinstancevariable,
visibleObjects,willallowtheCanvastorememberalistoftheobjectsithasdrawn.
To better understand the importance of these three instance variables, lets look at a new way of thinking
aboutwhatwillhappenwhenwemakeacalllikethefollowing:theCanvas.draw(myLine).
1.SetthevalueofthevisibleinstancevariableofthelinetoTrue.
2.SetthevalueofthemyCanvasinstancevariableofthelinetopointtotheCanvas.Atthemomentthis
mayseemcirculartoyou,buthopefullyitwillbecomeclearwhenwetalkaboutwhathappenswhen
youcallsetColor.
3.Callthelines_drawmethod.
4.AddthelineobjecttothevisibleObjectsinstancevariableofthecanvas.

At the end of this sequence, the line will be drawn on the canvas. But more importantly, the line will now
rememberwhichcanvasitwasdrawnon,andthecanvaswillknowalltheobjectsthathavebeendrawnon
it.
Nextletsconsiderthesequenceofeventswhenwemakeacalltochangethecolororwidthofthelinesuch
as:myLine.setColor('red').

1.ModifythelineColorinstancevariableofthelinetohavethenewvalueof'red'.
2.IfthevisibleinstancevariableisTrue,thenusethemyCanvasinstancevariabletocallthemethod
drawAllonthecanvas.

ThedrawAllmethodclearsthecanvasandredrawsallobjectsonthecanvasinthesameorderastheywere
drawnoriginally.Itistruethatwemaynotneedtoredrawalltheobjectsonthecanvas,butitwouldbemore
worktofigureoutwhichobjectsneedtoberedrawnthanitwouldbetosimplyredraweverything.Thenew
designforourclassesisshowninFigure12.7.Thisfigureincludesallthenewinstancevariablesandmethods
wehavediscussed.
EventhoughwehavemadesomemajorimprovementstothefunctionalityofthePointandLineclasses,we
willseethatwechangedonlytheCanvasandGeometricObjectclasses.Listing12.8presentsthenewversion
of the Canvas class. Compared with our earlier implementation in Listing 12.3, we have added two new
methods:drawAllandaddShape.Inaddition,wehavemadesmallmodificationstodrawand__init__.
Themodificationsto__init__anddrawinvolveinitializingandupdatingthevisibleObjectsinstancevariable.
Whenacanvasisfirstcreated,thelistofobjectsdrawnonthecanvasisinitializedtoanemptylist.Whenever
drawiscalled,theGraphicsObject passed asa parameter is appended tothe endof thelist. In this way all
objectsthatarevisibleonthecanvasarestoredinthevisibleObjectslist,andtheorderthattheyweredrawnis
preserved.

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 16/23
8/23/2016 Yuzu: Python Programming in Context

Figure12.7Newdesigntoallowpropercolorandwidthchanges

TheaddShapemethodissimplyaconvenientwaytoaddanobjecttothevisibleObjectslist.Thereasonwe
donotsimplycallvisibleObjects.appenddirectlyisthatbyconsistentlyusingtheaddShapemethodwelimit
anydependenciesonhowwerepresenttheobjectsdrawnonthecanvastoonemethod.Imaginethatsome
timeinthefutureyoudecidetouseadictionary,ratherthanalist,tokeeptrackoftheobjectsonthecanvas.
IfyouhadusedvisibleObjects.appendinseveralplaces,eachoneofthoseplaceswouldneedtobechangedto
accommodatethenewrepresentation.However,sincewehavebeencleverandusedtheaddShapemethod,
wewouldneedtochangeonlythewayweaddanobjecttothecanvasinoneplace.

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 17/23
8/23/2016 Yuzu: Python Programming in Context

Listing12.8AnewimprovedCanvasclass

ThebiggestadditiontotheCanvasclassisthedrawAllmethod.Themainpurposeofthismethodistoiterate
overalltheGeometricObjectobjectsonthevisibleObjectslistandhaveeachperformits_drawmethod.By
doingthis,wecancompletelyrecreatewhateverpicturewehaveonthecanvasbycallingasinglemethod.
This loop demonstrates polymorphism in objectoriented programming in a very powerful way. Notice that
wedonotneedtokeepaseparatelistoflines,points,andothershapes.Weneedonlyonelistofobjects,and
sinceeachobjectprovidesa_drawmethod,Pythonfindsthecorrect_drawmethodforeachobject.
Youmayalsonoticethatthefirstlineofthemethodcallsself.screen.reset().Theresetmethodofthescreen
erasesanythingtheturtlehaspreviouslydrawntogiveusablankcanvas,andputstheturtleinthecenterof
thecanvasagain.
ThenewGeometricObjectclassisshowninListing12.9.ThechangestoGeometricObjectarefairlysmall.
Wehaveaddedtwonewsettermethods:setVisibleandsetCanvas.ThesetColorandsetWidthmethodshave
beenmodifiedtocallthedrawAllmethodwhenevertheobjectbeingchangedisvisible.

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 18/23
8/23/2016 Yuzu: Python Programming in Context

Listing12.9AnewimprovedGeometricObjectclass

Exercises

12.10WriteanundrawmethodtoremoveaGeometricObjectfromthecanvas.

12.11Addtheabilitytomoveobjects.

12.12Sinceeachobjecthasareferencetothecanvasitisdrawnon,itwouldnolongerbenecessarytopassa
referencetoaturtleasaparameterto_draw.Modifytheclassestoremovethisparameter.

12.8ImplementingPolygons
We have neglected a large part of our initial inheritance hierarchy in order to focus on a few key
implementation details. Now that we have made it possible to properly modify the color, line width, and
visibility of Points and Lines we can turn our attention to adding new graphical objects. You will see that
inheritanceisgoingtomakeitfairlyeasytoaddnewobjectsthatarequitepowerfulwithoutwritingalotof
code.
Letsbeginbyworkingdowntheinheritancehierarchy,startingwithShape.Shapeisgoingtobeanabstract
classthatwillholdaninstancevariablethatwillletusknowwhetheraninstanceofShapeshouldappearas
anoutlineorwhethertheshapeshouldbefilledinwithacolor.

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 19/23
8/23/2016 Yuzu: Python Programming in Context

The next class to consider is Polygon, which can be either abstract or concrete. That is, we can probably
imaginecreatinganinstanceofaPolygontorepresentsomeirregularmultisidedclosedshape.Infact,wecan
just stop with Polygon and implement squares, triangles, octagons, etc., as polygons. This is one of those
designdecisionsthathasnorightorwronganswer.IfwechoosetomakePolygonabstract,thenweneedto
addexplicitchildclassesforeverypossiblepolygonwemightwanttouseinanapplication.Ifwechooseto
allowPolygontobeconcrete,thenwecanimplementsomechildclassestomakeiteasytoconstructthemost
commonpolygons.WewillchoosethelatterapproachandmakePolygonaconcreteclass.
For each of our shapes, the most important method for us to write is _draw. If we think carefully, we can
implementasingledrawmethodtodrawanypolygon.Thebigquestionis,howcanwerepresenttheshapeof
apolygon?Theansweristhatwecanrepresentanypolygonusingalistofpoints.Atrianglehasthreepoints
correspondingtoitsthreecorners.Asquarehasfourpointscorrespondingtoitsfourcornersandsoon.In
additiontoknowingthepositionofthecornersofourpolygon,itisalsoimportanttospecifytheminsome
order,sothatwecangofromcornertocornerandgetanoutlineoftheshape.Ifthecornersarerandomly
ordered,wemayjustgetabunchofrandomlines.
Therearetwologicalchoicesfortheorderinwhichtospecifythecorners:clockwiseandcounterclockwise.
Mostgraphicssystemsuseacounterclockwiseorderingforthecornersofapolygonforreasonsthatdonot
mattertousrightnow.

Figure12.8Acorrectlydrawnpolygonwithcornersspecifiedincounterclockwiseorder

Acounterclockwiseorderingmeansthatwecandrawanypolygonbydrawinglinesbetweenpointsthatare
adjacent to each other in our ordered list of points. Figure12.8 shows an example parallelogram with four
points.Thesefourpointscouldbestoredinalistsuchas[p0,p1,p2,p3].Wecandrawlinesfromp0top1,
fromp1top2,andp2top3sincetheyarenexttooneanotherinthelist.However,thiswillgiveusonlythree
ofthefoursides,sowemustalsodrawalinefromp3top0tocompletetheshape.

If we had specified the corners of our polygon in the order [p0, p1, p3, p2], we would get a completely
differentshapethatisnotapolygonatall.TheincorrectshapeisshowninFigure12.9.

Figure12.9Anincorrectlydrawnpolygonduetorandomcornerpositions

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 20/23
8/23/2016 Yuzu: Python Programming in Context

Figure12.10Usingtheinformationfromlowerleftandupperrighttodetermineothercorners

Toimplementthe_drawmethodforanypolygon,youcansimplyiterateoverthelistofcornersandhavethe
turtlegofrompointtopoint.Whenyourunoutofpointsinthelist,youmusthavetheturtledrawonefinal
linebacktothefirstpointonthelist.IfapolygonhasafillColorotherthanNone,thenyouwillneedtocall
thebeginfillandendfillmethodsoftheturtlebeforeandafteryoustartdrawinglines.

Now that you know how to represent and draw any polygon, what about Rectangle, Triangle, and Square?
Thekeyforthesethreeclassesishowwewriteourconstructor.Foranarbitrarypolygon,theconstructormust
take a list of points. For a specific polygon, we can make it a bit more user friendly. For example, the
constructorforTrianglecantakethreepoints,andtheconstructorsfortheSquareandRectanglecaneachtake
justtwopoints.

Thepointsneededtospecifyarectanglearesimplythelowerleftcornerandtheupperrightcorner.Ifyou
knowthelowerleftandupperright,youcanfigureoutthepositionoftheothertwocornersquiteeasily,as
shown in Figure 12.10. You can see examples of the constructors for a rectangle in Listing 12.1. The
constructor for the Square is identical to the constructor for Rectangle except that it should check that the
lengthsofallsidesarethesame.

A new version of our class hierarchy is shown in Figure12.11. We are not going to show you the Python
implementationforPolygon.Implementingtherestofthehierarchyisleftasanexerciseforyou.

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 21/23
8/23/2016 Yuzu: Python Programming in Context

Figure12.11Inheritancehierarchyincludingdetailsforpolygons

Exercises
12.13ImplementthedesignforPolygonanditschildren.

12.14ImplementadditionalPolygonconvenienceclassessuchasSquare,Triangle,Rectangle,andOctagon.

12.9Summary
In this chapter we learned about inheritance, the ability to design and implement classes so that they take
advantageofnaturalrelationshipsthatmayexistbetweenthem.Theserelationshipsprovideawayforusto
organizeourclasses.Parentclassescontaingeneraldetails(instancevariablesandmethods)thatpertaintoan
object. Child classes contain details that allow objects to be more specific. This childparent relationship,
calledanISArelationship,allowschildobjectstohaveallthefunctionalityofparentobjectswhileadding
specificadditionaldetails.

KeyTerms
abstractclass

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 22/23
8/23/2016 Yuzu: Python Programming in Context

baseclass
childclass
HASArelationship
inheritance
inheritancehierarchy
instancevariables
ISArelationship
methods
parentclass
polymorphism
subclass
superclass

PythonKeywords
class
isinstance
None
object
self
super

ProgrammingExercises
12.1DesignandimplementtheEllipsepartoftheshapehierarchy.

12.2DesignandimplementaTextclassfortheGeometricObjecthierarchy.

12.3TestyourentiresetofclassesusingListing12.1.

https://reader.yuzu.com/#/books/9781284089042/c/6/38!/4/434@0:72.7 23/23
8/23/2016 Yuzu: Python Programming in Context

CHAPTER13
VideoGames

13.1Objectives
Towriteaneventdrivenprogram
Tounderstandandwritecallbackfunctions
Topracticewithlistsofobjects
Toseeanotherpatternforusinginheritance
Tolearnaboutstaticvariables

13.2Introduction
Sofartheprogramswehavewrittenhaveabeginning,amiddle,andanend.Weknowtheorderinwhich
thingswillhappenbecausewehaveimplementedthestepsinalinearfashion.Butthatisnotthewaythat
most programs work. Think of the programs you use on a daily basisfrom your web browser and word
processor, to video games. These programs do not have a beginning, a middle, and an end. Rather they sit
thereandwaitforyoutoclickyourmouseorpressakey.Whenyoutakesomeaction,theprogramresponds
withitsownaction.Theorderoftheactionsisdeterminedbyyou,nottheprogram.

13.2.1EventDrivenProgramming
Programsthatwaitforaneventtooccurandthenrespondtotheeventbytakinganactionarecalledevent
drivenprograms.Inthischapteryouaregoingtolearnhowtowriteeventdrivenprogramsusingtheturtle
module. When writing eventdriven programs, one of the most important concepts is that of a callback
function,whichisjustlikeanyotherfunctionexceptthatitisdesignedtobecalledinresponsetoanevent.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 1/24
8/23/2016 Yuzu: Python Programming in Context

Figure13.1Theeventloop

Lets look at this idea of events in a bit more detail. The main structure of an eventdriven program is an
eventloop,whichdoesthefollowing:
Checkstheeventqueueforthenextevent.
Callsthecallbackfunctiontohandletheevent.

Figure13.1illustratestheideabehindtheeventloopgraphically.Youwillnoticethatthereissomesimilarity
betweenthiseventloopdiagramandthereadevalprintloopthatPythonuses.Oneofthedifferencesisthat
ineventdrivenprogrammingweuseaqueueadatastructurethatworkssomethinglikeatodolisttokeep
trackofeventsthatneedtobehandled.Aqueueallowsyoutoremovethefirstthingonyourtodolistandto
addthingsattheend.Inthiswayaqueueenforcesafirstcomefirstservedstrategyforhandlingevents.You
willlearnmuchmoreaboutqueuesandtheirusefulnessinadatastructuresclass.

13.2.2SimulatinganEventLoop
TheeventloopinFigure13.1looksprettysimple,butthereisoneimportantpieceofthepuzzlenotshownin
thediagramthatis,howdoweasprogrammerscontrolwhichfunctioniscalledforaparticularevent?Of
course,ifwewerewritingtheeventloop,wecoulddoiteasilywithsomeifstatements.Butsincewearenot
writingtheeventloop,anddonotevenhaveaccesstothesourcecodefortheeventloop,theremustbesome
otherway.
Thewaythatthiscontrolproblemissolvedistohavetheeventprocessingsystemprovideuswithawayto
register a function that we want to be called when a specific event occurs. For example, we might tell the
eventprocessingsystemtocallfunctionAwhenthemouseisclickedandfunctionBwhenakeyispressed
onthekeyboard.
To illustrate how this all works, lets write a simple simulation of an eventprocessing system. Our event
processorwillhavethefollowingfunctions:

Addaneventtothequeue.
Registerafunctiontobecalledinresponsetoanevent.
Runthesimulation.

Listing13.1Asimpleeventprocessingsimulation

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 2/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.1implementsasimpleclasstoillustrateaneventprocessingloop.Althoughthecodeisshortand
simple,therearesomepowerfulthingshappening.Inthe__init__methodwesetuptwoimportantinstance
variables:self.queue,whichwillholdthelistofeventsthatneedtobeprocessed,andself.eventKeeper,which
is a dictionary that maps events to their callback functions. Suppose that we have two events: 'mouse' and
'key'. We can store a reference to the callback function for the mouse event in self.eventKeeper using the
string 'mouse' as the key. We can then simply call the function using the rather oddlooking expression
self.eventKeeper['mouse']().
If we break that expression apart, it is not so mysterious. Python first evaluates self, then it finds the
eventKeeper dictionary. The square brackets tell Python to look up the key 'mouse' in the dictionary. The
dictionary lookup returns a reference to the function stored there. Finally, the left and right parentheses let
Pythonknowthatitshouldcallthereferencedobjectasafunction.
Lets give our simulation a try. You can follow along in Session 13.1. First, we will define two simple
callback functions. We will call the functions myMouse and myKey to handle mouse and key events
respectively. Although these two functions are simple, keep in mind that they could do anything you want
themtodo.Second,wewillregisterourcallbackfunctionswiththeeventhandler.AsyoucanseeinListing
13.1,allthisdoesisstoreareferencetothefunctionintheeventKeeperdictionary.Third,wewilladdsome
eventsusingtheaddEventmethod.

>>>defmyMouse():
print('Ohnothemousewasclicked.')
>>>defmyKey():
print('Akeyhasbeenpressed')
>>>eh=EventHandler()
>>>eh.registerCallback('key',myKey)
>>>eh.registerCallback('mouse',myMouse)
>>>eh.addEvent('mouse')
>>>eh.addEvent('key')
>>>eh.addEvent('mouse')
>>>eh.run()
Ohnothemousewasclicked.
Akeyhasbeenpressed
Ohnothemousewasclicked.
queueisempty
queueisempty
queueisempty

>>>

Session13.1Testingtheeventprocessingsimulation

Withthecallbackfunctionsregisteredandsomeeventsaddedtothequeue,wetelltheeventhandlertorun.
Notethattherunmethoddoessomethingthatwehavenevertriedtodoonpurposebefore.Therunmethod
entersaninfiniteloop.Ineachpassthroughtheloop,wecheckthelengthoftheeventqueuetoseeifthere
are entries. If there are entries on the queue, then the first one is removed and the appropriate callback
functioniscalled.Iftherearenoeventsonthequeue,thenthemessagequeueisemptyisprinted.Remember
thatqueueisemptywillbeprintedforeveroruntilyoustoptheprogramfromrunning.

13.2.3AMultithreadedEventLoop
The problem with the code in Listing 13.1 is that once you call the run method the program goes into an
https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 3/24
8/23/2016 Yuzu: Python Programming in Context

infiniteloop.Theonlyeventsthatwilleverbeprocessedareeventsthatyouplacedontheeventqueuebefore
you called the run method. In practice, this is not the case. The event loop runs simultaneously with other
codethatiscapableofputtingeventsontheeventqueue.Thetermmultithreadedprogramimpliesthatone
program can have multiple things happening at the same time. All modern desktop applications are
multithreadedapplications,includingtheturtlemodule.
Figure 13.2 illustrates the difference between singlethreaded and multithreaded execution. In the top
sequence,thereisasinglethreadofexecution,whichmeansthatoncetherunmethodisstartednothingelse
canhappen.Thestartcallinthemultithreadeddiagramstartsanewthreadandcallstherunmethodinsidethe
newthread.Thenewthreadrunsinparallelwiththeoldthread.Withtwothreadsofexecution,eventscanbe
addedinonethreadwhiletherunmethodisexecutingintheother.

Figure13.2Comparingsingleandmultithreadedexecution

InheritancemakesitpossibletoconvertasinglethreadedEventHandlerclasstoamultithreadedclassvery
easily.Eventhoughwedonotknowanythingabouthowthreadsworkbehindthescenes,wecanmakeour
classmultithreadedbyhavingitinheritfromtheclassThread.Thisisanextremelypowerfulpatternthatwe
will revisit when we implement our video game. For now we can just use the code shown in Listing 13.2.
NoticethatthereareonlyacoupleofsmallchangesbetweenListing13.1and13.2.Thefirstchangeisthatwe
nowimporttheThreadclassfromthethreadingmodule.Thesecondchangeisthatweimporttime,andthe
thirdisintheclassdeclaration.InsteadofinheritingfromobjectwenowinheritfromThread.
ByinheritingfromThread,wegainaccesstosomeadditionalmethods.Forourpurposes,thestartmethodis
theonlyoneweneedtoworryabout.Startcreatesanewthreadandcallstherunmethodoftheclassinside
thatnewthread.Oncetheruncommandstarts,itentersaninfiniteloopjustasbefore,exceptforthistime,if
therearenoeventsontheeventqueue,thefunctiontime.sleepiscalled.Thetime.sleepfunctioncausesthe
threadtopauseforthenumberofsecondspassedasaparameter.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 4/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.2Amultithreadedeventhandler

WecantestthemultithreadedversionofoureventloopasshowninSession13.2.Asyoucansee,oncethe
startmethodiscalledthethreeoriginaleventsarehandledasbefore.However,withtherunmethodexecuting
initsownthreadwecannowtypeadditionalPythonstatementsthatwillbeexecutedbytheoriginalthread.

>>>eh=EventHandler()
>>>eh.addEvent('mouse')
>>>eh.addEvent('key')
>>>eh.addEvent('mouse')
>>>eh.registerCallback('key',myKey)
>>>eh.registerCallback('mouse',myMouse)
>>>eh.start()
Ohnothemousewasclicked.
Akeyhasbeenpressed
Ohnothemousewasclicked.
>>>eh.addEvent('mouse')
Ohnothemousewasclicked.
>>>eh.addEvent('key')
Akeyhasbeenpressed
>>>eh.addEvent('mouse')
Ohnothemousewasclicked.

Session13.2Amultithreadedeventloopexample

Exercises

13.1 Write your own callback functions for 'mouse' and 'key' events. Register these events with the
EventHandlerandrerunthesimulation.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 5/24
8/23/2016 Yuzu: Python Programming in Context

13.2Addathirdeventtypecalled'timer'.Supplyasimplecallbackfunctionforthetimer.

13.3Drawareferencediagramtoillustratehowadictionaryofcallbackfunctionsworks.

13.4Deviseawaytomodifythesimulationsothatyoucanhaveakeyeventandpassthenameofthekey
asaparametertothecallbackfunction.

13.5Deviseawaytomodifythesimulationsothatyoucanhaveamouseeventandpasssimulatedwindow
coordinatesofthemousetothecallbackfunction.

13.6Modifythesimulationtosupportthetimerevent.Whenyouaddatimerevent,youshouldsupplythe
numberofsecondsuntilthetimerexpires.Whenthetimergoesoff,itshouldcallthetimercallback
function.

13.3EventDrivenProgrammingwithturtle
Theturtlemoduleprovidesthreemethodstoregistercallbacksforkey,mouse,andtimerevents.Thecallback
functionsare

onkey
onclick
ontimer

Wewillneedtomastertheuseofallthreeofthesekindsofcallbackstosuccessfullywriteourvideogame.In
thissectionwewillinvestigatetheuseofthesecallbacksinmoredetail.

13.3.1ASimpleEtchaSketch
Lets begin our investigation of eventdriven turtle programming by creating a simple drawing program.
Previouslywehaveusedtheturtletodrawbywritingafixedsetofcommandsthattheturtlefollowed.Now
wewouldliketocreateaprogramwheretheusercandrawjustbyusingthearrowkeys.Eacharrowkeywill
havethefollowingeffect:

UP:Theturtlegoesforward5pixels.
LEFT:Theturtleturnscounterclockwiseby10degrees.
RIGHT:Theturtleturnsclockwiseby10degrees.
DOWN:Theturtlebacksup5pixels.

Inaddition,pressingtheqkeywillcausetheprogramtoquit.Notethatthearrowkeysgobythespecial
namesUp,Down,Left,andRight.
TheturtlemoduleallowsustoaddacallbackfunctionforanykeybyusingtheonkeymethodofaScreen
object.Thismethodtakestwoparameters:thefirstisareferencetoafunctionandthesecondisthenameofa
key. For example, myScreen.onkey(myKey,'a') sets up a callback so that when the a key is pressed the
functionmyKeyiscalled.Thisapproachallowsustosetupadifferentcallbackfunctionforeachkeyonthe
keyboard.
Oneimportantquestionremains:Whataboutparameters?Thefirstsolutionthatmightoccurtoyouistohave
thecallbackfunctionfortheleftarrowcalltheturtlemethodleft.Buttheleftmethodrequiresaparameter.
Now you might think that the solution is to set up the callback by using
myScreen.onkey(myTurtle.left(5),'Left'). But this would result in an error. Why? If you think about how
Python evaluates the statement, you will realize that callbyassignment parameter passing evaluates the
parameterstoafunctioncall.PythonwillactuallytrytoevaluatemyTurtle.left(5),whichwillreturnthevalue

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 6/24
8/23/2016 Yuzu: Python Programming in Context

None.Theonkeymethodwantstohaveareferencetoafunction.
Differentcallbackfunctionswillhavedifferentconventionsforhowmanyparameterstheytake.Intheturtle
module,thecallbackfunctionsforkeycallbackscannotacceptanyparameters.Sowewillhavetoimplement
our callbacks with zero parameters and have them call the turtle methods with the appropriate parameters.
Withthisinmindwewillneedtowritefivedifferentcallbackfunctionsforourprogram,oneforeachofthe
arrowkeysandonefortheqkey.Inthecaseofthearrowkeys,thecallbackfunctionswillbeveryshort.All
we need to do is call the appropriate turtle function. The callback functions are shown on lines 1932 of
Listing13.3.
Themostinterestingpartforusisthe__init__method.In__init__wecreateaturtle,initializeitscolors,create
a screen, and set up the callback functions. The final line of __init__calls the screen method listen, which
specificallytellsthescreentostartlisteningforkeyboardevents.
Thefinalmethodismain.Allthismethoddoesiscalltheturtle.mainloopfunction.Bynowyoucanprobably
guesswhatkindofaloopmainloopis.Itistheeventloopfortheturtle.
Torunthislittledrawingprogram,allyouneedtodoisloadtheEtchclassasdefinedinListing13.3andthen
executethefollowingtwocommands:

>>>draw=Etch()
>>>draw.main()

Oncetheprogramisrunning,youcanmakeadrawingbypressingthearrowkeystomovetheturtlearound
thewindow.
The program in Listing 13.3 uses compositiona style of objectoriented programming in which the Etch
classusesaturtletodothebulkoftheworkandthereforehasareferencetoaturtleasaninstancevariable.
Thisisacommonandusefulwaytothinkaboutprogramming.However,thereisanotherwaytothinkabout
thisproblemusinginheritance.TheinheritanceviewofthisproblemwouldsaythatanEtchisaspecialkind
ofTurtle,andthereforeourEtchclasswouldinheritfromTurtle.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 7/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.3Asimpledrawingprogram

Listing13.4showshowtoimplementourdrawingprogramusinginheritance.Themaindifferencebetween
this program and the previous one is that we do not explicitly create a reference to a turtle inside the Etch
class.ThereasonisthatanEtchISATurtle.So,therealworkofcreatingthewindowandtheTurtlepartof
ourEtchobjecthappensonlines5and6.Noticethatthisalsosimplifiestherestofthecodebecausewecan
nowsimplycallself.forwardratherthanself.myT.forward.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 8/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.4Thedrawingprogramimplementedwithinheritance

ItisimportanttomentionthatneitherimplementationofEtchisrightorwrong.TheprogramsinListings
13.3 and 13.4 are two alternative ways of accomplishing the same goal. Each has advantages and
disadvantages.

Exercises

13.7AddanewkeyeventtotheEtchprogramtoallowtheturtletopickuporputdownitstail.

13.8AddakeyeventandresetfunctiontotheEtchprogramthatclearsthescreenandreturnstheturtleto
thehomeposition.

13.9TheEtchprograminListing13.4couldbemadeevencleanerbyusingpolymorphismandadefault
parameter. Rather than implementing a fwd method that calls self.forward, you can replace the
definition of forward using a default parameter for the distance. The implementation of the forward
methodwouldusethesuperfunctiontocalltheturtlesimplementationofforwardpassingthedefault
parameter. Rewrite the Etch implementation using forward, backward, left, and right with default
parameters.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 9/24
8/23/2016 Yuzu: Python Programming in Context

13.3.2PlacingTurtles
Wewillcontinueourinvestigationofeventdrivenprogrammingbylookingathowourprogramscanrespond
tomouseclickevents.Tosetupacallbackforamouseclick,wecalltheonclickmethodandsimplypassit
thefunctiontocallwheneverthemouseisclicked.Themaindifferencebetweenthekeyboardcallbacksand
themousecallbacksisthatthefunctionwewritetoacceptmousecallbacksmustaccepttwoparameters.The
twoparameterswillspecifythexandycoordinatesofwherethecursorwaswhenthemousewasclicked.
As a first example, lets write an application to place turtles in the window. Wherever the user clicks the
mouse,anewturtlewillappear.Thenumberofturtlestheuserisallowedtocreateispassedasaparameter
during startup time. Once the maximum number of turtles have been placed, no more turtles should be
created.
TosolvethisproblemwecreateaclasscalledTurtlePlace,whichwillhavethreemethods(seeListing13.5).
The__init__methodwillcreatetheinitialwindow,andunfortunatelyaninitialturtle.Wewillsimplyhidethe
initial turtle. The __init__method will also call onclick to arrange for the second method placeTurtle to be
called whenever the mouse is clicked. In addition, __init__sets up a turtle counter so we can keep track of
howmanyturtleswehavecreatedsofar.Finally,thethirdmethodcalleddrawFielddrawsarectangleforthe
turtlestolivein.Oncethesetupisdone,__init__callsmainlooptobeginprocessingevents.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 10/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.5Aturtleplacementprogram

The placeTurtle method will create a new turtle at position (x,y) in the window. It updates the number of
turtles created, gives the turtle a random heading, and gives the turtle a fancy shape. The last thing that
placeTurtle does is check to see if maxTurtles have been created. If the limit on turtles is reached, then
placeTurtle calls onClick and passes None as the function to call. Passing None to the onClick function
effectively cancels the callback mechanism so that placeTurtle will no longer be called when the mouse is
clicked.
To test the TurtlePlace implementation, you simply create an instance of TurtlePlace. Because mainloop is
calledonthelastlineofthe__init__method,__init__willneverreturnandtheeventloopisstarted.

Exercises

13.10ModifytheplaceTurtlemethodsothatturtlescannotbeplacedoutsidetheboundarybox.
https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 11/24
8/23/2016 Yuzu: Python Programming in Context

13.11 Modify the TurtlePlace class so that after all the turtles have been created subsequent mouse clicks
causealltheturtlestoturnandmove10unitstowardthepointthatthemousewasclickedon.

13.12AddanonclickeventtoEtchthatcausestheturtletomovetowhereverthemousewasclicked.

13.13ModifythedrawingpackagefromChapter12toallowausertoplacegeometricobjectsonacanvas.

13.14Createaprogramtodrawaregressionlinethroughasetofpoints.Theusershouldbeabletoclickin
thewindowtocreateasetofpoints.Whentheuserclicksinacertainpartofthewindow,youshould
calculateanddrawabestfitlinethroughthepoints.Youwillfindtheequationsneededforcalculating
theregressionlineinChapter4.

13.3.3BouncingTurtles
Thefinaleventweexamineisontimer,inwhichwewilluseatimertoanimatetheturtleswehaveplacedin
thewindowusingtheTurtlePlaceprogram.Theontimermethodtakestwoparameters.Thefirstparameteris
the callback function, and the second parameter is the number of milliseconds to wait before calling the
function.Timercallbackfunctionshavenoparameters.
Forthispartofourprogramwewouldliketohavetheturtlesthatweplacedinthelastsectionmove.Wecan
animateaturtlebycallingtheforwardmethodeveryfewmilliseconds.Furthermore,wewanttheturtlesto
bounceoffthewallsofanimaginaryboxandoffeachotherastheymovearound.
There are a couple of ways we can approach this problem. The first way, which we leave for you to
implementlater,istotakeadvantageoftheself.turtleListinstancevariable,whichkeepstrackofalltheturtles
thathavebeencreated.Everysomanymilliseconds,atimercallbackfunctioniteratesoveralltheturtleson
the list and call the forward method on each turtle. We classify this approach to the solution as the
compositionapproach.
Letslookatanalternativesolutionusinginheritance.SupposethatratherthanmakingaplainoldTurtlewe
makeaspecialkindofturtlecalledanAnimatedTurtle.OnceanAnimatedTurtleiscreated,itautomatically
beginstowanderaroundthebox,bouncingoffthewallsandtheotherturtles.
Becausetheanimatedturtleclassisnotmeanttobecontrolledlikearegularturtle,wearenotgoingtoadd
anymethodsthatweexpecttheusertocall.TheimportantpointtonoteintheAnimatedTurtleclassisonline
9ofListing13.6.Theontimermethodiscalledtosetupacallbackfor__moveOneStepin100milliseconds.
The__moveOneStepmethodcheckstoseeiftheturtlehasrunintooneoftheboundarywalls,movesforward
by5units,andthencallsontimeragain.Itisimportanttorememberthatatimercallbackisgoodforonlyone
intervalitdoesnotrepeat.Therefore,thelastthingwewouldwanttodoisresetthetimercallbacktocall
againafteranother100milliseconds.
We use the two underscores in the name __moveOneStep to ensure that the method cannot be called from
outsidetheAnimatedTurtleclass.NamesthatbeginwithtwounderscoresundergomanglingbyPython.The
effectofmanglinganameistohideitfromanyfunctionsoutsidetheclass.
InordertointegratetheTurtlePlaceclasswiththeAnimatedTurtleclass,weneedtomodifytheplaceTurtle
methodsothatitcreatesanAnimatedTurtleratherthanaplainTurtle.TheAnimatedTurtlealsorequiresthat
wepassitsboundariesasparameters.ThenewplaceTurtlemethodisshowninListing13.7.
AtthispointwemustloadboththemodifiedTurtlePlaceandAnimatedTurtleturtleclasses.Oncetheclasses
are loaded, it is easy to start the whole program by simply typing TurtlePlace(5,200,200). This statement
simplycreatesanewinstanceoftheTurtlePlaceclassthatallowsustocreatefivenewturtlesinaboxthatis
400by400units.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 12/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.6Aclasstoimplementananimatedturtle

Listing13.7AmodifiedplaceTurtlemethod

Theturtlesbounceoffthewalls,butatthispointtheygorightthrougheachotheriftheirpathscross.Clearly
thisisnotarealisticsimulationofturtlebehavior.Thenextsteptomakeourprogrammorerealisticistoadd
theabilityforturtlestobounceoffeachother.
Thereisnomagictothispartofthesolution.Tofindoutifaturtlehashitanotherturtle,wearegoingto
havetocheckthedistancebetweentheturtlethatiscurrentlymoving,andalltheotherturtles.Youmightbe
tempted to think that there must be a very elegant solution to this problem. In fact, for this application a
simplebruteforcecheckofallotherturtleswillbejustfine.
Thequestionishowtokeeptrackofalltheturtles.Ofcourse,wewillwanttousealist,butwherewillthat
listbekept?Whoisresponsibleforaddingturtlestothelist,andhowcanweensurethateachAnimatedTurtle
hasaccesstothelistofallotherturtles?WecouldusetheturtleListinstancevariableintheTurtlePlaceclass,
buttheneveryanimatedturtlewouldneedtohaveareferencetoaninstanceofTurtlePlace.
Theansweristouseastaticvariableavariablethatissharedbyallinstancesofaclassandisavailableto
https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 13/24
8/23/2016 Yuzu: Python Programming in Context

allthemethodsintheclass.Listing13.8createsastaticvariableonline3.Asyoucansee,staticvariablesare
definedinsidetheclassbutoutsideanymethoddefinition.
With the static variable allTurtles defined, the __init__method can add new turtles to this list as they are
created using the statement AnimatedTurtle.allTurtles.append(self). Similarly the __checkCollisions method
cannowaccessthelistofallturtlesbyreferencingthevariableAnimatedTurtle.allTurtles.Noticethatwhen
we reference a static variable, we use the class name rather than self. The beauty of the static variable
approachisthatitkeepstheaccountingofalltheanimatedturtlesinsidetheAnimatedTurtleclass.
Tocalculatetheapproximatedistancebetweentwoturtles,wewillusethedistancemethodprovidedbythe
turtlemodule.Thedistancemethodcalculatesthedistancebetweenthecenterpointsofthetwoturtles,butin
ordertohaveoursimulationlookmorerealisticwereallyneedtothinkofthedistancebetweentheedgesof
the circles that circumscribe the two turtles. In other words, if the edges of the circles touch, we should
considerthatthetwoturtleshavecollided.
Withthisinmind,takealookatthe__checkCollisionsmethodinListing13.8.Youwillseethatthemethod
uses two loops. One loop iterates over all the turtles on the AnimatedTurtle.allTurtles list. The statement if
self !=otherT ensures that we do not compute the distance between the same turtle using two different
references.Iftheturtlesaretooclose,thenwebouncethemoffeachotherbyexchangingtheirheadings.This
assumesthatturtlescollideinaperfectlyelasticcollision,whichmaynotbethecaseinnaturebutiseasyto
program.Finally,wehaveawhileloopthatmovestheturtlesforwardalongtheirnewheadingstomakesure
theyarenotinsidethecollisiondistanceanymore.Ifweleavethewhileloopout,wecangetsomefunny
lookingsimulationswheretheturtlesmayappeartobestucktogetherforashorttime.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 14/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.8AnAnimatedTurtleclasswithturtleturtlecollisiondetection

Exercises
13.15Modifythe__init__methodtogiveeachnewturtlearandomcolor.

13.16AddstartandstopmethodstotheAnimatedTurtleclass.

13.17 Add a speed method that allows you to alter the speed of a turtle by changing the number of
millisecondspassedtotheontimermethod.

13.18Addaqkeyeventtothebouncingturtlesprogram.Pressingqshouldcausetheprogramtoexit.

13.19Addamouseeventtothebouncingturtlesprogramsothatifyouclickonaturtlethatismovingit

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 15/24
8/23/2016 Yuzu: Python Programming in Context

stops,andifyouclickonaturtlethatisstoppeditstarts.

13.20Rewritethebouncingturtlesprogramusingcompositionratherthaninheritance.Afteryouaredone,
comparethetwosolutions.Whatarethestrengthsandweaknessesofeachapproach?

13.4CreatingYourOwnVideoGame
Bynowyoushouldhaveaprettygoodunderstandingofeventdrivenprogrammingusingtheturtlemodule.
Wewillfinishthischapterbypullingeverythingtogethertocreateasimplevideogameourownversionof
the old space invaders game, where aliens fall from the sky and you must shoot them with a laser cannon.
Everytimeyoushootanalien,yougetsomepointseverytimeanalienfallstotheground,youlosepoints.
Figure13.3showsascreenshotofthegameinaction.
Youcanseeinthefigurethethreemainobjectswearegoingtoimplementtomakeourvideogamework.At
thebottomcenterofthewindowisthelasercannon.Thelittlealiensaretheenemies,andthesmallcirclesare
thebombsthathavebeenshotfromthelasercannon.Thesethreethingswillbecomeourthreemainclasses:
LaserCannon,Alien,andBomb.EachoftheseclassesinheritsfromtheTurtleclass,sowecancontroleach
elementofourgameasaTurtle.Thedifferentappearanceofeachobjectcomesfromusingtheturtleshape
methodthatallowsustouseimagefilesandevenshapesyoudrawyourselftorepresentaturtle.

Figure13.3Spaceinvaders

Thenextstepistoidentifythecapabilitiesofeachofournewclasses.TheLaserCannonmustbeabletodo
twothings:

AimatAliens.
FireBombs.

ABombmustbeabletodothreethings:

Moveinthedirectionitwasfired.
DetectwhenithitsanAlien.
Detectwhenitdisappearsoutofthewindow.

AnAlienmustbeabletodotwothings:

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 16/24
8/23/2016 Yuzu: Python Programming in Context

Movefromthetopofthescreentothebottom.
Detectwhenitreachesthebottomofthescreen.

Figure13.4Spaceinvadersdesign

ThishighleveldesigniscapturedinFigure13.4.

13.4.1TheLaserCannonClass
LetslookattheimplementationoftheLaserCannonclassinmoredetail(seeListing13.9).Thisshouldlook
somewhat familiar to you after studying the bouncing turtles example from the previous section. The
LaserCannonwillserveasourinitialturtlesoitwillcreatethewindowforalltheotherturtlesthatcomelater.
Inthe__init__method,wecreateaturtle,changeitscolor,setthebackgroundcolorfortheentirewindow,and
setourworldcoordinatesystemsothaty=0isatthebottomofthewindow.
Inaddition,therearethreecallbackfunctionstoinstall:aim,shoot,andexit.Theaimmethodtakescareof
adjustingtheaimofthelasercannon.Amouseclickcausesthelasercannontoaimtowardthepositionofthe
cursor where the mouse was clicked. The shoot method is called when the skey is pressed. The shoot
methodisveryshortinthatitonlycreatesanewBomb.AsyouwillseelaterinListing13.12,theconstructor
forBombtakescareofalltherest.
If you want to test incrementally, you can load the LaserCannon class now and create an instance of
LaserCannon.Youshouldgetawindowwithatriangularlasercannonatthebottomcenter.Ifyoucallthe
mainloopfunction,youcantryaimingandshooting.Youcanaimthecannonwiththemouse,butifyoutryto
fireyouwillgetanerrorbecausewehavenotwrittentheBombclassyet.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 17/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.9TheLaserCannonclass

13.4.2TheBoundedTurtleClass
TheBoundedTurtleclassisanabstractclassthatistheparentofAlienandBomb.Thedifferencebetweena
regularturtleandaBoundedTurtleisthattheBoundedTurtleknowswhenitisoutsidethewindow.Whenthe
turtleisoutsidethewindow,itdisappears.
Listing 13.10 shows the code for the BoundedTurtle class. In the outOfBounds method, the statement
xpos,ypos =self.position() is equivalent to two statements that get the x and y coordinates of the turtle
separately. position returns a tuple but the assignment statement unpacks the tuple and assigns the
corresponding elements to the variables on the lefthand side. The outOfBounds returns True or False
dependingonwhethertheturtlehasgoneoutsideitsboundaries.
Themovemethodcheckstoseeiftheturtleisoutofbounds.Iftheturtlestaysinbounds,thetimerisresetso
thattheturtlewillmoveagainin200milliseconds.Iftheturtleisoutofbounds,itishiddenandthetimeris
notreset,whichmeansthattheturtlewillnolongermove.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 18/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.10Boundedturtlescancheckwhethertheyareoutofbounds

13.4.3TheAlienClass
ThenextclasswewillimplementistheAlienclass.Ouraliensneedtobeabletomovedownthescreen,and
theyneedtobeabletoremovethemselvesfromthegamewhentheyarehitbyaBomb.
Asinthebouncingturtlesimulation,wearegoingtoneedtokeeptrackofallalienssothatwhenabomb
moveswecantestwhetherithashitanyofthealiens.WewillusethestaticvariablealienListtokeeptrackof
allthealiens.
Tokeepthingssimple,wewantonlyonemethodtoaddorremovealiensfromthealienList.Wewilldothis
intheAlienconstructor.Whenanewalieniscreated,weappendthenewalientothelist.Whenanalienishit
byabomb,wesetthealiveinstancevariableforthatalientoFalse.
SincetheBombclassneedstoknowwhichaliensitcanblowup,itwillneedtogetalistofallthelivealiens.
Todothis,theAlienclasswillprovideastaticmethodcalledgetAlienstoreturnthislist.Justlikeastatic
variableprovidesasinglevariablefortheclass,astaticmethodissharedbythewholeclass.
Listing13.11showsthecodefortheAlienclass.Noticethatthereisnoselfparameterintheformalparameter
listofthestaticmethodgetAliens.Youwillalsonoticethattocreateastaticmethodweuseaspecialbitof
Pythonsyntaxcalledadecorator.Inthiscase,thedecoratoris@staticmethod.The@staticmethoddecorator
tellsthePythoninterpreterthatthereisnoneedforaninstancevariabletobepassedasanimplicitparameter
tothefunctiongetAliens.SowecancallthemethodusingAlien.getAliens()withoutneedinganinstanceof
theAliensclass.Aliens.getAliensreturnsallthelivealiensbyconstructingabrandnewlistcreatedfromthe
instances of Alien where alive is True. Like static attributes, static methods are useful when you have a
methodthatbelongsinsideaclassbutdoesnotapplytoanyparticularinstanceoftheclass.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 19/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.11TheAlienclass

TheotherPythonconstructthatispartofthegetAliensmethodisthelistcomprehension,whichprovidesa
concisewaytocreatealistwithoutusingaforloopwithanappendinsideit.Session13.3illustratessome
usesoflistcomprehensions.ItisalsolegaltoapplyPythonoperatorstothevariableyouareusingtocreate
the list. For example, to create a list of the first five numbers squared, you could write [x**2 for x in
range(5)].

>>>movies=['aliens','starwars','bourne']
>>>
>>>[x.capitalize()forxinmovies]
>>>['Aliens','Starwars','Bourne']
>>>
>>>[x[1]forxinmoviesiflen(x)>6]
>>>['s']
>>>
>>>[x[1]forxinmoviesiflen(x)<=6]
>>>['s','e']

Session13.3Usinglistcomprehensions

ThegetAliensmethodonline6ofListing13.11usesalistcomprehensionandisequivalenttothegetAliens
methodshownhere.

defgetAliens():
myList=[]
forxinAlien.alienList:
ifx.alive:

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 20/24
8/23/2016 Yuzu: Python Programming in Context

myList.append(x)
returnmyList

The__init__methodcontainsacoupleofimportantthingstonotice.First,itistheonlymethodthatwewill
allowtomodifythealienList.Thisensuresthatweavoidthesituationwhereonecallbackmethodcouldbe
addingtothelistwhileanotheristryingtoremovefromthelist.Thiscouldhappenifabombhitanalienat
thesametimeasanotheralienwasbeingcreated.
Second,weusealistcomprehensiontofilteroutallthedeadalienseverytimeanewalieniscreated.Thisisa
performanceimprovementtokeepthelistniceandshort.Theotherstatementtolookatistheconditionalon
lines12and13.Thisstatementsimplycheckstoseeifthe'PurpleAlien.gif'filehasbeenloaded.Ifithasbeen
loadedonce,itisnotnecessarytoloaditagainsincetheaddshapemethodisreallyloadingalistofshapes
thatworksforalltheturtlesinthesamewindow.
The move method for an Alien is inherited from the BoundedTurtle class. Although the move method is
inherited from BoundedTurtle, polymorphism ensures that the remove method of the Alien class is called
whenanalienmovesoutofbounds.
Theremovemethodmarksthealienasdeadandhidesitsoitisnolongerseen.Bysettingalive=False,the
alien stays on the list but will not be included in any calculations with bombs that are shot from the laser
cannon.

13.4.4TheBombClass
OurfinalclassistheBomb.Thisclassisnomorecomplicatedthanthebouncingturtle.Abombiscreated
withaninitialheadingcorrespondingtothedirectionthecannonwaspointing.Oncethebombiscreated,it
continues moving in the direction it was pointed. If it runs into an alien, the bomb explodes, the alien is
marked as dead, and the bomb hides itself and ceases to move. The entire Bomb class is shown in Listing
13.12.

13.4.5PuttingAllthePiecesTogether
To pull all the pieces of the game together and start things running, we will create one final class,
AlienInvaders,whichwillactasthemainapplication.ThecompletecodefortheAlienInvadersclassisfound
inListing13.13.ThemaintaskofthisclassistocreateaLaserCannon,andtosetupatimertoaddaliensto
theplayingfieldatregularintervals.Thesetasksareimplementedintheplaymethod.
Toplaythegame,weneedonlyexecutethetwocommandsshowninSession13.4.Ofcourse,theseshouldbe
added to the bottom of the file with all of the class definitions so we can run everything with a single
command.

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 21/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.12TheBomb

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 22/24
8/23/2016 Yuzu: Python Programming in Context

Listing13.13Puttingitalltogether

>>>game=AlienInvaders(200,200,0,400)
>>>game.play()

Session13.4Runningthealiensgame

Exercises
13.21Modifythespaceinvadersgametokeepscore.Theusershouldearn10pointsforeachinvaderhitand
shouldlose10pointsforeachinvaderthatfallstotheground.

13.22Modifytheinvadersgametoincreasethedifficultylevelasthescoregetshigher.Thatis,thereshould
bemoreinvadersonthescreen,andtheyshouldmovefaster.

13.23Modifytheinvadersgametousethearrowkeysinsteadofamouseclicktoaimthelasercannon.

13.24Modifytheinvadersgamesothatifanalienlandsonthelasercannonthegameisover.

13.25Modifytheinvadersgamesothatthelasercannonalwaysshootsstraightupbutcanbeslidbackand
forthacrossthebottomofthewindow.

13.26Modifytheinvadersgametohaveabuttonthatresetsthescoreandallinvaders.

13.5Summary
In this chapter we extended our discussion of objectoriented programming by introducing eventdriven
programmingthatwouldimplementasimplevideogame.Wedesignedourvideogamebycreatingclassesto
represent the components of the game. Functions were tied to mouse clicks and keys so that action would
occurwhenthoseinputeventstookplace.
Aspartoftheimplementation,weusedastaticvariablethatcouldbesharedbyeachinstanceoftheclass.We
alsocreatedastaticmethodthatdidnotbelongtoanyoneinstanceoftheclassbutcouldaccessdatadefined
intheclass.Finally,weusedtimerstoplacethevideogameinmotion.

KeyTerms
callbackfunction
composition
decorator
eventdrivenprogram
eventloop
inheritance
listcomprehension
multithreaded
namemangling
queue
staticmethod

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 23/24
8/23/2016 Yuzu: Python Programming in Context

staticvariable
thread

Bibliography

[pyg] PygamePythonGameDevelopment.Retrievedfrom:www.pygame.org.
[Ril03] SeanRiley.GameProgrammingwithPython.CharlesRiverMedia,2003.

PythonKeywords
@staticmethod
class
def
if
sys

https://reader.yuzu.com/#/books/9781284089042/c/6/40!/4/424@0:50.5 24/24
8/23/2016 Yuzu: Python Programming in Context

APPENDIXA

InstallingtheRequiredSoftware

AllofthePythonsoftwareusedinthisbookisfreeandopensource.Thefollowingsectionswillgiveyou
pointers about where to find the software as well as some links to instructions for installing this software.
SincesoftwareisalwaysamovingtargetandtheversionsofPythonandsomeofthemodulesweuseinthis
book are bound to change, we will not try to give you specific instructions for installing Python on every
operatingsystemandplatform.

A.1InstallingPython
The latest version of Python is always available from www.python.org. However, you should note that we
havewrittenthisbookusingPython3.x.Python3.xrepresentsabigchangeinPython.Althoughmanyofthe
programsinthisbookmayworkwithpreviousversionsofPython,youarestronglyencouragedtousePython
3.x.

MacOSX
IfyouhaveaMacandareusingOSX10.5oralaterversion,Python2.xisalreadyinstalledonyourmachine
andyoudonotneedtodoanythingtoinstallit.IfyouhaveanearlierversionofOSX,youwillhavePython
2.3.Inanycaseyoushoulddownloadandinstallversion3.xfromthepython.orgwebsite.Youwillfindvery
helpful instructions and links to videos that explain the process at:
http://wiki.python.org/moin/BeginnersGuide/Download. You will also find a link to download the OS X
installerathttp://python.org/download/releases.

Windows
IfyouarerunningWindowsXPorWindowsVista,youwillneedtodownloadandinstallPython.Youwill
find very helpful instructions and links to videos that explain the process at
http://wiki.python.org/moin/BeginnersGuide/Download. You can also find a quick link to the Windows
installerathttp://python.org.

Linux
IfyouarerunningLinux,thechancesareverygoodthatyoualreadyhavePythoninstalled.Youcanseewhat
versionyouhaveinstalledbytypingthecommandpythonorpython3fromthecommandline.Youshouldsee
somethinglikethefollowing:

Python3.x.0(#1,May22007,08:13:46)
[GCC4.1.0(SUSELinux)]onlinux2
Type"help","copyright","credits"or"license"formoreinformation.
>>>
https://reader.yuzu.com/#/books/9781284089042/c/6/42!/4/20/2@0:35.9 1/2
8/23/2016 Yuzu: Python Programming in Context

IfPython3.xisnotinstalledonyourLinuxbox,youwillneedtodownloadandinstallit.Youcanfindhelpful
instructions at: http://wiki.python.org/moin/BeginnersGuide/Download. A source tarball can be found at
http://python.org/download/releases.

A.2InstallingthePythonImageLibraryandcImage
TheimagelibraryusedinChapter6reliesontwoPythonmodules.Thefirstmoduleyouwillneedforimage
processing is cImage.py. This module is available from http://knuth.luther.edu/~bmiller/python.html. Click
thelinktodownloadcImage.py.MovethecImage.pyfiletoyoursitepackagesdirectory,whichispartof
yourPythoninstallation.OnWindows,youwillfinditinsidetheC:\Python30\libfolder.OnOSXyouwill
find it in the /Library/Frameworks/Python.framework/versions/3.0/lib/python3.0/folder. On Linux, it will
likelybein/usr/lib/python3.0.Ifyouhavetroublelocatingthesitepackagesfolder,analternativeistoputa
copyofcImage.pyinthefolderoftheprojectyouareworkingon.PythonwillbeabletofindcImage.pyin
eitherlocation.ThecImagemodulewillworkwithgifandppmfileswithnoadditionalmodulesrequired.

Thesecondmodule,calledthePythonImageLibrary(PIL),canbeinstalledtoallowimageprocessingofjpeg
files. You can download PIL from http://www.pythonware.com/products/pil/. Make sure you download the
version of PIL that is designated for your version of Python. If you are running Windows, download the
binaryinstallerandsimply

https://reader.yuzu.com/#/books/9781284089042/c/6/42!/4/20/2@0:35.9 2/2
8/23/2016 Yuzu: Python Programming in Context

APPENDIXB

PythonQuickReference

This quick reference provides an overview of the primary Python constructs used in this book. It is not a
detailed reference but rather a compendium of tables and syntax. These are described in much more detail
withinthebook.Further,moredetaileddocumentationcanbefoundathttp://python.org/release/3.2/library.

B.1PythonReservedWords
ThewordsshowninTableB.1arereservedbyPython.Youshouldnotuseanyofthesewordsasidentifiersin
yourprogram.Ifyoudo,youwillgetaSyntaxError.

TableB.1Pythonsreservedwords

B.2NumericDataTypes
Pythonhasthreenumericdatatypes:

Integers
Floatingpoints
Complexnumbers

TableB.2showsthemostcommonoperatorsfornumericdatatypes.

OperationName Operator Explanation


Addition + Calculatethesumoftwovalues.
Subtraction Calculatethedifferenceoftwovalues.
Multiplication * Calculatetheproductoftwovalues.
Division / Calculatethequotientoftwovalues.
Integerdivision // Calculatetheintegerquotientoftwointegers.
Remainder % Findtheremainderafterperformingadivisionontwointegers.
Exponentiation ** (x**y)Calculatexraisedtotheypower.

https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 1/12
8/23/2016 Yuzu: Python Programming in Context

TableB.2Arithmeticoperatorsinpython

B.3Sequences
Python has three sequential data types: Strings, Lists, and Tuples. TableB.3 summarizes the operators that
applytoallsequences.

OperationName Operator Explanation


Indexing [] Accessanelementofasequence.
Concatenation + Combinesequencestogether.
Repetition * Concatenatearepeatednumberoftimes.
Membership in Askwhetheranitemisinasequence.
Membership notin Askwhetheranitemisnotinasequence.
Length len Askthenumberofitemsinthesequence.
Slicing [:] Extractapartofasequence.

TableB.3OperationsonanysequenceinPython

B.3.1String
Stringsareimmutablesequencesofcharactersthatareindexedbyintegers,startingatzero.Tocreatestring
literals,youcanenclosetheminsingle,double,ortriplequotesasfollows:

a="Don'tsayHello"
b='Shesaid"HelloWorld!"'
c="""HeSaid"YouDon'tsay!"didn'the?"""

TableB.4summarizesthemoreusefulstringmethods.

Method Use Explanation


center astring.center(w) Returnsthestringastringbutsurroundedbyspacestomakethelengthof
astringw.
count astring.count(item) Returnsthenumberofoccurrencesofiteminastring.
ljust astring.ljust(w) Returnsastringleftjustifiedinafieldofwidthw.
rjust astring.rjust(w) Returnsastringrightjustifiedinafieldofwidthw.
upper astring.upper() Returnsastringinalluppercase.
lower astring.lower() Returnsastringinalllowercase.
index astring.index(item) Returnstheindexofthefirstoccurrenceofiteminastring,oranerrorif
notfound.
find astring.find(item) Returns the index of the first occurrence of item in astring, or 1 if not
found.
replace astring.replace(old,new) Replacesalloccurrencesofoldsubstringwithnewsubstringinastring.
split astring.split(schar) Returnsalistofsubstrings,splitatschar.

TableB.4Summaryofstringmethods

B.3.2List
Listsaremutablesequencesofreferencestoanyobject.Theyareindexedbyintegersstartingwithzero.You
https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 2/12
8/23/2016 Yuzu: Python Programming in Context

cancreatealistasfollows.

>>>emptyList=[]
>>>myList=[1,2,3.0,'hello',5,6]
>>>newList=list("hello")
>>>newList
['h','e','l','l','o']

Table B.5 summarizes the most useful list methods. You can convert the sequences to lists using the list
function.Youcanalsocreatesequencesusingthefollowingrangefunction.

MethodName Use Explanation


append alist.append(item) Addsanewitemtotheendofalist
insert alist.insert(i,item) Insertsanitemattheithpositioninalist
pop alist.pop() Removesandreturnsthelastiteminalist
pop alist.pop(i) Removesandreturnstheithiteminalist
sort alist.sort(key=keyfun,cmp=cmpfun) Modifiesalisttobesorted
reverse alist.reverse() Modifiesalisttobeinreverseorder
index alist.index(item) Returnstheindexofthefirstoccurrenceofitem
count alist.count(item) Returnsthenumberofoccurrencesofitem
remove alist.remove(item) Removesthefirstoccurrenceofitem

TableB.5MethodsprovidedbylistsinPython

range(stop)Createsasequenceofnumbersstartingat0andgoinguptostop1.
range(start,stop)Createsasequenceofnumbersstartingatstartandgoinguptostop1.
range(start,stop,step) Creates a sequence of numbers starting at start and going up to stop1 counting by
step.

>>>list(range(10))
[0,1,2,3,4,5,6,7,8,9]
>>>list(range(1,11))
[1,2,3,4,5,6,7,8,9,10]
>>>list(range(10,0,1))
[10,9,8,7,6,5,4,3,2,1]

B.3.3Tuple
Tuplesareimmutablesequencesofreferencestoanyobject.Youcancreateatupleasfollows:

>>>t=(1,2,3)
>>>u=('a','b')
>>>y=tuple([1,2,3])
>>>y
(1,2,3)
>>>z=tuple('abc')
>>>z
('a','b','c')

https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 3/12
8/23/2016 Yuzu: Python Programming in Context

Tuples support the same operators as lists, such as indexing and slicing. The main difference is that you
cannotuseanoperatorinawaythatwouldmodifythetuple.Youmaynotuseanyofthelistmethodswitha
tuple.

Thezipfunctioncanbeusedtocreatealistoftuplesfromtwoormoresequences.Forexample:

>>>list(zip('hello',range(len('hello'))))
[('h',0),('e',1),('1',2),('1',3),('o',4)]
>>>list(zip([1,2,3],[4,5,6]))
[(1,4),(2,5),(3,6)]
>>>list(zip('abc','def','ghi'))
[('a','d','g'),('b','e','h'),('c','f','i')]
>>>

B.4Dictionaries
Adictionaryisanunorderedcollectionofobjectsthatarereferencedbytheirkeys.YoucancreateaPython
dictionaryasfollows:

>>>emptyDict={}
>>>myD={'a':123,'b':789,'c':'246}
>>>myD['d']=359
>>>myD
{'a':123,'c':'246','b':789,'d':359}

ThePythondictionarymethodsaresummarizedinTableB.6.

MethodName Use Explanation


keys adict.keys() Returnsadictkeysobjectofkeysinthedictionary.
values adict.values() Returnsadictvaluesobjectofvaluesinthedictionary.
items adict.items() Returnsadictitemsobjectofkeyvaluetuples.
get adict.get(k) Returnsthevalueassociatedwithk,Noneotherwise.
get adict.get(k,alt) Returnsthevalueassociatedwithk,altotherwise.
in keyinadict ReturnsTrueifkeyisinthedictionary,Falseotherwise.
notin keynotinadict ReturnsTrueifkeyisnotinthedictionary,Falseotherwise.
index adict[key] Returnsthevalueassociatedwithkey.
del deladict[key] Removestheentryfromthedictionary.

TableB.6MethodsprovidedbydictionariesinPython

B.5Files
Filesaresequencesofcharactersthatarestoredonthedisk.Itisalsousefultothinkoffilesassequencesof
lines.TableB.7summarizesFilemethods.

Method Use Explanation


Name
open open(filename,'r') Open a file called filename and use it for reading. This will return a
https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 4/12
8/23/2016 Yuzu: Python Programming in Context

referencetoafileobject.
open open(filename,'w') Openafilecalledfilenameanduseitforwriting.Thiswillalsoreturna
referencetoafileobject.
close filevariable.close() Fileuseiscomplete.
write filevar.write(astring) Addastringtotheendofthefile.filevarmustrefertoafilethathasbeen
openedforwriting.
read(n) filevar.read(n) Reads and returns a string of n characters, or the entire file as a single
stringifnisnotprovided.
readline(n) filevar.readline(n) Returns the next line of the file with all text up to and including the
newlinecharacter.Ifnisprovidedasaparameter,thenonlyncharacters
willbereturnedifthelineislongerthann.
readlines(n) filevar.readlines(n) Returnsalistofnstrings,eachrepresentingasinglelineofthefile.Ifnis
notprovided,thenalllinesofthefilearereturned.

TableB.7Methodsprovidedbyafileobject

Thefollowingprogramopensafileandreadsitlinebyline,printingeachlineofthefile.

f=open('myfile.dat')
line=f.readline()
whileline:
print(line)
line=f.readline()

f.close()

Itisalsopossibletoreadandprintafileusingasimpleforloop:

f=open('myfile.dat')
forlineinf:
print(line)

B.6FormattingOutput
Pythonallowsyoutoformatstringsusingtheformatoperator%.Forexample:

>>>print('%d+%d=%d'%(2,3,5))
2+3=5

TablesB.8andB.9showthevariousformatcharactersthatcanbeused.Itisalsopossibletocontrolthewidth
ofthefieldusedbyanyoftheformatstrings.

Character OutputFormat
d,i Integerorlonginteger.
u Unsignedinteger.
f Floatingpointasm.ddddd.
e Floatingpointasm.ddddde+/xx.
E Floatingpointasm.dddddE+/xx.
g Use%eforexponentslessthan4orgreaterthanprecisionotherwiseuse%f.
c Singlecharacter.
s String,oranyPythondataobjectthatcanbeconvertedtoastringbyusingthestrfunction.
% Insertaliteral%character.

https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 5/12
8/23/2016 Yuzu: Python Programming in Context

TableB.8Stringformattingconversioncharacters

Modifier Example Description


number %20d Putthevalueinafieldwidthof20.
%20d Putthevalueinafield20characterswide,leftjustified.
+ %+20d Putthevalueinafield20characterswide,rightjustified.
0 %020d Putthevalueinafield20characterswide,fillinwithleadingzeros.
. %10.2f Putthevalueinafield20characterswidewith2characterstotherightofthedecimal
point.
(name) % Getthevaluefromthesupplieddictionaryusingnameasthekey.
(name)d

TableB.9Additionalformattingoptions

B.7Iteration
B.7.1SimpleIterationoverCollectionsofData
Theforstatementallowsyoutoeasilyiterateoveranycollectionofdatausingtheform:

foriteminsequence:
statement1
statement2

Thenextthreeexamplesshowyouhowtoiterateoverstrings,lists,andtuples.Inthisexample,thevariablei
willbeboundtoanewintegercreatedbytherangefunctioneachtimethroughtheloop.

foriinrange(n):
statement1
statement2

Inthisexample,theloopvariableiwillbeboundtothenextelementinthelisteachtimethroughtheloop.

foriin['a',1,'b',2,'c',3]:
statement1
statement2

Inthisexample,theloopvariableiisboundtoeachcharacterofthestring.

forchin"helloworld":
statement1
statement2

Itisalsopossibletoiterateoveradictionary.Thefollowingexamplesshowyoutwocommonpatterns:

foriinmyDictionary:
print(myDictionary[i])

Inthiscase,theloopvariableiisboundtoeachofthekeyscontainedinthedictionarymyDictionary.Itis
alsopossibletoiterateoverkeysandvaluessimultaneouslyusingthefollowing:

https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 6/12
8/23/2016 Yuzu: Python Programming in Context

forkey,valueinmyDictionary.items():
print(key,value)

Inthisexample,thekeyvaluepairsarepulledoutofthedictionaryaheadoftimeusingtheitemsmethod.

B.7.2Iterationwithwhile
Thewhileloopisanindefiniteloop.Itwillcontinuetoperformthestatementsinthebodyoftheloopuntilthe
conditionbecomesFalse.IftheconditionisneverFalse,thentheloopisaninfiniteloopandwillcontinue
forever.

while:
statement1
statement2

B.7.3ListComprehensions
Listcomprehensionsallowyoutocreatelistsbyembeddingaforloopinalistcreationexpression.Itismore
convenient than writing out a for loop and using the append method. Here is the general format for a list
comprehension:

[forin
forin

if]

Herearesomeexamplesofsimplelistcomprehensions:

>>>x=[1,2,3]
>>>y=['a','b','c']
>>>[a*aforainx]
[1,4,9]
>>>[aforainxifa%2!=0]
[1,3]
>>>[(a,b)forainxforbiny]
[(1,'a'),(1,'b'),(1,'c'),
(2,'a'),(2,'b'),(2,'c'),
(3,'a'),(3,'b'),(3,'c')]

B.8BooleanExpressions
B.8.1RelationalOperators
AsimpleBooleanexpressioncanbeconstructedusinganyoftherelationaloperatorsshowninTableB.10.

RelationalOperator Meaning
< Lessthan
<= Lessthanorequalto
> Greaterthan

>= Greaterthanorequalto
https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 7/12
8/23/2016 Yuzu: Python Programming in Context

== Equal
!= Notequal

TableB.10Relationaloperatorsandtheirmeaning

B.8.2CompoundBooleanExpressions
Multiple Boolean expressions can be joined using the logical operators and, or, and not as shown in Table
B.11.

xandy IfxisFalsereturnxotherwisereturny.
xory IfxisFalsereturnyotherwisereturnx.
notx IfxisFalsereturnTrueotherwisereturnFalse.

TableB.11Logicaloperatorbehavior

B.9Selection
B.9.1BinarySelection
if:
else:

B.9.2UnarySelection
if:

B.9.3NestedSelectionwithelif
if:

elif:

elif:

elif:

else:

B.10PythonModules
B.10.1Math
TableB.12summarizesthemainmathfunctionsusedinthisbook.

Methodname Use Explanation


cos math.cos(x) Returnsthecosineofxwherexisinradians.
sin math.sin(x) Returnsthesineofxwherexisinradians.
tan math.tan(x) Returnsthetangentofxwherexisinradians.
degrees math.degrees(r) Convertsrradianstodegrees.
radians math.radians(d) Convertsddegreestoradians.
sqrt math.sqrt(x) Returnsthesquarerootofx.

https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 8/12
8/23/2016 Yuzu: Python Programming in Context

TableB.12Simplemathfunctions

B.10.2RandomNumbers
TableB.13summarizesthemainfunctionsfromtherandommoduleusedinthisbook.

Method Use Explanation


name
random random.random() Returnsarandomnumberbetween0.0and1.0.
randint random.randint(1,3) Returnsarandomnumberintherange1,3includingtheendpoints.
randrange random.randrange(4) Returnsarandomnumberintherange0,3.Usesthesamesyntaxasthe
rangefunctionforspecifyingrangesofnumbers.
gauss random.gauss(mu,sigma) ReturnsarandomnumberfromaGaussiandistributionthathasmeanof
mu()andstandarddeviationofsigma().
shuffle random.shuffle(myList) Returns a copy of myList with the elements of the list shuffled into
randomorder.

TableB.13Randomnumbergeneration

B.10.3RegularExpressionPatterns
Thefollowingisalistofsomesimpleregularexpressionpatterns:

. Matchanycharacter.

[abc] Matcha,b,orc.

[^abc] Matchanycharactersotherthana,b,orc.

[abc]+ Matchoneormoreoccurrencesofthecharactersabc.Forexample,borabaorccba.

[abc]+[de]* Matchzeroormoreoccurrencesofthecharactersabc.Forexample,borabaorbdorabae.

(regex) Createacapturegroup.

TableB.14summarizesthemainregularexpressionmethods.

Method Use Explanation


name
match re.match('[abc]XY.') Matchesanystringthatstartswitha,b,orcfollowedbyXYfollowedbyany
character.ReturnsamatchobjectonsuccessorNone.
sub re.sub('[tv]','X', Returns 'XxyzbgXX'. Similar to replace, except that regular expression
'vxyzbgtt') matchingisused.

findall{re.findall('[bc]+', Returns ['bc','cb']. Returns a list of all substrings matching the regular
'abcdefedcba') expression.
groups matchObj.groups() Returnsalistofallcapturegroupsmatched.matchObjiscreatedbyacallto
match.
group matchObj.group(2) Returnsasinglecapturegroup.matchObjiscreatedbyacalltomatch.

TableB.14Regularexpressionmodulefunctions

https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 9/12
8/23/2016 Yuzu: Python Programming in Context

B.11DefiningFunctions
deffunctionName(param1,param2,):
statement1
statement2

B.12DefiningClasses
classclassname:

defmethod1()

defmethod2()

B.13CommonErrorMessages
Learninghowtoreadandunderstanderrormessagescansavealotoftimeandtroubleonceyouknowwhat
tolookfor.

Oneofthemostcommonerrorsisthesyntaxerror,whichindicatesthatyouhavewrittenalineofcodethat
doesnotfollowtherulesofPythonsgrammar.Itistheequivalentofnotendingasentencewithaperiodor
forgetting to capitalize the first word of a sentence. Here is an example of a syntax error that is due to a
missing:attheendofthelinedeffoo(x,y).Itisalsoverycommontogetasyntaxerrorlikethiswhenyouare
usinganifstatement.

>>>deffoo(x,y)
File"",line1
deffoo(x,y)
^

SyntaxError:invalidsyntax

Anothercommonerroristouseavariablethatyouhavenotyetassignedavalueto.Thefollowingexample
showstheuseofthevariablespamwhenspamhasnovalue.Notethatthiserrormessagestartswiththeword
Traceback, which can help you pinpoint exactly the line and the function where an error occurs. The
tracebackisfollowedbytheerrormessageitself.Thismessagetellsyouthat'spam'isundefined.Sincethe
onlywaytodefineavariableinPythonistoassignavaluetoit,thisshouldhelpyourealizethatyouhavenot
doneso.

>>>4+spam*3
Traceback(mostrecentcalllast):
File"",line1,in?
NameError:name'spam'isnotdefined

LetslookatanotherexampleofatracebackusingasimplePythonprogramwehavesavedinthefiletest.py
(ListingB.1).Whenwerunthisprogramwegetthefollowingoutput:

hello
Traceback(mostrecentcalllast):

https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 10/12
8/23/2016 Yuzu: Python Programming in Context

File"test.py",line11,in
main()
File"test.py",line8,inmain
x=foo()
File"test.py",line3,infoo
b=a+spam
NameError:globalname'spam'isnotdefined

ListingB.1AsimplePythontraceback

Thefirstlineofthetracebackpointstoline11ofthefiletest.py.Thisiswheremain()iscalled.Thenextline
ofthefilepointstoline8wherefoo()iscalledandthelastlinepointstoline3wherewefindtheexpressionb
=a+spam.OnceagainPythontellsusthatspamisnotdefinedanditgivesusexactlythelinethatcontains
theerror.

When working with a mixture of strings and numeric data types it is possible to try to add two things
togetherthatyoushouldnot.Thenextexampleshowsanexpressionthattriestoaddastringandaninteger
together.Pythondoesnotknowwhetheritshouldconvert'2'toanintegerandaddtwointegerstogether,orto
convert2toastringandconcatenatetwostringstogether.Itwillthusgiveyouthiserrormessage:

>>>'2'+2
Traceback(mostrecentcalllast):
File"",line1,in?
TypeError:cannotconcatenate'str'and'int'objects

HereisanothersituationwherePythoncannotfigureoutwhattoadd:

>>>defbar():
return2*5

>>>a=3+bar
Traceback(mostrecentcalllast):
File"",line1,in
TypeError:unsupportedoperandtype(s)for+:'int'and'function'

Inthiscase,thestatementa=3+barismissingtheparenthesesafterthefunctionnamedbar.Eventhough
bardoesnotrequireanyparameters,Pythondoesnotknowthatitshouldcallthefunctionwithoutthecall
operators().

Hereisanexampleofusingthecalloperatorsonanobjectthatisnotafunction:

>>>b=1
https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 11/12
8/23/2016 Yuzu: Python Programming in Context

>>>c=2+b()
Traceback(mostrecentcalllast):
File"",line1,in
TypeError:'int'objectisnotcallable

Thenexterrorisprettyeasytorecognize,butithappensquitecommonly.

>>>10*(1/0)
Traceback(mostrecentcalllast):
File"",line1,in?
ZeroDivisionError:intdivisionormodulobyzero

https://reader.yuzu.com/#/books/9781284089042/c/6/44!/4/488@0:41.8 12/12
8/23/2016 Yuzu: Python Programming in Context

APPENDIXC

turtleReference

Thedefinitiveguidetotheturtlemodulecanbefoundathttp://docs.python.org/release/3.2/library/turtle.html.

C.1BasicMoveandDraw
forward(distance)Moveforwardalongcurrentheadingbydistanceunits.
back(distance)Movebackwardalongcurrentheadingbydistanceunits.
right(angle)Turntheturtlerightbyangledegrees.
left(angle)Turntheturtleleftbyangledegrees.
goto(pos,y=None)Movetheturtletoanabsoluteposition.Ifthetailisdownthenalinewillbedrawn.The
turtles orientation does not change. The position may be specified by a pair of numbers, or a tuple
representingthex,ycoordinates.
setx(x)Movetheturtletoabsolutepositionx,ywherexisspecifiedasaparameterandydoesnotchange.
sety(y)Movetheturtletoabsolutepositionx,ywhereyisspecifiedasaparameterandxdoesnotchange.
setheading(toangle)Settheturtlefacingthegivenheading.Somecommondirectionsindegreesinclude0=
right,90=up,180=left,270=down.
circle(radius,extent)Drawacircleorpartofacircle.
dot(size=None,*color)Makeadotatthecurrentposition.

dot(30)
dot(40,'blue')

write(text,move=False,align=left,font=(Arial,8,normal))Writesometexttothescreen.Ifmoveis
False,thetextwillstartattheturtlescurrentpositionandtheturtlewillnotmove.Aligncanbe'left',
'right',or'center'.Thefontspecificationallowsyoutochooseafontandsizeforthemessage.

C.2TurtleState
position()Returntheturtlescurrentlocationasan(x,y)tuple.
towards(pos, y=None) Return the angle between the turtles current heading and the location passed as a
parameter.

towards(number,number)
towards((number,number))
towards(otherTurtle)

https://reader.yuzu.com/#/books/9781284089042/c/6/46!/4/188/2@0:0 1/4
8/23/2016 Yuzu: Python Programming in Context

xcor()Returnthecurrentxcoordinateoftheturtle.
ycor()Returnthecurrentycoordinateoftheturtle.
heading()Returnthecurrentheadingoftheturtle.
distance(pos, y=None) Return the distance from the turtles current location and the location passed as a
parameter.

distance(number,number)
distance((number,number))
distance(otherTurtle)

C.3DrawingState
down()Puttheturtlestaildown.
up()Raisetheturtlestailup.
width(width=None)Changethewidthofthelinetodraw.Ifnoparameterispassedthemethodwillreturn
thewidthoftheline.

C.4Filling
begin_fill()Callbeginfillbeforedrawingtheshapeyouwantfilledin.
end_fill()Callendfillafteryoudrawtheshapeyouwantfilledin.Forexample,youcanusethefollowing
codetocreateafilledsquare:

turtle.begin_fill()
foriinrange(4):
turtle.forward(100)
turtle.right(90)
turtle.end_fill()

C.5MoreDrawingControl
reset() Delete all drawing from the window. Recenter the turtle and set all turtle variables back to their
defaultvalues.
clear()Deletealltheturtlesdrawingbutleavetheturtleatcurrentposition.

C.6ControllingtheShapeandAppearance
Theturtleisnotlimitedtothetriangularshapeyouseebydefault.Theturtlecanappearasaturtle,orany
othershapethatyoucandrawwiththeturtle.Inaddition,anygiffilecanbeusedtoprovidetheturtlewitha
shape.Onceyouhaveaddedashapetoyourprogram,anyturtlecanusethatshapebyname.

addshape(name,shape=None) Adds a new shape to the dictionary of turtle shape names and shapes. The
argumentsareinterpretedasfollows:

NameisthenameofagiffileandshapeisNone.Thentheimageisinstalledusingthegiffilenameas
thedictionarykey.
Ifnameisastringandshapeisatupleofpairsofcoordinates,thenthetupleofpairsisusedtocreatea
polygontouseastheturtleshape.Itisstoredinthedictionarywithnameusedasthekey.

https://reader.yuzu.com/#/books/9781284089042/c/6/46!/4/188/2@0:0 2/4
8/23/2016 Yuzu: Python Programming in Context

Nameisastringandshapeisashapeobject.Thennameisthekeyandtheshapeobjectistheshape.

resizemode(rmode=None)Setresizemodetooneofthefollowingvalues:auto,user,ornoresize.Eachresize
modehasthefollowingeffect:autoadaptstheappearanceoftheturtleaccordingtothevalueofpensize.
useradaptstheappearanceoftheturtleaccordingtothevaluesofstretchfactorandoutlinewidth.Both
of these can be set by turtlesize. noresize forces the turtle to stay the same size. If no parameter is
passed,thenthemethodreturnsthecurrentmode.
turtlesize(stretchfactor=None, outline=None) If the resizemode is user, then the turtle will be displayed
stretchfactortimesasbigastheoriginalturtleusingapenwidthofoutline.
getshapes()Returnalistofallkeyscurrentlyintheshapedictionary.
polystart()Startrecordingtheverticesofapolygon.
polyend()Stoprecordingtheverticesofapolygon.
getpoly()Returnthelastrecordedpolygon.Thispolygonisanobjectthatcanbepassedtoaddshape.
shape(name=None)Returnorsettheturtleshape.Therearethreedefaultshapesfortheturtle'arrow','turtle',
and'circle'.Youcanmakeyourownshapesasshowninthefollowingexample.

turtle.polystart()
foriinrange(4):
turtle.forward(10)
turtle.right(90)
turtle.polyend()
squareShape=turtle.getpoly()
turtle.addshape('squarepants',squareShape)
turtle.shape('squarepants')

hideturtle()Maketheturtleinvisible.
showturtle()Maketheturtlevisible.

C.7MeasurementSettings
degrees(fullcircle=360.0)Changetheanglemeasurementunitstodegrees.Forsomeapplicationsitmaybe
convenientforafullcircletohaveadifferentnumberofdegrees.Forexample,youmightwanttomake
acirclehave100degreestomakeiteasytomappercentagestoportionsofacircle.Youcanmodifythis
usingthefullcircleparameter.
radians()Changeallanglemeasurementunitstoradians.

C.8DrawingSpeed
speed(speed=None)Ifnoparameterisgiven,returnthecurrentspeedoftheturtle.Otherwisesetthespeedto
anintegervaluebetween1and10,where1isslowand10isfast.Thefastestspeedcanbeobtainedby
callingspeedwithaparameterof0.
tracer(flag=None,delay=None)tracercanbecalledintwoways.Inthefirst,tracercanbecalledwitheither
TrueorFalse.IfFalseispassedasaparameter,thennoanimationisdone.Trueturnsonanimationusing
thespeedspecified.Inthesecond,tracercanbepassedasingleinteger.Ifasingleintegernispassed,
then only every nth update is really performed when animating the turtle. To conclude, the second
parameterspecifiesadelayvalue.(Seebelow.)
delay(delay=None)Setorreturnthedrawingdelayinmilliseconds.
update()Updatethescreenwithalldrawing.Thisisveryusefulwhentracerissetto0.
https://reader.yuzu.com/#/books/9781284089042/c/6/46!/4/188/2@0:0 3/4
8/23/2016 Yuzu: Python Programming in Context

C.9Color
bgcolor(*args)Setorreturnthebackgroundcolorofthewindow.Thecolorcanbesetinseveralways:

bgcolor(colorname) Mostreasonablecolornamesarerecognized.

bgcolor(r,g,b) r,g,andbarefloatingpointnumbersbetween0.0and1.0.

bgcolor((r,g,b)) r,g,andbarefloatingpointnumbersbetween0.0and1.0.

bgpic(picname=None)Setthebackgroundofthewindowtousetheimageinthefilename.
pencolor(*args)Setthecolorofthepen,usingthesameoptionsasbgcolor.

pencolor(color1,color2)
pencolor((r1,g1,b1),(r2,g2,b2))
pencolor(r1,g1,b1,r2,g2,b2)

Seecolorformoreexplanation.
fillcolor(*args)Setthecurrentfillcolorusingthesameoptionsasbgcolor.
color(*args)Whencalledwithoutparameters,returnsthecurrentpenandfillcolors.Otherwiseyoucanuse
this method to set the pencolor and fillcolor in one call. Legal ways to call this method include
color(r1,g1,b1,r2,g2,b2) or color((r1,g1,b1), (r2,g2,b2)), or color(string1,string2). The first group of
numberssetsthepencolorandthesecondsetofnumberssetsthefillcolor.
colormode(cmode=None)Changethespecificationofredgreenbluecolorstofloatingpointvaluesbetween
0and1ortointegervaluesbetween0and255.Callingcolormode(255)changestointegermode.

colormode(255)
color(200,149,58)
colormode(1.0)
color(.75,.69,.02)

C.10Events
onclick(fun,btn=1)Bindafunctiontoamouseclick,sothatfuniscalledwheneverthebuttonispressed.
Notethatfunmustaccepttwoparametersthatwillgivethepositionofthemousewhenthebuttonwas
clicked.
onkey(fun,key=None)Bindafunctiontoakeypress.funmustbedefinedwithnoparameters.
listen(xdummy=None,ydummy=None)Givethecurrentturtlescanvasfocussoitcanlistenforkeyevents.
Keyeventswillnotberecognizeduntilthisfunctioniscalled.Thislistenmethodisdefinedtotaketwo
dummyparameters,solistencanbeboundtoamouseclickevent.

https://reader.yuzu.com/#/books/9781284089042/c/6/46!/4/188/2@0:0 4/4
8/23/2016 Yuzu: Python Programming in Context

INDEX
TheindexthatappearedintheprintversionofthistitlewasintentionallyremovedfromtheeBook.Pleaseuse
thesearchfunctiononyoureReadingdevicetosearchfortermsofinterest.Foryourreference,thetermsthat
appearintheprintindexarelistedbelow.

A
abstractclass
abstraction
acceleration
accessormethod
accumulatorpattern
accumulatorvariable
actualparameter
algorithm
Alien
AlienInvaders
and
AnimatedTurtle
append
application
applyProduction
approximation
archimedes
ASCII
assignmentoperator
assignmentstatement
attribute
average
axiom

B
Babbage,Charles
barchart
basecase
baseclass
baseindex
__bases__
Bear
BernersLee,Tim
biology
https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 1/14
8/23/2016 Yuzu: Python Programming in Context

blackbox
block
Bomb
Booleanexpression
BoundedTurtle
bruteforce
builtinnamespace

C
Caesarcipher
callbyassignment
calltree
callbackfunction
cantordust
Canvas
capturegroup
centraltendency
centroid
changeofstate
character
checkWord
childclass
chr
cImagemodule
ciphertext
circle
circumference
class
__class__
classdefinitionobject
clusteranalysis
clusterAnalysis
clusters
collection
collectionclasses
column
columnmajorstorage
commadelimited
complexnumber
composition
compoundBooleanexpression
computerscience
computerscientist

https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 2/14
8/23/2016 Yuzu: Python Programming in Context

computersimulation
concatenation
condition
constructor
convolution
convolve
coordinate
Copernicus
copylist
copystring
correlation
correlation
createCentroids
createClusters
createWordDict
cryptanalysis
cryptography
cryptoquip
currentcharacter
currentnamespace

D
data
datamining
datamodel
decorator
decryption
def
definiteiteration
degrees
denominator
dereferencing
design
diameter
__dict__
dictionary
dictionarymethods
digitalimageprocessing
Dijkstra,Edsgar
dispersion
distance
divisionoperator
dotoperator

https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 3/14
8/23/2016 Yuzu: Python Programming in Context

double
drawCircle
drawLS
drawPolygon
drawSpiral
drawSquare
drawTriangle

E
earthquake
edgeDetect
edgedetection
elifstatement
emptylist
encryption
encryptMessage
encryptVignere
enlarge
equalityoperator
Etch
euclidD
Euclidiandistance
evaluation
evaluationloop
eventloop
eventdrivenprogram
EventHandler
experiment
exponentiationoperator
expression
mathematical
Python
regular
relational
extends

F
factorial
Fibonaccisequence
file
close
iteratingoverlines
open
read
write
FileTransferProtocol
https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 4/14
8/23/2016 Yuzu: Python Programming in Context

FileImagemethods
filtering
findLetters
Fish
flipaxis
flipimage
flipping
float
floatingpointnumber
forloop
force
formalparameter
formatoperator
formattedstring
fractal
frequency
frequencyanalysis
frequencydistribution
frequencyTableAlt
frequencyTable
frommoduleimport
function

G
Gates,Bill
generalization
generalTransform
genKeyFromPass
GeometricObject
getFreq
getMax
getRange
gettermethod
GPS
grammar
gravitationalconstant
gravity
grayscale
grayPixel

H
HASArelationship
height
help
https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 5/14
8/23/2016 Yuzu: Python Programming in Context

histogram
hostname
HypertextMarkupLanguage(HTML)
HypertextTransferProtocol(HTTP)
hypotenuse

I
Idle,Eric
IDLE
ifstatement
ifelsestatement
imageprocessing
ImageWinmethods
immutable
import
indefiniteiteration
index
index
indexoperator
indexing
indexToLetter
infiniteexpansion
infiniteloop
information
informationhiding
inheritance
inheritancehierarchy
__init__
initialization
input
installation
cImage
Python
PythonImageLibrary(PIL)
instance
instancedata
instancevariable
integer
integerdivision
intensity
interpreter
invocation
irrationalnumber
ISArelationship
https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 6/14
8/23/2016 Yuzu: Python Programming in Context

isinstance
items
iteration

J
join

K
Kmeansalgorithm
kernel
key
keyGen
keyvaluepair
keywords
Kochcurve

L
Lsystem
LaserCannon
latitude
lefthandside
leibniz
Leibnizformula
len
length
letterfrequency
letterFrequency
letterToIndex
Lindenmayer,Astrid
Line
list
list
listcomprehension
listmethods
listoflists
localnamespace
localvariable
logic
logicaloperator
longitude
loopvariable
lsystem
lunareclipse

M
https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 7/14
8/23/2016 Yuzu: Python Programming in Context

mainnamespace
mainloop
makeGrayScale
makeMagnitudeList
makeNegative
mapping
mask
mass
match
math
matplotlib
max
maybeAdd
mean
mean
median
median
medicalimaging
method
methods
midPoint
min
minmaxpattern
mirroring
mode
mode
module
moduloarithmetic
modulooperator
MonteCarlosimulation
montePi
motionpicture
multithreaded
mutable
mutatormethod

N
name
namemangling
namespace
builtinnamespace
localnamespace
mainnamespace
negative
https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 8/14
8/23/2016 Yuzu: Python Programming in Context

negativePixel
neighborCount
nestedBox
nestediteration
nestedselection
newline
Newton,SirIsaac
not
nouns
numerator
numerictype

O
object
object
objectorientedprogramming
OmniGraffle
onclick
onkey
ontimer
operator
assignment
division
dot
equality
exponentiation
format
index
integerdivision
logical
modulo
multiplication
relational
repetition
sequence
slicing
string
operand
optionalparameters
or
ord
ordinalvalue

P
palindrome
parameter
actualparameter

https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 9/14
8/23/2016 Yuzu: Python Programming in Context

formalparameter
parameterpassing
parentclass
password
patternmatching
patterns
abstraction
accumulator
defined
minmaxpattern
Pearsoncorrelationcoefficient
pi
piapproximations
Archimedesapproach
Leibnizformula
MonteCarlosimulation
Wallisformula
pixel
pixelMapper
Pixelmethods
plaintext
Planet
Plant
Point
polygon
polymorphism
pop
precision
predatorpreyrelationship
prediction
previous
primemeridian
primingread
primitiveelement
print
problemsolving
process
productionrule
program
programminglanguage
ProjectGutenberg
prompt
protocol
Ptolemy

https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 10/14
8/23/2016 Yuzu: Python Programming in Context

Pythagoreantheorem
Python

Q
queue

R
radians
radius
railfencecipher
railBreak
railDecrypt
random
randomnumbergenerator
range
rawdata
read
readevalprintloop
readFile
readline
recursion
recursivestep
referencediagram
regularexpression
relationaloperator
remainder
removeChar
removeDupes
removeMatches
repetition
repetitionoperator
representation
reservedwords
resizing
resolution
return
reverse
RGBcolormodel
righthandside
rosemary
rotate
rowmajorstorage
RSAalgorithm
run
https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 11/14
8/23/2016 Yuzu: Python Programming in Context

runningsum

S
scale
scientificnotation
scramble2Decrypt
scramble2Encrypt
security
selectionstatement
self
sepiatone
sequence
sequenceobject
sequenceoperators
sequentialcollection
set
setdefault
settermethod
setworldcoordinates
shortcircuitevaluation
showMontePi
sierpinski
Sierpinskitriangle
simplification
simulation
sleep
slice
slicingoperator
snowflakes
Sobel,Irwin
Sobeloperators
SolarSystem
sort
sortByLen
sorting
SpaceInvaders
specialeffect
specialmethods
split
square
squarebracket
squareIt
standarddeviation

https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 12/14
8/23/2016 Yuzu: Python Programming in Context

standardDev
start
state
statement
staticmethod
@staticmethod
staticvariable
statistics
stepwiseconstruction
stickylabel
stockexchange
stockCorrelate
str
__str__method
stretching
string
stringmethods
stringoperators
subclass
substitutioncipher
substitutionEncrypt
substring
sum
sumofsquares
sumofterms
Sun
super
superclass
symbol

T
tailnesting
testing
textfile
thread
Thread
throwingdarts
timemodule
TKinter
topdownanalysis
traffic
transpositioncipher
tree

https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 13/14
8/23/2016 Yuzu: Python Programming in Context

trigonometry
truncation
tuple
Turing,Alan
turtlemethods
turtlemodule
TurtlePlace
type
typeconversion

U
UniformResourceLocator(URL)
universalgravitation
urllib.requestmodule

V
value
variable
vector
velocity
verticalFlip
videogame
Vignrecipher
VignreSquare
vignereIndex
Visio

https://reader.yuzu.com/#/books/9781284089042/c/6/48!/4/1136@0:28.7 14/14

Potrebbero piacerti anche