Course set three full notes

Feb. 8, 2015
Upcoming SlideShare
python isn't just a snake
1 of 15

Course set three full notes

1. Raspberry Pi: a class in several parts III: Software 3-1: Introduction to Python Objectives:  Basic familiarity with the IDLE tool  Basic data manipulation using Python syntax All the software for this sequence should already be on the Pi, as part of Raspbian. Verify that the IDLE tool is available in the X Windows System. What's the point? Giving the computer instructions, to tell it to do exactly what you want. It can't do what it doesn't know, so we humans have to decide how to teach it, and craft our programs appropriately. Games, web, documents, security, music, virtually anything is possible. Basic data manipulation: Type a number into IDLE and press enter. It examines what you told it, does it, and tell you the result. In this case, it tells you what number you entered...because there is nothing else to do. Try some basic math, e.g. 1 + 1. It tells you the answer is 2. Some basic operators are as follows:  + (addition)  - (subtraction)  * (multiplication)  / (division)  % (modulo or remainder, as in 10 mod 3 == 1)  ** (power, as in 4**2 = 4 squared = 16) Try 10 / 3. Notice that the answer is 3. Now try 10 / 3.0. Notice that the answer is 3.3333333. If you tell it that you care about decimal places, you get decimal numbers. Otherwise, you get integers. Take a few minutes to use Python as a calculator. There are some editing shortcuts that might also help at this point.  Ctrl-End will always get you back to the current prompt, if you move the cursor elsewhere.  Alt-p will put the previous entry at the prompt again. If you hit it again, it will cycle through entries previous to that as well.  Alt-n will go to the next entry. This is particularly useful if you are looking for something in particular with Alt-p, but you pass it.
2.  Ctrl-c will interrupt what is currently happening, clear the current programming entry, and put your cursor at the prompt. If you don't know what is happening, this can help you reset to a known state.  Copy and paste do work. Be careful of indentation if you choose to paste, and also be careful of the >>> characters. Variables: (Attendees who have done some algebra will find this much easier.) In its basic form, a variable is a box. You can put things in the box, you can take things out of the box, you can forget what's inside, and then look to see what it is. When you carry it around, though, it's still a box. Boxes come in different shapes and sizes, but all of ours are going to be the same size for now. Let's put the number 12 in a variable, by telling Python “x = 12” – notice that Python responds by telling us absolutely nothing. Now, let's ask Python what x is: >>> x 12 >>> Next, let's use our variable in some arithmetic expressions. Try things like x + 1, x**x, 42 / x, etc. Notice that it works just like the number 12. Now, let's change x to something else. Try the following: >>> x = x - 3 >>> We can change what's in the box without actually looking at it, which is convenient. Have a look to see what its value currently is. >>> x 9 >>> You can call variables pretty much whatever you like, as long as you use letters and no spaces. Numbers are okay within a variable name, and multi-word variable names are usually distinguished by capitalizing all but the first one. So, the following are all valid: x, y, xy, george, john, paul, ringo, octopus, thisIsAVeryLongVariableName, theSong, grocerylist. Be descriptive, and try to label your boxes so you know what's inside them.
3. Strings: We've seen how Python can act as a calculator with numbers, and how we can use words to name variables. What if we want Python to talk to us? If you try to tell it “hello” you get a nasty-looking error: >>> hello Traceback (most recent call last): File "<pyshell#22>", line 1, in <module> hello NameError: name 'hello' is not defined >>> Such errors are commonplace and not to be feared. They simply indicate that something went wrong, and usually provide useful information for fixing the problem. In this case, Python doesn't know anything about hello – it is trying to interpret it as a variable or other identifier. We want it to be treated as a literal string of characters, so we have to put it inside quotation marks: >>> "hello" 'hello' >>> Notice that when Python tells us 'hello' it is between a pair of apostrophes, also called single quotes. Either single or double quotation marks are acceptable, as long as they match. We can also add strings together, and we can put them in variables. For example: >>> y = 'hello' >>> y 'hello' >>> y + " world”
4. 'hello world' Remember how we have the number 9 in x at the moment? It is often useful to insert the values of numeric variables into strings, but to do so we must use a different character: the backtick. It points toward the right, while the apostrophe On US keyboards, it is usually on the same key as the tilde. >>> y + " " + 'world' + `x` 'hello world9' >>> This may be a reasonable stopping point – the attention to detail required for success throughout these examples may take some effort for attendees, especially if you have some who do not have experience with algebra. Pack up the Pi kits as normal. 3-2: Structures Objectives:  Learn how to manipulate lists (arrays)  Learn how to automate repetitive actions (loops)  Learn how to make decisions in code (conditionals) Set up the Pi kits. Start the X Windows System (startx)and launch IDLE. Briefly review the syntax from the last session. We will make use of variables and strings, especially. Most of the time we don't deal with just one thing. Consider a trip to the grocery store: does the shopping list contain only one item? Sometimes, but most of the time it is rather longer than that. Python lets us represent lists of things easily. We put them between square brackets []. >>> groceries = ['milk', 'french fries', 'TACOS', 'ketchup', "lettuce", 'penguins', 'MORE TACOS'] >>> groceries ['milk', 'french fries', 'TACOS', 'ketchup', 'lettuce', 'penguins', 'MORE TACOS'] >>> Notice that they are in the same order as they were when you entered them. How many are there? In order to find out, we can use a function of Python called len (for length). Functions are repeatable actions. If you hand a box of stuff to somebody and ask
5. them to count what's inside, they can do that, and tell you the number. The len function follows exactly the same principles. We give the len function a list – or a variable that contains a list – and it tells us how many things are inside it. We do the giving by putting it inside parentheses (). >>> len(groceries) 7 >>> Okay, so we have seven items on the list. Can we get the second item on the list? Yes! But Python does a weird counting thing. The first item is number zero, the second item is number one, and so on. The last item on this grocery list is number six. Many programming languages start counting at zero, so as strange as this might be, it is a useful thing to remember. In order to get an item from the list, we enclose the item we want after the variable name. For the third item, we do the following: >>> groceries[2] 'TACOS' >>> Remember that we start counting from zero, so the third item is at position number two. This is commonly pronounced “groceries sub two” because we are finding an item with a particular subscript in the array. Now, what if we want to alphabetize the grocery list? For this task, we can ask the list to go off and sort itself. We do so with a dot, and since it already knows what to do, we don't have to give the sort function anything in particular, so we give it just an empty parameter list – that is, nothing between the parentheses. >>> groceries.sort() >>> groceries ['MORE TACOS', 'TACOS', 'french fries', 'ketchup', 'lettuce', 'milk', 'penguins'] >>> This idea of asking a thing to do something is very, very powerful: it allows us to model the world around us. What if we were to write a dog, and then we could ask it to bark(quietly)and fetch(newspaper)? After receiving the newspaper, could we ask it if it isCoveredInSlobber()or if it isCurrent()? At any rate, what if we want to print each item in the grocery list on a separate line? First of all, we can print something using the print()function: >>> print("hello") hello
6. >>> This is important because it can be used in virtually any program, not just the interactive IDLE environment. So now we can print each item in turn. >>> groceries[0] 'MORE TACOS' >>> groceries[1] 'TACOS' >>> groceries[2] 'french fries' >>> groceries[3] 'ketchup' I don't know about you, but I'm getting tired of typing “groceries” over and over again. Let's take a short cut and tell the computer to do that for us. The way we go about this is using a loop: for item in groceries: print(item) This is a fairly readable syntax. Python will go through the list of groceries, and for each thing on the list it will call it an “item” and then it will print it. At this stage it is critical to point out a couple of syntactic elements. First of all, the colon (:) indicates that we are not done with what we are doing. Second, indent ation matters. Although it is simply whitespace, many of the most common errors result from misaligned or missing indentation within program structures. Let's do the same thing another way. Instead of iterating over some items, let's iterate over some numbers. The following syntax lets us do this: >>> for index in range(0, 5): print(index) 0 1 2 3 4 >>> Notice that the first number of the range (0) is included, but the last number of the range (5) is excluded. Remember how len(groceries) told us there are 7 items in the list? This works out nicely for us, because the zero-based counting scheme means that item 6 is the last one in the list, and a range from 0 to 7 will include 6 but not 7.
7. Let's put the pieces together. >>> for index in range(0, len(groceries)): print(groceries[index]) MORE TACOS TACOS french fries ketchup lettuce milk penguins >>> Now let's number the items. >>> for index in range(0, len(groceries)): print(`index` + ": " + groceries[index]) 0: MORE TACOS 1: TACOS 2: french fries 3: ketchup 4: lettuce 5: milk 6: penguins That's great, but wouldn't it be nice (for humans) to start the list at 1? We could do the following: >>> for index in range(0, len(groceries)): print(`index + 1` + ": " + groceries[index]) 1: MORE TACOS 2: TACOS 3: french fries 4: ketchup 5: lettuce 6: milk 7: penguins >>> This is starting to get complicated. Let's simplify a little bit by splitting up the print
8. statement. >>> for index in range(0, len(groceries)): num = index + 1 print(`num` +": " + groceries[index]) 1: MORE TACOS 2: TACOS 3: french fries 4: ketchup 5: lettuce 6: milk 7: penguins >>> We can take the “index + 1” from the print and put that into num, then use num in the print instead. Notice that both lines in the loop are indented. If the indentation does not match, Python will complain. Decision making: There are lots of times when we need to make a decision in a program. If you're playing tic-tac-toe and get three in a row, then the game is over. If the damage your character receives is less than your current hit points, then your character lives. If the user enters invalid input, print a message that indicates what was wrong. If the train is carrying carrots, then give it top priority; otherwise, give the train priority just behind Amtrak. (Nobody likes spoiled carrots, after all.) Of course, in order to make a decision, we have to be able to test things. We do this using Boolean operators. Here is a brief list of the most common operators:  == true if two things are equal  != true if two things are not equal  > true if the thing on the left is greater than the thing on the right  < true if the thing on the left is less than the thing on the right  >= true if the thing on the left is greater than or equal to the thing on the right  <= true if the thing on the left is less than or equal to the thing on the right  && true if the thing on the left AND the thing on the right are both true  || true if either the thing on the left OR the thing on the right is true In Python, we can make make decisions using the if/elif/else construct. if <condition>: do something important if <condition>:
9. do something important else: do something less important if <condition>: do something important elif <different condition>: do something cool else do something normal For example, let's consider the numbers from 0 to 9, and determine whether they are even or odd. >>> for x in range(0, 10): if x == 0: print(`x` +": found zero!") elif x % 2 == 0: print(`x` +": even!") else: print(`x` +": odd.") 0: found zero! 1: odd. 2: even! 3: odd. 4: even! 5: odd. 6: even! 7: odd. 8: even! 9: odd. >>> Feel free to play with loops, lists, conditionals – these basic constructs can represent the decision making process for a lot of problems. It's also fun to find the limits of what Python can do. How many things can you put in a list? How long does it take to count to a million? How long does it take if you don't print each number? When you're satisfied, or your brains hurt, or you're out of time, pack up the Pi kits, as normal. 3-3: Project
10. Objectives:  Learn how to handle input  Learn how to define a function  Put together a program that acts as a “magic 8-ball” Set up the Pi kits as normal. Start the X Windows System and open IDLE. It can be useful to interact with a user. We have seen how to create output, but what about handling input from the keyboard? There are two related ways to do this. The first is the input function, which handles numbers well: >>> input("Number -->") Number -->12 12 >>> Notice how Python prints whatever you tell the input function to use, and waits for you to type something and press enter. If you enter anything other than a number, you will get some kind of error. [Exception: it is possible to enter valid Python code that evaluates to a number. For example, if you type len(“hello”) at the prompt, it will be accepted and return the number 5.] That's great, but what if you want to hold on to what the user entered? Use the input()function as part of a variable assignment. >>> x = input("What is x? ") What is x? 42 >>> x 42 >>> Being limited to numbers only is pretty restrictive. If you want to accept anything the user enters, you can use the raw_input function instead. >>> x = raw_input("What is x? ") What is x? The quick brown fox jumped over the lazy dogs. >>> x 'The quick brown fox jumped over the lazy dogs.' >>> Notice, however, that if you provide a number to raw_input, it is still interpreted as a string, so you cannot directly perform operations like “x + 1” meaningfully.
11. Functions: We have seen several cases where we ask something like len() or sort()or input()to do a task for us, repeatedly. We can define our own such tasks, and they are called functions. In a basic sense, they have a name, they may accept some input, and they may return a value. The easiest way to define a function is with the def keyword, and we use the return keyword to indicate what comes back. >>> def plus(a, b): return a + b As usual, when we put the colon on the end of the first line, we're saying “I'm not done” and the subsequent lines are consistently indented. Python allows us to do just about anything we like within a function definition, including calling (or even creating) other functions. Now we can call our function just like any other: >>> plus(3, 2) 5 >>> There are lots of functions that are already defined for us, but sometimes we have to tell Python where to find them. There is a random number generator, for example, that we can use if we ask Python to load it: >>> import random The random() function gives us a decimal number between 0 and 1: >>> random.random() 0.09922611904874357 >>> random.random() 0.5130440719955642 >>> random.random() 0.2534538950733807 >>> random.random() 0.8071376093891092 More frequently, however, we will use random numbers that are integers – consider a die roll (1d6), for example. The function randint(min, max)helps us with that. >>> random.randint(1, 6) 5 >>> random.randint(1, 6) 4 >>> random.randint(1, 6) 3 >>> random.randint(1, 6)