PEFACE V
SPECIAL FEATURES XVIII
CHAPTER 1 INTRODUCTION 1
1.1 Computer Programs 2
1.2 The Anatomy of a Computer 3
1.3 The Python Programming Language 5
1.4 Becoming Familiar with Your Programming Environment 6
1.5 Analyzing Your First Program 11
1.6 Errors 14
1.7 Problem Solving: Algorithm Design 16
CHAPTER 2 PROGRAMMING WITH NUMBERS AND STRINGS 29
2.1 Variables 30
2.2 Arithmetic 37
2.3 Problem Solving: First Do It By Hand 45
2.4 Strings 48
2.5 Input and Output 55
2.6 Graphics: Simple Drawings 65
CHAPTER 3 DECISIONS 91
3.1 The if Statement 92
3.2 Relational Operators 97
3.3 Nested Branches 106
3.4 Multiple Alternatives 109
3.5 Problem Solving: Flowcharts 112
3.6 Problem Solving: Test Cases 116
3.7 Boolean Variables and Operators 118
3.8 Analyzing Strings 124
3.9 Application: Input Validation 127
CHAPTER 4 LOOPS 155
4.1 The while Loop 156
4.2 Problem Solving: Hand-Tracing 163
4.3 Application: Processing Sentinel Values 166
4.4 Problem Solving: Storyboards 170
4.5 Common Loop Algorithms 173
4.6 The For Loop 177
4.7 Nested Loops 184
4.8 Processing Strings 190
4.9 Application: Random Numbers and Simulations 194
CHAPTER 5 FUNCTIONS 219
5.1 Functions as Black Boxes 220
5.2 Implementing and Testing Functions 222
5.3 Parameter Passing 226
5.4 Return Values 229
5.5 Functions Without Return Values 237
5.6 Problem Solving: Reusable Functions 239
5.7 Problem Solving: Stepwise Refinement 242
5.8 Variable Scope 251
5.9 Recursive Functions (Optional) 258
CHAPTER 6 LISTS 277
6.1 Basic Properties of Lists 278
6.2 List Operations 284
6.3 Common List Algorithms 290
6.4 Using Lists with Functions 297
6.5 Problem Solving: Adapting Algorithms 303
6.6 Problem Solving: Discovering Algorithms by Manipulating Physical Objects 310
6.7 Tables 314
CHAPTER 7 FILES AND EXCEPTIONS 341
7.1 Reading and Writing Text Files 342
7.2 Text Input and Output 346
7.3 Command Line Arguments 357
7.4 Binary Files and Random Access (Optional) 368
7.5 Exception Handling 377
7.6 Application: Handling Input Errors 383
CHAPTER 8 SETS AND DICTIONARIES 403
8.1 Sets 404
8.2 Dictionaries 414
8.3 Complex Structures 424
CHAPTER 9 OBJECTS AND CLASSES 443
9.1 Object-Oriented Programming 444
9.2 Implementing a Simple Class 446
9.3 Specifying the Public Interface of a Class 450
9.4 Designing the Data Representation 452
9.5 Constructors 454
9.6 Implementing Methods 457
9.7 Testing A Class 461
9.8 Problem Solving: Tracing Objects 469
9.9 Problem Solving: Patterns for Object Data 472
9.10 Object References 478
9.11 Application: Writing a Fraction Class 482
CHAPTER 10 INHERITANCE 507
10.1 Inheritance Hierarchies 508
10.2 Implementing Subclasses 513
10.3 Calling the Superclass Constructor 517
10.4 Overriding Methods 521
10.5 Polymorphism 524
10.6 Application: A Geometric Shape Class Hierarchy 538
CHAPTER 11 RECURSION 555
11.1 Triangle Numbers Revisited 556
11.2 Problem Solving: Thinking Recursively 560
11.3 Recursive Helper Functions 565
11.4 The Efficiency of Recursion 566
11.5 Permutations 571
11.6 Backtracking 575
11.7 Mutual Recursion 583
CHAPTER 12 SORTING AND SEARCHING 597
12.1 Selection Sort 598
12.2 Profiling the Selection Sort Algorithm 600
12.3 Analyzing the Performance of the Selection Sort Algorithm 602
12.4 Merge Sort 606
12.5 Analyzing the Merge Sort Algorithm 609
12.6 Searching 614
12.7 Problem Solving: Estimating the Running Time of An Algorithm 617
APPENDICES
Appendix A The Basic Latin And Latin-1 Subsets Of Unicode A-1
Appendix B Python Operator Summary A-4
Appendix C Python Reserved Word Summary A-6
Appendix D The Python Standard Library A-8
Appendix E Binary Numbers and Bit Operations A-29
Glossary G-1
Index I-1
Credits C-1