Skip to main content

Buy Practice of Computing Using Python plus MyLab Programming with Pearson eText Package, The, 2nd Edition TESTBANK with Paypal

Practice of Computing Using Python plus MyLab Programming with Pearson eText  Package, The, 2nd Edition

35.00$

Table of Contents

Contents
-1.0.1 Data Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
-1.0.2 Problem Solving and Case Studies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
-1.0.3 Code examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
-1.0.4 Interactive Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
-1.0.5 Exercises and Programming Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
-1.0.6 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
-1.0.7 Programming Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

I Thinking About Computing 23
0 The Study of Computer Science 25

0.1 Why Computer Science? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
0.1.1 Importance of Computer Science . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
0.1.2 Computer Science Around You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
0.1.3 Computer “Science” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
0.1.4 Computer Science Through Computer Programming . . . . . . . . . . . . . . . . . . . 27
0.2 The Difficulty and Promise of Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
0.2.1 Difficulty 1: Two Things at Once . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
A une Damoyselle malade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
My Sweet/Cute [One] (Feminine) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
A une Damoyselle malade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
My Sweet Dear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
A une Damoyselle malade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
0.2.2 Difficulty 2: What is a Good Program? . . . . . . . . . . . . . . . . . . . . . . . . . . 29
0.2.3 The Promise of a Computer Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
0.3 Choosing a Computer Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
0.3.1 Different Computer Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
0.3.2 Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
0.3.3 Is Python the Best Language? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
0.4 What Is Computation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
0.5 What Is a computer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
0.5.1 Computation in Nature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
0.5.2 The Human Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
0.6 The Modern, Electronic Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
0.6.1 It’s the Switch! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
0.6.2 The Transistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
0.7 A High-Level Look at a Modern Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
0.8 Representing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
0.8.1 Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
0.8.2 Working with Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
0.8.3 Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
0.8.4 Representing Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
0.8.5 Representing Other Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
0.8.6 What Does a Number Represent? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
0.8.7 How to Talk About Quantities of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
0.8.8 How Much Data is That? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
0.9 Overview of Coming Chapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

II Starting to Program 51
1 Beginnings 53

1.1 Practice, Practice, Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
1.2 QuickStart, the Circumference Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
1.2.1 Examining the code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
1.3 An Interactive Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
1.4 Parts of a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
1.4.1 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
1.4.2 Statements and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
1.4.3 Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
1.4.4 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
1.4.5 Special Python Elements: Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
1.4.6 Naming Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
1.4.7 Recommendations on Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
1.5 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
1.5.1 Variable Creation and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
1.6 Objects and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
1.6.1 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
1.6.2 Other Built-In Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
1.6.3 Object types: not variable types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
1.6.4 Constructing New Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
1.7 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
1.7.1 Integer Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
1.7.2 Floating Point Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
1.7.3 Mixed Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
1.7.4 Order of Operations and Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
1.7.5 Augmented Assignment Operators: A Shortcut! . . . . . . . . . . . . . . . . . . . . . . 76
1.8 Your First Module, Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
1.9 Developing an Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
1.9.1 New rule, testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
1.10 Visual Vignette: TURTLE GRAPHICS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
1.10.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
1.10.2 Programming Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
2 Control 93
2.1 The Selection Statement for Decisions: if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
2.1.1 Booleans for Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
2.1.2 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
2.1.3 Example: What Lead is Safe in Basketball? . . . . . . . . . . . . . . . . . . . . . . . . 97
2.1.4 Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
2.1.5 Example: Finding Perfect Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
2.1.6 Example: Classifying Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
2.2 In-Depth Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
2.2.1 True and False: Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
2.2.2 Boolean Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
2.2.3 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
2.2.4 Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
2.2.5 Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
2.2.6 Boolean Operators Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
2.2.7 Another Word on Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
2.2.8 The Selection Statement for Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
2.2.9 More on Python Decision Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
2.2.10 Repetition: the while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
2.2.11 Sentinel Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
2.2.12 Summary of Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
2.2.13 More on the for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
2.2.14 Nesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
2.2.15 Hailstone Sequence Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
2.3 Visual Vignette: Plotting Data with pylab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
2.3.1 First Plot and Using a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
2.3.2 More Interesting Plot: a Sine Wave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
2.4 Computer Science Perspectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
2.4.1 Minimal Universal Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
2.4.2 Programming Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
3 Algorithms and Program Development 155
3.1 What Is an Algorithm? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
3.1.1 Example Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
3.2 Algorithm Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
3.2.1 Algorithm versus Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
3.2.2 Qualities of an Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
3.2.3 Can We Really Do All That? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
3.3 What is a Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
3.3.1 Readability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
3.3.2 Robust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
3.3.3 Correctness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
3.4 Strategies for Program Design . . . . . . . . . . . . . . . . .
3.4.1 Engage and Commit . . . . . . . . . . . . . . . . . . .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 116634
3.4.2 Understand, Then Visualize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
3.4.3 Think Before You Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
3.4.4 Experiment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
3.4.5 Simplify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
3.4.6 Stop and Think . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
3.4.7 Relax: Give Yourself a Break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
3.5 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
3.5.1 Build the Skeleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
3.5.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
3.5.3 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
3.5.4 Doing the Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
4 Working with Strings 175
4.1 The String Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
4.1.1 The Triple Quote String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
4.1.2 Non Printing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
4.1.3 String Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
4.1.4 Strings as a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
4.1.5 More Indexing and Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
4.1.6 Strings are Iterable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
4.2 String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
4.2.1 Concatenation (+) and Repetition (*) . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
4.2.2 Determining When + Indicates Addition or Concatenation? . . . . . . . . . . . . . . . 184
4.2.3 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
4.2.4 The in Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
4.2.5 String Collections are Immutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
4.3 A Preview of Functions and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
4.3.1 First Cut: What is a Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
4.3.2 A String Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
4.3.3 Determining Method Names and Method Arguments . . . . . . . . . . . . . . . . . . . 191
4.3.4 String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
4.3.5 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
4.4 Formatted Output for Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
4.4.1 Descriptor Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
4.4.2 Width and Alignment Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
4.4.3 Floating-Point Precision Descriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
4.5 Control and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
4.6 Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
4.6.1 Example: Reordering a Person’s Name . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
4.6.2 Palindromes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
4.7 More String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
4.8 Computer Science Perspectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
4.8.1 Programming Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
5 Files and Exceptions I 217
5.1 What is a File? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
5.2 Accessing Files: Reading Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
5.2.1 What’s Really Happening? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
5.3 Accessing Files: Writing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
5.4 Reading and Writing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
5.5 File Creation and Overwriting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
5.6 First Cut, Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
5.6.1 Error Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
5.6.2 The try-except construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
5.6.3 try-except Flow of Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
5.6.4 Exception example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
5.7 Example: Counting Poker Hands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
5.7.1 Program to Count Poker Hands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

III Functions and Data Structures 239
6 Functions-QuickStart 241

6.1 What Is a Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
6.1.1 Why Have Functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
6.2 Python Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
6.3 Flow of Control with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
6.3.1 Function Flow in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
6.3.2 Parameter Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
6.3.3 Another Function Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
6.3.4 Function Example: Word Puzzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
6.3.5 Functions Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
6.3.6 When to Use a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
6.3.7 What If There is No Return Statement? . . . . . . . . . . . . . . . . . . . . . . . . . . 255
6.3.8 What If There Are Multiple Return Statements? . . . . . . . . . . . . . . . . . . . . . 256
6.4 Visual Vignette: Turtle Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
7 Lists and Tuples 263
7.1 What Is a List? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
7.2 What You Already Know How To Do With Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
7.2.1 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
7.2.2 Indexing and Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
7.2.3 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
7.2.4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
7.2.5 List Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
7.3 Lists are different than Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
7.3.1 Lists are Mutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
7.3.2 List Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
7.4 OLD AND NEW FRIENDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
7.4.1 Split and Multiple Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
7.4.2 List to String and Back Again, Using join . . . . . . . . . . . . . . . . . . . . . . . . 274
7.4.3 The Sorted Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
7.5 Working with Some Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
7.5.1 Anagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
7.5.2 Example: File Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
7.6 Mutable Objects and References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
7.6.1 Shallow vs. Deep Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
7.6.2 Mutable versus Immutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
7.7 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
7.7.1 Tuples from Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
7.7.2 Why Tuples? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
7.8 Lists: The Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
7.8.1 Example Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
7.8.2 Other Example Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
7.9 ALGORITHM EXAMPLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
7.10 Python Diversion: List Comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
7.10.1 Comprehensions, Expressions and the Ternary Operator . . . . . . . . . . . . . . . . . 308
7.11 Visual Vignette: More Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
7.11.1 Numpy Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
7.11.2 Plotting Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
7.11.3 Programming Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
8 More on Functions 325
8.1 Scope: A First Cut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
8.1.1 Arguments, Parameters, and Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . 326
8.1.2 Passing Mutable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
8.1.3 Returning a Complex Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
8.1.4 Refactoring evens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
8.2 DEFAULT VALUES AND PARAMETERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
8.2.1 Example: Default Values and Parameter Keywords . . . . . . . . . . . . . . . . . . . . 332
8.3 Functions as Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
8.3.1 Function Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
8.3.2 Docstrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
8.4 Example: Determining a Final Grade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
8.4.1 The Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
8.4.2 The Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
8.4.3 Function: weighted_grade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
8.4.4 Function: parse_line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
8.4.5 Function: main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
8.4.6 Example Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
8.5 Esoterica: ‘‘by value’’ or ‘‘by reference’’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
8.5.1 Programming Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
9 Dictionaries and Sets 345
9.1 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
9.1.1 Dictionary Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
9.1.2 Python Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
9.1.3 Dictionary Indexing and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
9.1.4 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
9.2 Word Count Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
9.2.1 Count Words in a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
9.2.2 Word Frequency for Gettysburg Address . . . . . . . . . . . . . . . . . . . . . . . . . . 353
9.2.3 Output and Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
9.3 Periodic Table Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
9.3.1 Working with CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
9.3.2 Algorithm Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
9.3.3 Functions for Divide and Conquer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
9.4 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
9.4.1 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
9.4.2 What’s in a Set? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
9.4.3 Python Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
9.4.4 Methods, Operators, and Functions for Python Sets . . . . . . . . . . . . . . . . . . . 363
9.4.5 Set Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
9.5 Set Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
9.5.1 Relationship between Words of Different Documents . . . . . . . . . . . . . . . . . . . 368
9.5.2 Output and Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
9.6 Scope: The Full Story . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
9.6.1 Namespaces and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
9.6.2 Search Rule for Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
9.6.3 Local . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
9.6.4 Global . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
9.6.5 Built-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
9.6.6 Enclosed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
9.7 PYTHON POINTER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
9.8 Python Diversion: Dictionary and Set Comprehension . . . . . . . . . . . . . . . . . . . . . . 377
9.9 Visual Vignette: Bar Graph of Word Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . 378
9.9.1 Getting the Data Right . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
9.9.2 Labels and the xticks Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
9.9.3 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
9.9.4 Programming Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
10 More Program Development 389
10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
10.2 Divide and Conquer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
10.2.1 Top-Down Refinement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
10.3 The Breast Cancer Classifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
10.3.1 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
10.3.2 The Approach: Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
10.3.3 Training and Testing the Classifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
10.3.4 Building the Classifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
10.4 Designing the Classifier Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
10.4.1 Divided, now Conquer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
10.4.2 Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
10.4.3 File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
10.4.4 The make_training_set Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
10.4.5 The make_test_set Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
10.4.6 The train_classifier Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
10.4.7 train_classifer, Round 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
10.4.8 Testing the Classifier on New Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
10.4.9 The report_results Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
10.5 Running the Classifier on Full Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
10.5.1 Training versus Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
10.6 Other Interesting Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
10.6.1 Tag Clouds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
10.6.2 S&P 500 Predictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
10.6.3 Predicting Religion with Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
10.6.4 Programming Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419

IV Classes, making your own Data Structures and Algorithms 421
11 Introduction to Classes 423

11.0.5 Simple Student Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
11.1 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
11.1.1 Python Is Object-Oriented! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
11.1.2 Characteristics of OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
11.2 Working with Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
11.2.1 Class and Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
11.3 Working with Classes and Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
11.3.1 Built-In Class and Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
11.3.2 Our First Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
11.3.3 Changing Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
11.3.4 The Special Relationship Between an Instance and Class: instance-of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
11.4 Object Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
11.4.1 Using Object Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
11.4.2 Writing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
11.4.3 The Special Argument self . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
11.4.4 Methods are the Interface to a Class Instance . . . . . . . . . . . . . . . . . . . . . . . 435
11.5 Fitting into the Python Class Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
11.5.1 Making Programmer-Defined Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
11.5.2 A Student Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
11.5.3 Python Standard Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
11.5.4 Now There Are Three: Class Designer, Programmer, and User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
11.6 Example: Point Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
11.6.1 Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
11.6.2 Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
11.6.3 Summing Two Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
11.6.4 Improving the Point Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
11.7 Python and OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
11.7.1 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
11.7.2 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
11.7.3 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
11.8 An Aside: Python and Other OOP languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
11.8.1 Public versus Private . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
11.8.2 Indicating Privacy Using Double Underscores (__) . . . . . . . . . . . . . . . . . . . . 446
11.8.3 Python’s Philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
11.8.4 Modifying an Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
11.8.5 Programming Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
12 More on Classes 453
12.1 More About Class Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
12.1.1 Rational Number (Fraction) Class Example . . . . . . . . . . . . . . . . . . . . . . . . 454
12.2 How Does Python Know? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
12.2.1 Classes, Types, and Introspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
12.2.2 Remember Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
12.3 Creating Your Own Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
12.3.1 Mapping Operators to Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 458
12.4 Building the Rational Number Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
12.4.1 Making the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
12.4.2 Review Fraction Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
12.4.3 Back to Adding Fractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
12.4.4 Equality and Reducing Rationals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
12.4.5 Divide and Conquer at Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
12.5 What Doesn’t Work (Yet) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
12.5.1 Introspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
12.5.2 Repairing “int + Rational” Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
12.6 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
12.6.1 The “Find the Attribute” Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
12.6.2 Using Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
12.6.3 Example: The Standard Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
13 Program Development with Classes 487
13.1 Predator--Prey Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
13.1.1 The Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
13.1.2 Simulation Using Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . 488
13.2 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
13.2.1 Island Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
13.2.2 Predator and Prey, Kinds of Animals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
13.2.3 Predator and Prey Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
13.2.4 Object Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
13.2.5 Filling the Island . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
13.3 Adding Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
13.3.1 Refinement: Add Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
13.3.2 Refinement: Time Simulation Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
13.4 Refinement: Eating, Breeding,and Keeping Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
13.4.1 Improved Time Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
13.4.2 Breeding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
13.4.3 Eating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
13.4.4 The Tick of the Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
13.5 Refinements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
13.5.1 Refinement: How Many Times to Move? . . . . . . . . . . . . . . . . . . . . . . . . . . 504
13.5.2 Refinement: Graphing Population Size . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
V Being a better programmer 507
14 Files and Exceptions II 509

14.1 More Details on Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
14.1.1 Other File Access Methods, Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
14.1.2 Other File Access Methods, Writing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
14.1.3 Universal New Line Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
14.1.4 Moving Around in a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
14.1.5 Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
14.1.6 The with Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
14.2 CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
14.2.1 CSV Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
14.2.2 CSV Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
14.2.3 CSV Writer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
14.2.4 Example: Update Some Grades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
14.3 Module: os . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
14.3.1 Directory (Folder) Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
14.3.2 os Module Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
14.3.3 os Module Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
14.4 More on Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
14.4.1 Basic Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
14.4.2 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
14.4.3 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
14.4.4 A Philosophy Concerning Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
14.5 Exception: else and finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
14.5.1 finally and with . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
14.5.2 Example: Refactoring the Reprompting of a File Name . . . . . . . . . . . . . . . . . 531
14.6 More on Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
14.6.1 Raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
14.6.2 Create Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
14.7 Example: Password Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
14.7.1 Programming Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
15 Testing 541
15.1 Why Testing? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
15.1.1 Kinds of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
15.1.2 “Bugs” and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
15.2 Kinds of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
15.2.1 Testing is Hard! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
15.2.2 Importance of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
15.3 Example Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
15.3.1 NBA Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
15.3.2 Basic Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
15.4 Incorporating Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
15.4.1 Catching User Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
15.4.2 Catching Developer Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
15.5 Automation of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
15.5.1 doctest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
15.5.2 Other Kinds of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
15.5.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
16 Recursion: Another Control Mechanism 553
16.1 What Is Recursion? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
16.2 Mathematics and Rabbits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
16.3 Let’s Write Our Own: Reversing a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
16.4 How Does Recursion Actually Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
16.4.1 Stack Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
16.4.2 Stacks and Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
16.5 Recursion in Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
16.5.1 Recursive Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
16.5.2 Sierpinski Triangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
16.6 Recursion to Non-recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
17 Other Fun Stuff with Python 569
17.1 Function Stuff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
17.1.1 Having a Varying Number of Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 569
17.1.2 Iterators and Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
17.1.3 Other Functional Programming Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
17.1.4 Some Functional Tools: map , reduce , filter . . . . . . . . . . . . . . . . . . . . . . 576
17.1.5 Decorators, Functions calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 577
17.2 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
17.2.1 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
17.2.2 Serializing an Instance, pickle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
17.2.3 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
17.3 Other Things in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
17.3.1 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
17.3.2 Built-in Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
17.3.3 Modules on the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
18 The End, or Perhaps the Beginning 591

A. Getting and Using Python 593
A.1 About Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
A.1.1 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
A.1.2 Python 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
A.1.3 Python is Free and Portable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
A.1.4 Starting Python Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
A.1.5 Working with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
A.1.6 Making a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
A.2 Some Conventions for This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
A.2.1 Interactive Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
A.2.2 Program: Written Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
A.2.3 Combined Program and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
A.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
B Simple Drawing with Turtle Graphics 605
B.0.1 What is a Turtle? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
B.0.2 Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
B.0.3 Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
B.0.4 Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
B.0.5 Drawing with Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
B.0.6 Other commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
B.1 Tidbits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
B.1.1 Keeping the Window Open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
B.1.2 Working Nicely With IDLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
C Plotting and Numeric Tools: A Quick Survey 615
C.1 Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
C.1.1 Getting matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
C.2 Working with matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
C.2.1 Plot Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
C.2.2 Plot Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
C.2.3 Tick Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
C.2.4 Bar Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
C.2.5 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
C.2.6 Pie Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
C.3 Numeric Python (Numpy) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
C.3.1 Arrays Are Not Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
C.3.2 Creating a numpy Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
C.3.3 Manipulating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
D Table of UTF-8 One Byte Encodings 627
E Precedence 629
F Naming Conventions 631

F.1 Python Style Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
F.2 Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
F.2.1 Our Added Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
F.3 Other Python Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
G Check Yourself Solutions 635
G.1 Chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
G.2 Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
G.3 Chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
G.4 Chapter 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
G.5 Chapter 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
G.6 Chapter 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
G.7 Chapter 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
G.8 Chapter 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
G.9 Chapter 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
G.10 Chapter 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
G.11 Chapter 14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639



Category : Higher Education
Instalant Download Practice of Computing Using Python plus MyLab Programming with Pearson eText Package, The, 2nd Edition by William F. Punch, Michigan State University Richard Enbody, Michigan State University with Paypal
Practice of Computing Using Python plus MyLab Programming with Pearson eText Package, The, 2nd Edition , buy Practice of Computing Using Python plus MyLab Programming with Pearson eText Package, The, 2nd Edition with paypal , William F. Punch, Michigan State University Richard Enbody, Michigan State University

How can I download Practice of Computing Using Python plus MyLab Programming with Pearson eText Package, The, 2nd Edition?

You will receive an email from testbank that contains the download link or the test bank will be attached to email

I am not able to download my test bank or solution manual ?

If you could not download your product for any reason, contact us and we will solve the issue immediately.

 

Comments

Popular posts from this blog

Buy Invitation to the Life Span 2nd Edition by Berger TESTBANK with Paypal

35.00$ Instant Download of Invitation to the Life Span 2nd Edition by Kathleen Stassen Berger Test Bank Instant download Invitation to the Life Span 2nd Edition by Kathleen Stassen Berger Test Bank pdf docx epub after payment. View More:  Andersons Nursing Leadership Management and Professional Practice For The LPN LVN In Nursing School and Beyond 5th Edition by Dahlkemper Test Bank Andersons Nursing Leadership Management and Professional Practice For The LPN LVN In Nursing School and Beyond 5th Edition by Dahlkemper Test Bank Table of content: Part One: The Beginning 1. The Beginning: The Science of Human Development 2. The Beginning: From Conception to Birth Part Two: The First Two Years 3. The First Two Years: Body and Mind 4. The First Two Years: The Social World Part Three: Early Childhood 5. Early Childhood: Body and Mind 6. Early Childhood: The Social World Part Four: Middle Childhood 7. Middle Childhood: Body and Mind 8. Middle Childhood: The Social

Buy Biology 10th Edition TESTBANK with Paypal

35.00$ Table of contents: Part 1: THE ORGANIZATION OF LIFE. 1. A View of Life. 2. Atoms and Molecules: The Chemical Basis of Life. 3. The Chemistry of Life: Organic Compounds. 4. Organization of the Cell. 5. Biological Membranes. 6. Cell Communication. Part 2: ENERGY TRANSFER THROUGH LIVING SYSTEMS. 7. Energy and Metabolism. 8. How Cells Make ATP: Energy-Releasing Pathways. 9. Photosynthesis: Capturing Light Energy. Part 3: THE CONTINUITY OF LIFE: GENETICS. 10. Chromosomes, Mitosis, and Meiosis. 11. The Basic Principles of Heredity. 12. DNA: The Carrier of Genetic Information. 13. Gene Expression. 14. Gene Regulation. 15. DNA Technology and Genomics. 16. Human Genetics and the Human Genome. 17. Developmental Genetics. Part 4: THE CONTINUITY OF LIFE: EVOLUTION. 18. Introduction to Darwinian Evolution. 19. Evolutionary Change in Populations. 20. Speciation and Macroevolution. 21. The Origin and Evolutionary History of Life. 22. The Evolution of P

Buy Joining Together: Group Theory and Group Skills, 11th Edition TESTBANK with Paypal

35.00$ Table of Contents Preface Chapter 1: Group Dynamics Basic Concepts to Be Covered in This Chapter Group Dynamics and Me What Is a Group? The Importance of Groups Group Structure Creating Productive Groups How to Create an Effective Group The Development of Groups Over Time The Field of Group Dynamics The Nature of This Book and How to Use It Summary Chapter 2: Experiential Learning Basic Concepts to Be Covered in This Chapter Procedural Learning Action Theories Gaining Expertise Through Experiential Learning Experiential Learning and Motivation Learning Group Skills Role Playing Learning How to Be a Participant—Observer Conducting Skill-Training Exercises Ethics of Experiential Learning Summary   Chapter 3: Group Goals, Social Interdependence, and Trust Basic Concepts to Be Covered in This Chapter Introduction What Is a Goal? START Goals Clarity of Goals Operational Goals Group Goals and L