Wonder Club world wonders pyramid logo
×

The Practice of Computing Using Python Book

The Practice of Computing Using Python
Be the First to Review this Item at Wonderclub
X
The Practice of Computing Using Python, A problem-solving approach to programming with Python. The Practice of Computing Using Python introduces CS1 students (majors and non-majors) to computational thinking using Python. With data-manipulation as a theme, readers quickly , The Practice of Computing Using Python
out of 5 stars based on 0 reviews
5
0 %
4
0 %
3
0 %
2
0 %
1
0 %
Digital Copy
PDF format
1 available   for $99.99
Original Magazine
Physical Format

Sold Out

  • The Practice of Computing Using Python
  • Written by author William F. Punch
  • Published by Addison-Wesley, 2/29/2012
  • A problem-solving approach to programming with Python. The Practice of Computing Using Python introduces CS1 students (majors and non-majors) to computational thinking using Python. With data-manipulation as a theme, readers quickly
Buy Digital  USD$99.99

WonderClub View Cart Button

WonderClub Add to Inventory Button
WonderClub Add to Wishlist Button
WonderClub Add to Collection Button

Book Categories

Authors

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 . . . . . . . . . . . . . . . . . . . . .


Login

  |  

Complaints

  |  

Blog

  |  

Games

  |  

Digital Media

  |  

Souls

  |  

Obituary

  |  

Contact Us

  |  

FAQ

CAN'T FIND WHAT YOU'RE LOOKING FOR? CLICK HERE!!!

X
WonderClub Home

This item is in your Wish List

The Practice of Computing Using Python, <i>A problem-solving approach to programming with Python.</i> 

<i>The Practice of Computing Using Python</i> introduces CS1 students (majors and non-majors) to computational thinking using Python. With data-manipulation as a theme, readers quickly , The Practice of Computing Using Python

X
WonderClub Home

This item is in your Collection

The Practice of Computing Using Python, <i>A problem-solving approach to programming with Python.</i> 

<i>The Practice of Computing Using Python</i> introduces CS1 students (majors and non-majors) to computational thinking using Python. With data-manipulation as a theme, readers quickly , The Practice of Computing Using Python

The Practice of Computing Using Python

X
WonderClub Home

This Item is in Your Inventory

The Practice of Computing Using Python, <i>A problem-solving approach to programming with Python.</i> 

<i>The Practice of Computing Using Python</i> introduces CS1 students (majors and non-majors) to computational thinking using Python. With data-manipulation as a theme, readers quickly , The Practice of Computing Using Python

The Practice of Computing Using Python

WonderClub Home

You must be logged in to review the products

E-mail address:

Password: