Table of Contents
[CHAPTER] 1 j INTRODUCTION 1
1.1 | Two Fundamental Ideas ofComputer Science: Algorithms and Information
| Processing 2
I 1,1.1 | Algorithms 2
| 1,1.2 I Information Processing 4
1.1 | Exercises 5
1.2 I The Structure of a Modern Computer System 6
| 1.2.1 '. Computer Hardware 6
j 1.2.2 < Computer Software 8
1.2 | Exercises,.. -10
1.3 I A Not-So-BriefHistory of Computing Systems ..10*
1.3.1 Before Electronic Digital Computers 11
J 1.3.2 i The First Electronic Digital Computers (1940-1950) 15
| 1.3.3 ; The First Programming Languages (1950-1965) 16
1 1.3.4 Integrated Circuits, Interaction, and Timesharing (1965-1975) 18
j 1.3.5 j Personal Computing and Networks (1975-1990) 19
ti 1.3.6 ,' Consultation, Communication, and Ubiquitous ComputingI ; (1990-Present) 21
1.4 I Getting Started with Python Programming 23
J 1.4.1 { Running Code in the Interactive Shell 23
J 1.4.2 : Input, Processing, and Output 25
| 1.4.3 Editing, Saving, and Running a Script 28
I 1,4.4 ; Behind the Scenes: How Python Works 29
1.4 j Exercises... 30
1.5 i Detecting and Correcting Syntax Errors , 31
1.5 Exercises 32
I Suggestions for Further Reading 32
I Summary 33
I Review Questions 35
I Projects 37
[CHAPTER] 2 * SOFTWARE DEVELOPMENT, DATA TYPES, AND
{ EXPRESSIONS 39
2.1 | The Software Development Process 40
2.1 | Exercises 43
2.2 I Case Study: Income 'lax Calculator 43
I 2.2.1 Request 43
| 2.2.2 Analysis 44
f 2.2.3, Design 44
2.2.4 ;: Implementation (Coding) 45v 2.2.5 Testing 46
2.3 § Strings, Assignment, and Comments 47
1 2.3.1 I Data Types 47
I 2.3.2 | String Literals 48
| 2.3.3 | Escape Sequences 50
| 2.3.4 | String Concatenation 50
| 2.3.5 j Variables and the Assignment Statement 51
I 2.3.6 S Program Comments and Docstrings 52
2.3 j Exercises 53
2.4 | Numeric Data Types and Character Sets 54
I 2.4.1 | Integers 54I 2.4.2 | Floating-Point Numbers 55
I 2.4.3 1 Character Sets 55
2.4 1 Exercises 572.5 j Expressions 58
I 2.5.1 | Arithmetic Expressions 58
I 2.5.2 1 Mixed-Mode Arithmetic and Type Conversions 60
2.5 | Exercises 63
2.6 I Using Functions and Modules 63
j 2.6.1 j Calling Functions: Arguments and Return Values 64
j 2.6.2 j The math Module 65
I 2.6.3 I The Main Module 66
j 2.6.4 | Program Format and Structure 67
I 2.6.5 | Running a Script from a Terminal Command Prompt 68
2.6 jj Exercises 70
| Summary 70
| Review Questions 72
| Projects 73
[CHAPTER] 3 !; CONTROL STATEMENTS 75
3.1 j Definite Iteration: The for Loop 76
I 3.1.1 1 Executing a Statement a Given Number ofTimes 76I 3.1.2 I Count-Controlled Loops 77I 3.1.3 j Augmented Assignment 79
I 3.1.4 | Loop Errors: Off-by-One Error 80
I 3.1.5 | Traversing the Contents of a Data Sequence 80
I 3.1.6 I Specifying the Steps in the Range 81
| 3.1.7 I Loops That Count Down 82
3.1 | Exercises 83
3.2 I FormattingText for Output 83
3.2 I Exercises 86
3.3 I Case Study: An Investment Report 873 3.3.1 f Request 87
| 3.3-2 Analysis 87
| 3.3.3 j Design 88
S 3.3.4 | Implementation (Coding) 88I 3.3.5 | Testing 90
3.4 i Selection: if and if-else Statements 91
1 3.4.1 | The Boolean Type, Comparisons, and Boolean Expressions 91
1 3.4.2 1 if-else Statements 92
I 3.4.3 I One-Way Selection Statements 94
| 3.4.4 | Multi-way if Statements 95
| 3.4.5 ; Logical Operators and Compound Boolean Expressions 97
3.4.6 5 Short-Circuit Evaluation 99
3.4.7 ; Testing Selection Statements 100
3.4 | Exercises •101
3.5 | Conditional Iteration: The while Loop 102
j 3.5.1 « The Structure and Behavior of a while Loop 102
3.5.2;
Count Control with a while Loop 104
| 3.5.3 i The while True Loop and the break Statement 105
| 3.5.4 1 Random Numbers 107
| 3.5.5 ,! Loop Logic, Errors, and Testing 109
3.5 Exercises 109
3.6 | Case Study: Approximating Square Roots 110
I 3.6.1 1 Request 110
1 3.6.2 f Analysis 110
J 3.6.3 | Design 110
| 3.6.4 I Implementation (Coding) 112
I 3.6.5 ! Testing 113
£ Summary 113
| Review Questions 116
Projects 118
[CHAPTER] 4 STRINGS AND TEXT FILES 121
4.1 J Accessing Characters and Substrings in Strings 122
| 4.1.1 | The Structure of Strings 122
| 4.1.2 | The Subscript Operator 123
| 4.1.3 }< Slicing for Substrings 124
| 4.1.4 1 Testing for a Substring with the in Operator 125
4.1 1 Exercises 126
4.2 1 Data Encryption 126
4.2 J Exercises 129
4.3 | Strings and Number Systems 129
| 4.3.1 ; The Positional System for Representing Numbers 130
| 4.3.2 J Converting Binary to Decimal 131
| 4.3.3 | Converting Decimal to Binary 132
| 4.3.4 | Conversion Shortcuts 133
| 4.3.5 1 Octal and Hexadecimal Numbers 134
4.3 | Exercises 136
4.4 | StringMethods 136
4.4 | Exercises 140
4.5 J Text Files 141
1 4.5.1 I Text Files and Their Format 141
| 4.5.2 j Writing Text to a File 142
3 4.5.3 } Writing Numbers to a File 142
I 4.5,4 j Reading Text from a File 143
I 4.5.5 | ReadingNumbers from a File 145
| 4.5.6 1 Accessing and Manipulating Files and Directories on Disk 146
[CHAPTER] 5
4.5 §j Exercises 148
4.6 % Case Study: Text Analysis 148
1 4.6.1 } Request 149
1 4.6.2 | Analysis 149
| 4.6.3 t Design 1501 4.6.4 5 Implementation (Coding) 151
| 4.6.5 I Testing 152
| Summary 153
'% Review Questions •154
| Projects 156
LISTS AND DICTIONARIES 159
5.1 | Lists 160
I 5.1.1 jj List Literals and Basic Operators 160
| 5.1.2 j Replacing an Element in a List 163
1 5.1.3 | List Methods for Inserting and Removing Elements 165
I 5.1.4 f Searching a List 167
I 5.1.5 j Sorting a List 168
| 5.1.6 ! Mutator Methods and the Value None 168
| 5.1.7 j Aliasing and Side Effects 169
| 5.1.8 | Equality: Object Identity and Structural Equivalence 171
1 5.1.9 \ Example: Using a List to Find the Median of a Set of Numbers 172
1 5.1.10 | Tuples 173
Exercises 174
Defining Simple Functions 1755.2.1 \ The Syntax of Simple Function Definitions 175
5.2.2 ] Parameters and Arguments 176
5.2.3 | The return Statement 1775.2.4 | Boolean Functions 177
5.2.5 > Defining a main Function 178
Exercises 179Case Study: Generating Sentences 1795.3.1 I Request 179
5.3.2 I Analysis 1795.3.3 | Design 180
5.3.4 | Implementation (Coding) 182
5.3.5 | Testing 183
Dictionaries 183
| 5.4.1 j Dictionary Literals 183
| 5.4.2 | Adding Keys and Replacing Values 184
I 5.4.3 1 Accessing Values 185
| 5.4.4 1 Removing Keys 186
| 5.4.5 I Traversing a Dictionary 186
I 5.4.6| Example: The Hexadecimal System Revisited 188
| 5.4.7 S Example: Finding the Mode of a List ofValues 189
5.4 I Exercises 190
5.1
5.2
5.2
5.3
5.4
5.5 I Case Study: Nondirective Psychotherapy 191I 5.5.1 1 Request 191I 5.5.2 \ Analysis 191
§ 5.5.3 \ Design 1921 5.5.4 'i Implementation (Coding) 193I 5.5.5 I Testing 195
| Summary 195j Review Questions 196| Projects 198
[CHAPTER] 6 j DESIGN WITH FUNCTIONS 201
6.1 | Functions as Abstraction Mechanisms, 202
j 6.1.1 I Functions Eliminate Redundancy 2021 6.1.2 j Functions Hide Complexity 203
I 6.1.3 | Functions Support General Methods with Systematic Variations 2041 6.1.4 t Functions Support die Division of Labor 205
6.1 Exercises205
6.2 I Problem Solving with Top-Down Design 206| 6.2.1 I The Design ofthe Text-Analysis Program 2061 6.2.2 I The Design of the Sentence-Generator Program 207I 6.2.3 I The Design of the Doctor Program 209
6.2 I Exercises210
6.3 1 Design with Recursive Functions 211I 6.3.1 | Defining a Recursive Function 211I 6.3.2 I Tracing a Recursive Function 213
I 6.3.3 | Using Recursive Definitions to Construct Recursive Functions .......214I 6.3.4 I Recursion in Sentence Structure ..214J 6.3.5 j Infinite Recursion 215
| 6.3.6 ! The Costs and Benefits of Recursion 2166.3 | Exercises 2186.4 J Case Study: Gathering Information from a File System 219
I 6.4.1 ! Request 219I 6.4.2 J Analysis 220| 6.4.3 | Design 222
| 6.4.4 j Implementation (Coding) 2246.5 | Managing a Program's Namespace 227
| 6.5.1 | Module Variables, Parameters, and temporary Variables 227I 6.5.2 | Scope 228
6.5.3 | Lifetime 229I 6.5.4 i Default (Keyword) Arguments 230
6.5 I Exercises 2326.6 | Higher-Order Functions (Advanced Topic) 233
| 6.6.1 I Functions as First-Class Data Objects 2336.6.2
'
Mapping 2346.6.3 \ Filtering 2366.6.4 I Reducing 2376.6.5 f| Using lambda to Create Anonymous Functions 2376.6.6 I Creating Jump Tables 238
6.6
[CHAPTER] 77.1
7.1
7.2
7.3
7.3
[CHAPTER] 88.1
Exercises 239
Summary 240
Review Questions 242
Projects 244
SIMPLE GRAPHICS AND IMAGE PROCESSING 247
Simple Graphics 248
7.1.1 ;! Overview ofTurtle Graphics 24871.2 Turtle Operations 249
71.3 | Object Instantiation and the turtle Module 252
7.1.4 s Drawing Two-Dimensional Shapes 25471.5 I Taking a Random Walk 25571.6 5 Colors and the RGB System 256
71.7 | Example: Drawing with Random Colors 25771.8 j Examining an Object's Attributes 259
7.1.9 j Manipulating a Turtle's Screen 259
7.1.10 I Setting up a cfg File and Running IDLE 260
Exercises 261
Case Study: Recursive Patterns in Fractals 262
72.1 | Request 263
72.2 I Analysis 263
72.3 i Design 264
7.2.4 ! Implementation (Coding) 266
Image Processing 26773.1 I Analog and Digital Information 2677.3.2 Sampling and Digitizing Images 268
73.3 I Image File Formats 26873.4 | Image-Manipulation Operations 269
7.3.5 • The Properties ofImages 270
73.6 | The images Module 27073.7 s A Loop Pattern for Traversing a Grid 274
73.8 | A Word on Tuples 275
73.9 | Converting an Image to Black and White 2767.3.10 I Converting an Image to Grayscale 278
73.11 j Copying an Image 279
7.3.12 : Blurring an Image 280
73.13 | Edge Detection 2817.3.14 1 Reducing the Image Size 282
Exercises 284
Summary••• 285
Review Questions 286
Projects 288
DESIGN WITH CLASSES 293
Getting Inside Objects and Classes 2948.1.1 i A First Example: The Student Class 295
8.1.2 i Docstrings 298
8.1.3 1 Method Definitions 298
f 8.1.4 \ The init Method and Instance Variables 2998.1.5 i The _str_ Method 3008.1.6 \ Accessors and Mutators 300
| 8.1.7 : The Lifetime of Objects 301
| 8.1.8 < Rules ofThumb for Defining a Simple Class 3028.1 | Exercises 3038.2 I Case Study: Playing the Game ofCraps 303
I 8.2.1 Request 303I 8.2.2 "'
Analysis 303| 8.2.3 ; Design 304I 8.2.4 ', Implementation (Coding) 306
8.3 I Data-Modeling Examples 309
| 8.3.1 f Rational Numbers 309I 8.3.2 l Rational Number Arithmetic and Operator Overloading 311
| 8.3.3 ! Comparison Methods 312
j| 8.3.4 | Equality and die eq Method..... 314I 8.3.5 * Savings Accounts and Class Variables 315| 8.3.6 J Putting the Accounts into a Bank 317S 8,3.7 \ Using pickle for Permanent Storage of Objects 319| 8.3.8 I Input of Objects and the try-except Statement 320
I 8.3.9 i Playing Cards 3218.3 I Exercises 3258.4 | Case Study: An ATM 325
I 8,4.1 \ Request 325| 8.4.2 i Analysis 325
I 8.4.3 Design 327| 8.4.4 ; Implementation (Coding) 329
8.5 f Structuring Classes with Inheritance and Polymorphism 331| 8.5,1 Inheritance Hierarchies and Modeling 332I 8.5.2 } Example: A Restricted Savings Account 333| 8.5.3
'
Example; The Dealer and a Player in the Game of Blackjack 335| 8.5.4 | Polymorphic Methods 340| 8.5.5 I Abstract Classes 340| 8.5.6 ; The Costs and Benefits ofObject-Oriented Programming.... 341
8.5 Exercises 343| Summary 343
I Review Questions 345| Projects 34d
[CHAPTER] 9 j GRAPHICAL USER INTERFACES 3499.1 | The Behavior ofTerminal-Based Programs and GUI-Based Programs 350
I 9.1.1 | The Terminal-Based Version 350
| 9.1.2 | The GUI-Based Version 351| 9.1,3 | Event-Driven Programming 353
9.1 | Exercises 3559.2 | Coding Simple GUI-Based Programs 355
| 9.2.1 1 Windows and Labels 356
| 9.2.2 j Displaying Images 357| 9.2.3 | Command Buttons and Responding to Events 358
| 9.2.4 { Viewing the Images of Playing Cards 360
I