文件名称:Python for Software Design - How to Think Like a Computer Scientist (2009).pdf
文件大小:784KB
文件格式:PDF
更新时间:2022-09-07 16:37:45
Python Software Design Computer Scientist
Contents Preface pagexi 1 TheWayoftheProgram 1 1.1 ThePythonProgrammingLanguage 1 1.2 WhatIsaProgram? 3 1.3 WhatIsDebugging? 3 1.3.1SyntaxErrors 3 1.3.2RuntimeErrors 4 1.3.3SemanticErrors 4 1.3.4ExperimentalDebugging 4 1.4 FormalandNaturalLanguages 5 1.5 TheFirstProgram 6 1.6 Debugging 7 1.7 Glossary 8 1.8 Exercises 9 2 Variables,Expressions,andStatements 10 2.1 ValuesandTypes 10 2.2 Variables 11 2.3 VariableNamesandKeywords 13 2.4 Statements 13 2.5 OperatorsandOperands 14 2.6 Expressions 15 2.7 OrderofOperations 15 2.8 StringOperations 16 2.9 Comments 17 2.10Debugging 17 2.11Glossary 18 2.12Exercises 19 v vi Contents 3 Functions 21 3.1 FunctionCalls 21 3.2 TypeConversionFunctions 21 3.3 MathFunctions 22 3.4 Composition 23 3.5 AddingNewFunctions 24 3.6 DefinitionsandUses 26 3.7 FlowofExecution 26 3.8 ParametersandArguments 27 3.9 VariablesandParametersAreLocal 28 3.10StackDiagrams 29 3.11FruitfulFunctionsandVoidFunctions 30 3.12WhyFunctions? 31 3.13Debugging 31 3.14Glossary 32 3.15Exercises 33 4 CaseStudy:InterfaceDesign 35 4.1 TurtleWorld 35 4.2 SimpleRepetition 36 4.3 Exercises 37 4.4 Encapsulation 38 4.5 Generalization 39 4.6 InterfaceDesign 40 4.7 Refactoring 41 4.8 ADevelopmentPlan 42 4.9 Docstring 43 4.10Debugging 43 4.11Glossary 44 4.12Exercises 44 5 ConditionalsandRecursion 46 5.1 ModulusOperator 46 5.2 BooleanExpressions 46 5.3 LogicalOperators 47 5.4 ConditionalExecution 48 5.5 AlternativeExecution 48 5.6 ChainedConditionals 49 5.7 NestedConditionals 49 5.8 Recursion 50 5.9 StackDiagramsforRecursiveFunctions 52 5.10InfiniteRecursion 52 5.11KeyboardInput 53 5.12Debugging 54 5.13Glossary 55 5.14Exercises 56 Contents vii 6 FruitfulFunctions 59 6.1 ReturnValues 59 6.2 IncrementalDevelopment 60 6.3 Composition 63 6.4 BooleanFunctions 64 6.5 MoreRecursion 65 6.6 LeapofFaith 67 6.7 OneMoreExample 67 6.8 CheckingTypes 68 6.9 Debugging 69 6.10Glossary 70 6.11Exercises 71 7 Iteration 73 7.1 MultipleAssignment 73 7.2 UpdatingVariables 74 7.3 ThewhileStatement 75 7.4 break 76 7.5 SquareRoots 77 7.6 Algorithms 79 7.7 Debugging 79 7.8 Glossary 80 7.9 Exercises 80 8 Strings 82 8.1 AStringIsaSequence 82 8.2 len 83 8.3 TraversalwithaforLoop 83 8.4 StringSlices 85 8.5 StringsAreImmutable 86 8.6 Searching 86 8.7 LoopingandCounting 87 8.8 stringMethods 87 8.9 TheinOperator 89 8.10StringComparison 89 8.11Debugging 90 8.12Glossary 92 8.13Exercises 92 9 CaseStudy:WordPlay 95 9.1 ReadingWordLists 95 9.2 Exercises 96 9.3 Search 97 9.4 LoopingwithIndices 99 9.5 Debugging 100 9.6 Glossary 101 9.7 Exercises 101 viii Contents 10 Lists 103 10.1 AListIsaSequence 103 10.2 ListsAreMutable 104 10.3 TraversingaList 105 10.4 ListOperations 106 10.5 ListSlices 106 10.6 ListMethods 107 10.7 Map,Filter,andReduce 108 10.8 DeletingElements 109 10.9 ListsandStrings 110 10.10ObjectsandValues 111 10.11Aliasing 113 10.12ListArguments 113 10.13Debugging 115 10.14Glossary 116 10.15Exercises 117 11 Dictionaries 119 11.1 DictionaryasaSetofCounters 121 11.2 LoopingandDictionaries 123 11.3 ReverseLookup 123 11.4 DictionariesandLists 124 11.5 Memos 126 11.6 GlobalVariables 128 11.7 LongIntegers 129 11.8 Debugging 130 11.9 Glossary 131 11.10Exercises 131 12 Tuples 133 12.1 TuplesAreImmutable 133 12.2 TupleAssignment 135 12.3 TuplesasReturnValues 136 12.4 Variable-LengthArgumentTuples 136 12.5 ListsandTuples 138 12.6 DictionariesandTuples 139 12.7 ComparingTuples 141 12.8 SequencesofSequences 142 12.9 Debugging 143 12.10Glossary 144 12.11Exercises 145 13 CaseStudy:DataStructureSelection 147 13.1 WordFrequencyAnalysis 147 13.2 RandomNumbers 148 13.3 WordHistogram 149 13.4 MostCommonWords 151 Contents ix 13.5 OptionalParameters 152 13.6 DictionarySubtraction 152 13.7 RandomWords 153 13.8 MarkovAnalysis 154 13.9 DataStructures 155 13.10Debugging 157 13.11Glossary 158 13.12Exercises 158 14 Files 159 14.1 Persistence 159 14.2 ReadingandWriting 159 14.3 FormatOperator 160 14.4 FilenamesandPaths 161 14.5 CatchingExceptions 163 14.6 Databases 164 14.7 Pickling 165 14.8 Pipes 166 14.9 WritingModules 167 14.10Debugging 168 14.11Glossary 169 14.12Exercises 169 15 ClassesandObjects 172 15.1 User-DefinedTypes 172 15.2 Attributes 173 15.3 Rectangles 174 15.4 InstancesasReturnValues 176 15.5 ObjectsAreMutable 176 15.6 Copying 177 15.7 Debugging 179 15.8 Glossary 179 15.9 Exercises 180 16 ClassesandFunctions 182 16.1 Time 182 16.2 PureFunctions 183 16.3 Modifiers 184 16.4 PrototypingversusPlanning 185 16.5 Debugging 187 16.6 Glossary 188 16.7 Exercises 188 17 ClassesandMethods 189 17.1 Object-OrientedFeatures 189 17.2 PrintingObjects 190 17.3 AnotherExample 192 17.4 AMoreComplicatedExample 192 x Contents 17.5 TheInitMethod 193 17.6 The__str__method 194 17.7 OperatorOverloading 195 17.8 Type-BasedDispatch 195 17.9 Polymorphism 197 17.10Debugging 198 17.11Glossary 199 17.12Exercises 199 18 Inheritance 201 18.1 CardObjects 201 18.2 ClassAttributes 202 18.3 ComparingCards 204 18.4 Decks 205 18.5 PrintingtheDeck 205 18.6 Add,Remove,Shuffle,andSort 206 18.7 Inheritance 207 18.8 ClassDiagrams 209 18.9 Debugging 210 18.10Glossary 211 18.11Exercises 212 19 CaseStudy:Tkinter 214 19.1 GUI 214 19.2 ButtonsandCallbacks 215 19.3 CanvasWidgets 216 19.4 CoordinateSequences 217 19.5 MoreWidgets 218 19.6 PackingWidgets 220 19.7 MenusandCallables 223 19.8 Binding 223 19.9 Debugging 226 19.10Glossary 227 19.11Exercises 228 Appendix 231 Index 241