Learn Python The Hard Way: A Very Simple Introduction To The .

Transcription

LEARN PYTHONTHE HARD WAYThird Edition

Zed Shaw’s Hard Way SeriesVisit informit.com/hardway for a complete list of available publications.Zed Shaw’s Hard Way Series emphasizes instruction and making things asthe best way to get started in many computer science topics. Each book in theseries is designed around short, understandable exercises that take you througha course of instruction that creates working software. All exercises are thoroughlytested to verify they work with real students, thus increasing your chance ofsuccess. 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

LEARN PYTHONTHE HARD WAYA Very Simple Introductionto the Terrifyingly Beautiful Worldof Computers and CodeThird EditionZed A. ShawUpper Saddle River, NJ Boston Indianapolis San FranciscoNew York Toronto Montreal London Munich Paris MadridCapetown Sydney Tokyo Singapore Mexico City

Many of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and the publisher was aware of a trademarkclaim, 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 orimplied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumedfor incidental or consequential damages in connection with or arising out of the use of the information orprograms contained herein.The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or specialsales, which may include electronic versions and/or custom covers and content particular to your business,training goals, marketing focus, and branding interests. For more information, please contact:U.S. Corporate and Government Sales(800) 382-3419corpsales@pearsontechgroup.comFor sales outside the United States, please contact:International Salesinternational@pearson.comVisit us on the Web: informit.com/awLibrary of Congress Cataloging-in-Publication DataShaw, Zed.Learn Python the hard way : a very simple introduction to the terrifyingly beautiful world of computers andcode / Zed A. Shaw.—Third edition.pages cmIncludes 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 2014005.13'3—dc232013029738Copyright 2014 Zed A. ShawAll rights reserved. Printed in the United States of America. This publication is protected by copyright, andpermission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrievalsystem, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, orlikewise. To obtain permission to use material from this work, please submit a written request to PearsonEducation, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you mayfax your request to (201) 236-3290.ISBN-13: 978-0-321-88491-6ISBN-10:0-321-88491-4Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.First printing, September 2013

VContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1The Hard Way Is Easier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Reading and Writing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Attention to Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Spotting Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Do Not Copy-Paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2A Note on Practice and Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3A Warning for the Smarties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Exercise 0The Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Mac OSX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6OSX: What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Windows: What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Linux: What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Warnings for Beginners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Exercise 1A Good First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Exercise 2Comments and Pound Characters . . . . . . . . . . . . . . . . . . . . 18What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Exercise 3Numbers and Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22V413HAV

viCONTENTSExercise 4Variables and Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Exercise 5More Variables and Printing . . . . . . . . . . . . . . . . . . . . . . . . . 28What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Exercise 6Strings and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Exercise 7More Printing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Exercise 8Printing, Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Exercise 9Printing, Printing, Printing . . . . . . . . . . . . . . . . . . . . . . . . . . 36What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Exercise 10 What Was That? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40Exercise 11 Asking Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

CONTENTSExercise 12 Prompting People . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Exercise 13 Parameters, Unpacking, Variables . . . . . . . . . . . . . . . . . . . . 46Hold Up! Features Have Another Name . . . . . . . . . . . . . . . . . . . . . . . 46What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Exercise 14 Prompting and Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Exercise 15 Reading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Exercise 16 Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 58What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60Exercise 17 More Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Exercise 18 Names, Variables, Code, Functions . . . . . . . . . . . . . . . . . . . . 66What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68Exercise 19 Functions and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71vii

viiiCONTENTSExercise 20 Functions and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Exercise 21 Functions Can Return Something . . . . . . . . . . . . . . . . . . . . . 78What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Exercise 22 What Do You Know So Far? . . . . . . . . . . . . . . . . . . . . . . . . . 81What You Are Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Exercise 23 Read Some Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82Exercise 24 More Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85Exercise 25 Even More Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Exercise 26 Congratulations, Take a Test! . . . . . . . . . . . . . . . . . . . . . . . . 90Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90Exercise 27 Memorizing Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92The Truth Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92The Truth Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94Exercise 28 Boolean Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Exercise 29 What If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

CONTENTSExercise 30 Else and If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Exercise 31 Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Exercise 32 Loops and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Exercise 33 While-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112Exercise 34 Accessing Elements of Lists . . . . . . . . . . . . . . . . . . . . . . . . . 114Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Exercise 35 Branches and Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 116What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118Exercise 36 Designing and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . 120Rules for If-Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Rules for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Tips for Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121Homework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121Exercise 37 Symbol Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123String Escape Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124String Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125ix

xCONTENTSReading Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127Exercise 38 Doing Things to Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Exercise 39 Dictionaries, Oh Lovely Dictionaries . . . . . . . . . . . . . . . . . . 132What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135Exercise 40 Modules, Classes, and Objects . . . . . . . . . . . . . . . . . . . . . . 138Modules Are Like Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Classes Are Like Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Objects Are Like Mini-Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . 140Getting Things from Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141A First-Class Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143Exercise 41 Learning to Speak Object Oriented . . . . . . . . . . . . . . . . . . 144Word Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144Phrase Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144Combined Drills. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145A Reading Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145Practice English to Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147Reading More Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148Exercise 42 Is-A, Has-A, Objects, and Classes . . . . . . . . . . . . . . . . . . . . . 150How This Looks in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151About class Name(object). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

CONTENTSExercise 43 Basic Object-Oriented Analysis and Design . . . . . . . . . . . . 156The Analysis of a Simple Game Engine . . . . . . . . . . . . . . . . . . . . . . . 157Write or Draw about the Problem . . . . . . . . . . . . . . . . . . . . . . . . 157Extract Key Concepts and Research Them . . . . . . . . . . . . . . . . . . 158Create a Class Hierarchy and Object Map for the Concepts . . . . 158Code the Classes and a Test to Run Them . . . . . . . . . . . . . . . . . . 159Repeat and Refine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161Top Down vs. Bottom Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161The Code for “Gothons from Planet Percal #25” . . . . . . . . . . . . . . . 162What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168Exercise 44 Inheritance vs. Composition . . . . . . . . . . . . . . . . . . . . . . . . 170What is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170Implicit Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171Override Explicitly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172Alter Before or After . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172All Three Combined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174The Reason for super() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Using super() with init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176When to Use Inheritance or Composition . . . . . . . . . . . . . . . . . . . . . 177Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Exercise 45 You Make a Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180Evaluating Your Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180Function Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181Class Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181Code Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182Good Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182Evaluate Your Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183Exercise 46 A Project Skeleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184Installing Python Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184Creating the Skeleton Project Directory . . . . . . . . . . . . . . . . . . . . . . 185xi

xiiCONTENTSFinal Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186Testing Your Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187Using the Skeleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188Required Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189Exercise 47 Automated Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190Writing a Test Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190Testing Guidelines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193Exercise 48 Advanced User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194Our Game Lexicon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194Breaking Up a Sentence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Lexicon Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Scanning Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Exceptions and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196What You Should Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Design Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Exercise 49 Making Sentences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200Match and Peek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200The Sentence Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201A Word on Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203What You Should Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204Exercise 50 Your First Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206Installing lpthw.web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206Make a Simple “Hello World” Project . . . . . . . . . . . . . . . . . . . . . . . . 207What’s Going On? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Fixing Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

CONTENTSCreate Basic Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211Exercise 51 Getting Input from a Browser . . . . . . . . . . . . . . . . . . . . . . 214How the Web Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214How Forms Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216Creating HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218Creating a Layout Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Writing Automated Tests for Forms . . . . . . . . . . . . . . . . . . . . . . . . . 221Study Drills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223Common Student Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Exercise 52 The Start

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--321-88491-6 (paperback : alkaline paper) 1. Python (Computer program language) 2. Python (Computer program language)—Problems, exercises, etc. 3.