Home > Store

Learn Python the Hard Way, 5th Edition

Register your product to gain access to bonus material or receive a coupon.

Learn Python the Hard Way, 5th Edition

Best Value Purchase

Book + eBook Bundle

  • Your Price: $40.91
  • List Price: $65.48
  • Includes EPUB and PDF
  • About eBook Formats
  • This eBook includes the following formats, accessible from your Account page after purchase:

    ePub EPUB The open industry format known for its reflowable content and usability on supported mobile devices.

    Adobe Reader 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.

More Purchase Options


  • Your Price: $31.99
  • List Price: $39.99
  • Usually ships in 24 hours.

eBook (Watermarked)

  • Your Price: $20.39
  • List Price: $25.49
  • Includes EPUB and PDF
  • About eBook Formats
  • This eBook includes the following formats, accessible from your Account page after purchase:

    ePub EPUB The open industry format known for its reflowable content and usability on supported mobile devices.

    Adobe Reader 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.


  • Copyright 2024
  • Dimensions: 7" x 9-1/8"
  • Pages: 352
  • Edition: 5th
  • Book
  • ISBN-10: 0-13-827057-0
  • ISBN-13: 978-0-13-827057-5

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.

  • Install a complete Python environment
  • Organize and write code
  • Fix and break code
  • Basic mathematics
  • Strings and text
  • Interact with users
  • Work with files
  • Looping and logic
  • Object-oriented programming
  • Data structures using lists and dictionaries
  • Modules, classes, and objects
  • Python packaging
  • Automated testing
  • Basic SQL for Data Science
  • Web scraping
  • Fixing bad data (munging)
  • The "Data" part of "Data Science"

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

  • Total beginners with zero programming experience
  • Junior developers who know one or two languages
  • Returning professionals who haven't written code in years
  • Aspiring Data Scientists or academics who need to learn to code
  • Seasoned professionals looking for a fast, simple crash course in Python for Data Science

Register your book for convenient access to downloads, updates, and/or corrections as they become available. See inside book for details.


Author's Site

Visit the author's site at learnpythonthehardway.org.

Sample Content

Online Sample Chapters

A Good First Program

From Dictionaries to Objects

The Five Simple Rules to the Game of Code

Table of Contents

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
Date and Time 294
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


Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020