Close
Close

Python For Loop


If you are here, it means you are going to be a coder soon. You will solve real-world computer problems, make your own programs, and a lot more. Yeah, sounds good!

Now let's talk about for. It is also a loop like while. Let's see how it works.

Python for Loop


Python for loop Syntax


for iter in sequence:
    statement
    statement
    ...

sequence is a sequence like list, tuple, string, etc. iter is a variable that takes the value of each item in the sequence.

iterator animation

The body of the for loop consists of all the indented statements below for iter in sequence:.

indentation for body of for loop

Python for loop Examples


colors = ["Blue", "Green", "Yellow", "Orange", "Red", "Brown"]
for color in colors:
    print(color)
Output
Blue
Green
Yellow
Orange
Red
Brown

By looking at the output, you might have got some idea of what the for loop does. Let’s understand the code.

colors is a list here containing six items. 

for color in colorscolor is a variable which goes to each element in the list colors and takes its value.

So, in the first iteration, color is equal to the 1st element of the list. In the second iteration, color is equal to the 2nd element of the list, and so on.

Therefore, in the first iteration, the value of color is “Blue” and so print(color) printed Blue. Similarly, in the second iteration, Green got printed, and so on.

animation of for loop

Notice that the body of for loop is also represented by equal indentation (margin) from the left.

Python Example of Sum of Marks using Loop

marks = [78, 98, 50, 37, 45]
sum = 0
for m in marks:
    sum = sum + m
print(sum)
Output
308

You must have understood the code. If not, then let us explain a bit.

sum = 0 → We are taking a variable sum and assigning it an initial value of 0.

In the 1st iteration, m is 78. So, sum + m (0 + 78 = 78) makes the value of sum as 78.

In the 2nd iteration, m is 98. So, sum + m (78 + 98 = 176) makes the value of sum as 176.

After all the iterations, sum becomes 308.

We will look at the use of for loop with lists, strings, tuples and other sequences in detail in later chapters.

Python range() function


The range() function is used to generate a sequence of numbers. 

Python range Syntax


range(start, stop, step_size)

start - Integer from which the sequence starts. It is optional. Its default value is 0.

stop - Integer before which the sequence stops. It is mandatory.

step_size - Increment between each integer in the sequence. It is optional. Its default value is 1.

range(10) generates ten integers starting from 0 to 9. Here, start = 0, stop = 10, step_size = 1.

range(1, 10) generates nine integers starting from 1 to 9. Here, start = 1, stop = 10, step_size = 1.

range(1, 10, 2) generates the integers 1, 3, 5, 7, 9. Here, start = 1, stop = 10, step_size = 2.

range function in Python

Python range Examples


In the last chapter, we printed the first 10 natural numbers using a while loop. Let’s do that using a for loop.

for n in range(1, 11):
    print(n)
Output
1
2
3
4
5
6
7
8
9
10

The next example prints the multiplication table of 14 using a for loop.

for i in range(1, 11):
    print(i*14)
Output
14
28
42
56
70
84
98
112
126
140

Now let’s print all the even numbers from 10 (included) to 20 (included).

for num in range(10, 21, 2):
    print(num)
Output
10
12
14
16
18
20

That was cool, right? We could print the desired results using just 2 lines of code. We suggest you practice more questions on loops from our practice section.

To learn from simple videos, you can always look at our Python video course on CodesDope Pro. It has over 500 practice questions and over 20 projects.
How difficult life may seem, there is always something you can do and succeed at.
- Stephen Hawking


Ask Yours
Post Yours
Doubt? Ask question