Acknowledgments xxix
Introduction xxxi
Chapter 1: Programming Basics and Strings 1
How Programming Is Different from Using a Computer 1
Programming Is Consistency 2
Programming Is Control 2
Programming Copes with Change 2
What All That Means Together 3
The First Steps 3
Starting codeEditor 3
Using codeEditor’s Python Shell 4
Try It Out: Starting the Python Shell 4
Beginning to Use Python — Strings 5
What Is a String? 5
Why the Quotes? 6
Try It Out: Entering Strings with Different Quotes 6
Understanding Different Quotes 6
Putting Two Strings Together 8
Try It Out: Using + to Combine Strings 8
Putting Strings Together in Different Ways 9
Try It Out: Using a Format Specifier to Populate a String 9
Try It Out: More String Formatting 9
Displaying Strings with Print 10
Try It Out: Printing Text with Print 10
Summary 10
Exercises 11
Chapter 2: Numbers and Operators 13
Different Kinds of Numbers 13
Numbers in Python 14
Try It Out: Using Type with Different Numbers 14
Try It Out: Creating an Imaginary Number 15
Contents
Program Files 15
Try It Out: Using the Shell with the Editor 16
Using the Different Types 17
Try It Out Including Different Numbers in Strings 18
Try It Out: Escaping the % Sign in Strings 18
Basic Math 19
Try It Out Doing Basic Math 19
Try It Out: Using the Modulus Operation 20
Some Surprises 20
Try It Out: Printing the Results 21
Using Numbers 21
Order of Evaluation 21
Try It Out: Using Math Operations 21
Number Formats 22
Try It Out: Using Number Formats 22
Mistakes Will Happen 23
Try It Out: Making Mistakes 23
Some Unusual Cases 24
Try It Out: Formatting Numbers as Octal and Hexadecimal 24
Summary 24
Exercises 25
Chapter 3: Variables — Names for Values 27
Referring to Data – Using Names for Data 27
Try It Out: Assigning Values to Names 28
Changing Data Through Names 28
Try It Out: Altering Named Values 29
Copying Data 29
Names You Can’t Use and Some Rules 29
Using More Built-in Types 30
Tuples — Unchanging Sequences of Data 30
Try It Out: Creating and Using a Tuple 30
Try It Out: Accessing a Tuple Through Another Tuple 31
Lists — Changeable Sequences of Data 33
Try It Out Viewing the Elements of a List 33
Dictionaries — Groupings of Data Indexed by Name 34
Try It Out: Making a Dictionary 34
Try It Out: Getting the Keys from a Dictionary 35
Treating a String Like a List 36
Special Types 38
Contents
Other Common Sequence Properties 38
Referencing the Last Elements 38
Ranges of Sequences 39
Try It Out: Slicing Sequences 39
Growing Lists by Appending Sequences 40
Using Lists to Temporarily Store Data 40
Try It Out: Popping Elements from a List 40
Summary 41
Exercises 42
Chapter 4: Making Decisions 43
Comparing Values — Are They the Same? 43
Try It Out: Comparing Values for Sameness 43
Doing the Opposite — Not Equal 45
Try It Out: Comparing Values for Difference 45
Comparing Values — Which One Is More? 45
Try It Out: Comparing Greater Than and Less Than 45
More Than or Equal, Less Than or Equal 47
Reversing True and False 47
Try It Out: Reversing the Outcome of a Test 47
Looking for the Results of More Than One Comparison 48
How to Get Decisions Made 48
Try It Out: Placing Tests within Tests 49
Repetition 51
How to Do Something — Again and Again 51
Try It Out: Using a while Loop 51
Stopping the Repetition 52
Try It Out: Using else While Repeating 54
Try It Out: Using continue to Keep Repeating 54
Handling Errors 55
Trying Things Out 55
Try It Out: Creating an Exception with Its Explanation 56
Summary 57
Exercises 58
Chapter 5: Functions 59
Putting Your Program into Its Own File 59
Try It Out: Run a Program with Python -i 61