IntroductiontoComputationandProgrammingUsingPythonRevisedandExpandedEditionJohnV.GuttagIntroductiontoComputationandProgrammingUsingPythonIntroductiontoComputationandProgrammingUsingPythonRevisedandExpandedEditionJohnV.GuttagTheMITPressCambridge,MassachusettsLondon,England©2013MassachusettsInstituteofTechnologyAllrightsreserved.Nopartofthisbookmaybereproducedinanyformbyanyelectronicormechanicalmeans(includingphotocopying,recording,orinformationstorageandretrieval)withoutpermissioninwritingfromthepublisher.MITPressbooksmaybepurchasedatspecialquantitydiscountsforbusinessorsalespromotionaluse.Forinformation,pleaseemailspecial_sales@mitpress.mit.eduorwritetoSpecialSalesDepartment,TheMITPress,55HaywardStreet,Cambridge,MA02142.PrintedandboundintheUnitedStatesofAmerica.LibraryofCongressCataloging-‐in-‐PublicationDataGuttag,John.IntroductiontocomputationandprogrammingusingPython/JohnV.Guttag.—Revisedandexpandededition.pagescmIncludesindex.ISBN978-‐0-‐262-‐52500-‐8(pbk.:alk.paper)1.Python(Computerprogramlanguage)2.Computerprogramming.I.Title.QA76.73.P48G882013005.13'3—dc2310987654321Tomyfamily:OlgaDavidAndreaMichaelMarkAddieCONTENTSPREFACE..............................................................................................................xiiiACKNOWLEDGMENTS...........................................................................................xv1GETTINGSTARTED...........................................................................................12INTRODUCTIONTOPYTHON.............................................................................72.1TheBasicElementsofPython......................................................................82.1.1Objects,Expressions,andNumericalTypes............................................92.1.2VariablesandAssignment..................................................................112.1.3IDLE..................................................................................................132.2BranchingPrograms..................................................................................142.3StringsandInput......................................................................................162.3.1Input..................................................................................................182.4Iteration....................................................................................................183SOMESIMPLENUMERICALPROGRAMS........................................................213.1ExhaustiveEnumeration...........................................................................213.2ForLoops..................................................................................................233.3ApproximateSolutionsandBisectionSearch.............................................253.4AFewWordsAboutUsingFloats..............................................................293.5Newton-Raphson.......................................................................................324FUNCTIONS,SCOPING,andABSTRACTION....................................................344.1FunctionsandScoping..............................................................................354.1.1FunctionDefinitions...........................................................................354.1.2KeywordArgumentsandDefaultValues...............