Table of Contents
Preface xiii
Acknowledgments xix
About this book xxi
1 Getting Started 1
Installing Python 1
Starting Python with IDLE 2
Instructions, please 3
Interacting with Python 5
Time to program 7
Running your first program 8
If something goes wrong 9
Your second program 11
2 Remember This-Memory and Variables 14
Input, processing, output 14
Names 16
What's in a name? 20
Numbers and strings 21
How "variable" are they? 22
The new me 23
3 Basic Math 26
The four basic operations 27
Operators 28
Order of operations 29
Two more operators 30
Really big and really small 33
4 Types of Data 38
Changing types 38
Getting more information: type() 41
Type-conversion errors 42
Using type conversions 42
5 Input 44
raw_input () 45
The print command and the comma 45
Inputting numbers 47
Input from the Web 49
6 GUIs-Graphical User Interfaces 52
What's a GUI? 52
Our first GUI 53
GUI input 54
Pick your flavor 55
The number-guessing game ... again 59
Other GUI pieces 60
7 Decisions, Decisions 62
Testing, testing 62
Indenting 65
Am I seeing double? 65
Other kinds of tests 66
What happens if the test is false? 67
Testing for more than one condition 69
Using "and" 69
Using "or" 70
Using "not" 70
8 Loop the Loop 74
Counting loops 75
Using a counting loop 77
A shortcut-range () 78
A matter of style-loop variable names 80
Counting by steps 82
Counting without numbers 84
While we're on the subject ... 84
Bailing out of a loop-break and continue 85
9 Just for You-Comments 89
Adding comments 89
Single-line comments 90
End-of-line comments 90
Multiline comments 90
Commenting style91
Commenting out 92
10 Game Time 94
Skier 94
11 Nested and Variable Loops 99
Nested loops 99
Variable loops 101
Variable nested loops 102
Even more variable nested loops 103
Using nested loops 105
12 Collecting Things Together-Lists 112
What's a list? 112
Creating a list 113
Adding things to a list 113
What's the dot? 114
Lists can hold anything 114
Getting items from a list 115
"Slicing" a list 116
Modifying items 118
Other ways of adding to a list 118
Deleting from a list 120
Searching a list 121
Looping through a list 122
Sorting lists 123
Mutable and immutable 126
Lists of lists: tables of data 126
13 Functions 131
Functions-the building blocks 131
Calling a function 133
Passing arguments to a function 134
Functions with more than one argument 137
Functions that return a value 139
Variable scope 140
Forcing a global 143
A bit of advice on naming variables 144
14 Objects 146
Objects in the real world 147
Objects in Python 147
Object = attributes + methods 148
What's the dot? 149
Creating objects 149
An example class-HotDog 154
Hiding the data 159
Polymorphism and inheritance 159
Thinking ahead 162
15 Modules 164
What's a module? 164
Why use modules? 164
Buckets of blocks 165
How do we create modules? 165
How do we use modules? 166
Namespaces 167
Standard modules 170
16 Graphics 174
Getting some help-Pygame 174
A Pygame window 175
Drawing in the window 178
Individual pixels 186
Images 190
Let's get moving! 192
Animation 193
Smoother animation 194
Bouncing the ball 196
Wrapping the ball 198
17 Sprites and Collision Detection 202
Sprites 202
Bump! Collision detection 208
Counting time 212
18 A New Kind of Input-Events 217
Events 217
Keyboard events 219
Mouse events 223
Timer events 225
Time for another game-PyPong 227
19 Sound 239
More help from Pygame-mixer 239
Making sounds versus playing sounds 240
Playing sounds 240
Controlling volume 243
Repeating music 245
Adding sounds to PyPong 245
More wacky sounds 246
Adding music to PyPong 250
20 More GUIs 254
Working with PythonCard 254
Components 255
Making our GUI do something 258
The return of event handlers 259
Moving the button 260
More useful GUIs 260
TempGUI 261
What's on the menu? 266
21 Print Formatting and Strings 273
New lines 274
Horizontal spacing-tabs 275
Inserting variables in strings 277
Number formatting 278
Strings 'n' things 282
22 File Input and Output 290
What's a file? 291
Filenames 291
File locations 292
Opening a file 296
Reading a file 297
Text files and binary files 299
Writing to a file 300
Saving your stuff in files: pickle 303
Game time again-Hangman 305
23 Take a Chance-Randomness 313
What's randomness? 313
Rolling the dice 314
Creating a deck of cards 319
Crazy Eights 323
24 Computer Simulations 336
Modeling the real world 336
Lunar Lander 337
Keeping time 342
Time objects 343
Saving time to a file 347
Virtual Pet 349
25 What's Next? 358
General programming 358
Python 359
Game programming and Pygame 359
Other Python stuff 360
Look around 362
Appendix A Variable Naming Rules 363
Answers to Self-Test Questions 365
Chapter 1 Getting Started 365
Chapter 2 Remember This-Memory and Variables 366
Chapter 3 Basic Math 367
Chapter 4 Types of Data 368
Chapter 5 Input 369
Chapter 6 GUIs-Graphical User Interfaces 371
Chapter 7 Decisions, Decisions 372
Chapter 8 Loop the Loop 374
Chapter 9 Just for You-Comments 375
Chapter 10 Game Time 376
Chapter 11 Nested and Variable Loops 376
Chapter 12 Collecting Things Together-Lists 377
Chapter 13 Functions 379
Chapter 14 Objects 380
Chapter 15 Modules 382
Chapter 16 Graphics 383
Chapter 17 Sprites and Collision Detection 385
Chapter 18 A New Kind of Input-Events 385
Chapter 19 Sound 386
Chapter 20 More GUIs 386
Chapter 21 Print Formatting and Strings 387
Chapter 22 File Input and Output 388
Chapter 23 Take a Chance-Randomness 390
Chapter 24 Computer Simulations 391
Index 393