1、Learn Python The Hard WayRelease 0.2Zed A. ShawMay 02, 2010CONTENTSThe Hard Way Is Easier 3Reading And Writing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Attention To Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2、. . . . . . . . . 3Spotting Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Do Not Copy-Paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4License . . . . . . . . . . . . . . . . . . . . . . .
3、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Exercise 0: The Setup 5Mac OSX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4、 . . . . 6Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Warnings For Beginners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Exercise 1: A Good First Program 9What You Should See . . . .
5、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Exercise 2: Comments And Pound Characters 11What You Should See . . . . . . . . . . . . . . . . .
6、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Exercise 3: Numbers And Math 13What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7、 . . . . . . . . . . 14Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Exercise 4: Variables And Names 15What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Extra Credit
8、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Exercise 5: More Variables And Printing 17What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Extra Credit . . . . . . . . . . . . .
9、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Exercise 6: Strings And Text 19What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10、. . . . . . . . . . . . . . . . . . . 20Exercise 7: More Printing 21What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21i
11、Exercise 8: Printing, Printing 23What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Exercise 9: Printing, Printing, Prin
12、ting 25What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Exercise 10: What Was That? 27What You Should See . . . . . .
13、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Exercise 11: Asking Questions 29What You Should See . . . . . . . . . . . . . . . . . . . . . . . . .
14、 . . . . . . . . . . . . . . . . . . . . . 29Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Exercise 12: Prompting People 31What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15、. . . 31Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Exercise 13: Parameters, Unpacking, Variables 33What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Extra Credit
16、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Exercise 14: Prompting And Passing 35What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Extra Credit . . . . . . . . . . . . . . . .
17、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Exercise 15: Reading Files 37What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
18、. . . . . . . . . . . . . . . 38Exercise 16: Reading And Writing Files 39What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19、. 40Exercise 17: More Files 41What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Exercise 18: Names, Variables, Code, Fu
20、nctions 43What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Exercise 19: Functions And Variables 47What You Should See
21、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Exercise 20: Functions And Files 49What You Should See . . . . . . . . . . . . . . . . .
22、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50Exercise 21: Functions Can Return 51What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23、 . . . . . . . . . . . . 52Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52iiExercise 27: Memorizing Logic 53The Truth Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53The Tr
24、uth Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54Advice From An Old Programmer 55Indices and tables 57iiiivLearn Python The Hard Way, Release 0.2Contents:CONTENTS 1Learn Python The Hard Way, Release 0.22 CONTENTSThe Hard Way Is EasierThis s
25、imple book is meant to give you a first start in programming. The title says it is the hard way to learn to writecode but its actually not. Its the “hard” way only in that its the way people used to teach things. In this book youwill do something incredibly simple that all programmers actually do to
26、 learn a language:1. Go through each exercise.2. Type in each sample exactly.3. Make it run.Thats it. This will be very difficult at first, but stick with it. If you go through this book, and do each exercise for1-2 hours a night, then youll have a good foundation for moving on to another book. You
27、might not really learn“programming” from this book, but you will learn the foundation skills you need to start learning the language.This books job is to teach you the three most basic essential skills that a beginning programmer needs to know:Reading And Writing, Attention To Detail, Spotting Diffe
28、rences.Reading And WritingIt seems stupidly obvious, but if you have a problem typing youll have a problem learning to code. Not only that,but if you have a problem typing the fairly odd characters in source code then youll be unable to learn even the mostbasic things about how software works.Typing
29、 the code samples and getting them to run will help you learn the names of the symbols, get familiar with typingthem, and get you reading the language.Attention To DetailThe one skill that separates bad programmers from good programmers is attention to detail. In fact, its what separatesthe good fro
30、m the bad in any profession. Without an attention to the tiniest details of your work youll miss keyimportant 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 you
31、r brain to focus on exactingdetails of what you are doing.Spotting DifferencesA very important skill that most programmers develop over time is the ability to visually notice differences betweenthings. An experienced programmer can take two pieces of code that are slightly different and immediately
32、start3Learn Python The Hard Way, Release 0.2pointing out the differences. In fact, programmers have invented tools to make this even easier. Of course, the inventionof these tools also means that even with years of training finding little differences in programming is difficult.While you do these ex
33、ercises and type each one in, youll be making mistakes. Its inevitable, and even seasonedprogrammers would make a few mistakes. Your job is to compare what youve written to whats required and fix allthe differences, and by doing this youll train yourself to notice your mistakes, bugs, and other prob
34、lems caused bythem.Do Not Copy-PasteThe last thing Ill say before we begin is that you must type each of these exercises in manually. If you copy and pasteyou might as well just not even do them. The point of these exercises is to train your hands, your brain, and your mindin how to read, write, and
35、 see code. If you copy-paste you are cheating yourself out of the effectiveness of the lessons.LicenseThis book is Copyright (C) 2010 by Zed A. Shaw. You are free to distribute this book to anyone you want so long asyou do not charge anything for it and it is not altered. You must give away the book
36、 in its entirety or not at all.4 The Hard Way Is EasierExercise 0: The SetupThis exercise has no code. It is simply the exercise you complete in order to get your computer setup to run Python.You should follow these instructions as exactly as possible. For example, Mac OSX computers already have Pyt
37、hon2, so dont install Python 3 (or any Python).Mac OSXTo complete this exercise you have to finish the following tasks:1. Go to http:/learnpythonthehardway.org/wiki/ExerciseZero with your browser, get the gedit text editor, andinstall it.2. Put gedit (your editor) in your Dock so you can reach it ea
38、sily.3. Find your “Terminal” program. Search for it. Youll find it.4. Put your Terminal in your Dock as well.5. Run your Terminal program. It wont look like much.6. In your Terminal program, run python. You run things in Terminal by just typing their name and hittingRETURN.7. Hit CTRL-D (D) and get
39、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. Search online for help.10. Learn how to change into a directory in the Terminal. Again search online.11. Use your editor to create a
40、file in this directory. Typically you will make the file and then “Save” or “Save As”and pick this directory.12. Go back to Terminal using just the keyboard to switch windows. Look it up if you cant figure it out.13. Back in Terminal see if you can list the directory to see your newly created file.
41、Search online for how to list adirectory.OSX: What You Should SeeHeres me doing the above on my computer in Terminal. Your computer would be different, so see if you can figureout all the differences between what I did and what you should do. Notice I use a text editor called vim. You shouldntuse it
42、, its too hard to use for you right now.5Learn Python The Hard Way, Release 0.2Last login: Sat Apr 24 00:56:54 on ttys001 $ pythonPython 2.5.1 (r251:54863, Feb 6 2009, 19:02:12)GCC 4.0.1 (Apple Inc. build 5465) on darwinType “help“, “copyright“, “credits“ or “license“ for more information. D $ mkdir
43、 mystuff $ cd mystuffmystuff $ lsmystuff $ vim test.txtmystuff $ lstest.txtmystuff $WindowsIm looking for someone to help with the instructions for Windows. I dont have one and Id like to make the instruc-tions fairly similar to Linux and OSX. Feel free to create a wiki page on http:/learnpythontheh
44、ardway.org/ with whatyou think are good instructions and expected output.LinuxLinux is a varied operating system with a bunch of different ways to install software. Im assuming if youre runningLinux then you know how to install packages so heres your instructions:1. Go to http:/learnpythonthehardway
45、.org/wiki/ExerciseZero with your browser, get the gedit text editor, andinstall it.2. Make sure you can get to gedit easily by putting it in your window managers menu.3. Find your “Terminal” program. It could be called GNOME Terminal, Konsole, or xterm.4. Put your Terminal in your Dock as well.5. Ru
46、n your Terminal program. It wont look like much.6. In your Terminal program, run python. You run things in Terminal by just typing their name and hittingRETURN. a. If you run python and its not there install it. Make sure you install Python 2 not Python 3.7. Hit CTRL-D (D) and get out of python.8. Y
47、ou 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. Search online for help.10. Learn how to change into a directory in the Terminal. Again search online.11. Use your editor to create a file in this direc
48、tory. Typically you will make the file and then “Save” or “Save As”and pick this directory.12. Go back to Terminal using just the keyboard to switch windows. Look it up if you cant figure it out.13. Back in Terminal see if you can list the directory to see your newly created file. Search online for
49、how to list adirectory.6 Exercise 0: The SetupLearn Python The Hard Way, Release 0.2Linux: What You Should See$ pythonPython 2.6.5 (r265:79063, Apr 1 2010, 05:28:39)GCC 4.4.3 20100316 (prerelease) on linux2Type “help“, “copyright“, “credits“ or “license“ for more information.$ mkdir mystuff$ cd mystuffmystuff$ vim test.txtmystuff$ lstest.txtmystuff$You will probably see a very different prompt, Python information, and other stuff but this is the general idea.Warnings For BeginnersYoure done with this exercise. This exercise could ac