Control Flow

The For Loop

For loops are a mechanism to repeat something for a certain number of times. For loops provide us with a different way of thinking about looping. We get to define a fixed end as part of the statement. Just like a while loop and an if statement, for loops are blocks. They begin with the for keyword and are followed by a variable declaration. There is then the in keyword and then a list. It is completed with a colon and then the body of the loop is indented. We haven't covered lists yet so till then you are going to learn about and use a special function called range() that creates lists.

for num in range(1,11,1):
    print num

One thing to note from this loop is that the variable declaration num that we made in the loop can be used within the block. Python lets you use it outside of the block but in my opinion this is bad. The reason why is because what if you had a loop like this:

for num in range(0, 0, 0):
    print num
print num

The print statement that is outside of the block would fail because we never initialize the num variable since there were no iterations. It would work if you had any iterations (eg. range(0, 1, 1)). This is inconsistent and something that most other programming languages disallow. You should never expect the variables that you initialize inside of a block to exist because if you never actually looped, then they won't.

Try running the for loops above with both of the range functions I have shown! One should work and the other should fail!

The Range Function

The range() function returns a list of numbers and takes three arguments:

  1. the starting number in the list
  2. the terminating number that all numbers in the list will be less than
  3. the step number.

For example range(1, 10, 2) would return [1, 3, 5, 7, 9]. And range(0, 10, 1) would return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. We use this function in the for loop to give us a list of items to iterate.

Try writing a loop that prints all of the multiples of 3 less than 50!

For vs While

Notice that if we were to do the same thing as our example for loop using a while loop it would look like:

num = 1
while num < 11:
    print num
    num += 1

The for loop is a much better fit because we have a fixed number of times we wish to iterate. This doesn't mean that a while loop isn't as good. They simply are better at different things. You might recall that in the while loop lesson I challenged you to create a program that continued to ask "Why?" until the user answered with "just because!". That kind of loop isn't really possible with a for loop.

To know when to use a certain kind of loop try saying aloud how you want to loop. If you end up saying something like, "I want to loop for as many times as there are ..." then you probably want a for loop. If you say something like, "I want to keep printing "You're wrong" until they get the right answer, or while their answer isn't right" then you probably want a while loop.

Infinite Loops

for loops don't typically have the same problem with infinity as while loops. The reason is because your terminating condition is often better defined. Infinite loops can still happen and that is when you add to the loop you are iterating while you are looping over it. Imagine I have a list: [1, 2, 3, 4, 5] that I am iterating. Before the first iteration I have 5 numbers to go through. If I add 6 to the list during the first iteration then my list will be: [1, 2, 3, 4, 5, 6]. I just used 1 so the remaining number of items in my list is 5... Uh oh! We still have the same number of iterations. If we were to continue adding numbers in every iteration, then we would never reach 0. This is an infinite loop. Since you are going to be using the range() function for all of your for loops you won't run in to this problem but remember when we do learn about lists that you shouldn't add to the list you are looping over.

results matching ""

    No results matching ""