Learn r in hard way zed shaw pdf download






















This exercise will be daunting at first. Finding Python source code for things you need. Reading through the code and looking for files. Trying to understand code you find. At your level you really do not have the skills to evaluate the things you find, but you can benefit from getting exposure and seeing how things look.

When you do this exercise, think of yourself as an anthropologist, trucking through a new land with just barely enough of the local language to get around and survive. Go to bitbucket. Pick a random project and click on it. Click on the Source tab and browse through the list of files and directories until you find a. Start at the top and read through it, taking notes on what you think it does. If any symbols or strange words seem to interest you, write them down to research later.

Your job is to use what you know so far and see if you can read the code and get a grasp of what it does. Try skimming the code first, and then read it in detail. Maybe also try taking very difficult parts and reading each symbol you know outloud. This exercise is longer and all about building up stamina.

The next exercise will be similar. Do them, get them exactly right, and do your checks. We can also do that this way: We'd have beans, jars, and 5 crates. Make sure to do your checks: read it backwards, read it out loud, put comments above confusing parts. Break the file on purpose, then run it to see what kinds of errors you get. Make sure you can fix it.

This exercise should be straight forward for you to type in, break down, and understand. However, this exercise is a little different. Instead you will import it into your python and run the functions yourself. Once you have found all of the errors you can and fixed them, you will then want to follow the WYSS section to complete the exercise. Notice you do not need to put the.

When you do this you make a module that has all your functions in it to use. It looks weird but this is a list which you will learn about later.

I made a mistake and typed the words variable as wrods so python gave me an error on Lines Notice that since we printed the first and last one, those words are now missing. The remaining lines are for you to figure out and analyze in the extra credit. Take the remaining lines of the WYSS output and figure out what they are doing. Make sure you understand how you are running your functions in the ex25 module. Try doing this: help ex25 and also help ex Notice how you get help for your module, and how the help is those odd """ strings you put after each function in ex25?

Type ex Start a new session and see how all your functions are right there. Try breaking your file and see what it looks like in python when you use it. You are almost done with the first half of the book.

The second half is where things get interesting. You will learn logic and be able to do useful things like make decisions. Before you continue, I have a quiz for you. They will very frequently claim that their code is perfect. These programmers are stupid people who care little for others. I have poorly copied exercises 24 and 25 into a file and removed random characters and added flaws.

Most of the errors are things Python will tell you, while some of them are math errors you should find. Others are formatting errors or spelling mistakes in the strings. All of these errors are very common mistakes all programmers make.

Even experienced ones. Your job in this exercise is to correct this file. Use all of your skills to make this file better. Analyze it first, maybe printing it out to edit it like you would a school term paper. Fix each flaw and keep running it and fixing it until the script runs perfectly. Try not to get help, and instead if you get stuck take a break and come back to it later. Even if this takes days to do, bust through it and make it right. This is the only time you are allowed to copy-paste.

Exercise Memorizing Logic Today is the day you start learning about logic. Up to this point you have done everything you possibly can reading and writing files, to the terminal, and have learned quite a lot of the math capabilities of Python. From now on, you will be learning logic. Learning logic has to come after you do some memorization.

I want you to do this exercise for an entire week. Do not falter. Even if you are bored out of your mind, keep doing it. This exercise has a set of logic tables you must memorize to make it easier for you to do the later exercises. It will be downright boring and tedious but this is to teach you a very important skill you will need as a programmer.

You will need to be able to memorize important concepts as you go in your life. Most of these concepts will be exciting once you get them. You will struggle with them, like wrestling a squid, then one day snap you will understand it.

All that work memorizing the basics pays off big later. Do not try to sit down for two hours straight and memorize these tables. Your brain will really only retain whatever you studied in the first 15 or 30 minutes anyway.

Instead, what you should do is create a bunch of index cards with each column on the left on one side True or False and the column on the right on the back. Once you can do that, start writing out your own truth tables each night into a notebook.

Do not just copy them. Try to do them from memory, and when you get stuck glance quickly at the ones I have here to refresh your memory. Doing this will train your brain to remember the whole table. Do not spend more than one week on this, because you will be applying it as you go. Logic on a computer is all about seeing if some combination of these characters and some variables is True at that point in the program. The phrases and, or, not actually work the way you expect them to, just like in English.

The Truth Tables We will now use these characters to make the truth tables you need to memorize. NOT True? Remember though, there is no failing in this book, just trying as hard as you can each day, and then a little bit more. Boolean logic is used everywhere in programming. They are essential fundamental parts of computation and knowing them very well is akin to knowing your scales in music. In this exercise you will be taking the logic exercises you memorized and start trying them out in python.

Take each of these logic problems, and write out what you think the answer will be. In each case it will be either True or False. Once you have the answers written down, you will start python in your terminal and type them in to confirm your answers. True and True 2. False and True 3. False and 0! Whenever you see these boolean logic statements, you can solve them easily by this simple process: 1. Find each not and invert it. When you are done you should have True or False.

I will demonstrate with a variation on 3! Solve each equality test: a 3! Find each not and invert it: a not True is False: True and False 4. Warning: The more complicated ones may seem very hard at first. You should be able to give a good first stab at solving them, but do not get discouraged. There are a lot of operators in Python similar to!

Write out the names of each of these equality operators. For example, I call! Play with the python by typing out new boolean operators, and before you hit enter try to shout out what it is. Do not think about it, just the first thing that comes to mind.

Write it down then hit enter, and keep track of how many you get right and wrong. Throw away that piece of paper from 3 away so you do not accidentally try to use it later. The world is doomed! The world is saved! The world is dry! People are greater than equal to dogs. People are less than equal to dogs. People are dogs. Try to answer these questions in your own words before moving onto the next exercise: 1. What do you think the if does to the code under it? Why does the code under the if need to be indented 4 spaces?

Can you put other boolean expressions from Ex. Try it. What happens if you change the initial variables for people, cats, and dogs? You did the extra credit right? This is exactly the same thing you did when you made functions in the first half of the book. Python expects you to indent something after you end a line with a : colon. Yes you can, and they can be as complex as you like, although really complex things generally are bad style. Because you are comparing numbers, if you change the numbers, different if-statements will evaluate to True and the blocks of code under them will run.

Go back and put different numbers in and see if you can figure out in your head what blocks of code will run. This is important for when you do the next exercise where you write all the parts of if-statements that you can use. Type this one in and make it work too. Maybe we could take the buses. Alright, let's just take the buses. Try to guess what elif and else are doing. Change the numbers of cars, people, and buses and then trace through each if-statement to see what will be printed.

Above each line write an English description of what the line does. Your scripts ran starting at the top, and went to the bottom where they ended. Now that you have if, else, and elif you can start to make scripts that decide things. In the last script you wrote out a simple set of tests asking some questions.

In this script you will ask the user questions and make decisions based on their answers. Write this script, and then play with it quite a lot to figure it out. Do you go through door 1 or door 2? What do you do? Take the cake. Scream at the bear. Good job! Bear runs away.

Yellow jacket clothespins. Understanding revolvers yelling melodies. Make sure you understand this concept of if-statements inside if-statements. What You Should See Here is me playing this little adventure game.

I do not do so well. Expand the game out as much as you can before it gets ridiculous. If you have been keeping up, you should realize that now you can combine all the other things you have learned with if-statements and boolean expressions to make your programs do smart things. However, programs also need to do repetitive things very quickly.

We are going to use a for-loop in this exercise to build and print various lists. When you do the exercise, you will start to figure out what they are. You have to figure it out. Before you can use a for-loop, you need a way to store the results of loops somewhere. The best way to do this is with a list. A list is exactly what its name says, a container of things that are organized in order. Then you put each item you want in the list separated by commas, just like when you did function arguments.

Python then takes this list and all its contents, and assigns them to the variable. Your brain has been taught that the world is flat. Remember in the last exercise where you put if-statements inside if-statements?

In programming this is all over the place. You will find functions that call other functions that have if-statements that have lists with lists inside lists. Adding 1 to the list.

Adding 2 to the list. Adding 3 to the list. Adding 4 to the list. Adding 5 to the list. Take a look at how you used range.

Look up the range function to understand it. Could you have avoided that for-loop entirely on line 23 and just assigned range 0,6 directly to elements? Find the Python documentation on lists and read about them. What other operations can you do to lists besides append? A while-loop will keep executing the code block under it as long as a boolean expression is True.

Wait, you have been keeping up with the terminology right? That if we write a line and end it with a : colon then that tells Python to start a new block of code? This is all about structuring your programs so that Python knows what you mean. If you do not get that idea then go back and do some more work with if-statements, functions, and the for-loop until you get it. Back to while-loops. It keeps doing this until the expression is False. This is great if your intention is to just keep looping until the end of the universe.

Otherwise you almost always want your loops to end eventually. Make sure that you use while-loops sparingly. Usually a for-loop is better. Review your while statements and make sure that the thing you are testing will become False at some point. Now use this function to rewrite the script to try different numbers. Rewrite the script again to use this function to see what effect that has. Now, write it to use for-loops and range instead. Do you need the incrementor in the middle anymore?

What happens if you do not get rid of it? You can already go through the elements of a list in order, but what if you want say, the 5th element? You need to know how to access the elements of a list. How does that work? Because of the way math works, Python start its lists at 0 rather than 1.

The best way to explain why is by showing you the difference between how you use numbers and how programmers use numbers. They win in the order we have them in this list.

Your friend however shows up late and wants to know who won. How can you have a nothing win a race? To a programmer, the above list is more like a deck of cards. If they want the tiger, they grab it. If they want the zeebra, they can take it too. Trust me on this, the math is way easier for these kinds of accesses.

So, how does this help you work with lists? You have to do this because you have spent your whole life using ordinal numbers, and now you have to think in cardinal. Just subtract 1 and you will be good. Take this list of animals, and follow the exercises where I tell you to write down what animal you get for that ordinal or cardinal number. If I give you cardinal 0, 1, 2 then use it directly.

The animal at 1. The 3rd animal. The 1st animal. The animal at 3. The 5th animal. The animal at 2. The 6th animal. The animal at 4. Read about ordinal and cardinal numbers online. With what you know of the difference between these types of numbers, can you explain why this really is ?

Write some more lists and work out similar indexes until you can translate them. Use Python to check your answers to this as well. I recommend you avoid his writings on this unless you enjoy being yelled at by someone who stopped programming at the same time programming started. How much do you take? You can go through it now. There is a door to your right and left. Which one do you take? The bear has a bunch of honey. The fat bear is in front of another door.

How are you going to move the bear? Draw a map of the game and how you flow through it. Fix all of your mistakes, including spelling mistakes. Write comments for the functions you do not understand. Remember doc comments? Add more to the game. What can you do to both simplify and expand it. What are all the bugs in this way of doing it? Look at how int works for clues. Finally, you are going to design a similar little game as in the last exercise but with a slight twist. Rules For If-Statements 1.

Every if-statement must have an else. This will find many errors. Never nest if-statements more than 2 deep and always try to do them 1 deep. This means if you put an if in an if then you should be looking to move that second if into another function. Treat if-statements like paragraphs, where each if,elif,else grouping is like a set of sentences. Put blank lines before and after.

Your boolean tests should be simple. If they are complex, move their calculations to variables earlier in your function and use a good name for the variable.

If you follow these simple rules, you will start writing better code than most programmers. Go back to the last exercise and see if I followed all of these rules. If not, fix it. Warning: Never be a slave to the rules in real life. For training purposes you need to follow these rules to make your mind strong, but in real life sometimes these rules are just stupid. If you think a rule is stupid, try not using it. Rules For Loops 1. Use a while-loop only to loop forever, and that means probably never.

This only applies to Python, other languages are different. Use a for-loop for all other kinds of looping, especially if there is a fixed or limited number of things to loop over. Tips For Debugging 1. A debugger is like doing a full-body scan on a sick person. The best way to debug a program is to use print to print out the values of variables at points in the program to see where they go wrong.

Make sure parts of your programs work as you work on them. Do not write massive files of code before you try to run them. Code a little, run a little, fix a little.

Homework Now write a similar game to the one that I created in the last exercise. It can be any kind of game you want in the same flavor. Spend a week on it making it as interesting as possible. For extra credit, use lists, functions, and modules remember those from Ex.

There is one catch though, write up your idea for the game first. Before you start coding you must write up a map for your game. Create the rooms, monsters, and traps that the player must go through on paper before you code. Once you have your map, try to code it up. If you find problems with the map then adjust it and make the code match. One final word of advice: Every programmer becomes paralyzed by irrational fear starting a new large project.

They then use procrastination to avoid confronting this fear and end up not getting their program working or even started. I do this. Everyone does this. The best way to avoid this is to make a list of things you should do, and then do them one at a time.

Just start doing it, do a small version, make it bigger, keep a list of things to do, and do them. In this lesson take each keyword, and first try to write out what it does from memory. Next, search online for it and see what it really does. It may be hard because some of these are going to be impossible to search for, but keep trying. Finally, use each of these in a small Python program, or as many as you can get done. The key here is to find out what the symbol does, make sure you got it right, correct it if you do not, then use it to lock it in.

For example, with strings write out how you create a string. For numbers write out a few numbers. The point is to try to get coverage on all these symbols and make sure they are locked in your head.

You should be reading any Python code you can and trying to steal ideas that you find. You actually should have enough knowledge to be able to read, but maybe not understand what the code does. First, print out the code you want to understand. Yes, print it out, because your eyes and brain are more used to reading paper than computer screens. Make sure you only print a few pages at a time. Second, go through your printout and take notes of the following: 1.

Functions and what they do. Where each variable is first given a value. Any variables with the same names in different parts of the program.

These may be trouble later. Any if-statements without else clauses. Are they right? Any while-loops that might not end. Third, once you have all of this marked up, try to explain it to yourself by writing comments as you go.

Explain the functions, how they are used, what variables are involved, anything you can to figure this code out. Lastly, on all of the difficult parts, trace the values of each variable line by line, function by function. Once you have a good idea of what the code does, go back to the computer and read it again to see if you find new things.

Keep finding more code and doing this until you do not need the printouts anymore. If you find errors in code you are reading, try to fix them and send the author your changes. Another technique for when you are not using paper is to put comments with your notes in the code. Some- times, these could become the actual comments to help the next person. There was also extra credit where you were supposed to find all the other things you can do to lists in the Python documentation.

Found it? Remember it? When you type Python code that reads mystuff. Python sees you mentioned mystuff and looks up that variable. Either way it has to find the mystuff first. Once it finds mystuff it then hits the. Since mystuff is a list, it knows that mystuff has a bunch of functions. If append is in there it is then it grabs that to use.

That extra argument is I know, weird right? Well, this is me typing into the Python shell and showing you some magic. For now you see how Python said test takes exactly 1 argument 2 given.

If you see this it means that python changed a. Adding: Boy There's 7 items now. Adding: Girl There's 8 items now. Adding: Banana There's 9 items now. Adding: Corn There's 10 items now. Take each function that is called, and go through the steps outlined above to translate them to what Python does.

Translate these two ways to view the function calls in English. Yeah I was too. Do not worry. You will learn enough to be dangerous, and you can slowly learn more later. That will only mess you up. Programmers like to feel smart so they invented Object Oriented Programming, named it OOP, and then used it way too much. It is the most useful container ever: the dictionary. What does matter is what they do when compared to lists.

You should know this by now, but what a dict does is let you use anything, not just numbers. Yes, a dict associates one thing to another, no matter what it is. We can also put new things into the dictionary with strings. I could use anything. Well almost but just pretend you can use anything for now.

It is a very interesting exercise that will hopefully make a big light turn on in your head very soon. Go find the Python documentation for dictionaries a. A big one is that they do not have order, so try playing with that.

Try doing a for-loop over them, and then try the items function in a for-loop. Can you explain it to yourself? Let me explain it and you can compare your explanation with mine. The 2nd line of code used later in the previous exercise can be broken down like this: 1. That gives us a function. Keep going counter-clock-wise and you get to the parameters.

The parameters are passed to the function, and that returns a result. Go counter-clock-wise again. After decades of programming I do not even think about these three ways to read code.

I just glance at it and know what it means, and I can even glance at a whole screen of code, and all the bugs and errors jump out at me. That took an incredibly long time and quite a bit more study than is sane. To get that way, I learned these three ways of reading most any programming language: 1. Front to back. Back to front. Try them out when you have a difficult statement to figure out. This one is gonna be fun. You kinda suck at this. If she were smarter. It then looks at" 87 print 'your bloody stump and says, "Oh crap, sorry about that.

Nobody ever told a bear" 94 print "with a broadsword 'no'. I could go get one! And berries! Love this katana. I mean, the door's right there. Bears are cool.

She offers you some cake. Then she smiles and cries and thanks you for saving her. You walk close and see a massive fin poke out. You peek in and a creepy looking huge Koi stares at you. It opens its mouth waiting for food. You can see it's happy, it then grunts, thrashes That' my diamond! Where'd you get that!? Nobody ever told a bear with a broadsword 'no'. It asks, "Is it because it's not a katana?

There are mountains. Oh, but then the bear comes with his katana and stabs you. Explain how returning the next room works. Create more rooms, making the game bigger. See if you can write the room descrip- tion as doc comments, and change the runner to print them.

Once you have doc comments as the room description, do you need to have the function prompt even? Have the runner prompt the user, and pass that in to each function. You functions should just be if-statements printing the result and returning the next room. Go read about them. They might not make sense but try anyway. Classes have all sorts of powerful features and uses that I could never go into in this book.

Instead, you will just use them like they are fancy dictionaries with functions. You have been doing a lot of this. A whole lot. The variable stuff is actually a list class. You will learn what those are after you make some classes.

How do you make classes? Python is an old language with lots of really ugly obnoxious pieces that were bad decisions. To cover up these bad decisions they make new bad decisions and then yell at people to adopt the new bad decisions. The phrase class TheThing object is an example of a bad decision. You see that self in the parameters?

You know what that is? Why use self? That way you can write your function and it will always work. You could actually use another name rather than self but then every Python programmer on the planet would hate you, so do not.

Only jerks change things like that and I taught you better. Be nice to people who have to read what you write because ten years later all code is horrible. That is how you setup a Python class with internal variables. You can set them on self with the. Later you can see how we use this to add to our number and print it. Classes are very powerful, so you should go read about them.

Read everything you can and play with them. You actually know how to use them, you just have to try it. You are going to go write an exercise using classes. It then looks at" print 'your bloody stump and says, "Oh crap, sorry about that. Nobody ever told a bear" print "with a broadsword 'no'. Compare this new version of the game and with the last one so you understand the changes that were made.

Key things to really get are: 1. How you made a class Game object and put functions inside it. How you added functions to the class by indenting them so they were deeper under the class keyword. How you indented again to put the contents of the functions under their names.

How colons are being used. In fact, try doing this by hand inside Python to really get it. How a Game was created at the end and then told to play and how that got everything started.

Try adding some rooms to make sure you know how to work with a class. Create a two-class version of this, where one is the Map and the other is the Engine. Hint: play goes in the Engine. Hopefully as you have worked through this book, you have learned that all the information you need is on the internet, you just have to go search for it. The only thing you have been missing are the right words and what to look for when you search. Here are your requirements: 1. Make a different game from the one I made.

Use more than one file, and use import to use them. Make sure you know what that is. Use one class per room and give the classes names that fit their purpose. Your runner will need to know about these rooms, so make a class that runs them and knows about them. Other than that I leave it to you. Spend a whole week on this and make it the best game you can. Use classes, functions, dicts, lists anything you can to make it nice. The purpose of this lesson is to teach you how to structure classes that need other classes inside other files.

Go figure it out. Program- ming is problem solving, and that means trying things, experimenting, failing, scrapping your work, and trying again. When you get stuck, ask for help and show people your code. If they are mean to you, ignore them, focus on the people who are not mean and offer to help. Good luck, and see you in a week with your game. Maybe you got part-way through it and you got stuck. Maybe you got it working but just barely.

Why would I have you try to do it yourself and then show you how to do it right? You will struggle at first and probably be very frustrated but stick with it and eventually you will build a mind for solving problems. You will start to find creative solutions to problems rather than just copy solutions out of textbooks.

For some reason when people start learning about classes they forget this. It makes them harder to use. They should be fairly self-contained. Consistency is good, but foolishly following some idiotic mantra because everyone else does is bad style. Think for yourself. You will find some very bad programmers who are able to write reasonable code, but who do not add any spaces. This is bad style in any language because the human eye and brain use space and vertical alignment to scan and separate visual elements.

Not having space is the same as giving your code an awesome camouflage paint job. If you are having a problem making something easy to use, try reading it out loud. Not only does this force you to slow down and really read it, but it also helps you find difficult passages and things to change for readability. Trust me, you will probably have really bad taste too and not even realize it. Ignore them and write comments. The code already says how, but why you did things the way you did is more important.

You do not have to go crazy, but a nice little sentence about what someone does with that function helps a lot. Evaluate Your Game I want you now to pretend you are me. Adopt a very stern look, print out your code, and take a red pen and mark every mistake you find. Anything from this exercise and from other things you have known. Once you are done marking your code up, I want you to fix everything you came up with.

Then repeat this a couple of times, looking for anything that could be better. The purpose of this exercise is to train your attention to detail on classes. Go through a printed copy of some part of it and point out all the mistakes and style errors you find.

Then fix it and see if your fixes can be done without breaking their program. They are actually the same thing at different points in time.

I will demonstrate by a Zen koan: What is the difference between a Fish and a Salmon? Did that question sort of confuse you? Really sit down and think about it for a minute.

I mean, a Fish and a Salmon are different but, wait, they are the same thing right? So a Salmon and a Fish are the same but different. This question is confusing because most people do not think about real things this way, but they intuitively understand them. You do not need to think about the difference between a Fish and a Salmon because you know how they are related.

You know a Salmon is a kind of Fish and that there are other kinds of Fish without having to understand that. Now, think about this question: What is the difference between Mary and a Salmon? Salmon question. Joe and Frank are also instances of Salmon. But, what do I mean when I say instance? I mean they were created from some other Salmon and now represent a real thing that has Salmon-like attributes.

Think about that for a second. Got fins? Got gills? Lives in water? Someone with a Ph. Longer nose, reddish flesh, big, lives in the ocean or fresh water, tasty? Ok, probably a Salmon. Finally, a cook comes along and tells the Ph. It has become an Object. There you have it: Mary is a kind of Salmon that is a kind of Fish.

Object is a Class is a Class. I will show you two tricks to help you figure out whether something is a Class or Object. You use the phrase is-a when you talk about objects and classes being related to each other by a class relationship. You use has-a when you talk about objects and classes that are related only because they reference each other.

Now, go through this piece of code and replace each?? Remember, is-a is the relationship between Fish and Salmon, while has-a is the relationship between Salmon and Gills. Now I can tell you, because you just learned about the difference between a class and an object. By the time they admitted the fault it was too late, and they had to support it.

Confusing right? Now you can try to understand the concept of a class that is an object if you like. However, I would suggest you do not. You will learn from very basic level and move toward advance.

This edition. This book helps you growing slowing and gradually. Though it starts with basic as all programming books should do but,by the end of this book you will learn advance programming techniques. This book works very well with beginners who know nothing and trying to get started with python.

As this is third edition of the book so it is most likely an error free version. Starting with a good news for all of you that this book is free. You must have seen many e-commerce websites selling this book. But you can download it free. All you need to do is,click here or the PDF Button below and book is in your device.

Have a good python learning journey. Download PDF. Save my name, email, and website in this browser for the next time I comment.



0コメント

  • 1000 / 1000