Python for Software Design

 - How to Think Like a Computer Scientist -

Allen B. Downey


e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 2.50
 Pages
 271 p
 File Size 
 1,147 KB
 File Type
 PDF format
 ISBN-13
 978-0-511-50155-5 eBook (Adobe Reader) 
 978-0-521-89811-9 hardback
 978-0-521-72596-5 paperback


 Copyright©   
 Allen B. Downey 2009  

Preface
THE STRANGE HISTORY OF THIS BOOK
In January 1999, I was preparing to teach an introductory programming class in Java.
I had taught it three times and I was getting frustrated. The failure rate in the class
was too high, and, even for students who succeeded, the overall level of achievement
was too low.

One of the problems I saw was the books. I had tried three different books (and had
read a dozen more), and they all had the same problems. They were too big, with
too much unnecessary detail about Java and not enough high-level guidance about
how to program. And they all suffered from the trap door effect: they would start out
easy, proceed gradually, and then somewhere around Chapter 4 the bottom would
fall out. The students would get too much new material, too fast, and I would spend
the rest of the semester picking up the pieces.

Two weeks before the first day of classes, I decided to write my own book. I wrote
one 10-page chapter a day for 13 days. I made some revisions on Day 14 and then
sent it out to be photocopied.
My goals were:
■ Keep it short. It is better for students to read 10 pages than not read 50 pages.
■ Be careful with vocabulary. I tried to minimize the jargon and define each term at first use.
■ Build gradually. To avoid trap doors, I took the most difficult topics and split
them into a series of small steps.
■ Focus on programming, not the programming language. I included the minimum
useful subset of Java and left out the rest.

I needed a title, so on a whim I chose How to Think Like a Computer Scientist.
My first version was rough, but it worked. Students did the reading, and they understood
enough that I could spend class time on the hard topics, the interesting topics,
and (most important) letting the students practice.

I released the book under theGNUFree Documentation License, which allows users
to copy, modify, and distribute the book.
What happened next is the cool part. Jeff Elkner, a high school teacher in Virginia,
adopted my book and translated it into Python. He sent me a copy of his
translation, and I had the unusual experience of learning Python by reading my own book.

Jeff and I revised the book, incorporated a case study by Chris Meyers, and in 2001
we released How to Think Like a Computer Scientist: Learning with Python, also
under the GNU Free Documentation License. As Green Tea Press, I published the
book and started selling hard copies through Amazon.com and college book stores.
Other books from Green Tea Press are available at greenteapress.com.

In 2003, I started teaching at Olin College, and I got to teach Python for the first time.
The contrast with Java was striking. Students struggled less, learned more, worked
on more interesting projects, and generally had a lot more fun.

Over the last five years I have continued to develop the book, correcting errors,
improving some of the examples, and adding material, especially exercises. In 2008,
I started work on a major revision of the book – at the same time, I was contacted by
an editor at Cambridge University Press who was interested in publishing the next
edition. Good timing!
The result is this book, now with the less grandiose title Python for Software Design.
Some of the changes are:
■ I added a section about debugging at the end of each chapter. These sections
present general techniques for finding and avoiding bugs, and warnings about Python pitfalls.
■ I removed the material in the last few chapters about the implementation of lists
and trees. I still love those topics, but I thought they were incongruent with the rest of the book.
■ I added more exercises, ranging from short tests of understanding to a few substantial projects.
■ I added a series of case studies – longer examples with exercises, solutions, and
discussion. Some of them are based on Swampy, a suite of Python programs I
wrote for use in my classes. Swampy, code examples, 
and some solutions are available from thinkpython.com.
■ I expanded the discussion of program development plans and basic design patterns.
■ The use of Python is more idiomatic. The book is still about programming, not
Python, but now I think the book gets more leverage from the language.
I hope you enjoy working with this book, and that it helps you learn to program and
think, at least a little bit, like a computer scientist.


Table of Contents
Preface page xi
1 The Way of the Program 1
1.1 The Python Programming Language 1
1.2 What Is a Program? 3
1.3 What Is Debugging? 3
1.3.1 Syntax Errors 3
1.3.2 Runtime Errors 4
1.3.3 Semantic Errors 4
1.3.4 Experimental Debugging 4
1.4 Formal and Natural Languages 5
1.5 The First Program 6
1.6 Debugging 7
1.7 Glossary 8
1.8 Exercises 9
2 Variables, Expressions, and Statements 10
2.1 Values and Types 10
2.2 Variables 11
2.3 Variable Names and Keywords 13
2.4 Statements 13
2.5 Operators and Operands 14
2.6 Expressions 15
2.7 Order of Operations 15
2.8 String Operations 16
2.9 Comments 17
2.10 Debugging 17
2.11 Glossary 18
2.12 Exercises 19
3 Functions 21
3.1 Function Calls 21
3.2 Type Conversion Functions 21
3.3 Math Functions 22
3.4 Composition 23
3.5 Adding New Functions 24
3.6 Definitions and Uses 26
3.7 Flow of Execution 26
3.8 Parameters and Arguments 27
3.9 Variables and Parameters Are Local 28
3.10 Stack Diagrams 29
3.11 Fruitful Functions and Void Functions 30
3.12 Why Functions? 31
3.13 Debugging 31
3.14 Glossary 32
3.15 Exercises 33
4 Case Study: Interface Design 35
4.1 TurtleWorld 35
4.2 Simple Repetition 36
4.3 Exercises 37
4.4 Encapsulation 38
4.5 Generalization 39
4.6 Interface Design 40
4.7 Refactoring 41
4.8 A Development Plan 42
4.9 Docstring 43
4.10 Debugging 43
4.11 Glossary 44
4.12 Exercises 44
5 Conditionals and Recursion 46
5.1 Modulus Operator 46
5.2 Boolean Expressions 46
5.3 Logical Operators 47
5.4 Conditional Execution 48
5.5 Alternative Execution 48
5.6 Chained Conditionals 49
5.7 Nested Conditionals 49
5.8 Recursion 50
5.9 Stack Diagrams for Recursive Functions 52
5.10 Infinite Recursion 52
5.11 Keyboard Input 53
5.12 Debugging 54
5.13 Glossary 55
5.14 Exercises 56
6 Fruitful Functions 59
6.1 Return Values 59
6.2 Incremental Development 60
6.3 Composition 63
6.4 Boolean Functions 64
6.5 More Recursion 65
6.6 Leap of Faith 67
6.7 One More Example 67
6.8 Checking Types 68
6.9 Debugging 69
6.10 Glossary 70
6.11 Exercises 71
7 Iteration 73
7.1 Multiple Assignment 73
7.2 Updating Variables 74
7.3 The while Statement 75
7.4 break 76
7.5 Square Roots 77
7.6 Algorithms 79
7.7 Debugging 79
7.8 Glossary 80
7.9 Exercises 80
8 Strings 82
8.1 A String Is a Sequence 82
8.2 len 83
8.3 Traversal with a for Loop 83
8.4 String Slices 85
8.5 Strings Are Immutable 86
8.6 Searching 86
8.7 Looping and Counting 87
8.8 string Methods 87
8.9 The in Operator 89
8.10 String Comparison 89
8.11 Debugging 90
8.12 Glossary 92
8.13 Exercises 92
9 Case Study: Word Play 95
9.1 Reading Word Lists 95
9.2 Exercises 96
9.3 Search 97
9.4 Looping with Indices 99
9.5 Debugging 100
9.6 Glossary 101
9.7 Exercises 101
10 Lists 103
10.1 A List Is a Sequence 103
10.2 Lists Are Mutable 104
10.3 Traversing a List 105
10.4 List Operations 106
10.5 List Slices 106
10.6 List Methods 107
10.7 Map, Filter, and Reduce 108
10.8 Deleting Elements 109
10.9 Lists and Strings 110
10.10 Objects and Values 111
10.11 Aliasing 113
10.12 List Arguments 113
10.13 Debugging 115
10.14 Glossary 116
10.15 Exercises 117
11 Dictionaries 119
11.1 Dictionary as a Set of Counters 121
11.2 Looping and Dictionaries 123
11.3 Reverse Lookup 123
11.4 Dictionaries and Lists 124
11.5 Memos 126
11.6 Global Variables 128
11.7 Long Integers 129
11.8 Debugging 130
11.9 Glossary 131
11.10 Exercises 131
12 Tuples 133
12.1 Tuples Are Immutable 133
12.2 Tuple Assignment 135
12.3 Tuples as Return Values 136
12.4 Variable-Length Argument Tuples 136
12.5 Lists and Tuples 138
12.6 Dictionaries and Tuples 139
12.7 Comparing Tuples 141
12.8 Sequences of Sequences 142
12.9 Debugging 143
12.10 Glossary 144
12.11 Exercises 145
13 Case Study: Data Structure Selection 147
13.1 Word Frequency Analysis 147
13.2 Random Numbers 148
13.3 Word Histogram 149
13.4 Most Common Words 151
13.5 Optional Parameters 152
13.6 Dictionary Subtraction 152
13.7 Random Words 153
13.8 Markov Analysis 154
13.9 Data Structures 155
13.10 Debugging 157
13.11 Glossary 158
13.12 Exercises 158
14 Files 159
14.1 Persistence 159
14.2 Reading and Writing 159
14.3 Format Operator 160
14.4 Filenames and Paths 161
14.5 Catching Exceptions 163
14.6 Databases 164
14.7 Pickling 165
14.8 Pipes 166
14.9 Writing Modules 167
14.10 Debugging 168
14.11 Glossary 169
14.12 Exercises 169
15 Classes and Objects 172
15.1 User-Defined Types 172
15.2 Attributes 173
15.3 Rectangles 174
15.4 Instances as Return Values 176
15.5 Objects Are Mutable 176
15.6 Copying 177
15.7 Debugging 179
15.8 Glossary 179
15.9 Exercises 180
16 Classes and Functions 182
16.1 Time 182
16.2 Pure Functions 183
16.3 Modifiers 184
16.4 Prototyping versus Planning 185
16.5 Debugging 187
16.6 Glossary 188
16.7 Exercises 188
17 Classes and Methods 189
17.1 Object-Oriented Features 189
17.2 Printing Objects 190
17.3 Another Example 192
17.4 A More Complicated Example 192
17.5 The Init Method 193
17.6 The __str__ method 194
17.7 Operator Overloading 195
17.8 Type-Based Dispatch 195
17.9 Polymorphism 197
17.10 Debugging 198
17.11 Glossary 199
17.12 Exercises 199
18 Inheritance 201
18.1 Card Objects 201
18.2 Class Attributes 202
18.3 Comparing Cards 204
18.4 Decks 205
18.5 Printing the Deck 205
18.6 Add, Remove, Shuffle, and Sort 206
18.7 Inheritance 207
18.8 Class Diagrams 209
18.9 Debugging 210
18.10 Glossary 211
18.11 Exercises 212
19 Case Study: Tkinter 214
19.1 GUI 214
19.2 Buttons and Callbacks 215
19.3 Canvas Widgets 216
19.4 Coordinate Sequences 217
19.5 More Widgets 218
19.6 Packing Widgets 220
19.7 Menus and Callables 223
19.8 Binding 223
19.9 Debugging 226
19.10 Glossary 227
19.11 Exercises 228
Appendix 231
Index 241


Bookscreen
e-books shop

Python for Software Design
Python for Software Design is a concise introduction to software design
using the Python programming language. Intended for people with no
programming experience, this book starts with the most basic concepts
and gradually adds new material. Some of the ideas students find most
challenging, like recursion and object-oriented programming, are divided
into a sequence of smaller steps and introduced over the course of several
chapters. The focus is on the programming process, with special emphasis
on debugging. The book includes a wide range of exercises, from short
examples to substantial projects, so that students have ample opportunity
to practice each new concept.
Exercise solutions and code examples along with Swampy, a suite of
Python programs that is used in some of the exercises, are available from

Allen B. Downey, Ph.D., is an Associate Professor of Computer Science
at the Olin College of Engineering in Needham, Massachusetts. He
has taught at Wellesley College, Colby College, and UC Berkeley. He
has a doctorate in computer science from UC Berkeley and a master’s
degree from MIT. Professor Downey is the author of a previous version
of this book, titled How to Think Like a Computer Scientist: Learning with
Python, which he self-published in 2001.
Previous Post Next Post