FOR Loop:

Till the iteration of the last item in the sequence, for loop run the instructions. It iterates over sets of instructions in sequence, arrays, and a tuple for a pre-defined period or until the last item and calculation are executed. For loop can be categorized in three ways.

For loop in python:

As loops play an important role in programming language and make the task easy for the programmer, like other programming languages, Python also uses loops. Python allows the facility to use for loops in different sequences and segments, .i.e. for lists, strings, tuples, etc.

Syntax:

The keyword “for” is used to execute a set of conditions in for loop. The syntax for loop follows as below: Here are some examples of for loop in python.

For loop iteration over a list:

We can iterate through the list of numbers using for loop, and it will run once for each item in the list and print all the numbers.

Output:

For loop for a string:

Suppose we want to iterate through a string and print each alphabet separately. In that case, we can use for loop to iterate through the string and runs every single time for each alphabet.

Output:

For loop using range function:

We use the range function in python to perform a task a specific number of times. Here in the example given below, the range function calculates the sum till the first ten numbers and stores them in the sum variable.

Output:

For loop to iterate over Dictionary:

As a dictionary consists of key-value pairs, we can iterate through the dictionary and get a single key-value pair using a for loop. In the example given below, the Article type, category, and topic keys are available. You can obtain the key-value pair by writing a print statement using for loop.

Output:

For loop with else statements:

We can use for loop with else block in python, where else would be executed when the for loop gets completed and the num is out of range.

Output:

We can stop else statement execution by using break. The above example executes all the statements, but here, it will run only if block.

Output:

Nested for loop in python:

Python also allows users to execute nested for loops. In the mentioned example, we are running two for loops inside the outer loop. The control first goes to the outer loop and prints the statement; then it will go to the next for loop to execute the print statement and then to the last loop to print both statements. The third loop gets executed again and executes both print statements until the third loop finishes. As the third loop’s range is two, it will be executed two times, get out of the loop, and get back into the second loop. Then, the second loop’s print statement gets executed, and it will get back into the third loop once again. Thus, it will iterate till it finishes.

Output:

For loop in python with a break statement:

The break statements are also used in For loop. The following example contains an array having a list of different items in it. In the for loop, we are iterating through each item and printing that item. We have used the if statement. In the if statement, we put a condition that if the item value is equal to the keyword, then break the loop, not iterate through the rest of the array items, and immediately get out of the loop.

Output:

While in this example, the break statement will execute first and print values afterward.

Python for loop using continue statement:

We can also use for loop with continue statement. For example, you can skip the mentioned item in the if statement. The loop will continue execution until the end of the list.

Output:

Conclusion:

This article explains for loop in python using different functions, lists, and statements. The loop is used to iterate the instructions over a specified period until the condition is right. In python, for loop is used for numerous purposes to achieve the goal. This article has explained simple ways to use for loop in python that would be beneficial for newbies.

Python for Loops with Examples   TecAdmin - 57Python for Loops with Examples   TecAdmin - 10Python for Loops with Examples   TecAdmin - 12Python for Loops with Examples   TecAdmin - 52Python for Loops with Examples   TecAdmin - 54Python for Loops with Examples   TecAdmin - 4Python for Loops with Examples   TecAdmin - 3Python for Loops with Examples   TecAdmin - 30Python for Loops with Examples   TecAdmin - 66