Home > Store

Learn Python the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code, 3rd Edition

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

Learn Python the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code, 3rd Edition

eBook (Watermarked)

  • Your Price: $21.75
  • List Price: $27.19
  • 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 2014
  • Dimensions: 7" x 9-1/8"
  • Pages: 320
  • Edition: 3rd
  • eBook (Watermarked)
  • ISBN-10: 0-13-312432-0
  • ISBN-13: 978-0-13-312432-3

You Will Learn Python!

Zed Shaw has perfected the world's best system for learning Python. Follow it and you will succeed-just like the hundreds of thousands of beginners Zed has taught to date! You bring the discipline, commitment, and persistence; the author supplies everything else.

In Learn Python the Hard Way, Third Edition, you'll learn Python by working through 52 brilliantly crafted exercises. Read them. Type their code precisely. (No copying and pasting!) Fix your mistakes. Watch the programs run. As you do, you'll learn how software works; what good programs look like; how to read, write, and think about code; and how to find and fix your mistakes using tricks professional programmers use. Most importantly, you'll learn the following, which you need to start writing excellent Python software of your own:

  • Installing a complete Python environment
  • Organizing and writing code
  • Basic mathematics
  • Variables
  • Strings and text
  • Interacting with users
  • Working with files
  • Looping and logic
  • Data structures using lists and dictionaries
  • Program design
  • Object-oriented programming
  • Inheritance and composition
  • Modules, classes, and objects
  • Python packaging
  • Debugging
  • Automated testing
  • Basic game development
  • Basic web development

It'll be hard at first. But soon, you'll just get it-and that will feel great!

This tutorial 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.

Watch Zed, too! The accompanying DVD contains 5+ hours of passionate, powerful teaching: a complete Python video course!

Sample Content

Table of Contents

Preface 1
Acknowledgments 1
The Hard Way Is Easier 1
Do Not Copy-Paste 2
A Note on Practice and Persistence 3
A Warning for the Smarties 3


Exercise 0: The Setup 6
Mac OSX 6
Windows 7
Linux 9
Warnings for Beginners 10

Exercise 1: A Good First Program 12
What You Should See 14
Study Drills 15
Common Student Questions 16

Exercise 2: Comments and Pound Characters 18
What You Should See 18
Study Drills 18
Common Student Questions 19

Exercise 3: Numbers and Math 20
What You Should See 21
Study Drills 21
Common Student Questions 22

Exercise 4: Variables and Names 24
What You Should See 25
Study Drills 25
Common Student Questions 25

Exercise 5: More Variables and Printing 28
What You Should See 28
Study Drills 29
Common Student Questions 29

Exercise 6: Strings and Text 30
What You Should See 31
Study Drills 31
Common Student Questions 31

Exercise 7: More Printing. 32
What You Should See 32
Study Drills 32
Common Student Questions 33

Exercise 8: Printing, Printing 34
What You Should See 34
Study Drills 34
Common Student Questions 34

Exercise 9: Printing, Printing, Printing 36
What You Should See 36
Study Drills 36
Common Student Questions 37

Exercise 10: What Was That? 38
What You Should See 39
Escape Sequences 39
Study Drills 40
Common Student Questions 40

Exercise 11: Asking Questions 42
What You Should See 42
Study Drills 43
Common Student Questions 43

Exercise 12: Prompting People 44
What You Should See 44
Study Drills 44
Common Student Questions 45

Exercise 13: Parameters, Unpacking, Variables 46
Hold Up! Features Have Another Name 46
What You Should See 47
Study Drills 48
Common Student Questions 48

Exercise 14: Prompting and Passing 50
What You Should See 50
Study Drills 51
Common Student Questions 51

Exercise 15: Reading Files 54
What You Should See 55
Study Drills 55
Common Student Questions 56

Exercise 16: Reading and Writing Files 58
What You Should See 59
Study Drills 59
Common Student Questions 60

Exercise 17: More Files 62
What You Should See 63
Study Drills 63
Common Student Questions 63

Exercise 18: Names, Variables, Code, Functions 66
What You Should See 67
Study Drills 68
Common Student Questions 68

Exercise 19: Functions and Variables 70
What You Should See 71
Study Drills 71
Common Student Questions 71

Exercise 20: Functions and Files 74
What You Should See 75
Study Drills 75
Common Student Questions 75

Exercise 21: Functions Can Return Something 78
What You Should See 79
Study Drills 79
Common Student Questions 80

Exercise 22: What Do You Know So Far? 81
What You Are Learning 81

Exercise 23: Read Some Code 82


Exercise 24: More Practice 84
What You Should See 85
Study Drills 85
Common Student Questions 85

Exercise 25: Even More Practice 86
What You Should See 87
Study Drills 88
Common Student Questions 89

Exercise 26: Congratulations, Take a Test! 90
Common Student Questions 90

Exercise 27: Memorizing Logic 92
The Truth Terms 92
The Truth Tables 93
Common Student Questions 94

Exercise 28: Boolean Practice 96
What You Should See 98
Study Drills 98
Common Student Questions 98

Exercise 29: What If 100
What You Should See 100
Study Drills 101
Common Student Questions 101

Exercise 30: Else and If 102
What You Should See 103
Study Drills 103
Common Student Questions 103

Exercise 31: Making Decisions 104
What You Should See 105
Study Drills 105
Common Student Questions 105

Exercise 32: Loops and Lists 106
What You Should See 107
Study Drills 108
Common Student Questions 108

Exercise 33: While-Loops 110
What You Should See 111
Study Drills 111
Common Student Questions 112

Exercise 34: Accessing Elements of Lists 114
Study Drills 115

Exercise 35: Branches and Functions 116
What You Should See 117
Study Drills 118
Common Student Questions 118

Exercise 36: Designing and Debugging 120
Rules for If-Statements 120
Rules for Loops 120
Tips for Debugging 121
Homework 121

Exercise 37: Symbol Review 122
Keywords 122
Data Types 123
String Escape Sequences 124
String Formats 124
Operators 125
Reading Code 126
Study Drills 127
Common Student Questions 127

Exercise 38: Doing Things to Lists 128
What You Should See 129
Study Drills 130
Common Student Questions 130

Exercise 39: Dictionaries, Oh Lovely Dictionaries 132
What You Should See 134
Study Drills 135
Common Student Questions 135

Exercise 40: Modules, Classes, and Objects 138
Modules Are Like Dictionaries 138
What You Should See 142
Study Drills 142
Common Student Questions 143

Exercise 41: Learning to Speak Object Oriented 144
Word Drills 144
Phrase Drills 144
Combined Drills 145
A Reading Test 145
Practice English to Code 147
Reading More Code 148
Common Student Questions 148

Exercise 42: Is-A, Has-A, Objects, and Classes 150
How This Looks in Code 151
About class Name(object) 153
Study Drills 153
Common Student Questions 154

Exercise 43: Basic Object-Oriented Analysis and Design 156
The Analysis of a Simple Game Engine 157
Top Down vs. Bottom Up 161
The Code for "Gothons from Planet Percal #25" 162
What You Should See 167
Study Drills 168
Common Student Questions 168

Exercise 44: Inheritance vs. Composition 170
What Is Inheritance? 170
The Reason for super() 175
Composition 176
When to Use Inheritance or Composition 177
Study Drills 177
Common Student Questions 178

Exercise 45: You Make a Game 180
Evaluating Your Game 180
Function Style 181
Class Style 181
Code Style 182
Good Comments 182
Evaluate Your Game 183

Exercise 46: A Project Skeleton 184
Installing Python Packages 184
Creating the Skeleton Project Directory 185
Testing Your Setup 187
Using the Skeleton 188
Required Quiz 188
Common Student Questions 189

Exercise 47: Automated Testing 190
Writing a Test Case 190
Testing Guidelines 192
What You Should See 192
Study Drills 193
Common Student Questions 193

Exercise 48: Advanced User Input 194
Our Game Lexicon 194
What You Should Test 196
Design Hints 198
Study Drills 198
Common Student Questions 198

Exercise 49: Making Sentences 200
Match and Peek 200
The Sentence Grammar 201
A Word on Exceptions 203
What You Should Test 204
Study Drills 204
Common Student Questions 204

Exercise 50: Your First Website 206
Installing lpthw.web 206
Make a Simple "Hello World" Project 207
What's Going On? 208
Fixing Errors 209
Create Basic Templates 209
Study Drills 211
Common Student Questions 211

Exercise 51: Getting Input from a Browser 214
How the Web Works 214
How Forms Work 216
Creating HTML Forms 218
Creating a Layout Template 220
Writing Automated Tests for Forms 221
Study Drills 223
Common Student Questions 224

Exercise 52: The Start of Your Web Game 226
Refactoring the Exercise 43 Game 226
Sessions and Tracking Users 231
Creating an Engine 232
Your Final Exam 235
Common Student Questions 236

Next Steps 237
How to Learn Any Programming Language 238

Advice from an Old Programmer 241

Appendix: Command Line Crash Course 243
Introduction: Shut Up and Shell 243
Exercise 1: The Setup 245
Exercise 2: Paths, Folders, Directories (pwd) 248
Exercise 3: If You Get Lost 250
Exercise 4: Make a Directory (mkdir) 250
Exercise 5: Change Directory (cd) 252
Exercise 6: List Directory (ls) 256
Exercise 7: Remove Directory (rmdir) 260
Exercise 8: Move Around (pushd, popd) 262
Exercise 9: Make Empty Files (Touch, New-Item) 265
Exercise 10: Copy a File (cp) 266
Exercise 11: Move a File (mv) 269
Exercise 12: View a File (less, MORE) 271
Exercise 13: Stream a File (cat) 272
Exercise 14: Remove a File (rm) 273
Exercise 15: Exit Your Terminal (exit) 275
Command Line Next Steps 276

Index 279


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