xviii Contributor List
xviii Contributor List
Contents Foreword Preface vii Contributor List xiii 1 The way of the program 1 1.1 The Python programming language.····.···· 1 1.2 What is a program? 3 1.3 What is debugging? 4 1.4 Formal and natural languages···.. 6 1.5 The first program········· 8 1.6 Glossary.·········· 8 2 Variables,expressions and statements 11 2.1 Values and types..........·.·.··· 11 2.2 Variables........··· 12 2.3 Variable names and keywords 13 2.4 Statements.········ 15 2.5 Evaluating expressions······ 16 2.6 Operators and operands..... 17
Contents Foreword v Preface vii Contributor List xiii 1 The way of the program 1 1.1 The Python programming language . . . . . . . . . . . . . . . . . 1 1.2 What is a program? . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.3 What is debugging? . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4 Formal and natural languages . . . . . . . . . . . . . . . . . . . . 6 1.5 The first program . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2 Variables, expressions and statements 11 2.1 Values and types . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3 Variable names and keywords . . . . . . . . . . . . . . . . . . . . 13 2.4 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.5 Evaluating expressions . . . . . . . . . . . . . . . . . . . . . . . . 16 2.6 Operators and operands . . . . . . . . . . . . . . . . . . . . . . . 17
XX Contents 2.7 Order of operations..·.·..······.· 17 2.8 Operations on strings.·.·· 18 2.9 Composition...···. 19 2.10 Comments..... 19 2.11 Glossary.··· 20 3 Functions 23 3.1 Function calls.... 23 3.2 Type conversion.· 24 3.3 Type coercion.··. 24 3.4 Math functions...· 25 3.5 Composition...... 26 3.6 Adding new functions 26 3.7 Definitions and use..... 29 3.8 Flow of execution...... 29 3.9 Parameters and arguments.··· 30 3.10 Variables and parameters are local 31 3.11 Stack diagrams。,。.。······· 32 3.12 Functions with results..... 33 3.13 Glossary...······ 34 4 Conditionals and recursion 37 4.1 The modulus operator..... 37 4.2 Boolean expressions 44 37 4.3 Logical operators···· 38 4.4 Conditional execution 39 4.5 Alternative execution.··· 39 4.6 Chained conditionals..... 40
xx Contents 2.7 Order of operations . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.8 Operations on strings . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.9 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.10 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 3 Functions 23 3.1 Function calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.2 Type conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.3 Type coercion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.4 Math functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.5 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.6 Adding new functions . . . . . . . . . . . . . . . . . . . . . . . . 26 3.7 Definitions and use . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.8 Flow of execution . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.9 Parameters and arguments . . . . . . . . . . . . . . . . . . . . . . 30 3.10 Variables and parameters are local . . . . . . . . . . . . . . . . . 31 3.11 Stack diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.12 Functions with results . . . . . . . . . . . . . . . . . . . . . . . . 33 3.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 4 Conditionals and recursion 37 4.1 The modulus operator . . . . . . . . . . . . . . . . . . . . . . . . 37 4.2 Boolean expressions . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.3 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 4.4 Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . 39 4.5 Alternative execution . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.6 Chained conditionals . . . . . . . . . . . . . . . . . . . . . . . . . 40
Contents xxi 4.7 Nested conditionals........·.········· 41 4.8 The return statement····· 4 42 4.9 Recursion 42 4.10 Stack diagrams for recursive functions.····.···.· 4 44 4.11 Infinite recursion 45 4.12 Keyboard input.. 45 4.13 Glossary···· 46 5 Fruitful functions 49 5.1 Return values..... 49 5.2 Program development 。4 50 5.3 Composition.·... 53 5.4 Boolean functions... 54 5.5 More recursion..·. 4 55 5.6 Leap of faith 57 5.7 One more example 58 5.8 Checking types 58 5.9 Glossary 60 6 Iteration 61 6.1 Multiple assignment.. 61 6.2 The while statement... 62 6.3 Tables......... 64 6.4 Two-dimensional tables..... 66 6.5 Encapsulation and generalization 67 6.6 More encapsulation ..... 68 6.7 Local variables 4 4 69 6.8 More generalization.. 70 6.9 Functions 71 6.10 Glossary····· 72
Contents xxi 4.7 Nested conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.8 The return statement . . . . . . . . . . . . . . . . . . . . . . . . 42 4.9 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.10 Stack diagrams for recursive functions . . . . . . . . . . . . . . . 44 4.11 Infinite recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 4.12 Keyboard input . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 4.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 5 Fruitful functions 49 5.1 Return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 5.2 Program development . . . . . . . . . . . . . . . . . . . . . . . . 50 5.3 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 5.4 Boolean functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 5.5 More recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 5.6 Leap of faith . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 5.7 One more example . . . . . . . . . . . . . . . . . . . . . . . . . . 58 5.8 Checking types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 5.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 6 Iteration 61 6.1 Multiple assignment . . . . . . . . . . . . . . . . . . . . . . . . . 61 6.2 The while statement . . . . . . . . . . . . . . . . . . . . . . . . . 62 6.3 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 6.4 Two-dimensional tables . . . . . . . . . . . . . . . . . . . . . . . 66 6.5 Encapsulation and generalization . . . . . . . . . . . . . . . . . . 67 6.6 More encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . 68 6.7 Local variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 6.8 More generalization . . . . . . . . . . . . . . . . . . . . . . . . . . 70 6.9 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 6.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
xxii Contents 7 Strings 73 7.1 A compound data type.·· 73 7.2 Length 74 7.3 Traversal and the for loop.. 74 7.4 String slices...·.·.· 4 76 7.5 String comparison 76 7.6 Strings are immutable 4 77 7.7 A find function...·. 78 7.8 Looping and counting 78 7.9 The string module 79 7.10 Character classification. 80 7.11 Glossary ..... 81 8 Lists 83 8.1 List values.····· 83 8.2 Accessing elements··· 84 8.3 List length.:····· 85 8.4 List membership 86 8.5 Lists and for loops. 86 8.6 List operations····· 87 8.7 List slices 88 8.8 Lists are mutable 4 88 8.9 List deletion..... 89 8.10 Objects and values 91 8.11 Aliasing·。.· 92 8.12 Cloning lists.. 92 8.13 List parameters . 93 8.14 Nested lists..... 94
xxii Contents 7 Strings 73 7.1 A compound data type . . . . . . . . . . . . . . . . . . . . . . . . 73 7.2 Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 7.3 Traversal and the for loop . . . . . . . . . . . . . . . . . . . . . . 74 7.4 String slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 7.5 String comparison . . . . . . . . . . . . . . . . . . . . . . . . . . 76 7.6 Strings are immutable . . . . . . . . . . . . . . . . . . . . . . . . 77 7.7 A find function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 7.8 Looping and counting . . . . . . . . . . . . . . . . . . . . . . . . 78 7.9 The string module . . . . . . . . . . . . . . . . . . . . . . . . . 79 7.10 Character classification . . . . . . . . . . . . . . . . . . . . . . . . 80 7.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 8 Lists 83 8.1 List values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 8.2 Accessing elements . . . . . . . . . . . . . . . . . . . . . . . . . . 84 8.3 List length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 8.4 List membership . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 8.5 Lists and for loops . . . . . . . . . . . . . . . . . . . . . . . . . . 86 8.6 List operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 8.7 List slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 8.8 Lists are mutable . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 8.9 List deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 8.10 Objects and values . . . . . . . . . . . . . . . . . . . . . . . . . . 91 8.11 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 8.12 Cloning lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 8.13 List parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 8.14 Nested lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94