### Iteration

Computers are great at performing some task a million times very fast.

Iteration is the fancy term for repeating some programming commands multiple times.

Python provides the while construct to iterate over  commands:

while <some condition>:
<some command>

As a simple example, suppose we want to print the numbers 0 through 4. Without using iteration, we could type the following in a Python program:

print 0
print 1
print 2
print 3
print 4

Obviously, this can get laborious-- just think if we wanted to print the first thousand or million numbers.

Iteration can help. We set up a counter variable to tell us how many times we've done something. In this case, we start the variable at 0, and add one to it until it gets to 5:

number = 0
while number<5:
print number  # this statement and the one below is executed many times, within the while loop.
number = number +1

Note that you must put a colon after the condition, and you must indent the commands that belong within the while loop.

The above code will execute the print command and the 'number=number+1' statement over and over while the condition 'number<5' remains true.

What will happen if the last line is not indented?

number = 0
while number<5:
print number
number = number +1

Note that you can stop a program, stuck in a never-ending loop, by pressing control-C. And don't worry, the computer won't get bored or start smoking from overwork.

Before we move on, let's make another mistake to see what will happen. What will happen for the following code:

while number<5:

print number
number=number+1

O.K., enough mistakes. Here’s a template for iterating through some commands n times (n=5 for this sample) Memorize this code—you’ll write very similar code anytime you want to repeat a process n times.

i=0   # initialize your loop variable
while i<5:
print i  # in general, do what you want to do 5 times here
i=i+1   # increment your loop variable

Key Things to Remember
• Remember to initialize the iteration variable (often to zero).
• Remember to increment the iteration variable at the bottom of the code within the loop

#### IN-CLASS ASSIGNEMENT, PART I

1. Try the code samples in the notes above. Enter them in a file loop.py within your samples directory.  Remember, if you get a 'never-ending loop' hold down the control key and hit C to stop it.

2. Create a program backloop.py that prints the numbers from 10 down to 1 (reverse order). Run your program by typing 'python backloop.py' at the Linux command.

3. Create a program everythird.py that prints every third number from 1 to 100 (1, 4, 7, etc.).

4. Programmer Milestone 1: Write a program total.py that adds up the first n numbers using a loop, where n is an arbitrary number. Begin by writing your program so it adds up the first five numbers. The program should print 15 (1+2+3+4+5).

Hint: you’ll need both a counter variable (i) and a variable to hold the total as you count.

Once you get the 'up to 5' working, add another variable, n, and modify your program so that the user can enter the value of n and the program will add up that many numbers.

When you have completed these problems, copy the code onto your Google Sites portfolio on a page named 'iteration'. Provide a short explanation of how each of the programs works.

## Iteration and Lists

We often use iteration to process the elements of a list. In Python, you specify a list with code such as:

students = ['bob', 'joe', 'sylvia']

If we have such a list, we can iterate through it as in the following code:

students = ['bob', 'joe', 'sylvia']
i=0
while i<3:
print students[i]
i=i+1

What happens if we loop too many times? Consider the following code:

students= ['bob', 'joe', 'sylvia']
i=0
while i<4:
print students[i]
i=i+1

Unfortunately, you'll see this  'index out of range' error a million times in your programming life.

Python provides some help-- there is a function that tells you how big a list is. The function is len, and it accepts one parameter which is the list. The following code would return 3 as a result:

len(students)

The function len is very important because it allows us to write code that will work on any list, no matter what its size. Here's another template that you'll use often:

students = ['bob', 'joe', 'sylvia']
i=0
while i<len(students):
print students[i]
i=i+1

Note that this code will work even if I add more elements to the list 'students', or remove elements.

#### Programmer Milestone 2:

A major milestone for a beginning programmer is the 'total of a list' problem: compute the sum of a list of integers, no matter how big the list.

So if you had the list [5,3,9] the program would print the result 17. Write this program in the file 'totallist.py'. Make sure your code works for different sized lists.

When you complete it, add the code and an explanation to your portfolio.