 The while loop is ideal for cases when we don't know how many times a loop will iterate. Sometimes, though, we know the exact number of times we want to go through a loop. In these cases, we'll use something called the FOR loop. Let's say we have the numbers for how many things we've sold each quarter in one year. We'd like to find the sum of the four numbers. We could have four separate variables and add them up. But what if we had 12 months of sales? We'd need 12 variables and things would quickly get out of hand. Returning to our original example, we can solve this problem in Python by using a list. You create a list in Python by separating the numbers with commas and putting them in square brackets. We'll learn much more about lists in Chapter 7. We can now set a total to zero and start a loop with the keyword FOR, followed by a loop variable. It can have any name you want, but for this example, item is a good name. The keyword IN and the name of the list you want to process. And here's the body of the loop. The loop starts by setting the item variable to the first element in the list, and then does the body of the loop. The next time through the loop, it sets the item variable to the next element in the list, and does the body of the loop again. And the same for the next item, all the way through the last item. Once the last item is processed, the loop ends, and the program goes on to print the total. Here's the program. Let's run it to see that it works. And there's our total. I'll add a print statement inside the loop to show what's happening as the loop progresses. I'll print the current item and the current total each time I go through the loop. And you can see the item being assigned each element of the list in turn and the total accumulating. If we wanted to do the monthly sales total, we'd replace the list with the list of 12 items, and I've commented out loop progress printing. And when we run it, it works as well, giving us the accumulated total for all 12 months. In fact, using a loop to accumulate a total is called the accumulator pattern. That's one way to use a for loop. Another way to use a for loop is to do counting. Here's a program that calculates compound interest on $100 at 10% a year for five years. The for loop will use a list of the numbers one through five to make sure it goes through five times. The program works when we run it. But it's not very generic. Instead of always having a balance of $100, we'll say compound interest at a given percent on a starting balance and display results for as many years as the user requests. That means we'll change balance from $100 to float of input, enter starting balance, and the percent will be float input, enter percentage rate. We'll want the number of years, so n years will become int of input, enter number of years. And now we have a problem. If the user says they want 20 years instead of 5, we won't know what n years is until run time, but we have to make this list at compile time if we're going to use numbers separated by commas. Python has a solution for this problem, the range function. In its simplest form, you give range a number. And that's equivalent to using this list starting at zero going up to but not including the number you specified. Notice by the way there are five elements in the list, which is the number that we gave. If you give range two numbers, that's equivalent to a list starting at the first number going up to but not including the second number. If you give range three numbers, it's equivalent to a list starting at the first number going up to but not including the second number, increasing by the third number that you give it. In this case, it starts at three, adds two to get five, then adds two again to get seven. Range won't add two to get nine because nine isn't included. Any of the numbers you give to range can be in a variable. For example, if we've set a variable n, this version of range will create the equivalent of a list from one up to but not including n. And that's the kind of thing we need in this program. We'll use range to start at one and go to n years plus one. We have to add one because remember range goes up to but not including our end limit. And now we have a program that will calculate compound interest with any values we want. Let's run it and start with $250 and 25 cents and a percentage rate of seven and a half percent for nine years. And there are the results and that's our for loop. A for loop can process the items in a list one after another and we can use range to do accounting loop.