Contents


Preface
Acknowledgments
About this book
 
1 Getting Started
Installing Python
Starting Python with IDLE
Instructions, please
Interacting with Python
Time to program
Running your first program
If something goes wrong
Your second program
2 Remember This—Memory and Variables
Input, processing, output
Names
What’s in a name?
Numbers and strings
How “variable” are they?
The new me
3 Basic Math
The four basic operations
Operators
Order of operations
Two more operators
Really big and really small
4 Types of Data
Changing types
Getting more information: type()
Type-conversion errors
Using type conversions
5 Input
raw_input()
The print command and the comma
Inputting numbers
Input from the Web
6 GUIs—Graphical User Interfaces
What’s a GUI?
Our first GUI
GUI input
Pick your flavor
The number-guessing game . . . again
Other GUI pieces 60
7 Decisions, Decisions
Testing, testing
Indenting
Am I seeing double?
Other kinds of tests
What happens if the test is false?
Testing for more than one condition
Using “and”
Using “or”
Using “not”
8 Loop the Loop
Counting loops
Using a counting loop
A shortcut—range()
A matter of style—loop variable names
Counting by steps
Counting without numbers
While we’re on the subject . . .
Bailing out of a loop—break and continue
9 Just for You—Comments
Adding comments
Single-line comments
End-of-line comments
Multiline comments
Commenting style
Commenting out
10 Game Time
Skier
11 Nested and Variable Loops
Nested loops
Variable loops
Variable nested loops
Even more variable nested loops
Using nested loops
12 Collecting Things Together—Lists
What’s a list?
Creating a list
Adding things to a list
What’s the dot?
Lists can hold anything
Getting items from a list
“Slicing” a list
Modifying items
Other ways of adding to a list
Deleting from a list
Searching a list
Looping through a list
Sorting lists
Mutable and immutable
Lists of lists: tables of data
13 Functions
Functions—the building blocks
Calling a function
Passing arguments to a function
Functions with more than one argument
Functions that return a value
Variable scope
Forcing a global
A bit of advice on naming variables
14 Objects
Objects in the real world
Objects in Python
Object = attributes + methods
What’s the dot?
Creating objects
An example class—HotDog
Hiding the data
Polymorphism and inheritance
Thinking ahead
15 Modules
What’s a module?
Why use modules?
Buckets of blocks
How do we create modules?
How do we use modules?
Namespaces
Standard modules
16 Graphics
Getting some help—Pygame
A Pygame window
Drawing in the window
Individual pixels
Images
Let’s get moving!
Animation
Smoother animation
Bouncing the ball
Wrapping the ball
17 Sprites and Collision Detection
Sprites
Bump! Collision detection
Counting time
18 A New Kind of Input—Events
Events
Keyboard events
Mouse events
Timer events
Time for another game—PyPong
19 Sound
More help from Pygame—mixer
Making sounds versus playing sounds
Playing sounds
Controlling volume
Repeating music
Adding sounds to PyPong
More wacky sounds
Adding music to PyPong
20 More GUIs
Working with PythonCard
Components
Making our GUI do something
The return of event handlers
Moving the button
More useful GUIs
TempGUI
What’s on the menu?
21 Print Formatting and Strings
New lines
Horizontal spacing—tabs
Inserting variables in strings
Number formatting
Strings ’n’ things
22 File Input and Output
What’s a file?
Filenames
File locations
Opening a file
Reading a file
Text files and binary files
Writing to a file
Saving your stuff in files: pickle
Game time again—Hangman
23 Take a Chance—Randomness
What’s randomness?
Rolling the dice
Creating a deck of cards
Crazy Eights
24 Computer Simulations
Modeling the real world
Lunar Lander
Keeping time
Time objects
Saving time to a file
Virtual Pet
25 What’s Next?
General programming
Python
Game programming and Pygame
Other Python stuff
Look around

Appendix: Variable Naming Rules
Answers to Self-Test Questions
Index