Learn using Python for loop: 7 examples

The for loop in Python

The Python for loop is the way of executing a given block of code repeatedly to the given number of times.

In Python, the for loop iterates over the items of a given sequence.

The items can be strings unlike in Pascal where it iterates over the arithmetic progression of numbers.

This type of loop is generally used when you know the number of iterations. For the infinite number of loops, you may use the while loop.

Structure of using the for loop

Following is the general structure of using the for loop in Python:

 

 

Where a sequence can be a list, tuple, array etc.

You may write multiple statements to be executed in each iteration at the same indentation. See examples below to learn more about this.

An example of for loop with a list of five numbers

Let me start with a basic example where a list of five numbers is created. The list is used as a sequence in the for loop. In each iteration, the value of the current number of the list is assigned to the loop variable. The variable’s value is displayed by using the print statement:

See online demo and code

The following code is used in the for loop example:

 

You can see in the output, the numbers are displayed. This is how the flow of the for loop worked:

  • The first number from the list is assigned to the loop variable, x.
  • The block of code executed within the for loop, that will display the variable’s value.
  • After that, the execution will move to the next list item.
  • The iteration will go on until the last item is entertained.

Executing multiple statements in for loop example

In above example, only the single line of code is executed in the for loop. As motioned earlier, you may use multiple statements at the same indentation. See the example below:

Python for loop

See online demo and code

This is how multiple statements are used in the for loop of Python:

 

All the statements are at the same indentation. If you use one less or more spaces for any line, the compiler will generate an error.

You can see the output and code in the demo page.

An example of using string list of sequence in the for loop

In this demo of using the for loop in Python, a list of string is created. The list is used in the for loop just like the above examples. See the code and output yourself:

for loop string list

See online demo and code

In the demo page, you can see this is how the list of strings is created:

 

After that, a for loop is used:

for currword in str_lst:

 

print (currword)

It will display the current string in the list at each iteration.

An example of using break with if statement in for loop

Sometimes, you may need to exit the for loop earlier than waiting to end the items in the given sequence. For that, you may use the Python break statement.

The break statement is used to exit the current loop and execution moves outside of that loop where the break is used.

See the following example where a list of five numbers is created. As the current value reaches the value of 30 in for loop, the loop will exit. See the code and output by clicking the link or image below:

break statement with for loop

See online demo and code

This is how the break statement is used in the example:

 

The print statement displayed the value of x till it reached 30. As value reached 30, the if statement evaluated as true, in which case the break statement executed that exit the loop.

Note, if you use the break statement in nested for loop (an inner for loop), then it will exit the inner loop only.

An example of using continue statement

In certain scenarios, you may not need to exit the loop entirely but just the current item in the sequence. For that purpose, you may use the Python continue statement.

The continue statement will skip the execution of current block in the for loop. See the following example to better understand this.

continue statement

See online demo and code

The continue statement is used as follows:

 

In the output, you can see the value of 30 is omitted as the continue statement executed when the value of variable reached 30.

The difference between the break and continue can also be seen there. The break would have exit the for loop whereas continue statement made the for loop keep on executing after omitting the current item only.

An example of for loop with Python array

In this example, an array is created by importing the array module. The array is of integer type with five elements:

num_arr=array(‘b’,[10,20,30,40,50])

After that, a for loop is used to iterate through the array items as shown in the example below:

for array

See online demo and code

This is how the for loop is used with array:

 

An example of using for..in loop to loop through Python tuple

In this example, a Python tuple is created with four items. A for..in loop is used to display the tuple items, as follows:

for tuple

See online demo and code

This is how tuple is created and its items are displayed by using the for..in loop: