Startseite Learn Python the Hard Way, 3rd Edition: A Very Simple Introduction to the Terrifyingly Beautiful World..

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

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.
Jahr: 2013
Verlag: Addison-Wesley
Sprache: english
Seiten: 320
ISBN 13: 978-0-321-88491-6
File: PDF, 3.94 MB
Download (pdf, 3.94 MB)
Lese Bücher online

You may be interested in

 

Most frequently terms

 
 
You can write a book review and share your experiences. Other readers will always be interested in your opinion of the books you've read. Whether you've loved the book or not, if you give your honest and detailed thoughts then people will find new books that are right for them.
www.it-ebooks.info

LEARN PYTHON
THE HARD WAY
Third Edition

www.it-ebooks.info

Zed Shaw’s Hard Way Series

Visit informit.com/hardway for a complete list of available publications.

Z

ed Shaw’s Hard Way Series emphasizes instruction and making things as
the best way to get started in many computer science topics. Each book in the

series is designed around short, understandable exercises that take you through
a course of instruction that creates working software. All exercises are thoroughly
tested to verify they work with real students, thus increasing your chance of
success. The accompanying video walks you through the code in each exercise.
Zed adds a bit of humor and inside jokes to make you laugh while you’re learning.

Make sure to connect with us!
informit.com/socialconnect

www.it-ebooks.info

LEARN PYTHON
THE HARD WAY
A Very Simple Introduction
to the Terrifyingly Beautiful World
of Computers and Code

Third Edition

Zed A. Shaw

Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City

www.it-ebooks.info

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.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special
sales, which may include electronic versions and/or custom covers and content particular to your business,
training goals, marketing foc; us, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
corpsales@pearsontechgroup.com
For sales outside the United States, please contact:
International Sales
international@pearson.com
Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Shaw, Zed.
Learn Python the hard way : a very simple introduction to the terrifyingly beautiful world of computers and
code / Zed A. Shaw.—Third edition.
pages cm
Includes index.
ISBN 978-0-321-88491-6 (paperback : alkaline paper)
1. Python (Computer program language) 2. Python (Computer program language)—Problems, exercises,
etc. 3. Computer programming—Problems, exercises, etc. I. Title.
QA76.73.P98S53 2014
005.13'3—dc23
2013029738
Copyright © 2014 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. To obtain permission to use material from this work, please submit a written request to Pearson
Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may
fax your request to (201) 236-3290.
ISBN-13: 978-0-321-88491-6
ISBN-10:
0-321-88491-4
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.
First printing, September 2013

www.it-ebooks.info

V

Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The Hard Way Is Easier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Reading and Writing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Attention to Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Spotting Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
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
OSX: What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Windows: What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Linux: What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
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

www.it-ebooks.info

vi

CONTENTS

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

www.it-ebooks.info

CONTENTS

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

www.it-ebooks.info

vii

viii

CONTENTS

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

www.it-ebooks.info

CONTENTS

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

www.it-ebooks.info

ix

x

CONTENTS

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
Classes Are Like Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Objects Are Like Mini-Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Getting Things from Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
A First-Class Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
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

www.it-ebooks.info

CONTENTS

Exercise 43 Basic Object-Oriented Analysis and Design . . . . . . . . . . . . 156
The Analysis of a Simple Game Engine . . . . . . . . . . . . . . . . . . . . . . . 157
Write or Draw about the Problem . . . . . . . . . . . . . . . . . . . . . . . . 157
Extract Key Concepts and Research Them . . . . . . . . . . . . . . . . . . 158
Create a Class Hierarchy and Object Map for the Concepts . . . . 158
Code the Classes and a Test to Run Them . . . . . . . . . . . . . . . . . . 159
Repeat and Refine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
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
Implicit Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Override Explicitly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Alter Before or After . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
All Three Combined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
The Reason for super() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Using super() with __init__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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

www.it-ebooks.info

xi

xii

CONTENTS

Final Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
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
Breaking Up a Sentence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Lexicon Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Scanning Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Exceptions and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
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

www.it-ebooks.info

CONTENTS

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
How to Use This Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
You Will Be Memorizing Things . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Exercise 1: The Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Exercise 2: Paths, Folders, Directories (pwd) . . . . . . . . . . . . . . . . . . . 248
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Exercise 3: If You Get Lost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

www.it-ebooks.info

xiii

xiv

CONTENTS

Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Exercise 4: Make a Directory (mkdir) . . . . . . . . . . . . . . . . . . . . . . . . . 250
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Exercise 5: Change Directory (cd). . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Exercise 6: List Directory (ls) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Exercise 7: Remove Directory (rmdir). . . . . . . . . . . . . . . . . . . . . . . . . 260
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Exercise 8: Move Around (pushd, popd) . . . . . . . . . . . . . . . . . . . . . . 262
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Exercise 9: Make Empty Files (Touch, New-Item) . . . . . . . . . . . . . . . 265
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Exercise 10: Copy a File (cp) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Exercise 11: Move a File (mv) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

www.it-ebooks.info

CONTENTS

Exercise 12: View a File (less, MORE) . . . . . . . . . . . . . . . . . . . . . . . . . 271
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Exercise 13: Stream a File (cat) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Exercise 14: Remove a File (rm) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Exercise 15: Exit Your Terminal (exit) . . . . . . . . . . . . . . . . . . . . . . . . . 275
Do This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
You Learned This . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Do More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Command Line Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Unix Bash References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
PowerShell References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

www.it-ebooks.info

xv

This page intentionally left blank

www.it-ebooks.info

1

Preface

T

his simple book is meant to get you started in programming. The title says it’s the hard way to
learn to write code, but it’s actually not. It’s 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. It’s 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 like
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.

Acknowledgments
I would like to thank Angela for helping me with the first two versions of this book. Without her,
I probably wouldn’t have bothered to finish it at all. She did the copy editing of the first draft and
supported me immensely while I wrote it.
I’d also like to thank Greg Newman for doing the cover art for the first two editions, Brian Shumate for early website designs, and all the people who read previous editions of this book and
took the time to send me feedback and corrections.
Thank 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 sample exactly.

3.

Make it run.

That’s 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

www.it-ebooks.info

2

LEARN PYTHON THE HARD WAY

book. You might not really learn “programming” from this book, but you will learn the foundation skills you need to start learning the language.
This book’s 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
It seems stupidly obvious, but 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.
Typing the code samples and getting them to run will help you learn the names of the symbols,
get you familiar with typing them, and get you reading the language.

Attention to Detail
The one skill that separates bad programmers from good programmers is attention to detail. In
fact, it’s what separates the good from the bad in any profession. Without paying attention to the
tiniest details of your work, you will miss key elements of what you create. In programming, this
is how you end up with bugs and difficult-to-use systems.
By going through this book and copying each example exactly, you will be training your brain to
focus on the details of what you are doing, as you are doing it.

Spotting Differences
A very important skill—which most programmers develop over time—is the ability to visually
notice differences between things. An experienced programmer can take two pieces of code
that are slightly different and immediately start pointing out the differences. Programmers have
invented tools to make this even easier, but we won’t be using any of these. You first have to train
your brain the hard way—then you can use the tools.
While you do these exercises, typing each one in, you will make mistakes. It’s inevitable; even seasoned
programmers make a few. Your job is to compare what you have written to what’s required and fix
all the differences. By doing so, you will train yourself to notice mistakes, bugs, and other problems.

Do Not Copy-Paste
You must type each of these exercises in, manually. If you copy and paste, you might as well just
not even do them. The point of these exercises is to train your hands, your brain, and your mind

www.it-ebooks.info

PREFACE

3

in how to read, write, and see code. If you copy-paste, you are cheating yourself out of the effectiveness of the lessons.

A Note on Practice and Persistence
While you are studying programming, I’m studying how to play guitar. I practice it every day for
at least two hours a day. I play scales, chords, and arpeggios for an hour at least and then learn
music theory, ear training, songs, and anything else I can. Some days I study guitar and music for
eight hours because I feel like it and it’s fun. To me, repetitive practice is natural and is just how
to learn something. I know that to get good at anything you have to practice every day, even if
I suck that day (which is often) or it’s difficult. Keep trying and eventually it’ll be easier and fun.
As you study this book and continue with programming, remember that anything worth doing
is difficult at first. Maybe you are the kind of person who is afraid of failure, so you give up at
the first sign of difficulty. Maybe you never learned self-discipline, so you can’t do anything that’s
“boring.” Maybe you were told that you are “gifted,” so you never attempt anything that might
make you seem stupid or not a prodigy. Maybe you are competitive and unfairly compare yourself
to someone like me who’s been programming for 20+ years.
Whatever your reason for wanting to quit, keep at it. Force yourself. If you run into a Study Drill
you can’t do or a lesson you just do not understand, then skip it and come back to it later. Just
keep going because with programming there’s this very odd thing that happens. At first, you will
not understand anything. It’ll be weird, just like with learning any human language. You will
struggle with words and not know what symbols are what, and it’ll all be very confusing. Then
one day—BANG—your brain will snap and you will suddenly “get it.” If you keep doing the exercises and keep trying to understand them, you will get it. You might not be a master coder, but
you will at least understand how programming works.
If you give up, you won’t ever reach this point. You will hit the first confusing thing (which is
everything at first) and then stop. If you keep trying, keep typing it in, trying to understand it and
reading about it, you will eventually get it.
But if you go through this whole book and you still do not understand how to code, at least you
gave it a shot. You can say you tried your best and a little more and it didn’t work out, but at least
you tried. You can be proud of that.

A Warning for the Smarties
Sometimes people who already know a programming language will read this book and feel I’m
insulting them. There is nothing in this book that is intended to be interpreted as condescending,
insulting, or belittling. I simply know more about programming than my intended readers. If you
think you are smarter than me, then you will feel talked down to and there’s nothing I can do
about that because you are not my intended reader.

www.it-ebooks.info

4

LEARN PYTHON THE HARD WAY

If you are reading this book and flipping out at every third sentence because you feel I’m insulting
your intelligence, then I have three points of advice for you:
1.

Stop reading my book. I didn’t write it for you. I wrote it for people who don’t already
know everything.

2.

Empty before you fill. You will have a hard time learning from someone with more
knowledge if you already know everything.

3.

Go learn Lisp. I hear people who know everything really like Lisp.

For everyone else who’s here to learn, just read everything as if I’m smiling and I have a mischievous little twinkle in my eye.

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

6

EXERCISE 0

The Setup

T

his exercise has no code. It is simply the exercise you complete to get your computer to run
Python. You should follow these instructions as exactly as possible. For example, Mac OSX
computers already have Python 2, so do not install Python 3 (or any Python).

WARNING! If you do not know how to use PowerShell on Windows or the Terminal
on OSX or “Bash” on Linux, then you need to go learn that first. I have included an
abbreviated version of my book The Command Line Crash Course in the appendix. Go
through that first and then come back to these instructions.

Mac OSX
To complete this exercise, complete the following tasks:
1.

Go to http://www.barebones.com/products/textwrangler with your browser, get the
TextWrangler text editor, and install it.

2.

Put TextWrangler (your editor) in your dock so you can reach it easily.

3.

Find your Terminal program. Search for it. You will find it.

4.

Put your Terminal in your dock as well.

5.

Run your Terminal program. It won’t look like much.

6.

In your Terminal program, run python. You run things in Terminal by just typing the
name and hitting RETURN.

7.

Hit CTRL-Z (^Z), Enter, and get out of python.

8.

You should be back at a prompt similar to what you had before you typed python. If not,
find out why.

9.

Learn how to make a directory in the Terminal.

10.

Learn how to change into a directory in the Terminal.

11.

Use your editor to create a file in this directory. You will make the file, “Save” or
“Save As . . . ,” and pick this directory.

12.

Go back to Terminal using just the keyboard to switch windows.

13.

Back in Terminal, see if you can list the directory to see your newly created file.

www.it-ebooks.info

THE SETUP

7

OSX: What You Should See
Here’s me doing this on my computer in Terminal. Your computer would be different, so see if you
can figure out all the differences between what I did and what you should do.
Last login: Sat Apr 24 00:56:54 on ttys001
~ $ python
Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
~ $ mkdir mystuff
~ $ cd mystuff
mystuff $ ls
# ... Use TextWrangler here to edit test.txt....
mystuff $ ls
test.txt
mystuff $

Windows
1.

Go to http://notepad-plus-plus.org with your browser, get the Notepad++ text editor,
and install it. You do not need to be the administrator to do this.

2.

Make sure you can get to Notepad++ easily by putting it on your desktop and/or in
Quick Launch. Both options are available during setup.

3.

Run PowerShell from the Start menu. Search for it and you can just hit Enter to run it.

4.

Make a shortcut to it on your desktop and/or Quick Launch for your convenience.

5.

Run your Terminal program. It won’t look like much.

6.

In your Terminal program, run python. You run things in Terminal by just typing the
name and hitting Enter.
a. If you run python and it’s not there (python is not recognized.), install it from
http://python.org/download.
b. Make sure you install Python 2, not Python 3.
c. You may be better off with ActiveState Python, especially if you do not have administrative rights.
d. If after you install it python still isn’t recognized, then in PowerShell enter this:
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")

e. Close PowerShell and then start it again to make sure Python now runs. If it doesn’t,
restart may be required.

www.it-ebooks.info

8

LEARN PYTHON THE HARD WAY

7.

Type quit() and hit Enter to exit python.

8.

You should be back at a prompt similar to what you had before you typed python. If not,
find out why.

9.

Learn how to make a directory in the Terminal.

10.

Learn how to change into a directory in the Terminal.

11.

Use your editor to create a file in this directory. Make the file, Save or Save As... and
pick this directory.

12.

Go back to Terminal using just the keyboard to switch windows.

13.

Back in Terminal, see if you can list the directory to see your newly created file.

WARNING! If you missed it, sometimes you install Python on Windows and it doesn’t
configure the path correctly. Make sure you enter [Environment]::SetEnvironment
Variable("Path", "$env:Path;C:\Python27", "User") in PowerShell to
configure it correctly. You also have to either restart PowerShell or restart your whole
computer to get it to really be fixed.

Windows: What You Should See
> python
ActivePython 2.6.5.12 (ActiveState Software Inc.) based on
Python 2.6.5 (r265:79063, Mar 20 2010, 14:22:52) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z

> mkdir mystuff
> cd mystuff
... Here you would use Notepad++ to make test.txt in mystuff ...
>
<bunch of unimportant errors if you installed it as non-admin - ignore them - hit Enter>
> dir
Volume in drive C is
Volume Serial Number is 085C-7E02
Directory of C:\Documents and Settings\you\mystuff
04.05.2010
04.05.2010
04.05.2010

23:32
23:32
23:32

<DIR>
<DIR>

.
..
6 test.txt

www.it-ebooks.info

THE SETUP

9

1 File(s)
6 bytes
2 Dir(s) 14 804 623 360 bytes free
>

You will probably see a very different prompt, Python information, and other stuff, but this is the
general idea.

Linux
Linux is a varied operating system with a bunch of different ways to install software. I’m assuming
if you are running Linux then you know how to install packages, so here are your instructions:
1.

Use your Linux package manager and install the gedit text editor.

2.

Make sure you can get to gedit easily by putting it in your window manager’s menu.
a. Run gedit so we can fix some stupid defaults it has.
b. Open Preferences and select the Editor tab.
c. Change Tab width: to 4.
d. Select (make sure a check mark is in) Insert spaces instead of tabs.
e. Turn on Automatic indentation as well.
f. Open the View tab and turn on Display line numbers.

3.

Find your Terminal program. It could be called GNOME Terminal, Konsole, or xterm.

4.

Put your Terminal in your dock as well.

5.

Run your Terminal program. It won’t look like much.

6.

In your Terminal program, run Python. You run things in Terminal by just typing the
name and hitting Enter.
a. If you run Python and it’s not there, install it. Make sure you install Python 2, not
Python 3.

7.

Type quit() and hit Enter to exit Python.

8.

You should be back at a prompt similar to what you had before you typed python. If not,
find out why.

9.

Learn how to make a directory in the Terminal.

10.

Learn how to change into a directory in the Terminal.

11.

Use your editor to create a file in this directory. Typically you will make the file, Save or
Save As..., and pick this directory.

www.it-ebooks.info

10

LEARN PYTHON THE HARD WAY

12.

Go back to Terminal using just the keyboard to switch windows. Look it up if you can’t
figure it out.

13.

Back in Terminal, see if you can list the directory to see your newly created file.

Linux: What You Should See
$ python
Python 2.6.5 (r265:79063, Apr 1 2010, 05:28:39)
[GCC 4.4.3 20100316 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ mkdir mystuff
$ cd mystuff
# ... Use gedit here to edit test.txt ...
$ ls
test.txt
$

You will probably see a very different prompt, Python information, and other stuff, but this is the
general idea.

Warnings for Beginners
You are done with this exercise. This exercise might be hard for you, depending on your familiarity
with your computer. If it is difficult, take the time to read and study and get through it, because
until you can do these very basic things, you will find it difficult to get much programming done.
If a programmer tells you to use vim or emacs, just say “no.” These editors are for when you are a
better programmer. All you need right now is an editor that lets you put text into a file. We will
use gedit, TextWrangler, or Notepad++ (from now on called “the text editor” or “a text editor”)
because it is simple and the same on all computers. Professional programmers use these text editors, so it’s good enough for you starting out.
A programmer may try to get you to install Python 3 and learn that. Say, “When all the Python
code on your computer is Python 3, then I’ll try to learn it.” That should keep him or her busy for
about 10 years.
A programmer will eventually tell you to use Mac OSX or Linux. If the programmer likes fonts and
typography, he’ll tell you to get a Mac OSX computer. If he likes control and has a huge beard,
he’ll tell you to install Linux. Again, use whatever computer you have right now that works. All
you need is an editor, a Terminal, and Python.

www.it-ebooks.info

THE SETUP

11

Finally, the purpose of this setup is so you can do four things very reliably while you work on the
exercises:
1.

Write exercises using your text editor, gedit on Linux, TextWrangler on OSX, or Notepad++
on Windows.

2.

Run the exercises you wrote.

3.

Fix them when they are broken.

4.

Repeat.

Anything else will only confuse you, so stick to the plan.

www.it-ebooks.info

12

EXERCISE 1

A Good First Program

R

emember, you should have spent a good amount of time in Exercise 0, learning how to install
a text editor, run the text editor, run the Terminal, and work with both of them. If you haven’t
done that, then do not go on. You will not have a good time. This is the only time I’ll start an
exercise with a warning that you should not skip or get ahead of yourself.

Type the following text into a single file named ex1.py. This is important, as Python works best
with files ending in .py.
ex1.py
1
2
3
4
5
6
7

print
print
print
print
print
print
print

"Hello World!"
"Hello Again"
"I like typing this."
"This is fun."
'Yay! Printing.'
"I'd much rather you 'not'."
'I "said" do not touch this.'

If you are on Mac OSX, then this is what your text editor might look like if you use TextWrangler:

www.it-ebooks.info

A GOOD FIRST PROGRAM

13

If you are on Windows using Notepad++, then this is what it would look like:

Don’t worry if your editor doesn’t look exactly the same; the key points are as follows:
1.

Notice I did not type the line numbers on the left. Those are printed in the book so I can
talk about specific lines by saying, “See line 5 . . .” You do not type those into Python
scripts.

2.

Notice I have the print at the beginning of the line and how it looks exactly the same
as what I have above. Exactly means exactly, not kind of sort of the same. Every single
character has to match for it to work. But the colors are all different. Color doesn’t matter; only the characters you type.

Then in Terminal, run the file by typing:
python ex1.py

If you did it right, then you should see the same output I have below. If not, you have done something wrong. No, the computer is not wrong.

www.it-ebooks.info

14

LEARN PYTHON THE HARD WAY

What You Should See
On Max OSX in the Terminal, you should see this:

On Windows in PowerShell, you should see this:

www.it-ebooks.info

A GOOD FIRST PROGRAM

15

You may see different names, the name of your computer or other things, before the python ex1.py,
but the important part is that you type the command and see the output is the same as mine.
If you have an error, it will look like this:
$ python ex/ex1.py
File "ex/ex1.py", line 3
print "I like typing this.
^
SyntaxError: EOL while scanning string literal

It’s important that you can read these, since you will be making many of these mistakes. Even I
make many of these mistakes. Let’s look at this line by line.
1.

Here we ran our command in the Terminal to run the ex1.py script.

2.

Python then tells us that the file ex1.py has an error on line 3.

3.

It then prints this line for us.

4.

Then it puts a ^ (caret) character to point at where the problem is. Notice the missing "
(double-quote) character?

5.

Finally, it prints out a SyntaxError and tells us something about what might be the error.
Usually these are very cryptic, but if you copy that text into a search engine, you will find
someone else who’s had that error and you can probably figure out how to fix it.

WARNING! If you are from another country and you get errors about ASCII encodings,
then put this at the top of your Python scripts:
# -*- coding: utf-8 -*-

It will fix them so that you can use Unicode UTF-8 in your scripts without a problem.

Study Drills
Each exercise also contains Study Drills. The Study Drills contain things you should try to do. If you
can’t, skip it and come back later.
For this exercise, try these things:
1.

Make your script print another line.

2.

Make your script print only one of the lines.

3.

Put a “#” (octothorpe) character at the beginning of a line. What did it do? Try to find
out what this character does.

www.it-ebooks.info

16

LEARN PYTHON THE HARD WAY

From now on, I won’t explain how each exercise works unless an exercise is different.

NOTE: An “octothorpe” is also called a “pound,” “hash,” “mesh,” or any number of
names. Pick the one that makes you chill out.

Common Student Questions
These are actual questions by real students in the comments section of the book when it was
online. You may run into some of these, so I’ve collected and answered them for you.
Can I use IDLE?
No, you should use Terminal on OSX and PowerShell on Windows, just like I have here. If you don’t
know how to use those, then you can go read the Command Line Crash Course in the appendix.
How do you get colors in your editor?
Save your file first as a .py file, such as ex1.py. Then you’ll have color when you type.
I get SyntaxError: invalid syntax when I run ex1.py.
You are probably trying to run Python, then trying to type Python again. Close your Terminal, start
it again, and right away type only python ex1.py.
I get can't open file 'ex1.py': [Errno 2] No such file or directory.
You need to be in the same directory as the file you created. Make sure you use the cd command to
go there first. For example, if you saved your file in lpthw/ex1.py, then you would do cd lpthw/
before trying to run python ex1.py. If you don’t know what any of that means, then go through
the Command Line Crash Course (CLI-CC) mentioned in the first question.
How do I get my country’s language characters into my file?
Make sure you type this at the top of your file: # -*- coding: utf-8 -*-.
My file doesn’t run; I just get the prompt back with no output.
You most likely took the previous code literally and thought that print "Hello World!" meant
to literally print just "Hello World!" into the file, without the print. Your file has to be exactly
like mine in the previous code and all the screenshots; I have print "Hello World!" and print
before every line. Make sure your code is like mine and it should work.

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

18

EXERCISE 2

Comments and Pound Characters

C

omments are very important in your programs. They are used to tell you what something does
in English, and they also are used to disable parts of your program if you need to remove them
temporarily. Here’s how you use comments in Python:
ex2.py

1
2
3
4
5
6
7
8
9

# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print "I could have code like this." # and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run."

From now on, I’m going to write code like this. It is important for you to understand that everything does not have to be literal. Your screen and program may visually look different, but what’s
important is the text you type into the file you’re writing in your text editor. In fact, I could work
with any text editor and the results would be the same.

What You Should See
Exercise 2 Session
$ python ex2.py
I could have code like this.
This will run.

Again, I’m not going to show you screenshots of all the Terminals possible. You should understand
that the above is not a literal translation of what your output should look like visually, but the text
between the first $ Python ... and last $ lines will be what you focus on.

Study Drills
1.

Find out if you were right about what the # character does and make sure you know
what it’s called (octothorpe or pound character).

2.

Take your ex2.py file and review each line going backward. Start at the last line, and
check each word in reverse against what you should have typed.

www.it-ebooks.info

COMMENTS AND POUND CHARACTERS

19

3.

Did you find more mistakes? Fix them.

4.

Read what you typed previously out loud, including saying each character by its name.
Did you find more mistakes? Fix them.

Common Student Questions
Are you sure # is called the pound character?
I call it the octothorpe and that is the only name that no country uses and that works in every
country. Every country thinks its way to call this one character is both the most important way to
do it and also the only way it’s done. To me this is simply arrogance and, really, y’all should just
chill out and focus on more important things like learning to code.
If # is for comments, then how come # -*- coding: utf-8 -*- works?
Python still ignores that as code, but it’s used as a kind of “hack” or workaround for problems
with setting and detecting the format of a file. You also find a similar kind of comment for editor
settings.
Why does the # in print "Hi # there." not get ignored?
The # in that code is inside a string, so it will be put into the string until the ending " character is
hit. These pound characters are just considered characters and aren’t considered comments.
How do I comment out multiple lines?
Put a # in front of each one.
I can’t figure out how to type a # character on my country’s keyboard?
Some countries use the Alt key and combinations of those to print characters foreign to their
language. You’ll have to look online in a search engine to see how to type it.
Why do I have to read code backward?
It’s a trick to make your brain not attach meaning to each part of the code, and doing that makes
you process each piece exactly. This catches errors and is a handy error-checking technique.

www.it-ebooks.info

20

EXERCISE 3

Numbers and Math

E

very programming language has some kind of way of doing numbers and math. Do not worry:
programmers lie frequently about being math geniuses when they really aren’t. If they were
math geniuses, they would be doing math, not writing ads and social network games to steal
people’s money.
This exercise has lots of math symbols. Let’s name them right away so you know what they are
called. As you type this one in, say the names. When saying them feels boring, you can stop saying
them. Here are the names:
+

plus

-

minus

/

slash

*

asterisk

%

percent

<

less-than

>

greater-than

<= less-than-equal
>= greater-than-equal
Notice how the operations are missing? After you type in the code for this exercise, go back and
figure out what each of these does and complete the table. For example, + does addition.
ex3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14

print "I will now count my chickens:"
print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4
print "Now I will count the eggs:"
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
print "Is it true that 3 + 2 < 5 - 7?"
print 3 + 2 < 5 - 7
print "What is 3 + 2?", 3 + 2

www.it-ebooks.info

NUMBERS AND MATH

15
16
17
18
19
20
21
22
23

21

print "What is 5 - 7?", 5 - 7
print "Oh, that's why it's False."
print "How about some more."
print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2

What You Should See
Exercise 3 Session
$ python ex3.py
I will now count my chickens:
Hens 30
Roosters 97
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5
What is 5 - 7? -2
Oh, that's why it's False.
How about some more.
Is it greater? True
Is it greater or equal? True
Is it less or equal? False

Study Drills
1.

Above each line, use the # to write a comment to yourself explaining what the line does.

2.

Remember in Exercise 0 when you started Python? Start Python this way again and, using
the above characters and what you know, use Python as a calculator.

3.
4.

Find something you need to calculate and write a new .py file that does it.
Notice the math seems “wrong”? There are no fractions, only whole numbers. Find out
why by researching what a “floating point” number is.

5.

Rewrite ex3.py to use floating point numbers so it’s more accurate (hint: 20.0 is floating
point).

www.it-ebooks.info

22

LEARN PYTHON THE HARD WAY

Common Student Questions
Why is the % character a “modulus” and not a “percent”?
Mostly that’s just how the designers chose to use that symbol. In normal writing, you are correct
to read it as a “percent.” In programming, this calculation is typically done with simple division
and the / operator. The % modulus is a different operation that just happens to use the % symbol.
How does % work?
Another way to say it is “X divided by Y with J remaining.” For example, “100 divided by 16 with
4 remaining.” The result of % is the J part, or the remaining part.
What is the order of operations?
In the United States we use an acronym called PEMDAS, which stands for Parentheses Exponents
Multiplication Division Addition Subtraction. That’s the order Python follows as well.
Why does / (divide) round down?
It’s not really rounding down; it’s just dropping the fractional part after the decimal. Try doing
7.0 / 4.0 and compare it to 7 / 4 and you’ll see the difference.

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

24

EXERCISE 4

Variables and Names
ow you can print things with print and you can do math. The next step is to learn about
variables. In programming, a variable is nothing more than a name for something so you
can use the name rather than the something as you code. Programmers use these variable
names to make their code read more like English and because they have lousy memories. If
they didn’t use good names for things in their software, they’d get lost when they tried to
read their code again.

N

If you get stuck with this exercise, remember the tricks you have been taught so far of finding
differences and focusing on details:
1.

Write a comment above each line explaining to yourself what it does in English.

2.

Read your .py file backward.

3.

Read your .py file out loud, saying even the characters.
ex4.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven

print
print
print
print
print
print

"There are", cars, "cars available."
"There are only", drivers, "drivers available."
"There will be", cars_not_driven, "empty cars today."
"We can transport", carpool_capacity, "people today."
"We have", passengers, "to carpool today."
"We need to put about", average_passengers_per_car, "in each car."

NOTE: The _ in space_in_a_car is called an underscore character. Find out how to
type it if you do not already know. We use this character a lot to put an imaginary
space between words in variable names.

www.it-ebooks.info

VARIABLES AND NAMES

25

What You Should See
Exercise 4 Session
$ python ex4.py
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.

Study Drills
When I wrote this program the first time I had a mistake, and Python told me about it like this:
Traceback (most recent call last):
File "ex4.py", line 8, in <module>
average_passengers_per_car = car_pool_capacity / passenger
NameError: name 'car_pool_capacity' is not defined

Explain this error in your own words. Make sure you use line numbers and explain why.
Here’s more Study Drills:
1.

I used 4.0 for space_in_a_car, but is that necessary? What happens if it’s just 4?

2.

Remember that 4.0 is a “floating point” number. Find out what that means.

3.

Write comments above each of the variable assignments.

4.

Make sure you know what = is called (equals) and that it’s making names for things.

5.

Remember that _ is an underscore character.

6.

Try running Python as a calculator like you did before and use variable names to do your
calculations. Popular variable names are also i, x, and j.

Common Student Questions
What is the difference between = (single-equal) and == (double-equal)?
The = (single-equal) assigns the value on the right to a variable on the left. The == (double-equal)
tests if two things have the same value, and you’ll learn about this in Exercise 27.

www.it-ebooks.info

26

LEARN PYTHON THE HARD WAY

Can we write x=100 instead of x = 100?
You can, but it’s bad form. You should add space around operators like this so that it’s easier to
read.
How can I print without spaces between words in print?
You do it like this: print "Hey %s there." % "you". You will do more of this soon.
What do you mean by “read the file backward”?
Very simple. Imagine you have a file with 16 lines of code in it. Start at line 16, and compare it to
my file at line 16. Then do it again for 15, and so on, until you’ve read the whole file backward.
Why did you use 4.0 for space?
It is mostly so you can then find out what a floating point number is and ask this question. See
the Study Drills.

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

28

EXERCISE 5

More Variables and Printing

N

ow we’ll do even more typing of variables and printing them out. This time we’ll use something called a “format string.” Every time you put " (double-quotes) around a piece of text,
you have been making a string. A string is how you make something that your program might
give to a human. You print them, save them to files, send them to web servers, all sorts of things.

Strings are really handy, so in this exercise you will learn how to make strings that have variables
embedded in them. You embed variables inside a string by using specialized format sequences
and then putting the variables at the end with a special syntax that tells Python, “Hey, this is a
format string, put these variables in there.”
As usual, just type this in even if you do not understand it and make it exactly the same.
ex5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print
print
print
print
print
print

"Let's talk about %s." % my_name
"He's %d inches tall." % my_height
"He's %d pounds heavy." % my_weight
"Actually that's not too heavy."
"He's got %s eyes and %s hair." % (my_eyes, my_hair)
"His teeth are usually %s depending on the coffee." % my_teeth

# this line is tricky, try to get it exactly right
print "If I add %d, %d, and %d I get %d." % (
my_age, my_height, my_weight, my_age + my_height + my_weight)

WARNING! Remember to put # -- coding: utf-8 -- at the top if you use nonASCII characters and get an encoding error.

What You Should See
Exercise 5 Session
$ python ex5.py
Let's talk about Zed A. Shaw.
He's 74 inches tall.

www.it-ebooks.info

MORE VARIABLES AND PRINTING

29

He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.

Study Drills
1.

Change all the variables so there isn’t the my_ in front. Make sure you change the name
everywhere, not just where you used = to set them.

2.

Try more format characters. %r is a very useful one. It’s like saying “print this no matter
what.”

3.

Search online for all the Python format characters.

4.

Try to write some variables that convert the inches and pounds to centimeters and kilos.
Do not just type in the measurements. Work out the math in Python.

Common Student Questions
Can I make a variable like this: 1 = 'Zed Shaw'?
No, the 1 is not a valid variable name. They need to start with a character, so a1 would work, but
1 will not.
What does %s, %r, and %d do again?
You’ll learn more about this as you continue, but they are “formatters.” They tell Python to take
the variable on the right and put it in to replace the %s with its value.
I don’t get it, what is a “formatter”? Huh?
The problem with teaching you programming is that to understand many of my descriptions, you
need to know how to do programming already. The way I solve this is I make you do something,
and then I explain it later. When you run into these kinds of questions, write them down and see
if I explain it later.
How can I round a floating point number?
You can use the round() function like this: round(1.7333).
I get this error TypeError: 'str' object is not callable.
You probably forgot the % between the string and the list of variables.
Why does this not make sense to me?
Try making the numbers in this script your measurements. It’s weird, but talking about yourself
will make it seem more real.

www.it-ebooks.info

30

EXERCISE 6

Strings and Text

W

hile you have already been writing strings, you still do not know what they do. In this exercise, we create a bunch of variables with complex strings so you can see what they are for.
First an explanation of strings.

A string is usually a bit of text you want to display to someone or “export” out of the program
you are writing. Python knows you want something to be a string when you put either " (doublequotes) or ' (single-quotes) around the text. You saw this many times with your use of print
when you put the text you want to go to the string inside " or ' after the print. Then Python
prints it.
Strings may contain the format characters you have discovered so far. You simply put the formatted variables in the string, and then a % (percent) character, followed by the variable. The only
catch is that if you want multiple formats in your string to print multiple variables, you need to
put them inside ( ) (parentheses) separated by , (commas). It’s as if you were telling me to buy
you a list of items from the store and you said, “I want milk, eggs, bread, and soup.” Only as a
programmer we say, “(milk, eggs, bread, soup).”
We will now type in a whole bunch of strings, variables, and formats, and print them. You will
also practice using short abbreviated variable names. Programmers love saving themselves time
at your expense by using annoying cryptic variable names, so let’s get you started being able to
read and write them early on.
ex6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
print x
print y
print "I said: %r." % x
print "I also said: '%s'." % y
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
print joke_evaluation % hilarious
w = "This is the left side of..."
e = "a string with a right side."
print w + e

www.it-ebooks.info

STRINGS AND TEXT

31

What You Should See
Exercise 6 Session
$ python ex6.py
There are 10 types of people.
Those who know binary and those who don't.
I said: 'There are 10 types of people.'.
I also said: 'Those who know binary and those who don't.'.
Isn't that joke so funny?! False
This is the left side of...a string with a right side.

Study Drills
1.

Go through this program and write a comment above each line explaining it.

2.

Find all the places where a string is put inside a string. There are four places.

3.

Are you sure there are only four places? How do you know? Maybe I like lying.

4.

Explain why adding the two strings w and e with + makes a longer string.

Common Student Questions
What is the difference between %r and %s?
We use %r for debugging, since it displays the “raw” data of the variable, but we use %s and
others for displaying to users.
What’s the point of %s and %d when you can just use %r?
The %r is best for debugging, and the other formats are for actually displaying variables to users.
If you thought the joke was funny could you write hilarious = True?
Yes, and you’ll learn more about these boolean values in Exercise 27.
Why do you put ' (single-quotes) around some strings and not others?
Mostly it’s because of style, but I’ll use a single-quote inside a string that has double-quotes. Look
at line 10 to see how I’m doing that.
I get the error TypeError: not all arguments converted during string formatting.
You need to make sure that the line of code is exactly the same. What happens in this error is you
have more % format characters in the string than variables to put in them. Go back and figure out
what you did wrong.

www.it-ebooks.info

32

EXERCISE 7

More Printing

N

ow we are going to do a bunch of exercises where you just type code in and make it run. I
won’t be explaining much since it is just more of the same. The purpose is to build up your
chops. See you in a few exercises, and do not skip! Do not paste!
ex7.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

print
print
print
print

"Mary had a little lamb."
"Its fleece was white as %s." % 'snow'
"And everywhere that Mary went."
"." * 10 # what'd that do?

end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
# watch that comma at the end. try removing it to see what happens
print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end12

What You Should See
Exercise 7 Session
$ python ex7.py
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Burger

Study Drills
For these next few exercises, you will have the exact same Study Drills.

www.it-ebooks.info

MORE PRINTING

33

1.

Go back through and write a comment on what each line does.

2.

Read each one backward or out loud to find your errors.

3.

From now on, when you make mistakes, write down on a piece of paper what kind of
mistake you made.

4.

When you go to the next exercise, look at the last mistakes you made and try not to
make them in this new one.

5.

Remember that everyone makes mistakes. Programmers are like magicians who like
everyone to think they are perfect and never wrong, but it’s all an act. They make mistakes all the time.

Common Student Questions
How does the “end” statement work?
These are not really an “end statement,” but actually the names of variables that just happen to
have the word “end” in them.
Why are you using the variable named 'snow'?
That’s actually not a variable: it is just a string with the word snow in it. A variable wouldn’t have
the single-quotes around it.
Is it normal to write an English comment for every line of code like you say to do in Study Drills #1?
No, normally you write comments only to explain difficult to understand code or why you did
something. Why (or your motivation) is usually much more important, and then you try to write
the code so that it explains how something is being done on its own. However, sometimes you just
have to write such nasty code to solve a problem that it does need a comment on every line. In this
case, though, it’s strictly for you to get better at translating from code to English.
Can I use single-quotes or double-quotes to make a string or do they do different things?
In Python either way to make a string is acceptable, although typically you’ll use single-quotes for
any short strings like 'a' or 'snow'.
Couldn’t you just not use the comma , and turn the last two lines into one single-line print?
Yes, you could very easily, but then it’d be longer than 80 characters, which in Python is bad style.

www.it-ebooks.info

34

EXERCISE 8

Printing, Printing
ex8.py
1
2
3
4
5
6
7
8
9
10
11
12

formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)

What You Should See
Exercise 8 Session
$ python ex8.py
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it didn't sing."
'So I said goodnight.'

Study Drills
1.

Do your checks of your work, write down your mistakes, and try not to make them on
the next exercise.

2.

Notice that the last line of output uses both single-quotes and double-quotes for individual pieces. Why do you think that is?

Common Student Questions
Should I use %s or %r for formatting?
You should use %s and only use %r for getting debugging information about something. The %r
will give you the “raw programmer’s” version of variable, also known as the “representation.”

www.it-ebooks.info

PRINTING, PRINTING

35

Why do I have to put quotes around “one” but not around True or False?
That’s because Python recognizes True and False as keywords representing the concept of true
and false. If you put quotes around them, then they are turned into strings and won’t work right.
You’ll learn more about how these work in Exercise 27.
I tried putting Chinese (or some other non-ASCII characters) into these strings, but %r prints out
weird symbols.
Use %s to print that instead and it’ll work.
Why does %r sometimes print things with single-quotes when I wrote them with double-quotes?
Python is going to print the strings in the most efficient way it can, not replicate exactly the way
you wrote them. This is perfectly fine since %r is used for debugging and inspection, so it’s not
necessary that it be pretty.
Why doesn’t this work in Python 3?
Don’t use Python 3. Use Python 2.7 or better, although Python 2.6 might work fine.
Can I use IDLE to run this?
No, you should learn to use the command line. It is essential to learning programming and is a
good place to start if you want to learn about programming. IDLE will fail for you when you get
further in the book.

www.it-ebooks.info

36

EXERCISE 9

Printing, Printing, Printing
ex9.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Here's some new strange stuff, remember type it exactly.
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print "Here are the days: ", days
print "Here are the months: ", months
print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""

What You Should See
Exercise 9 Session
$ python ex9.py
Here are the days: Mon Tue Wed Thu Fri Sat Sun
Here are the months: Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.

Study Drills
1.

Do your checks of your work, write down your mistakes, and try not to make them on
the next exercise.

www.it-ebooks.info

PRINTING, PRINTING, PRINTING

37

Common Student Questions
What if I wanted to start the months on a new line?
You simply start the string with \n like this:
"\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"

Why do the \n newlines not work when I use %r?
That’s how %r formatting works; it prints it the way you wrote it (or close to it). It’s the “raw”
format for debugging.
Why do I get an error when I put spaces between the three double-quotes?
You have to type them like """ and not " " ", meaning with no spaces between each one.
Is it bad that my errors are always spelling mistakes?
Most programming errors in the beginning (and even later) are simple spelling mistakes, typos, or
getting simple things out of order.

www.it-ebooks.info

38

EXERCISE 10

What Was That?

I

n Exercise 9 I threw you some new stuff, just to keep you on your toes. I showed you two ways
to make a string that goes across multiple lines. In the first way, I put the characters \n (backslash
n) between the names of the months. What these two characters do is put a new line character
into the string at that point.

This use of the \ (backslash) character is a way we can put difficult-to-type characters into a string.
There are plenty of these “escape sequences” available for different characters you might want to
put in, but there’s a special one, the double backslash, which is just two of them \. These two
characters will print just one backslash. We’ll try a few of these sequences so you can see what I
mean.
Another important escape sequence is to escape a single-quote ' or double-quote ". Imagine
you have a string that uses double-quotes and you want to put a double-quote in for the output.
If you do this "I "understand" joe." then Python will get confused since it will think the "
around "understand" actually ends the string. You need a way to tell Python that the " inside
the string isn’t a real double-quote.
To solve this problem, you escape double-quotes and single-quotes so Python knows what to
include in the string. Here’s an example:
"I am 6'2\" tall."
'I am 6\'2" tall.'

# escape double-quote inside string
# escape single-quote inside string

The second way is by using triple-quotes, which is just """ and works like a string, but you also can
put as many lines of text as you want until you type """ again. We’ll also play with these.
ex10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""
print
print
print
print

tabby_cat
persian_cat
backslash_cat
fat_cat

www.it-ebooks.info

WHAT WAS THAT?

39

What You Should See
Look for the tab characters that you made. In this exercise, the spacing is important to get right.
Exercise 10 Session
$ python ex10.py
I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.
I'll do a list:
* Cat food
* Fishies
* Catnip
* Grass

Escape Sequences
This is the list of all the escape sequences Python supports. You may not use many of these, but
memorize their format and what they do anyway. Also try them out in some strings to see if you
can make them work.

Escape

What it does.

\\

Backslash (\)

\'

Single-quote (')

\"

Double-quote (")

\a

ASCII bell (BEL)

\b

ASCII backspace (BS)

\f

ASCII formfeed (FF)

\n

ASCII linefeed (LF)

\N{name}

Character named name in the Unicode database (Unicode only)

\r

ASCII carriage return (CR)

\t

ASCII horizontal tab (TAB)

\uxxxx

Character with 16-bit hex value xxxx (Unicode only)

\Uxxxxxxxx

Character with 32-bit hex value xxxxxxxx (Unicode only)

\v

ASCII vertical tab (VT)

\ooo

Character with octal value oo

\xhh

Character with hex value hh

www.it-ebooks.info

40

LEARN PYTHON THE HARD WAY

Here’s a tiny piece of fun code to try out:
while True:
for i in ["/","-","|","\\","|"]:
print "%s\r" % i,

Study Drills
1.

Memorize all the escape sequences by putting them on flash cards.

2.

Use ''' (triple-single-quote) instead. Can you see why you might use that instead of
"""?

3.

Combine escape sequences and format strings to create a more complex format.

4.

Remember the %r format? Combine %r with double-quote and single-quote escapes and
print them out. Compare %r with %s. Notice how %r prints it the way you’d write it in
your file, but %s prints it the way you’d like to see it?

Common Student Questions
I still haven’t completely figured out the last exercise. Should I continue?
Yes, keep going, and instead of stopping, take notes listing things you don’t understand for each
exercise. Periodically go through your notes and see if you can figure these things out after you’ve
completed more exercises. Sometimes, though, you may need to go back a few exercises and go
through them again.
What makes \\ special compared to the other ones?
It’s simply the way you would write out one backslash (\) character. Think about why you would
need this.
When I write // or /n it doesn’t work.
That’s because you are using a forward-slash / and not a backslash \. They are different characters
that do very different things.
When I use a %r format none of the escape sequences work.
That’s because %r is printing out the raw representation of what you typed, which is going to
include the original escape sequences. Use %s instead. Always remember this: %r is for debugging;
%s is for displaying.

www.it-ebooks.info

WHAT WAS THAT?

41

I don’t get Study Drills #3. What do you mean by “combine” escapes and formats?
One of the things I try to get you to understand is that each of these exercises can be combined to
solve problems. Take what you know about format sequences and write some new code that uses
those and the escapes from this exercise.
What’s better, ''' or """?
It’s entirely based on style. Go with the ''' (triple-single-quote) style for now, but be ready to use
either, depending on what feels best or what everyone else is doing.

www.it-ebooks.info

42

EXERCISE 11

Asking Questions

N

ow it is time to pick up the pace. I have got you doing a lot of printing so that you get used to
typing simple things, but those simple things are fairly boring. What we want to do now is get
data into your programs. This is a little tricky because you have to learn to do two things that may
not make sense right away, but trust me and do it anyway. It will make sense in a few exercises.
Most of what software does is the following:
1.

Take some kind of input from a person.

2.

Change it.

3.

Print out something to show how it changed.

So far you have only been printing, but you haven’t been able to get any input from a person or
change it. You may not even know what “input” means, so rather than talk about it, let’s have you
do some and see if you get it. In the next exercise, we’ll do more to explain it.
ex11.py
1
2
3
4
5
6
7
8
9

print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()
print "So, you're %r old, %r tall and %r heavy." % (
age, height, weight)

NOTE: Notice that we put a , (comma) at the end of each print line. This is so that
print doesn’t end the line with a new line character and go to the next line.

What You Should See
Exercise 11 Session
$ python ex11.py
How old are you? 38
How tall are you? 6'2"
How much do you weigh? 180lbs
So, you're '38' old, '6\'2"' tall and '180lbs' heavy.

www.it-ebooks.info

ASKING QUESTIONS

43

Study Drills
1.

Go online and find out what Python’s raw_input does.

2.

Can you find other ways to use it? Try some of the samples you find.

3.

Write another “form” like this to ask some other questions.

4.

Related to escape sequences, try to find out why the last line has '6\'2"' with that \'
sequence. See how the single-quote needs to be escaped because otherwise it would end
the string?

Common Student Questions
How do I get a number from someone so I can do math?
That’s a little advanced, but try x = int(raw_input()), which gets the number as a string from
raw_input() then converts it to an integer using int().
I put my height into raw input like raw_input("6'2") but it doesn’t work.
You don’t put your height in there; you type it directly into your Terminal. First thing is, go back
and make the code exactly like mine. Next, run the script, and when it pauses, type your height in
at your keyboard. That’s all there is to it.
Why do you have a new line on line 8 instead of putting it on one line?
That’s so that the line is less than 80 characters long, which is a style that Python programmers
like. You could put it on one line if you like.
What’s the difference between input() and raw_input()?
The input() function will try to convert things you enter as if they were Python code, but it has
security problems so you should avoid it.
When my strings print out there’s a u in front of them, as in u'35'.
That’s how Python tells you that the string is Unicode. Use a %s format instead and you’ll see it
printed like normal.

www.it-ebooks.info

44

EXERCISE 12

Prompting People
hen you typed raw_input(), you were typing the ( and ) characters, which are parenthesis characters. This is similar to when you used them to do a format with extra variables, as in "%s %s" % (x, y). For raw_input, you can also put in a prompt to show to a person
so he knows what to type. Put a string that you want for the prompt inside the () so that it looks
like this:

W

y = raw_input("Name? ")

This prompts the user with “Name?” and puts the result into the variable y. This is how you ask
someone a question and get the answer.
This means we can completely rewrite our previous exercise using just raw_input to do all the
prompting.
ex12.py
age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ")

1
2
3
4
5
6

print "So, you're %r old, %r tall and %r heavy." % (
age, height, weight)

What You Should See
Exercise 12 Session
$ python ex12.py
How old are you? 38
How tall are you? 6'2"
How much do you weigh? 180lbs
So, you're '38' old, '6\'2"' tall and '180lbs' heavy.

Study Drills
1.

In Terminal, where you normally run python to run your scripts, type pydoc raw_input.
Read what it says. If you’re on Windows try python -m pydoc raw_input instead.

2.

Get out of pydoc by typing q to quit.

www.it-ebooks.info

PROMPTING PEOPLE

45

3.

Look online for what the pydoc command does.

4.

Use pydoc to also read about open, file, os, and sys. It’s alright if you do not understand those; just read through and take notes about interesting things.

Common Student Questions
How come I get SyntaxError: invalid syntax whenever I run pydoc?
You aren’t running pydoc from the command line; you’re probably running it from inside python.
Exit out of python first.
Why does my pydoc not pause like yours does?
Sometimes if the help document is short enough to fit on one screen, then pydoc will just print it.
When I run pydoc I get more is not recognized as an internal.
Some versions of Windows do not have that command, which means pydoc is broken for you.
You can skip this Study Drill and just search online for Python documentation when you need it.
Why would I use %r over %s?
Remember, %r is for debugging and is “raw representation” while %s is for display. I will not
answer this question again, so you must memorize this fact. This is the #1 thing people ask repeatedly, and asking the same question over and over means you aren’t taking the time to memorize
what you should. Stop now, and finally memorize this fact.
Why can’t I do print "How old are you?" , raw_input()?
You’d think that’d work, but Python doesn’t recognize that as valid. The only answer I can really
give is, you just can’t.

www.it-ebooks.info

46

EXERCISE 13

Parameters, Unpacking, Variables

I

n this exercise, we will cover one more input method you can use to pass variables to a script
(script being another name for your .py files). You know how you type python ex13.py to run
the ex13.py file? Well the ex13.py part of the command is called an “argument.” What we’ll do
now is write a script that also accepts arguments.
Type this program and I’ll explain it in detail:
ex13.py
1
2
3
4
5
6
7
8

from sys import argv
script, first, second, third = argv
print
print
print
print

"The script is called:", script
"Your first variable is:", first
"Your second variable is:", second
"Your third variable is:", third

On line 1 we have what’s called an “import.” This is how you add features to your script from the
Python feature set. Rather than give you all the features at once, Python asks you to say what you
plan to use. This keeps your programs small, but it also acts as documentation for other programmers who read your code later.
The argv is the “argument variable,” a very standard name in programming that you will find
used in many other languages. This variable holds the arguments you pass to your Python script
when you run it. In the exercises you will get to play with this more and see what happens.
Line 3 “unpacks” argv so that, rather than holding all the arguments, it gets assigned to four
variables you can work with: script, first, second, and third. This may look strange, but
“unpack” is probably the best word to describe what it does. It just says, “Take whatever is in
argv, unpack it, and assign it to all these variables on the left in order.”
After that, we just print them out like normal.

Hold Up! Features Have Another Name
I call them “features” here (these little things you import to make your Python program do
more) but nobody else calls them features. I just used that name because I needed to trick you
into learning what they are without jargon. Before you can continue, you need to learn their real
name: modules.

www.it-ebooks.info

PARAMETERS, UNPACKING, VARIABLES

47

From now on we will be calling these “features” that we import modules. I’ll say things like, “You
want to import the sys module.” They are also called “libraries” by other programmers, but let’s
just stick with modules.

What You Should See
Run the program like this (and you must pass three command line arguments):
Exercise 13 Session
$ python ex13.py first 2nd 3rd
The script is called: ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3rd

This is what you should see when you do a few different runs with different arguments:
Exercise 13 Session
$ python ex13.py stuff things that
The script is called: ex13.py
Your first variable is: stuff
Your second variable is: things
Your third variable is: that
$
$ python ex13.py apple orange grapefruit
The script is called: ex13.py
Your first variable is: apple
Your second variable is: orange
Your third variable is: grapefruit

You can actually replace first, second, and third with any three things you want.
If you do not run it correctly, then you will get an error like this:
Exercise 13 Session
$ python ex13.py first 2nd
Traceback (most recent call last):
File "ex13.py", line 3, in <module>
script, first, second, third = argv
ValueError: need more than 3 values to unpack

This happens when you do not put enough arguments on the command when you run it (in
this case just first 2nd). Notice when I run it I give it first 2nd, which caused it to give an
error about “need more than 3 values to unpack,” telling you that you didn’t give it enough
parameters.

www.it-ebooks.info

48

LEARN PYTHON THE HARD WAY

Study Drills
1.

Try giving fewer than three arguments to your script. See that error you get? See if you
can explain it.

2.

Write a script that has fewer arguments and one that has more. Make sure you give the
unpacked variables good names.

3.

Combine raw_input with argv to make a script that gets more input from a user.

4.

Remember that modules give you features. Modules. Modules. Remember this because
we’ll need it later.

Common Student Questions
When I run it I get ValueError: need more than 1 value to unpack.
Remember that an important skill is paying attention to details. If you look at the What You
Should See (WYSS) section, you see that I run the script with parameters on the command line.
You should replicate how I ran it exactly.
What’s the difference between argv and raw_input()?
The difference has to do with where the user is required to give input. If they give your script
inputs on the command line, then you use argv. If you want them to input using the keyboard
while the script is running, then use raw_input().
Are the command line arguments strings?
Yes, they come in as strings, even if you typed numbers on the command line. Use int() to convert them just like with raw_input().
How do you use the command line?
You should have learned to use it real quick by now, but if you need to learn it at this stage, then
read the Command Line Crash Course appendix.
I can’t combine argv wi