For Loops

(NOTE: If you have already completed the while-loops lessons, some concepts here will be familiar. For-loops and While loops have many similarities, but also a few differences, so pay close attention!)

There are many instances in program-writing when we want the ability to run the same code multiple times in a row. One easy and obvious solution to accomplish this would be to just copy and paste whatever code we want to repeat however many times we need it to run.

word = input('Give me a word: ')
first_last = word[0] + word[-1]
print("Here\'s the first and last letter: " + first_last)

If we want to repeat this four times, we could just write:

word = input('Give me a word: ')
first_last = word[0] + word[-1]
print("Here's the first and last letter: " + first_last)

word = input('Give me a word: ')
first_last = word[0] + word[-1]
print("Here's the first and last letter: " + first_last)

word = input('Give me a word: ')
first_last = word[0] + word[-1]
print("Here's the first and last letter: " + first_last)

word = input('Give me a word: ')
first_last = word[0] + word[-1]
print("Here's the first and last letter: " + first_last)

This works (you’re welcome to give it a try in your IDE) but it is bad for several reasons:

One way to get around this issue is by using for loops. A for-loop is a feature of python (and other programming languages too) that gives us the ability to repeat code a customizable amount of times. The basic structure of a for-loop is:

before code
for index in range(0, 4):
    inner code
after code

The key here is the second line that says for index in range(0, 4):. When this line of code is reached, python created a variable named index (of whatever the user types after the if). This variable will be incremented by 1, starting at the value 0, and ending at the value 4, since that is the range that we specified. Before each increment, it will run whatever code is indented underneath it by four spaces in the inner code area. In the example I only put one line there, but you can indent multiple lines underneath it and it will execute the whole thing. The flow of this example outline is as follows.

graph LR classDef blue fill:#cef,stroke:#059; BF[before code] --> F[for....] F --> |index < 4| IC[inner code] IC --> |add 1 to index| F F ----> |index >= 4| AF[after code] class BF blue; class AF blue; class IC blue; class F blue;

Here’s a basic, working example using valid python:

 1  print('before')   
 2  for i in range(1,5):    
 3      print('hi')   
 4      print('i variable is: ' + str(i))
 5  print('after')

When you run this, you should get:

before
hi
i variable is: 1
hi
i variable is: 2
hi
i variable is: 3
hi
i variable is: 4
after

Notice how the two lines indented under the for loop on line 2 (lines 3 and 4) were repeated 4 times. The first time the code ran, the i variable was 1. On each iteration thereafter, it incrementally increased by 1 each time until it hit 4. The loop will end when the loop variable (named i in this case) reaches 1 less than the second number specified in the range to loop through.

Now, to revisit the original problem. We want a way to repeat the code that prints the first and last letter of the user input 4 times (or other amounts of times too, if we change our mind!) As you might imagine, we could plug this code into a for-loop that is set up to repeat 4 times to get this done. We could use:

for count in range(0, 4):
    word = input('Give me a word: ')
    first_last = word[0] + word[-1]
    print("Here's the first and last letter: " + first_last)

However, we don’t have to use the range 0-4. We could instead write this and it would work the same way, since we never print out the count variable:

for count in range(101, 105):
    word = input('Give me a word: ')
    first_last = word[0] + word[-1]
    print("Here's the first and last letter: " + first_last)

Both of those loops will repeat the code four times, and then the program would end. If our needs changed, and all of a sudden we needed to repeat this code 10 times instead of four, all it would take is one, small change.

for count in range(0, 10):
    word = input('Give me a word: ')
    first_last = word[0] + word[-1]
    print("Here's the first and last letter: " + first_last)

Nice! Much easier and neater than having to copy and paste the same code a bunch of times.



PyFlo Home Complete Bookmark Next