Lesson 2: Loops, if, and beer Today we're going to do some real programming: "for" loops and variables. ================= A simple loop ========================= A loop is a simple concept: something that gets executed over and over. There are lots of types of loops. Let's start with a numeric loop that lets you do something a certain number of times. Suppose you want to print "Hello, world!" five times instead of once. Here's the easiest way to do that in Python: for i in range(1, 6) : print "Hello, world!" First of all, what's i? It's a variable, like name was in lesson 1, only this time it's going to store a number. That number is going to be 1 the first time through, then 2, then 3, then 4, then 5 ... then it will stop before it gets to 6, the second number you specified in range(1, 6). It's very common when you're looping over numbers to call your loop variable i, but you don't have to. It would work just as well if you said for hippopotamus in range(1, 6) : Note the indentation of the print line. I've indented four spaces, but you can use two, or three, or whatever you prefer. But it's very important that you be consistent about your indentation. Python cares about that, and if you forget to indent to the right level, your program won't work right. That sounds like kind of a pain, but it's actually a good thing. In languages that let you be sloppy about indentation, it's easy to write code that seems fine but has bugs you don't immediately notice, and it can be a lot harder to read other people's code. The colon at the end of the for line is important too: it tells Python this is a statement that takes multiple lines. A for loop makes no sense unless you do something inside the loop; the colon tells it to expect those lines, and the indentation tells it which lines are inside the loop. You don't have to put a space before the colon. A lot of Python programmers do, but a lot of others don't. It's just a style choice. So each time Python sets i to a new value, it will print "Hello, world!" one more time. So what about that range() part? range(1, 6) gives you a list of numbers, starting at 1 and stopping just before getting to 6: [1, 2, 3, 4, 5]. So what this loop says is: For every number in the list [1, 2, 3, 4, 5] : print "Hello, world!" If you want to keep tabs on what your variable is, you can print it: for i in range(1, 6) : print "Hello, world! i =", i ========== Python console, and more on range() ============ If you're confused about range, an easy way to get more comfortable with it is to type it into Python directly. Run python -- just that, without a filename or any other arguments. Then when you see the >>> prompt, type: range(1, 6) and see what it prints out. The Python console is handy any time you have a few simple commands to test, and you don't want to make a new file just to test them. You can even type your whole loop into it: just don't forget to indent the lines inside your loop! range can do more than count by ones. If you ask for range(0, 10, 2) it will count two at a time. At the Python >>> prompt, try typing: range(2, 10, 2) [2, 4, 6, 8] You can even tell range to count backward. Try this: range(5, 0, -1) See what it's doing? To get each new number, it's taking the current number and adding -1. Then if it's gotten to the end point, 0, it stops and doesn't include that. ================= if statements ======================= One more thing for today's lesson: if statements. for i in range(1, 6) : if i == 3 : print "Hello, world! i =", i If statements are fairly straightforward. If the variable i is equal to 3, do whatever's inside the if (any lines that are indented). The only tricky part is that double equals sign. Why double? A single equals is for when you're setting a variable, like in lesson 1: name = "Amelia Earhart" So you have to use == when you're comparing two values (in this case, i and 3), or else Python will give you a syntax error. You'll find == in most modern languages, so it's a good rule to be familiar with. You can stack up a bunch of conditions by "elif", meaning "else if". for i in range(1, 6) : if i == 3 : print "i is three!" elif i == 4 : print "now it's four!" else : print "Hello, world! i =", i ===================== Homework ======================= 1. Your first programming assignment! Write a program for the old "bottles of beer" song. It should print something like: 99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall ... all the way down to 1. Extra credit: make it print the number twice, e.g. 99 bottles of beer on the wall, 99 bottles of beer 2. What does range(0, 10, -1) give? Any idea why? 3. How was the length of this lesson? Too much, too little or just right? Problem 4 is optional -- it's a little harder, but give it a try, and if you don't get it, take a look at the answers other students post. But try it on your own first, if you can! 4. Write a loop using range() that prints out the first five numbers as words: one two three four five