Learning python 3 visually pdf download






















We will now type in a whole bunch of strings, variables, and formats, and print them. You will also practice using short abbreviated variable names. Those who know binary and those who don't. I said: There are 10 types of people. I also said: 'Those who know binary and those who don't. False 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. Are you sure there are only four places?

How do you know? Maybe I like lying. Break It You are now at a point where you can try to break your code to see what happens. Think of this as a game to devise the most clever way to break the code. You can also find the simplest way to break it. Once you break the code, you then need to fix it. Give your friend your ex6. Then you try to find their error and fix it. Have fun with this, and remember that if you wrote this code once you can do it again.

If you take your damage too far, you can always type it in again for extra practice. 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 backward 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 mistakes you have made and try not to make them in this new one.

Remember that everyone makes mistakes. They make mistakes all the time. Break It Did you have fun breaking the code in Exercise 6? Your goal is to find as many different ways to break your code until you get tired or exhaust all possibilities.

Is it normal to write an English comment for every line of code like you say to do in Study Drill 1? No, you write comments only to explain difficult to understand code or why you did something. Why is usually much more important, and then you try to write the code so that it explains how some- thing is being done on its own.

However, sometimes you have to write such nasty code to solve a problem that it does need a comment on every line. When you see me write formatter. Take the formatter string defined on line 1. Call its format function, which is similar to telling it to do a command line command named format.

This is like passing arguments to the command line command format. This is what print is now printing out. Do your checks, write down your mistakes, and try not to make the same mistakes on the next exercise. Python recognizes True and False as keywords representing the concept of true and false. No, you should learn to use the command line. It is essential to learning programming and is a good place to start if you want to learn about programming.

IDLE will fail for you when you get further in the book. I start with code that you might not understand, then more exercises explain the concept. 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. Check your work, write down your mistakes, try not to make them on the next exercise.

Are you breaking your code and then fixing it? You have to type them like """ and not " " ", meaning with no spaces between each one. What if I wanted to start the months on a new line?

Most programming errors in the beginning and even later are simple spelling mistakes, typos, or getting simple things out of order. 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. These two characters put a new line character into the string at that point. An important escape sequence is to escape a single-quote ' or double-quote ".

Imagine you have a string that uses double-quotes and you want to put a double-quote inside the string. If you write "I "understand" joe. To solve this problem you escape double-quotes and single-quotes so Python knows to include them in the string. In this exercise the spacing is important to get right.

You may not use many of these, but memorize their format and what they do anyway. Try them out in some strings to see if you can make them work. Escape What it does. Memorize all the escape sequences by putting them on flash cards.

Use ''' triple-single-quote instead. Can you see why you might use that instead of """? Combine escape sequences and format strings to create a more complex format. Should I continue? Yes, keep going. Sometimes, though, you may need to go back a few exercises and do them again.

Think about why you would need this. They are different characters that do very different things. One concept I need you to understand is that each of these exercises can be combined to solve problems. Take what you know about format strings and write some new code that uses format strings and the escape sequences from this exercise.

Go with the ''' triple-single-quote style for now, but be ready to use either depending on what feels best or what everyone else is doing. You are doing a lot of printing to get you familiar with typing simple things, but those simple things are fairly boring.

What we want to do now is get data into your programs. This is a little tricky because you have to learn to do two things that may not make sense right away, but trust me and do it anyway. It will make sense in a few exercises. Most of what software does is the following: 1. Take some kind of input from a person. Change it. Print out something to show how it changed. This tells print to not end the line with a newline character and go to the next line.

Can you find other ways to use it? Try some of the samples you find. First thing is, go back and make the code exactly like mine. Next, run the script, and when it pauses, type your height in at your keyboard. For input you can also put in a prompt to show to a person so he knows what to type. This is how you ask someone a question and get the answer. This means we can completely rewrite our previous exercise using just input to do all the prompting.

In Terminal, where you normally run python3. Read what it says. Get out of pydoc by typing q to quit. Look online for what the pydoc command does. Use pydoc to also read about open, file, os, and sys. Exit out of python3. Why does my pydoc not pause like yours does? Sometimes if the help document is short enough to fit on one screen then pydoc will just print it.

When I run pydoc I get more is not recognized as an internal. Some versions of Windows do not have that command, which means pydoc is broken for you. You can skip this Study Drill and just search online for Python documentation when you need it. Try this, and then try to print out what you type. You know how you type python3. Well the ex 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. Hold Up! I just used that name because I needed to trick you into learning what they are without jargon. Before you can continue, you need to learn their real name: modules.

Pay attention! You have been running python scripts without command line arguments. If you type only python3. Pay close attention to how I run it. This applies any time you see argv being used. 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. Combine input with argv to make a script that gets more input from a user.

Just use argv to get something, and input to get something else from the user. Remember that modules give you features. Remember that an important skill is paying attention to details. If you look at the What You Should See section you see that I run the script with parameters on the command line. You should replicate how I ran it exactly. The difference has to do with where the user is re- quired to give input. If they give your script inputs on the command line, then you use argv.

If you want them to input using the keyboard while the script is running, then use input. Are the command line arguments strings? Yes, they come in as strings, even if you typed numbers on the command line.

Use int to convert them just like with int input. How do you use the command line? You should have learned to use it very quickly and fluently by now, but if you need to learn it at this stage, then read the Command Line Crash Course I wrote for this book in Appendix A.

Just slap two lines at the end of this script that uses input to get something and then print it. From that, start playing with more ways to use both in the same script. You will need this for the next exercise where you 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. What You Should See When you run this, remember that you have to give the script your name for the argv arguments. I'd like to ask you a few questions. Do you like me zed? You live in San Francisco. And you have a Tandy computer. Find out what the games Zork and Adventure were. Try to find a copy and play it. Change the prompt variable to something else entirely. Again, you have to run it right on the com- mand line, not inside Python. If you type python3.

Close your window and then just type python3. Change that to have a different value. I get the error ValueError: need more than 1 value to unpack. Can I use double-quotes for the prompt variable? You totally can. Go ahead and try that. You have a Tandy computer? I did when I was little. I get NameError: name 'prompt' is not defined when I run it.

You either spelled the name of the prompt variable wrong or forgot that line. Go back and compare each line of code to mine, from at the bottom of the script to the top. Any time you see this error, it means you spelled something wrong or forgot to create the variable.

Now you will learn about reading from a file. Working with files is an easy way to erase your work if you are not careful. This exercise involves writing two files. One is the usual ex Next we have line 5, where we use a new command open. Right now, run pydoc open and read the instructions.

You just opened a file. Line 7 prints a little message, but on line 8 we have something very new and exciting. We call a function on txt named read. What you get back from open is a file, and it also has commands you can give it. You give a file a command by using the. The difference is that txt. Do your read command with no parameters!

I said pay attention! You have been running scripts with just the name of the script, but now that you are using argv you have to add arguments. Look at the very first line of the example below and you will see I do python ex If you do not type that you will get an error so pay attention! It is really cool stuff. Lots and lots of fun to have in here. Study Drills This is a big jump, so be sure you do this Study Drill as best you can before moving on. Above each line, write out in English what that line does.

If you are not sure, ask someone for help or search online. Get rid of the lines where you use input and run the script again. Use only input and try the script that way. Why is one way of getting the filename better than another? Notice how you can open files and run read on them from within python3.

First thing, from the command line just type python3. Now you are in python3. Then you can type in code and Python will run it in little pieces.

Play with that. To get out of it type quit and hit Enter. Why is there no error when we open the file twice?

Python will not restrict you from opening a file more than once, and sometimes this is necessary. What does from sys import argv mean? For now just understand that sys is a package, and this phrase just says to get the argv feature from that package. Make the code exactly like mine, then run it from the command line the exact same way I do.

You can assign the result to a variable. Watch out if you care about the file. In the early days of computers data was stored on each of these kinds of media, so many of the file operations still resemble a storage system that is linear. 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. What You Should See There are actually two things you will see.

Opening the file Truncating the file. Now I'm going to ask you for three lines. Now, open up the file you made in my case test. Neat, right? If 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. Find out why we had to pass a 'w' as an extra parameter to open. Hint: open tries to be safe by making you explicitly say you want to write a file. If you open the file with 'w' mode, then do you really need the target. Common Student Questions Is the truncate necessary with the 'w' parameter?

See Study Drills 5. What does 'w' mean? What modifiers to the file modes can I use? This will open the file in both read and write mode and, depending on the character use, position the file in different ways. Does just doing open filename open it in 'r' read mode? 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. Alright, all done. It should work with any file. Try a bunch more and see what happens. Just be careful you do not blast an important file. Did you see the trick I did with echo to make a file and cat to show the file?

This script is really annoying. Try to make the script more friendly to use by removing features. See how short you can make the script. I could make this one line long. Type man cat to read about it. Try importing some things and see if you can get it right.

Make sure you know what a string is. No way you can make this one line! That ; depends ; on ; how ; you ; define ; one ; line ; of ; code.

Is it normal to feel like this exercise was really hard? Yes, it is totally normal. Everyone is different, so just keep going and keep reviewing exer- cises that you had trouble with until it clicks. Be patient. What does the len function do? It gets the length of the string that you pass to it then returns that as a number. Play with it. When I try to make this script shorter I get an error when I close the files at the end. It should already be closed by Python once that one line runs.

I get a Syntax:EOL while scanning string literal error. You forgot to end a string properly with a quote. Go look at that line again.

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: 1. They name pieces of code the way variables name strings and numbers.

They take arguments the way your scripts take argv. On the same line as def we give the function a name. This has to go inside parentheses to work. Then we end this line with a : colon and start indenting. 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 arguments and just use the names we want right inside. This is very important. What You Should See If you run ex Right away you can see how a function works. This means you can make your own commands and use them in your scripts too. Study Drills Create a function checklist for later exercises. Write these checks 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 the index card anymore: 1.

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 : after the arguments? Did you indent all lines of code you want in the function four spaces? No more, no less. Did you put the 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. The same as variable names. That tells Python to take all the arguments to the function and then put them in args as a list. This feels really boring and monotonous.

To make it less boring, take everything I tell you to type in, and then break it on purpose. 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. 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 backward, saying all the important characters. Write at least one more function of your own design, and run it 10 different ways. See how creative you can get with functions, vari- ables, and input from a user. Is there a way to analyze what this function is doing so I can understand it better?

There are many dif- ferent ways, but try putting an English comment above each line describing what the line does. Another trick is to read the code out loud. What if I want to ask the user for the numbers of cheese and crackers? You need to use int to con- vert what you get from input. No, those variables are separate and live outside the function. When the function exits these tempo- rary variables go away and everything keeps working. Keep going in the book, and this should become clearer.

But sometimes necessity means you have to use the same name, or you might do it on accident. Just avoid it whenever you can. Is there a limit to the number of arguments a function can have?

The practical limit though is about five arguments before the function becomes annoying to use. Can you call a function within a function? Write English comments for each line to understand what that line does. Find each place a function is used, and 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. Then try pydoc file. A file in Python is kind of like an old tape drive on a mainframe or maybe a DVD player.

Each time you do f. This will be explained more as you go on. First, the seek function is dealing in bytes, not lines. The code seek 0 moves the file to the 0 byte first byte in the file. We are manually incrementing it. How does readline know where each line is? The file f is responsible for maintaining the current position in the file after each readline call, so that it will keep reading each line.

Why are there empty lines between the lines in the file? There will be one thing to pay close attention to, but first type this in: ex What this does is the following: 1. Our function is called with two arguments: a and b. Python adds the two numbers. To help there is extra credit to solve a puzzle and learn something cool. 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. Do the inverse. Write 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. Remember int input?

Convert that to use the functions. In fact, this exercise is like one giant Study Drills. 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 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 step it up by writing out tables of symbols, their names, and what they do from memory. 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. Just take it slow and do not hurt your brain. Giving your brain a rest will help you learn faster with less frustration.

How modern computers store human languages for display and processing and how Python 3 calls this strings. How to handle errors in your string and byte handling. For now your job is to get a taste of the future and learn the four topics in the preceding list. This exercise is hard! I recommend you take this exercise painfully slow. Take a paragraph at a time if you must. Just chip away at it for as long as it takes.

The languages. There may be quite a few things that are new, so scan the file a few times. Just give it a try. Switches, Conventions, and Encodings Before I can get into what this code means, you need to learn some basics about how data is stored in a computer. Modern computers are incredibly complex, but at their core they are like a huge array of light switches. Computers use electricity to flip switches on or off.

These switches can represent 1 for on, or 0 for off. One represents energy, electricity, on, power, substance. Zero represents off, done, gone, power down, the lack of energy. Computers take these 1s and 0s and use them to encode larger numbers. At the small end a computer will use 8 of these 1s and 0s to encode numbers There were even huge wars in the early history of computers on nothing more than the order of these bits because they were simply conventions we all had to agree on.

There are futher conventions for encoding large numbers using 16, 32, 64, and even more bits if you get into really big math. Once you have bytes you can start to store and display text by deciding on another convention for how a number maps to a letter. In the early days of computing there were many conventions that mapped 8 or 7 bits or less or more onto lists of characters kept inside a computer. This standard maps a number to a letter. Most of the early text in computers was nothing more than sequences of bytes, stored in memory, that a computer used to display text to a person.

Again, this is just a sequence of conventions that turned switches on and off. Re- member that a byte can hold numbers , or Different countries created their own en- coding conventions for their languages, and that mostly worked, but many encodings could only handle one language.

That meant if you want to put the title of an American English book in the middle of a Thai sentence you were kind of in trouble. To solve this problem a group of people created Unicode. You can use 32 bits to encode a Unicode character, and that is more characters than we could possibly find.

Right now we use the extra space for important things like poop and smile emojis. That means we have one more convention that is nothing more than a compression encoding, making it possible for most common characters to use 8 bits and then escape out into 16 or 32 bits as needed.

You can also use other conventions encodings , but utf-8 is the current standard. On the left is the numbers for each byte of the utf-8 shown in hexadecimal , and the right has the character output as actual utf Disecting the Code We have an understanding of strings and byte sequences. In Python a string is a UTF-8 encoded se- quence of characters for displaying or working with text.

This is all based on conventions for how Python wants to work with text. Raw bytes have no convention to them. Again, Python knows its internal convention, but it has no idea what convention you need. In that case, you must use. This will be called at the end of this script to get things going. You have done this before so nothing new here. Just readline as before when dealing with text files. You will learn about this in the second half of the book, so consider this a teaser of interesting things to come.

This is an if-statement, and it lets you make decisions in your Python code. The readline function will return an empty string when it reaches the end of the file and if line simply tests for this empty string. As long as readline gives us something, this will be true, and the code under indented in, lines will run. When this is false, Python will skip lines This simplifies my code and makes it easier for me to understand it. If I want to learn what this function does, I can jump to it and study.

I am calling main again inside main. All the information you need is there. This looks like I am calling the function inside itself, which seems like it should be illegal to do. Ask yourself, why should that be illegal? That would make it loop. I pass to encode the encoding I want and how to handle errors. Remember that this then jumps to where the main function is defined on line 5, and on line 10 main is called again, causing this to keep looping.

The if line: on line 8 will prevent our loop from going forever. Encodings Deep Dive We can now use our little script to explore other encodings. Breaking It Rough ideas include the following: 1. Find strings of text encoded in other encodings and place them in the ex Extra challenging: Rewrite this using the b'' bytes instead of the UTF-8 strings, effectively revers- ing the script.

If you can do that, then you can also break these bytes by removing some to see what happens. How much do you need to remove to cause Python to break? Use what you learned from 4 to see if you can mangle the files. What errors do you get? 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 Make sure to do your checks: read it backward, read it out loud, and put comments above con- fusing parts.

Break the file on purpose, then run it to see what kinds of errors you get. Make sure you can fix it. Remember that inside the function the variable is temporary. When you return it then it can be assigned to a variable for later. What do you mean by reading the code backward? Start at the last line. Compare that line in your file to the same line in mine.

Do this until you get to the first line of the file. Who wrote that poem? I did. Not all of my poems suck. This exercise should be straightforward for you to type in, break down, and understand. However, this exercise is a little different. Instead you will import it into 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 What You Should See section to complete the exercise.

You run python3. Using this I want you to type each of these lines of Python code into python3. Take the remaining lines of the What You Should See 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? Typing 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 will have to quit python with quit to be able to reload it. Common Student Questions I get None printed out for some of the functions.

You probably have a function that is missing the re- turn at the end. Go backward through the file and confirm that every line is right. I get -bash: import: command not found when I type import ex That means you first run Python.

I get ImportError: No module named ex Python knows the file ends in. I get SyntaxError: invalid syntax when I run this. That means you have something like a missing or " or similar syntax error on that line or above it. How can the words. This is similar to how files and many other things worked when you were working with them. When should I print instead of return in a function? The return from a function gives the line of code that called the function a result.

You can think of a function as taking input through its arguments and returning output through return. The print is completely unrelated to this and only deals with printing output to the terminal. 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. Programmers 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. 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. Common Student Questions Do I have to import ex You can do either. This file has the functions from ex25 though, so first go with removing references to it.

You most certainly may. The computer is there to help, so use it as much as possible. But, how can many people be so lazy to check out? They will choose to invest their spare time to talking or hanging around.

When in fact, reading Learn Python 3. It can be one of your morning readings Learn Python 3. As recognized, in this innovative age, innovation will certainly reduce you in doing some activities. Also it is simply checking out the visibility of publication soft data of Learn Python 3. It is not just to open up and save in the gizmo. This moment in the early morning and also other spare time are to read guide Learn Python 3. Guide Learn Python 3.

Completing guide Learn Python 3. The objective is by getting the positive worth from the book till completion of guide. This is why; you need to learn more while reading this Learn Python 3. Taking into consideration guide Learn Python 3. You could pick the book based on the preferred styles that you such as. It will involve you to enjoy reading various other publications Learn Python 3.

As this Learn Python 3. So, locate your preferred book below and also obtain the connect to download guide soft file. It's a beautifully illustrated full-color Python 3.

How to read and write Python 3. This book is for Visual Learners. Visual Learners retain information very differently than their left brained counter-parts, and thus benefit from different approaches.

Full color illustrations, schema and color code snippets help memory triggers as your brain never forgets an image or a metaphor.

One page of this visual Python guide can equal one chapter of a conventional book as illustrations can make a world of difference over strictly words on a page! By Jenn G I have taught and written programming books myself and as a result have read hundreds of books on programming.

I did like the way the author approached the complex topics of variables visually, but ultimately it is just another book on Python programming.



0コメント

  • 1000 / 1000