SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
Also available in other formats.
Register your product to gain access to bonus material or receive a coupon.
You Will Learn Python!
Zed Shaw has created the world's most reliable system for learning Python. Follow it and you will succeed--just like the millions of beginners Zed has taught to date! You bring the discipline, persistence, and attention; the author supplies the masterful knowledge you need to succeed.
In Learn Python the Hard Way, Fifth Edition, you'll learn Python by working through 60 lovingly crafted exercises. Read them. Type in the code. Run it. Fix your mistakes. Repeat. As you do, you'll learn how a computer works, how to solve problems, and how to enjoy programming . . . even when it's driving you crazy.
It'll be frustrating at first. But if you keep trying, you'll get it--and it'll feel amazing! This course will reward you for every minute you put into it. Soon, you'll know one of the world's most powerful, popular programming languages. You'll be a Python programmer.
This Book Is Perfect For
Register your book for convenient access to downloads, updates, and/or corrections as they become available. See inside book for details.
Preface xix
Module 1: Getting Started in Python 1
Exercise 0: Gearing Up 2
General Instructions 2
Minimalist Start 3
Complete Instructions 3
Testing Your Setup 3
Learning the Command Line 4
Next Steps 5
Exercise 1: A Good First Program 6
What You Should See 7
Study Drills 8
Common Student Questions 9
The Blue Plus 9
Exercise 2: Comments and Pound Characters 10
What You Should See 10
Study Drills 10
Common Student Questions 11
Exercise 3: Numbers and Math 12
What You Should See 13
Study Drills 13
Common Student Questions 13
Exercise 4: Variables and Names 16
What You Should See 17
Study Drills 17
Common Student Questions 17
Exercise 5: More Variables and Printing 20
What You Should See 20
Study Drills 21
Common Student Questions 21
Exercise 6: Strings and Text 22
What You Should See 23
Study Drills 23
Break It 23
Common Student Questions 24
Exercise 7: Combining Strings 26
What You Should See 26
Study Drills 26
Break It 27
Common Student Questions 27
Exercise 8: Formatting Strings Manually 28
What You Should See 28
Study Drills 29
Common Student Questions 29
Exercise 9: Multi-Line Strings 30
What You Should See 30
Study Drills 31
Common Student Questions 31
Exercise 10: Escape Codes in Strings 32
What You Should See 33
Escape Sequences 33
Study Drills 34
Common Student Questions 34
Exercise 11: Asking People Questions 36
What You Should See 36
Study Drills 37
Common Student Questions 37
Exercise 12: An Easier Way to Prompt 38
What You Should See 38
Study Drills 38
Common Student Questions 39
Exercise 13: Parameters, Unpacking, Variables 40
Code Description 41
Hold Up! Features Have Another Name 42
What You Should See 42
Study Drills 43
Common Student Questions 43
Exercise 14: Prompting and Passing 46
What You Should See 47
Study Drills 47
Common Student Questions 47
Exercise 15: Reading Files 50
What You Should See 51
Study Drills 51
Common Student Questions 52
Exercise 16: Reading and Writing Files 54
What You Should See 55
Study Drills 55
Common Student Questions 56
Exercise 17: More Files 58
What You Should See 59
Study Drills 59
Common Student Questions 59
Module 2: The Basics of Programming 61
Exercise 18: Names, Variables, Code, Functions 62
Exercise Code 63
What You Should See 65
Study Drills 65
Common Student Questions 66
Exercise 19: Functions and Variables 68
What You Should See 69
Study Drills 70
Common Student Questions 70
Exercise 20: Functions and Files 72
What You Should See 73
Study Drills 73
Common Student Questions 74
Exercise 21: Functions Can Return Something 76
What You Should See 77
Study Drills 77
Common Student Questions 78
Exercise 22: Strings, Bytes, and Character Encodings 80
Initial Research 80
Switches, Conventions, and Encodings 82
Dissecting the Output 84
Dissecting the Code 84
Encodings Deep Dive 86
Breaking It 87
Exercise 23: Introductory Lists 88
Accessing Elements of a List 88
Practicing Lists 89
The Code 89
The Challenge 90
Final Challenge 91
Exercise 24: Introductory Dictionaries 92
Key/Value Structures 92
Combining Lists with Data Objects 93
The Code 94
What You Should See 95
The Challenge 95
Final Challenge 96
Exercise 25: Dictionaries and Functions 98
Step 1: Function Names Are Variables 98
Step 2: Dictionaries with Variables 98
Step 3: Dictionaries with Functions 99
Step 4: Deciphering the Last Line 99
Study Drill 100
Exercise 26: Dictionaries and Modules 102
Step 1: Review of import 102
Step 2: Find the __dict__ 102
Step 3: Change the __dict__ 103
Study Drill: Find the "Dunders" 104
Exercise 27: The Five Simple Rules to the Game of Code 106
Rule 1: Everything Is a Sequence of Instructions 106
Rule 2: Jumps Make the Sequence Non-Linear 108
Rule 3: Tests Control Jumps 110
Rule 4: Storage Controls Tests 111
Rule 5: Input/Output Controls Storage 112
Putting It All Together 113
Exercise 28: Memorizing Logic 116
The Truth Terms 116
The Truth Tables 117
Common Student Questions 119
Exercise 29: Boolean Practice 120
What You Should See 122
Study Drills 122
Common Student Questions 122
Exercise 30: What If 124
What You Should See 124
dis() It 125
Study Drill 125
Common Student Questions 125
Exercise 31: Else and If 126
What You Should See 127
dis() It 127
Study Drills 128
Common Student Questions 128
Exercise 32: Making Decisions 130
What You Should See 131
dis() It 131
Study Drills 132
Common Student Questions 132
Exercise 33: Loops and Lists 134
What You Should See 135
dis() It 136
Study Drills 137
Common Student Questions 137
Exercise 34: While Loops 138
What You Should See 139
dis() It 139
Study Drills 140
Common Student Questions 140
Exercise 35: Branches and Functions 142
What You Should See 143
Study Drills 144
Common Student Questions 144
Exercise 36: Designing and Debugging 146
From Idea to Working Code 146
Rules for If-Statements 149
Rules for Loops 149
Tips for Debugging 149
Homework 150
Exercise 37: Symbol Review 152
Keywords 152
Data Types 153
String Escape Sequences 154
Old-Style String Formats 154
Operators 155
Reading Code 156
Study Drills 157
Common Student Questions 157
Module 3: Applying What You Know 159
Exercise 38: Beyond Jupyter for Windows 160
Why Learn PowerShell? 161
What Is PowerShell? 161
Crash Landing 171
Exercise 39: Beyond Jupyter for macOS/Linux 172
Why Learn Bash or ZSH? 173
What Is Bash? 173
Crash Landing 184
Exercise 40: Advanced Developer Tools 186
Managing conda Environments 186
Adding conda-forge 187
Using pip 188
Using a .condarc 188
General Editing Tips 189
Going Further 189
Exercise 41: A Project Skeleton 190
Activate an Environment 190
Just Use cookiecutter 190
Building Your Project 191
Installing Your Project 191
Testing the Install 192
Remove test-project 192
Common Errors 193
Study Drills 193
Exercise 42: Doing Things to Lists 194
What You Should See 195
What Lists Can Do 196
When to Use Lists 197
Study Drills 197
Common Student Questions 198
Exercise 43: Doing Things to Dictionaries 200
A Dictionary Example 201
What You Should See 203
What Dictionaries Can Do 203
Study Drills 204
Common Student Questions 204
Exercise 44: From Dictionaries to Objects 206
Step 1: Passing a Dict to a Function 206
Step 2: talk inside the Dict 207
Step 3: Closures 208
Step 4: A Person Constructor 209
Study Drills 211
Exercise 45: Basic Object-Oriented Programming 212
Python's People 212
Using dir() and __dict__ 213
About the Dot (.) 214
Terminology 215
A Word on self 216
Study Drills 217
Common Student Questions 217
Exercise 46: Inheritance and Advanced OOP 218
How This Looks in Code 219
About class Name(object) 221
Study Drills 221
Common Student Questions 222
Exercise 47: Basic Object-Oriented Analysis and Design 224
The Analysis of a Simple Game Engine 225
Top Down versus Bottom Up 229
The Code for "Gothons from Planet Percal #25" 230
What You Should See 236
Study Drills 237
Common Student Questions 237
Exercise 48: Inheritance versus Composition 238
What Is Inheritance? 238
The Reason for super() 243
Composition 243
When to Use Inheritance or Composition 245
Study Drill 245
Common Student Questions 246
Exercise 49: You Make a Game 248
Evaluating Your Game 248
Function Style 249
Class Style 249
Code Style 250
Good Comments 250
Evaluate Your Game 250
Exercise 50: Automated Testing 252
What Is the Purpose of Testing? 252
How to Test Efficiently 252
Install PyTest 253
Simple PyTest Demo 254
Running pytest 255
Exceptions and try/except 255
Getting Coverage Reports 256
Study Drills 256
Common Student Questions 257
Module 4: Python and Data Science 259
Exercise 51: What Is Data Munging? 260
Why Data Munging? 261
The Problem 261
The Setup 262
How to Code 262
Process Example 263
Solution Strategies 265
Awesome ETL Tools 266
Study Drills 266
Exercise 52: Scraping Data from the Web 268
Introducing with 268
The Problem 269
The Setup 269
The Clue 270
Awesome Scraping Tools 270
Study Drills 271
Exercise 53: Getting Data from APIs 272
Introducing JSON 272
The Problem 273
The Setup 274
The Clue 274
Awesome API Tools 275
Study Drills 275
Exercise 54: Data Conversion with pandas 276
Introducing Pandoc 276
The Problem 276
The Setup 277
The Clue 277
Study Drills 278
Exercise 55: How to Read Documentation (Featuring pandas) 280
Why Programmer Documentation Sucks 280
How to Actively Read Programmer Docs 281
Step #1: Find the Docs 281
Step #2: Determine Your Strategy 282
Step #3: Code First, Docs Second 283
Step #4: Break or Change the Code 283
Step #5: Take Notes 284
Step #6: Use It on Your Own 284
Step #7: Write About What You Learned 284
Step #8: Whats the Gestalt? 285
Reading My pandas Curriculum 286
Exercise 56: Using Only pandas 288
Make a Project 288
The Problem 288
The Setup 289
Study Drill 289
Exercise 57: The SQL Crash Course 290
What Is SQL? 290
The Setup 291
Fixing and Loading 292
Create, Read, Update, Delete 293
SELECT 293
Date and Time 294
INSERT 295
UPDATE 296
DELETE and Transactions 297
Math, Aggregates, and GROUP BY 298
Python Access 299
Exercise 58: SQL Normalization 300
What Is Normalization? 300
First Normal Form 301
Second Normal Form (2NF) 303
Querying 2NF Data 306
Querying with Joins 308
Study Drills 308
Exercise 59: SQL Relationships 310
One-to-Many (1:M) 310
Many-to-Many (M:M) 311
One-to-One (1:1) 312
Attributed Relations 313
Querying M:M Tables 313
Your Last Study Drill 314
Exercise 60: Advice from an Even Older Programmer 316
Index 318