Building Skills in Python Release 2.6.2
Total Page:16
File Type:pdf, Size:1020Kb
Building Skills in Python Release 2.6.2 Steven F. Lott January 03, 2010 CONTENTS I Front Matter 3 1 Preface 5 1.1 Why Read This Book? ....................................... 5 1.2 Audience ............................................... 6 1.3 Organization of This Book ..................................... 7 1.4 Limitations .............................................. 8 1.5 Programming Style ......................................... 9 1.6 Conventions Used in This Book .................................. 9 1.7 Acknowledgements .......................................... 10 II Language Basics 11 2 Background and History 15 2.1 History ................................................ 15 2.2 Features of Python ......................................... 15 2.3 Comparisons ............................................. 16 3 Python Installation 21 3.1 Windows Installation ........................................ 21 3.2 Macintosh Installation ........................................ 24 3.3 GNU/Linux and UNIX Overview ................................. 25 3.4 “Build from Scratch” Installation ................................. 28 4 Getting Started 31 4.1 Command-Line Interaction ..................................... 31 4.2 The IDLE Development Environment ............................... 34 4.3 Script Mode ............................................. 36 4.4 Getting Help ............................................. 40 4.5 Syntax Formalities .......................................... 41 4.6 Exercises ............................................... 42 4.7 Other Tools .............................................. 44 4.8 Style Notes: Wise Choice of File Names .............................. 45 5 Simple Numeric Expressions and Output 47 5.1 Seeing Output with the print() Function (or print Statement) ................ 47 5.2 Numeric Types and Operators ................................... 50 5.3 Numeric Conversion (or “Factory”) Functions .......................... 53 5.4 Built-In Math Functions ...................................... 54 i 5.5 Expression Exercises ......................................... 56 5.6 Expression Style Notes ....................................... 60 6 Advanced Expressions 61 6.1 Using Modules ............................................ 61 6.2 The math Module .......................................... 61 6.3 The random Module ......................................... 63 6.4 Advanced Expression Exercises ................................... 64 6.5 Bit Manipulation Operators .................................... 66 6.6 Division Operators .......................................... 68 7 Variables, Assignment and Input 71 7.1 Variables ............................................... 71 7.2 The Assignment Statement .................................... 73 7.3 Input Functions ........................................... 75 7.4 Multiple Assignment Statement .................................. 78 7.5 The del Statement .......................................... 78 7.6 Interactive Mode Revisited ..................................... 79 7.7 Variables, Assignment and Input Function Exercises ....................... 80 7.8 Variables and Assignment Style Notes ............................... 81 8 Truth, Comparison and Conditional Processing 83 8.1 Truth and Logic ........................................... 83 8.2 Comparisons ............................................. 85 8.3 Conditional Processing: the if Statement ............................. 88 8.4 The pass Statement ......................................... 90 8.5 The assert Statement ........................................ 91 8.6 The if-else Operator ........................................ 92 8.7 Condition Exercises ......................................... 93 8.8 Condition Style Notes ........................................ 94 9 Loops and Iterative Processing 95 9.1 Iterative Processing: For All and There Exists .......................... 95 9.2 Iterative Processing: The for Statement .............................. 96 9.3 Iterative Processing: The while Statement ............................ 97 9.4 More Iteration Control: break and continue .......................... 98 9.5 Iteration Exercises .......................................... 100 9.6 Condition and Loops Style Notes .................................. 103 9.7 A Digression ............................................. 104 10 Functions 107 10.1 Semantics ............................................... 107 10.2 Function Definition: The def and return Statements ...................... 109 10.3 Function Use ............................................. 110 10.4 Function Varieties .......................................... 111 10.5 Some Examples ........................................... 112 10.6 Hacking Mode ............................................ 113 10.7 More Function Definition Features ................................. 115 10.8 Function Exercises .......................................... 118 10.9 Object Method Functions ...................................... 121 10.10 Functions Style Notes ........................................ 122 11 Additional Notes On Functions 125 11.1 Functions and Namespaces ..................................... 125 11.2 The global Statement ........................................ 127 ii 11.3 Call By Value and Call By Reference ............................... 127 11.4 Function Objects ........................................... 129 III Data Structures 131 12 Sequences: Strings, Tuples and Lists 135 12.1 Sequence Semantics ......................................... 135 12.2 Overview of Sequences ....................................... 136 12.3 Exercises ............................................... 139 12.4 Style Notes .............................................. 139 13 Strings 141 13.1 String Semantics ........................................... 141 13.2 String Literal Values ......................................... 141 13.3 String Operations .......................................... 143 13.4 String Comparison Operations ................................... 146 13.5 String Statements .......................................... 146 13.6 String Built-in Functions ...................................... 147 13.7 String Methods ............................................ 148 13.8 String Modules ............................................ 151 13.9 String Exercises ........................................... 152 13.10 Digression on Immutability of Strings ............................... 153 14 Tuples 155 14.1 Tuple Semantics ........................................... 155 14.2 Tuple Literal Values ......................................... 155 14.3 Tuple Operations .......................................... 156 14.4 Tuple Comparison Operations ................................... 157 14.5 Tuple Statements .......................................... 157 14.6 Tuple Built-in Functions ...................................... 158 14.7 Tuple Exercises ........................................... 160 14.8 Digression on The Sigma Operator ................................. 161 15 Lists 163 15.1 List Semantics ............................................ 163 15.2 List Literal Values .......................................... 163 15.3 List Operations ........................................... 164 15.4 List Comparison Operations .................................... 164 15.5 List Statements ........................................... 165 15.6 List Built-in Functions ....................................... 166 15.7 List Methods ............................................. 167 15.8 Using Lists as Function Parameter Defaults ............................ 169 15.9 List Exercises ............................................ 170 16 Mappings and Dictionaries 175 16.1 Dictionary Semantics ........................................ 175 16.2 Dictionary Literal Values ...................................... 176 16.3 Dictionary Operations ........................................ 176 16.4 Dictionary Comparison Operations ................................. 178 16.5 Dictionary Statements ........................................ 178 16.6 Dictionary Built-in Functions .................................... 179 16.7 Dictionary Methods ......................................... 180 16.8 Using Dictionaries as Function Parameter Defaults ........................ 181 16.9 Dictionary Exercises ......................................... 182 iii 16.10 Advanced Parameter Handling For Functions ........................... 184 17 Sets 187 17.1 Set Semantics ............................................ 187 17.2 Set Literal Values .......................................... 187 17.3 Set Operations ............................................ 188 17.4 Set Comparison Operators ..................................... 190 17.5 Set Statements ............................................ 191 17.6 Set Built-in Functions ........................................ 191 17.7 Set Methods ............................................. 192 17.8 Using Sets as Function Parameter Defaults ............................ 194 17.9 Set Exercises ............................................. 195 18 Exceptions 199 18.1 Exception Semantics ......................................... 199 18.2 Basic Exception Handling ...................................... 200 18.3 Raising Exceptions .......................................... 203 18.4 An Exceptional Example ...................................... 204 18.5 Complete Exception Handling and The finally Clause .....................