Python For Loop

In python, for loop is useful to iterate through the items of any sequence type such as list, tuple, string, etc. and execute the block of statements repeatedly for each item of the sequence type.

 

In for loop, you can use break, continue, and pass statements to pass or continue the execution of statements inside of the loop based on your requirements.

For Loop Syntax

In python, we will use for keyword to define for loop. Following is the syntax of defining for loop in python to iterate through the sequence items.

 

for item in sequence:

    statement1

    statement2

    …

    statementN

In the above for loop syntax, the item variable will refer to the values in the sequence starting from index 0. The statements inside of for loop will repeatedly execute for all the items in the sequence type.

 

As discussed in python if and if-else statements, you need to follow the indentation to create for loop block statements. To know more about specifying the indentation in python, refer Python basic syntaxes.

Python For Loop Flow Chart

Following is the flow chart diagram of for loop process flow in python.

 

For loop in python with examples

 

If you observe the above for loop flow chart diagram, for loop block statements will repeatedly execute for every item in the sequence type such as list, tuplestring, etc.

For Loop Example

Following is the example of using for loop on list object to iterate through the items in python to execute the block of statements repeatedly based on the requirements.

 

lst = [30, 10, 20, "tutlane"]

for item in lst:

    print(item)

If you observe the above example, we used for loop to iterate through the items of list object and followed the indentation to define the block of statements inside for loop.

 

When you execute the above python program, you will get the result like as shown below.

 

30

10

20

tutlane

For Loop with Tuple

Following is the example of iterating on a tuple object using for loop in python.

 

tpl = (30, 10, 20, "tutlane")

for item in tpl:

    print(item)

When you execute the above python program, you will get the result like as shown below.

 

30

10

20

Tutlane

For Loop with Dictionary

In python, the dictionary object is a collection of key-value pair items. So, to access dictionary object elements, you need to use the items() method.

 

Following is the example of iterating over the dictionary object using for loop in python.

 

dic = {"name": "Suresh", "location": "Guntur", "Age": 33}

for item in dic.items():

    print(item)

When you execute the above python program, you will get the result like as shown below.

 

('name', 'Suresh')

('location', 'Guntur')

('Age', 33)

If required, you can also get the dictionary object pair (key-value) values separately using for loop in python.

 

dic = {"name": "Suresh", "location": "Guntur", "Age": 33}

for k,v in dic.items():

    print("Key: {}, Value: {}".format(k,v))

When you execute the above python program, you will get the result like as shown below.

 

Key: name, Value: Suresh

Key: location, Value: Guntur

Key: Age, Value: 33

For Loop with String

In python, the string objects are also considered as iterable objects. So, you can use for loop to iterate through the string characters.

 

Following is the example of iterating over string object using for loop in python.

 

str = "Tutlane"

for c in str:   

    print(c)

When you execute the above python program, you will get the result like as shown below.

 

T

u

t

l

a

n

e

For Loop with Else Block

Same as python if-else statement, you can also use else block in for loop but the else block statements will execute only after completion for loop execution.

 

for x in range(4):

  print("Iteration {}".format(x+1))

else:

  print("Else Block Executed")

If you observe the above code, the range() function is useful to generate the sequence of numbers starting from 0. Here, the range(4) function will generate the numbers from 0 to 3.

 

When you execute the above python program, you will get the result like as shown below.

 

Iteration 1

Iteration 2

Iteration 3

Iteration 4

Else Block Executed

If you observe the above result, the else block statements have been executed only after completion of all iterations in for loop.

Nested For Loop

Generally, if you use one loop inside of another loop we will call it a nested loop. In python, you can create one for loop inside of another for loop based on your requirements.

 

Following is the example of creating the nested for loop in python.

 

names = ["Suresh", "Rohini", "Trishi"]

locations = ["Hyderabad", "Guntur"]

for x in names:

  for y in locations:

    print ("{}, {}".format(x, y))

print("Outside of Loop")

When you execute the above python program, you will get the result like as shown below.

 

Suresh, Hyderabad

Suresh, Guntur

Rohini, Hyderabad

Rohini, Guntur

Trishi, Hyderabad

Trishi, Guntur

Outside of Loop

This is how you can use for loop in python to execute the block of statements repeatedly based on your requirements.