,Contents
Contents 8
Foreword 13
1 Introduction 20
1.1 Why This Book? . . . . . . . . . . . . . . . . . . . . 21
1.2 About Real Python . . . . . . . . . . . . . . . . . . 23
1.3 How to Use This Book . . . . . . . . . . . . . . . . . 24
1.4 Bonus Material and Learning Resources . . . . . . . 25
2 Setting Up Python 29
2.1 A Note on Python Versions . . . . . . . . . . . . . . 30
2.2 Windows . . . . . . . . . . . . . . . . . . . . . . . 31
2.3 macOS . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.4 Ubuntu Linux . . . . . . . . . . . . . . . . . . . . . 37
3 Your First Python Program 42
3.1 Write a Python Program . . . . . . . . . . . . . . . 43
3.2 Mess Things Up . . . . . . . . . . . . . . . . . . . . 47
3.3 Create a Variable . . . . . . . . . . . . . . . . . . . 50
3.4 Inspect Values in the Interactive Window . . . . . . 55
3.5 Leave Yourself Helpful Notes . . . . . . . . . . . . . 58
3.6 Summary and Additional Resources . . . . . . . . . 60
4 Strings and String Methods 62
4.1 What Is a String? . . . . . . . . . . . . . . . . . . . 63
4.2 Concatenation, Indexing, and Slicing . . . . . . . . . 69
8
, Contents
4.3 Manipulate Strings With Methods . . . . . . . . . . 79
4.4 Interact With User Input . . . . . . . . . . . . . . . 85
4.5 Challenge: Pick Apart Your User’s Input . . . . . . . 88
4.6 Working With Strings and Numbers . . . . . . . . . 88
4.7 Streamline Your Print Statements . . . . . . . . . . 94
4.8 Find a String in a String . . . . . . . . . . . . . . . . 96
4.9 Challenge: Turn Your User Into a L33t H4x0r . . . . 99
4.10 Summary and Additional Resources . . . . . . . . . 100
5 Numbers and Math 102
5.1 Integers and Floating-Point Numbers . . . . . . . . 103
5.2 Arithmetic Operators and Expressions . . . . . . . . 107
5.3 Challenge: Perform Calculations on User Input . . . 115
5.4 Make Python Lie to You . . . . . . . . . . . . . . . . 116
5.5 Math Functions and Number Methods . . . . . . . . 118
5.6 Print Numbers in Style . . . . . . . . . . . . . . . . 123
5.7 Complex Numbers . . . . . . . . . . . . . . . . . . 126
5.8 Summary and Additional Resources . . . . . . . . . 130
6 Functions and Loops 132
6.1 What Is a Function, Really? . . . . . . . . . . . . . . 133
6.2 Write Your Own Functions . . . . . . . . . . . . . . 137
6.3 Challenge: Convert Temperatures . . . . . . . . . . 146
6.4 Run in Circles . . . . . . . . . . . . . . . . . . . . . 147
6.5 Challenge: Track Your Investments . . . . . . . . . . 156
6.6 Understand Scope in Python . . . . . . . . . . . . . 157
6.7 Summary and Additional Resources . . . . . . . . . 162
7 Finding and Fixing Code Bugs 164
7.1 Use the Debug Control Window . . . . . . . . . . . 165
7.2 Squash Some Bugs . . . . . . . . . . . . . . . . . . 171
7.3 Summary and Additional Resources . . . . . . . . . 179
8 Conditional Logic and Control Flow 181
8.1 Compare Values . . . . . . . . . . . . . . . . . . . . 182
8.2 Add Some Logic . . . . . . . . . . . . . . . . . . . . 186
8.3 Control the Flow of Your Program . . . . . . . . . . 194
9
, Contents
8.4 Challenge: Find the Factors of a Number . . . . . . . 206
8.5 Break Out of the Pattern . . . . . . . . . . . . . . . 207
8.6 Recover From Errors . . . . . . . . . . . . . . . . . 211
8.7 Simulate Events and Calculate Probabilities . . . . . 217
8.8 Challenge: Simulate a Coin Toss Experiment . . . . . 223
8.9 Challenge: Simulate an Election . . . . . . . . . . . 223
8.10 Summary and Additional Resources . . . . . . . . . 224
9 Tuples, Lists, and Dictionaries 226
9.1 Tuples Are Immutable Sequences . . . . . . . . . . . 227
9.2 Lists Are Mutable Sequences . . . . . . . . . . . . . 237
9.3 Nesting, Copying, and Sorting Tuples and Lists . . . 251
9.4 Challenge: List of lists . . . . . . . . . . . . . . . . . 257
9.5 Challenge: Wax Poetic . . . . . . . . . . . . . . . . 258
9.6 Store Relationships in Dictionaries . . . . . . . . . . 260
9.7 Challenge: Capital City Loop . . . . . . . . . . . . . 270
9.8 How to Pick a Data Structure . . . . . . . . . . . . . 272
9.9 Challenge: Cats With Hats . . . . . . . . . . . . . . 273
9.10 Summary and Additional Resources . . . . . . . . . 274
10 Object-Oriented Programming (OOP) 276
10.1 Define a Class . . . . . . . . . . . . . . . . . . . . . 277
10.2 Instantiate an Object . . . . . . . . . . . . . . . . . 281
10.3 Inherit From Other Classes . . . . . . . . . . . . . . 287
10.4 Challenge: Model a Farm . . . . . . . . . . . . . . . 296
10.5 Summary and Additional Resources . . . . . . . . . 297
11 Modules and Packages 298
11.1 Working With Modules . . . . . . . . . . . . . . . . 299
11.2 Working With Packages . . . . . . . . . . . . . . . . 310
11.3 Summary and Additional Resources . . . . . . . . . 318
12 File Input and Output 320
12.1 Files and the File System . . . . . . . . . . . . . . . 321
12.2 Working With File Paths in Python . . . . . . . . . . 324
12.3 Common File System Operations . . . . . . . . . . . 333
12.4 Challenge: Move All Image Files to a New Directory . 350
10