3) b. For Loop

Loops are an essential part of Python or any programming language for that matter. They’re especially useful when dealing with data structures. That is why before you learn Data Structures, you must learn about the for-loop.

How can you repeat something a given number of times? To do that you can use a for-loop combined with the range function.

First, the range function generates integers in a range that you specify.
For example, if you specify range(5) , then it will be translated to a bunch of values 0,1,2,3,4.

Note that there are totally 5 numbers created. Since we started from 0 we end on 4.

Similarly, if you use range(10), then it creates a range of numbers from 0..9. So how is this helpful?

You combine the range function with a for loop. This causes the for to repeat a code the given number of times.

This is an easy way to repeat a code. You might wonder how exactly this works. It’s pretty simple actually.

Explanation: First the range function generates a series of n integers starting from 0 and going upto n-1.

Then the for loop causes the variable number to hold each of these values in turn and each time runs the code specified within the for loop. You can see number holds with the following program:

# Output:
# 0
# 1
# 2
# 3
# 4