About This E-Book
EPUB is an open, industry-standard format for e-books. However, support for EPUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the e-book in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Learn Python 3 The Hard Way
A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code
Zed A. Shaw
Boston Columbus Indianapolis New York San Francisco Amsterdam Cape Town
Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City
So Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact .
For questions about sales outside the U.S., please contact .
Visit us on the Web: informit.com/aw
Library of Congress Control Number: 2017940290
Copyright 2017 Zed A. Shaw
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/.
ISBN-13: 978-0-13-469288-3
ISBN-10: 0-13-469288-8
1 17
Contents
Preface
This simple book is meant to get you started in programming. The title says its the hard way to learn to write code, but its actually not. Its only the hard way because it uses a technique called instruction. Instruction is where I tell you to do a sequence of controlled exercises designed to build a skill through repetition. This technique works very well with beginners who know nothing and need to acquire basic skills before they can understand more complex topics. Its used in everything from martial arts to music to even basic math and reading skills.
This book instructs you in Python by slowly building and establishing skills through techniques such as practice and memorization, then applying them to increasingly difficult problems. By the end of the book you will have the tools needed to begin learning more complex programming topics. I like to tell people that my book gives you your programming black belt. What this means is that you know the basics well enough to now start learning programming.
If you work hard, take your time, and build these skills, you will learn to code.
Improvements in the Python 3 Edition
Learn Python 3 the Hard Way now uses Python 3.6. Ive standardized on this version of Python because it has a new, improved string formatting system that is easier to use than the previous 4 (or 3, I forget, there were many) versions. There are a few problems with Python 3.6 for beginners, but I help you navigate these issues in the book. A particularly hairy problem is that Python 3.6 has very poor error messages in some key areas that I help you understand.
I have also improved the videos based on my experiences over the last five years teaching people Python. You can watch these videos online at informit.com/title/9780134692883. In the past the videos simply let you watch me do the exercise. The Python 3 edition videos also show you how to breakand then fixevery exercise. This skill is called debugging. It teaches you how to fix problems you run into but also how Python runs the programs youre creating. The goal of this new methodology is to build a mental model of how Python runs your code so you can more easily figure out why its broken. Youll also learn many useful tricks for debugging broken software.
Last, the Python 3 edition fully supports Microsoft Windows 10 from beginning to end. The previous edition focused mostly on the Unix-style systems such as macOS and Linux, with Windows being more of an afterthought. At the time I started writing the Python 3 edition Microsoft had started to take open source tools and developers seriously, and it was difficult to ignore them as a serious Python development platform. The videos feature Microsoft Windows using Python in various scenarios and also show macOS and Linux for full compatibility. I tell you about any gotchas on each platform, cover installation instructions, and provide any other tips I can give you.
The Hard Way Is Easier
With the help of this book, you will do the incredibly simple things that all programmers do to learn a programming language:
1. Go through each exercise.
2. Type in each file exactly.
3. Make it run.
Thats it. This will be very difficult at first, but stick with it. If you go through this book and do each exercise for one or two hours a night, you will have a good foundation for moving on to another book about Python to continue your studies. This book wont turn you into a programmer overnight, but it will get you started on the path to learning how to code.
This books job is to teach you the three most essential skills that a beginning programmer needs to know: reading and writing, attention to detail, and spotting differences.
Reading and Writing
If you have a problem typing, you will have a problem learning to code, especially if you have a problem typing the fairly odd characters in source code. Without this simple skill you will be unable to learn even the most basic things about how software works.