For loop in Python (with examples)

for loop in python

Hello Techies,

In this tutorial, we will learn about For Loop in Python with the help of examples.

The For loop is the most common loop in the Python programming language. So let us check the use of for loop in python with the help of an example.

What is for loop in Python?

The For Loop is useful to iterate over the elements of sequence such as string, list, tuple, etc. The syntax of the for loop in Python is different from C ++ or Java.

Python for loop syntax:

for each in sequence:
    body of the for loop ...

The for loop starts with for keyword, then you need to assign a variable that provides each element in the sequence, then the in keyword, and finally the sequence.

Lets see a Python for loop Example:

>>> for each in ['P','Y','T','H','O','N']:
...     print(each)
... 
P
Y
T
H
O
N

In the above program, we checked how to Iterate over a list in Python using For loop.

Let’s look at another example of the Python program to iterate over dictionaries using for loops.

>>> d = {'A': 1, 'B': 2, 'C': 3}
>>> for key, value in d.items():
...     print(key, 'corresponds to ', d[key])
...
A corresponds to  1
B corresponds to  2
C corresponds to  3

Flowchart of For Loop

This flowchart will give you an idea of how For Loop works.


range() in Python for loop

range() allows you to generate a series of numbers in a given range.

We can define start, stop, and step sizes (start, stop, step_size) as a range. If we don’t provide step_size, it is 1 by default.

We can use the range() function in the loop to iterate through the sequence of numbers. Here is an example.

1. Example of range function in Python:

>>> for each in range(5):
...     print(each)
...
0
1
2
3
4

2. Example of range function in Python:

>>> for each in range(2, 5):
...     print(each)
...
2
3
4

3. Example of range function in Python:

>>> for each in range(1, 12, 2):
...     print(each)
...
1
3
5
7
9
11

4. Example of range function in Python:

>>> for each in range(-1, -12, -2):
...     print(each)
...
-1
-3
-5
-7
-9
-11

else in for loop Python

The for loop is useful to iterate over the elements of sequence such as string, list, tuple, etc. The else keywords in the for loop specify the block of code to be executed at the end of the loop.

Syntax:

for each in sequence:
    statements
else:
    statements

Example of else in for loop Python:

>>> for each in "TechPlusLifeStyle":
...     print(each)
... else:
...     print("Else Part")
...
T
e
c
h
P
l
u
s
L
i
f
e
S
t
y
l
e
Else Part
Interview Question:

Q: What is the use of else in for loop in Python?

Ans: If loop did not find a break statement, The loop normally executes else 
         clauses after completion. 

Nested For Loop in Python

Nested For Loop means, For loop inside another for loop

Syntax:

for i in sequence:
    for j in sequence1:
        statements
    statements

Example of Nested For Loop in Python:

>>> for i in range(2):
...     print("Outer Loop", i)
...     for j in range(3):
...         print("Inner Loop", j)
...
Outer Loop 0
Inner Loop 0
Inner Loop 1
Inner Loop 2
Outer Loop 1
Inner Loop 0
Inner Loop 1
Inner Loop 2 

Python for loop index

Now, let us see some examples of how to iterate through a list in python with index.

#1. enumerate() Function

enumerate() is Python’s built-in function which is used to get a counter and value from the iterable.

Example of enumerate Function:

>>> char_list = ['A', 'B', 'C']
>>> for index, value in enumerate(char_list):
...     print((index, value))
...
(0, 'A')
(1, 'B')
(2, 'C')

#2. range() Function

Another way to iterate over a list index is by combining range() and len() as follows:

>>> char_list = ['A', 'B', 'C']
>>> for i in range(len(char_list)):
...     print((i, chars[i]))
...
(0, 'A')
(1, 'B')
(2, 'C')

#3. zip() function

The zip() function returns an iterator of the tuples that combine elements from each sequence.

Example of zip function:

>>> char_list = ['A', 'B', 'C']
>>> for x in zip(range(len(char_list)), char_list):
...     print(x)
...
(0, 'A')
(1, 'B')
(2, 'C')

Conclusion

In this tutorial, we learned about Python for loop with the help of examples also the syntax of For loop in python. Also, we learned how to use else in for loop python, range() function, and Nested For loop. Finally, we learned about Python for loop index with the help of various examples.

Leave a Comment