Learn Python the hard way is one of the best books to learn Python today. This article will bring it to you, the lastest edition of Python. Learn Python The Hard Way. Contribute to chris-void/pyway development by creating an account on GitHub. Learn Python the Hard Way is a beginner's programming book written by Zed Shaw. It was written for Python “The Hard Way is Easier” is exactly what they .
|Language:||English, Spanish, Japanese|
|Genre:||Academic & Education|
|ePub File Size:||23.36 MB|
|PDF File Size:||13.36 MB|
|Distribution:||Free* [*Regsitration Required]|
This purchase includes a PDF, paid HTML view you can access from There is a new version of this book called Learn Python 3 The Hard Way which you can. They name pieces of code the way variables name strings and numbers. buy Learn Python 3 The Hard Way, you'll receive the Python 3 Edition PDF, special. Learn Python The Hard Way Release Zed A. Shaw June 24, CONTENTS The Hard Way Is Easier 3 Reading and Writing.
The animal at 2. You can be proud of that. Still trying to figure out the regex to get rid all the "orphaned" line numbers created by this method. When you do this you make a module that has all your functions in it to use. If she were smarter.
Then in Terminal run the file by typing: If not, you have done something wrong. No, the computer is not wrong. Hello Again I like typing this. I'd much rather you 'not'. I "said" do not touch this. If you have an error it will look like this: Even I make many of these mistakes. Here we ran our command in the terminal to run the ex1. Python then tells us that the file ex1. It then prints this line for us. Notice the missing " double- quote character? Extra Credit You will also have Extra Credit.
The Extra Credit contains things you should try to do. For this exercise, try these things: Make your script print another line. Make your script print only one of the lines. What did it do? Try to find out what this character does. Pick the one that makes you chill out.
Comments And Pound Characters Comments 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.
This will run. Take your ex2. Start at the last line, and check each word in reverse against what you should have typed. Did you find more mistakes? Fix them. Read what you typed above out loud, including saying each character by its name. Numbers And Math Every programming language has some kind of way of doing numbers and math. This exercise has lots of math symbols.
As you type this one in, say the names. When saying them feels boring you can stop saying them. Here are the names: After you type in the code for this exercise, go back and figure out what each of these does and complete the table. Hens 30 Roosters 97 Now I will count the eggs: How about some more.
Is it greater? True Is it greater or equal? True Is it less or equal? Above each line, use the to write a comment to yourself explaining what the line does.
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. Find something you need to calculate and write a new. There are no fractions, only whole numbers. Rewrite ex3. Variables And Names Now 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 you get stuck with this exercise, remember the tricks you have been taught so far of finding differences and focusing on details: Write a comment above each line explaining to yourself what it does in English.
Read your. 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. There are only 30 drivers available. There will be 70 empty cars today. We can transport We have 90 to carpool today. We need to put about 3 in each car. Traceback most recent call last: File "ex4. Make sure you use line numbers and explain why. Explain why the 4.
Remember that 4. Find out what that means. Write comments above each of the variable assignments. Try running python as a calculator like you did before and use variable names to do your calcu- lations.
Popular variable names are also i, x, and j. 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 embed- ded in them. He's 74 inches tall.
He's 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 I get Try more format characters. Search online for all of the Python format characters. 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. Strings And Text While 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. Then Python prints it. Strings may contain the format characters you have discovered so far.
The only catch is that if you want multiple formats in your string to print multiple variables, you need to put them inside parenthesis separated by , commas. We will now type in a whole bunch of strings, variables, formats, and print them. You will also practice using short abbreviated variable names. False 7 This is the left side of Go through this program and write a comment above each line explaining it. Find all the places where a string is put inside a string.
There are four places. How do you know? Maybe I like lying. More Printing Now we are going to do a bunch of exercises where you just type code in and make it run. The purpose is to build up your chops. See you in a few exercises, and do not skip!
Do not paste! Its fleece was white as snow. And everywhere that Mary went. Go back through and write a comment on what each line does. Read each one backwards or out loud to find your errors. From now on, when you make mistakes write down on a piece of paper what kind of mistake you made. When you go to the next exercise, look at the last mistakes you made and try not to make them in this new one. Remember that everyone makes mistakes. They make mistakes all the time.
Do your checks of your work, write down your mistakes, try not to make them on the next exercise. Notice that the last line of output uses both single and double quotes for individual pieces. Why do you think that is? Printing, Printing, Printing 1 Here's some new strange stuff, remember type it exactly. 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. In 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.
What these two characters do is put a new line character into the string at that point. These two characters will print just one back-slash. 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. To solve this problem you escape double-quotes and single-quotes so Python knows to include in the string. In this exercise the spacing is important to get right. I'm split on a line. I'll do a list: Search online to see what other escape sequences are available.
Can you see why you might use that instead of """? Combine escape sequences and format strings to create a more complex format. Exercise Asking Questions Now 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 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: Take some kind of input from a person. Change it. Print out something to show how it changed. Notice that we put a , comma at the end of each print line.
Can you find other ways to use it? Try some of the samples you find. See how the single-quote needs to be escaped because otherwise it would end the string?
Put a string that you want for the prompt inside the so that it looks like this: This is how you ask someone a question and get their answer. In Terminal where you normally run python to run your scripts, type: Read what it says. Get out of pydoc by typing q to quit.
Go look online for what the pydoc command does. Use pydoc to also read about open, file, os, and sys. Parameters, Unpacking, Variables In this exercise we will cover one more input method you can use to pass variables to a script script being another name for your. You know how you type python ex Well the ex 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. 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. 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: What You Should See Run the program like this and you must pass three command line arguments: Shaw The script is called: Zed Your second variable is: Your third variable is: You do not have to give these parameters either, you can give any 3 strings you want: Extra Credit 1. Try giving fewer than three arguments to your script. See that error you get?
See if you can explain it. Write a script that has fewer arguments and one that has more. Make sure you give the unpacked variables good names. Remember that modules give you features. You will need this for the next exercise where we learn to read and write files. This is similar to a game like Zork or Adventure. Not sure where that is. Now if we want to make the prompt something else, we just change it in this one spot and rerun the script. Very handy. I'd like to ask you a few questions.
Do you like me Zed? You live in 'America'. And you have a 'Tandy' computer. Find out what Zork and Adventure were. Try to find a copy and play it. Change the prompt variable to something else entirely. Add another argument and use it in your script. Working with files is an easy way to erase your work if you are not careful. This exercise involves writing two files. One is your usual ex Here are the contents of that file: This is stuff I typed into a file.
It is really cool stuff. Lots and lots of fun to have in here. Line should be a familiar use of argv to get a filename. Right now, run pydoc open and read the instructions.
You just opened a file. Line 7 we print a little line, but on line 8 we have something very new and exciting. We call a function on txt.
You give a file a command by using the. The difference is that when you say txt. Do your read command with no parameters! I'll also ask you to type it again: Above each line write out in English what that line does. If you are not sure ask someone for help or search online. Search around online to see what other people do to define these. Do not worry if they confuse you. Think of why one way of getting the filename would be better than another.
See all the other ones you can use? Try some of the other commands. Startup python again and use open from the prompt. Notice how you can open files and run read on them right there? For now these are the important commands you need to know. Some of them take parameters, but we do not really care about that. You only need to remember that write takes a parameter of a string you want to write to the file.
So go slow, do your checks, and make it run. One trick is to get bits of it running at a time. Get lines running, then 5 more, then a few more, etc.
What You Should See There are actually two things you will see, first the output of your new script: Opening the file Truncating the file. Now I'm going to ask you for three lines.
To all the people out there. I say I don't like my hair. I need to shave it off. I'm going to write these to the file. And finally, we close it.
Neat right? If you feel you do not understand this, go back through and use the comment trick to get it squared away in your mind. One simple English comment above each line will help you understand, or at least let you know what you need to research more. Write a script similar to the last exercise that uses read and argv to read the file you just created.
Use strings, formats, and escapes to print out line1, line2, and line3 with just one target. This returns True if a file exists, based on its name in a string as an argument. It returns False if not. Using import is a way to get tons of free code other better well, usually programmers have written so you do not have to write it.
If we use your test. Alright, all done. Try a bunch more and see what happens. Just be careful you do not blast an important file. Did you see that trick I did with cat? Try importing some things and see if you can get it right.
This script is really annoying. Try to make it more friendly to use by removing features. See how short you can make the script. I could make this 1 line long. Type man cat to read about it.
Do not worry about man since there is nothing like that. Find out why you had to do output. Names, Variables, Code, Functions Big title right? I am about to introduce you to the function!
Dum dum dah! Every programmer will go on and on about functions and all the different ideas about how they work and what they do, but I will give you the simplest explanation you can use right now. Functions do three things: They name pieces of code the way variables name strings and numbers. They take arguments the way your scripts take argv. You can create a function by using the word def in Python. This has to go inside parenthesis to work. Then we end this line with a: Our first indented line is one that unpacks the arguments the same as with your scripts.
To demonstrate how it works we print these arguments out, just like we would in a script. In Python we can skip the whole unpacking args and just use the names we want right inside.
This is very important. What You Should See If you run the above script you should see: This means you can make your own commands and use them in your scripts too. Extra Credit Write out a function checklist for later exercises. Write these on an index card and keep it by you while you complete the rest of these exercises or until you feel you do not need it: Did you start your function definition with def?
Did you put an open parenthesis right after the function name? Did you put your arguments after the parenthesis separated by commas? Did you make each argument unique meaning no duplicated names. Did you put a close parenthesis and a colon: Did you indent all lines of code you want in the function 4 spaces? No more, no less. Did you put character after the name to run it? Did you put the values you want into the parenthesis separated by commas? Did you end the function call with a character.
Use these two checklists on the remaining lessons until you do not need them anymore. Finally, repeat this a few times: Functions And Variables Functions may have been a mind-blowing amount of information, but do not worry. Just keep doing these exercises and going through your checklist from the last exercise and you will eventually get it.
The variables in your function are not connected to the variables in your script. We can give it straight numbers. We can give it variables. We can give it math. We can even combine math and variables. What You Should See You should study the output of this script and compare it with what you think you should get for each of the examples in the script.
You have 20 cheeses! You have 30 boxes of crackers!
Man that's enough for a party! Get a blanket. OR, we can use variables from our script: You have 10 cheeses! You have 50 boxes of crackers! We can even do math inside too: You have 30 cheeses! You have 11 boxes of crackers! And we can combine the two, variables and math: You have cheeses!
You have boxes of crackers! Go back through the script and type a comment above each line explaining in English what it does. Start at the bottom and read each line backwards, saying all the important characters.
Write at least one more function of your own design, and run it 10 different ways.
Functions And Files Remember your checklist for functions, then do this exercise paying close attention to how functions and files can work together to make useful stuff. Now let's rewind, kind of like a tape. Let's print three lines: Find each place a function is used, and go check its def to make sure that you are giving it the right arguments.
Research online what the seek function for file does. Try pydoc file and see if you can figure it out from there. There will be one thing to pay close attention to, but first type this in: What this does is the following: Our function is called with two arguments: Then we tell Python to do something kind of backward: Python adds the two numbers.
At the end of the script is a puzzle. It looks really weird, but if you run the script you can see the results. What you should do is try to figure out the normal formula that would recreate this same set of operations.
Once you have the formula worked out for the puzzle, get in there and see what happens when you modify the parts of the functions. Try to change it on purpose to make another value. Finally, do the inverse. Write out a simple formula and use the functions in the same way to calculate it. This exercise might really whack your brain out, but take it slow and easy and treat it like a little game. In fact, this exercise is like one giant Extra Credit.
Make sure your list of symbols is complete. Next to each word or symbol, write its name and what it does. If you do not know what a word or symbol does, then go read about it again and try using it in some code.
This may get boring but push through and really nail it down. Once you have memorized the list and what they do, then you should step it up by writing out tables of symbols, their names, and what they do from memory.
The most important thing when doing this exercise is: It helps you focus on a goal and know the purpose of all your efforts. In this exercise you are learning the names of symbols so that you can read source code more easily. Giving your brain a rest will help you learn faster with less frustration. Read Some Code You should have spent last week getting your list of symbols straight and locked in your mind.
Now you get to apply this to another week reading code on the internet. This exercise will be daunting at first. Finding Python source code for things you need. All Rights Reserved. Theme by. He kept updating it from time to time and eventually Addison-Wesley turned it into a book by the same name and they have now brought it out in its third edition.
The website is made up of lots of short exercises that help beginner programmers learn the various nuances of Python but in bite-sized chunks. A DVD with more than 5 hours of tutorial is bundled with the book so that readers also have access to video material. Two things to note before you begin are that the book is on Python 2 and you are warned against installing Python 3.
The book is split into 52 exercises or chapters if you will. Most of the chapters are under four pages in length. In fact, there are a lot of chapters that have a blank page between it and the next chapter, so there is some filler going on here.
Each chapter also has some drills and some common student questions in them. From simple things like strings, dictionaries, and lists, to conditionals, loops, functions, and classes. They are all covered here. You basically get to watch me make mistakes typing the exercises in and learn how I do it. In addition to that I answer questions people ask on the site.
The course is on sale at AppSumo: I have not taken the course, but I really liked the first edition book. I am doing all of it with Python 3. So far, very little needs changing to make the examples work. Very clean and concise. RexRollman on June 27, So this targets an earlier version of Python?
Yes, the 2.
I am learning with Python 3 because of a class I am taking and supplementing with Zed's tutorial and just writing the examples in Python 3 format. So far, so good. I might hit a snag when I get to the web.
All of it rolled together is helping me a lot. Yes, and for a reason. Python 3 is still not widely adopted [sic]. Python 3 Wall of Shame: Stop the "wall of shame" shtick, please.
It helps nobody. Projects know about Python 3 and projects are planning to move to Python 3. It isn't trivial and there is no "shame" in it at all. Planning to buy the paperback. While checking out the paperback info, I noticed that the pdf is still free on lulu. Yeah, I changed that. I was trying to give people who bought the paper versions the PDF for free, but lulu considers "free" to mean you get it without buying the book.
I've had to remove that option for now since they don't allow it. On a side note: WTF is up with these companies? This stuff isn't hard, but the whole time I'm trying to do this it's been nothing but friction. So annoying. So it is. Been using v1 and pretty pleased with it so far and I was happy to pay such a low amount for v2. Might buy the paperback in time just so I can have it on my bookshelf.
Thanks Zed, I just bought the epub for my kindle. I am going to give it a go. Just bought a copy for my iPad. I look forward to reading it! Ideka on June 28, I checked the first couple of sections, and I'm sure learning Python "the Hard Way" would have been much more exciting than reading the official documentation Oh, well. What is done is done. Hacker News new past comments ask show jobs submit.
Ntagg on June 27, I can't tell either. Sukotto on June 27, LPTHW has a good rep, but it looks like it focuses on teaching someone who's never programmed before.
Sukotto on June 27, Thanks Zed. Ntagg on June 27, Zed, are you going to be writing any other books for Python, like Python 3 or more advanced topics? Ntagg on June 27, The course is on sale at AppSumo: