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 the break, continue, and pass statements to pass or continue the execution of statements inside of the loop based on your requirements.
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.
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 to Python basic syntaxes.
Following is the flow chart diagram of for loop process flow in python.
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, tuple, string, etc.
Following is the example of using for loop on the list object to iterate through the items in python to execute the block of statements repeatedly based on the requirements.
If you observe the above example, we used for loop to iterate through the items of the 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 as shown below.
Following is the example of iterating on a tuple object using for loop in python.
When you execute the above python program, you will get the result as shown below.
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.
When you execute the above python program, you will get the result as shown below.
If required, you can also get the dictionary object pair (key-value
) values separately using for loop in python.
When you execute the above python program, you will get the result as shown below.
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 objects using for loop in python.
When you execute the above python program, you will get the result as shown below.
Same as the 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.
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 as shown below.
If you observe the above result, the else
block statements have been executed only after completion of all iterations in for loop.
Generally, if you use one loop inside 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.
When you execute the above python program, you will get the result as shown below.
This is how you can use for loop in python to execute the block of statements repeatedly based on your requirements.