This page intentionally left blank Introduction to Computing Using Python This page intentionally left blank Introduction to Computing Using Python An Application Development Focus Ljubomir Perkovic DePaul University VPANDEXECUTIVEPUBLISHER DonFowley EXECUTIVEEDITOR BethLangGolub PROJECTLEAD SamanthaMandel EDITORIALPROGRAMASSISTANT ElizabethMills EXECUTIVEMARKETINGMANAGER ChristopherRuel CREATIVEDIRECTOR HarryNolan SENIORDESIGNER WendyLai COVERPHOTO ©simon2579/iStockphoto SENIORPRODUCTIONEDITOR SujinHong ThisbookwassetinTimesNewRoman10byLjubomirPerkovicandprintedandbound byCourier.ThecoverwasprintedbyCourier. Thisbookisprintedonacid-freepaper. ∞ Foundedin1807,JohnWiley&Sons,Inc.hasbeenavaluedsourceofknowledgeand understandingformorethan200years,helpingpeoplearoundtheworldmeettheirneeds andfulfilltheiraspirations.Ourcompanyisbuiltonafoundationofprinciplesthatinclude responsibilitytothecommunitiesweserveandwhereweliveandwork.In2008,we launchedaCorporateCitizenshipInitiative,aglobalefforttoaddresstheenvironmental, social,economic,andethicalchallengeswefaceinourbusiness.Amongtheissuesweare addressingarecarbonimpact,paperspecificationsandprocurement,ethicalconduct withinourbusinessandamongourvendors,andcommunityandcharitablesupport.For moreinformation,pleasevisitourwebsite:www.wiley.com/go/citizenship. Copyright©2012 JohnWiley&Sons,Inc. Allrightsreserved. Nopartofthispublicationmaybereproduced,storedinaretrievalsystemortransmitted inanyformorbyanymeans,electronic,mechanical,photocopying,recording,scanning orotherwise,exceptaspermittedunderSection107or108ofthe1976UnitedStates CopyrightAct,withouteitherthepriorwrittenpermissionofthePublisheror authorizationthroughpaymentoftheappropriateper-copyfeetotheCopyrightClearance Center,Inc.,222RosewoodDrive,Danvers,MA01923,websitewww.copyright.com. RequeststothePublisherforpermissionshouldbeaddressedtothePermissions Department,JohnWiley&Sons,Inc.,111RiverStreet,Hoboken,NJ07030-5774, (201)748-6011,fax(201)748-6008,websitewww.wiley.com/go/permissions. Evaluationcopiesareprovidedtoqualifiedacademicsandprofessionalsforreview purposesonly,foruseintheircoursesduringthenextacademicyear.Thesecopiesare licensedandmaynotbesoldortransferredtoathirdparty.Uponcompletionofthereview period,pleasereturntheevaluationcopytoWiley.Returninstructionsandafreeofcharge returnmailinglabelareavailableatwww.wiley.com/go/returnlabel.Ifyouhavechosento adoptthistextbookforuseinyourcourse,pleaseacceptthisbookasyourcomplimentary deskcopy.OutsideoftheUnitedStates,pleasecontactyourlocalsalesrepresentative. ISBN: 978-0-470-61846-2 PrintedintheUnitedStatesofAmerica 10 9 8 7 6 5 4 3 2 1 Tomyfather,MilanPerkovic´ (1937-1970), whodidnotgetthechancetocompletehisbook. This page intentionally left blank Contents Preface xvii 1 Introduction to Computer Science 1 1.1 Computer Science . . . . . . . . . . . . . . . . . . . 2 WhatDoComputingProfessionalsDo? . . . . . . . . . . 2 Models,Algorithms,andPrograms. . . . . . . . . . . . 3 ToolsoftheTrade . . . . . . . . . . . . . . . . . 3 WhatIsComputerScience?. . . . . . . . . . . . . . 4 1.2 Computer Systems. . . . . . . . . . . . . . . . . . . 4 ComputerHardware . . . . . . . . . . . . . . . . 4 OperatingSystems . . . . . . . . . . . . . . . . . 5 NetworksandNetworkProtocols . . . . . . . . . . . . 6 ProgrammingLanguages. . . . . . . . . . . . . . . 7 SoftwareLibraries . . . . . . . . . . . . . . . . . 7 1.3 Python Programming Language . . . . . . . . . . . . . 8 ShortHistoryofPython . . . . . . . . . . . . . . . 8 SettingUpthePythonDevelopmentEnvironment. . . . . . . 8 1.4 Computational Thinking . . . . . . . . . . . . . . . . . 9 ASampleProblem . . . . . . . . . . . . . . . . . 9 AbstractionandModeling. . . . . . . . . . . . . . . 10 Algorithm . . . . . . . . . . . . . . . . . . . . 10 DataTypes . . . . . . . . . . . . . . . . . . . 11 AssignmentsandExecutionControlStructures . . . . . . . 12 Chapter Summary . . . . . . . . . . . . . . . . . . . . . 13 vii viii Contents 2 Python Data Types 15 2.1 Expressions, Variables, and Assignments. . . . . . . . . . 16 AlgebraicExpressionsandFunctions. . . . . . . . . . . 16 BooleanExpressionsandOperators . . . . . . . . . . . 18 VariablesandAssignments . . . . . . . . . . . . . . 20 VariableNames . . . . . . . . . . . . . . . . . . 22 2.2 Strings. . . . . . . . . . . . . . . . . . . . . . . . 23 StringOperators. . . . . . . . . . . . . . . . . . 23 IndexingOperator . . . . . . . . . . . . . . . . . 25 2.3 Lists. . . . . . . . . . . . . . . . . . . . . . . . . 27 ListOperators . . . . . . . . . . . . . . . . . . 27 ListsAreMutable,StringsAreNot . . . . . . . . . . . . 29 ListMethods . . . . . . . . . . . . . . . . . . . 29 2.4 Objects and Classes . . . . . . . . . . . . . . . . . . 31 ObjectType . . . . . . . . . . . . . . . . . . . 32 ValidValuesforNumberTypes . . . . . . . . . . . . . 33 OperatorsforNumberTypes. . . . . . . . . . . . . . 34 CreatingObjects. . . . . . . . . . . . . . . . . . 35 ImplicitTypeConversions. . . . . . . . . . . . . . . 36 ExplicitTypeConversions. . . . . . . . . . . . . . . 37 ClassMethodsandObject-OrientedProgramming . . . . . . 38 2.5 Python Standard Library. . . . . . . . . . . . . . . . . 39 Modulemath . . . . . . . . . . . . . . . . . . . 39 Modulefractions. . . . . . . . . . . . . . . . . 40 2.6 Case Study: Turtle Graphics Objects. . . . . . . . . . . . 41 Chapter Summary . . . . . . . . . . . . . . . . . . . . . 45 Solutions to Practice Problems. . . . . . . . . . . . . . . . 46 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 48 3 Imperative Programming 53 3.1 Python Programs . . . . . . . . . . . . . . . . . . . 54 OurFirstPythonProgram. . . . . . . . . . . . . . . 54 PythonModules . . . . . . . . . . . . . . . . . . 56 Built-InFunctionprint() . . . . . . . . . . . . . . 56 InteractiveInputwithinput() . . . . . . . . . . . . . 57 Functioneval(). . . . . . . . . . . . . . . . . . 58
Description: