Counting with a for Loop


Counting with a for Loop

When you write a program, you'll often find that you need to count. And for loops are usually the best way to go. In combination with the for loop, you can use Python's range() function to count in all kinds of ways.

Introducing the Counter Program

The Counter program is nothing fancy, but it shows you how to use the range() function to generate lists of numbers. Paired with a for loop, you can use the list to count forwards or backwards, or even to skip numbers if you like. Take a look at Figure 4.3 to see the results of the program.

click to expand
Figure 4.3: The range() function and for loop allow you to count forwards, by fives, and backwards.

Here's the code for the program:

 # Counter # Demonstrates the range() function # Michael Dawson - 1/26/03 print "Counting:" for i in range(10):     print i, print "\n\nCounting by fives:" for i in range(0, 50, 5):     print i, print "\n\nCounting backwards:" for i in range(10, 0, -1):     print i, raw_input("\n\nPress the enter key to exit.\n") 

start sidebar
IN THE REAL WORLD

It's traditional to name generic counter and loop variables i, j, or k. Normally, you want to create descriptive, clear variable names. Believe it or not, i, j, and k are clear to experienced programmers, who know when reading your code that you just need a quick, counter variable.

end sidebar

Counting Forwards

The first loop in the program counts forwards:

 for i in range(10):     print i, 

This for loop works just like the for loop you saw in the Loopy String program—it loops through a sequence. It just may be hard to tell what the sequence is. The sequence the loop moves through is created by the range() function. It creates a sequence of numbers. Give range() a positive integer and it will create a sequence starting with 0, up to, but not including, the number you gave it. Take a look at part of an interactive session I ran with IDLE:

 >>> range(5) [0, 1, 2, 3, 4] >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

TRICK

Even experienced programmers sometimes forget the way a function or a command works. But instead of guessing, they open an interactive window and experiment. When they get the results they want, they jump back to script mode and use what they learned to continue coding.

Another way to look at this loop is to substitute the results of the range() function into the code when you read it. So, when you look at the code, you can imagine that it reads:

 for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:     print i, 

and that the range() function call is replaced with the sequence of numbers it creates. In fact, this loop is a valid one. You can create a list of values by enclosing them in brackets, separated by commas. But don't go off creating a bunch of lists just yet. You'll learn all about lists in the Chapter 5, "Lists and Dictionaries: The Hangman Game," I promise.

Counting by Fives

The next loop counts by fives:

 for i in range(0, 50, 5):     print i, 

It does this with a call to range() that creates a list of numbers that are multiples of 5. To create a sequence of numbers with range(), you can give it the start point, the end point, and the number by which to count. Here, the sequence starts at 0, and goes up by 5 each time, to, but not including, 50. I used interactive mode again so that you can see the exact sequence range(0, 50, 5) produces:

 >>> range(0, 50, 5) [0, 5, 10, 15, 20, 25, 30, 35, 40, 45] 

Notice though that the sequence ends at 45. Remember, 50 is the end point, so it's not included. If you wanted to include 50, your end point would have to be greater than 50. So, range(0, 51, 5) would do the trick.

Counting Backwards

The last loop in the program counts backwards:

 for i in range(10, 0, -1):     print i, 

It does this because the last number in the range() call is -1. This tells the function to go from the start point to the end point by adding -1 each time. This is the same as saying "subtract 1." Again, the end point isn't included, so the loop counts from 10 down to 1 and does not include 0.

TRICK

There's no law that says you have to use the loop variable inside a for loop. You might find that you want to repeat some action a specific number of times. To do this, create a for loop and just ignore the loop variable. For example, let's say I just wanted to print "Hi!" 10 times. The following two lines are all I would need:

 for i in range(10):     print "Hi!" 




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net