For loop in Python (for in loop) | FACE Prep

For loop in Python is used to iterate over a sequence of numbers, a string, a list, a tuple or a dictionary. The working of the for loop remains the same in all programming languages. However, the syntax is what differs. In this article, we will discuss Python for loop with several examples.

Why do we need a for loop?

For loop in Python is used to traverse sequentially through lists, tuples, strings, dictionary, and sets. Using the for looping technique, we can execute a set of statements once for each item in the sequence. The statements inside the for loop are separated from the rest of the code using indentation (Here is how to indent it the right way!)

Flowchart of for loop in Python

Here is the execution flow of a for loop in Python programming.

flowchart of for loop in python

Syntax of for loop in Python

Here is the syntax of for loop in Python programming.

for variable in sequence:
    statement (s)

Examples of for loop in Python

In Python, for loop can be applied to lists, strings, etc. Let us see how it is applied with examples.

a) for loop in Python list

Since a list contains a sequence of elements, we can use the for loop to traverse through it and access each element of the list. For example,

#printing the elements of a list using for loop
list = [20, "FACEPrep", 50.98]
for i in list:
    print(i)
Output:
20
FACEPrep
50.98
Explanation: Here, i is a variable that stores the element of the list in each iteration and the for loop continues until it reaches the last element of the list. Also, note that in Python, the for loop does not require an indexing variable (such as i in this case) to be declared beforehand. Now, let us look at how to iterate over a list using the range function.
#printing a list using range function
color = ['Green', 'Pink', 'Blue']
for i in range(len(color)):
    print(color[i])
Output:
Green
Pink
Blue

Explanation: In the print statement, if color[i] is replaced with i, then the interpreter prints the index values of the elements as shown below.

Output:
0
1
2

b) for loop in a string 

A Python string contains a sequence of characters. So we can use the forloop to traverse through it and access each character of the list. For instance, 

#printing the elements of a string using for loop
for i in 'FACE prep':
    print(i)
Output:
F
A
C
E
P 
r
e
p

c) for loop in Numbers (Using range() function in for loop)

To print or access a sequence of numbers in Python, for loop could be used along with the range() function. Here is how this combination works (Know more about the range function)
#printing a series of numbers using for and range
for i in range(5):
    print (i)
Output: 
0
1
2
3
4
#printing a range of numbers using for and range
for i in range(5, 10):
    print (i)
Output: 
5
6
7
8
9
#printing a series of numbers using for and range
for i in range(5, 10, 2):
    print (i)
Output: 
5
7
9

c) Nested for loops

One for loop inside another for loop is called nested for loops. The inner for loop will be executed once per iteration of the outer for loop.

#iterating through nested for loops
color = ['Green', 'Pink']
object = ['flower', 'ball']
for i in color:
    for j in object:
        print(i, j)
Output:
Green flower
Green ball
Blue flower
Blue ball

Using Control Statement in for loops in Python

Control statements in Python like break, continue etc can be used to control the execution flow of for loop in Python. Let us now understand how this can be done.

a) break in for loop

Using the break statement, we can exit from the for loop before it has looped through all the elements in the sequence as shown below. As soon as it breaks out of the for loop, the control shifts to the immediate next line of code. For example,

color = ['Green', 'Pink', 'Blue']
for i in color:
    if(i == 'Pink'):
        break
print (i)
Output: Pink
Explanation: Here, in the second iteration, the if condition becomes True. Hence the loop beaks out of the for loop and the immediate next line of code i.e print (i) is executed and as a result, pink is outputted.

b) continue in for loop 

The continue statement is used to stop the current iteration of the loop and continue with the next iteration. An example of how this works is shown below.
color = ['Green', 'Pink', 'Blue']
for i in color:
    if(i == 'Pink'):
       continue
print(i)
Output:
Green
Blue

Explanation: Here, in the second iteration, the condition becomes True. Hence the interpreter skips the print (i) statement and immediately executes the next iteration.