The for loop in Python
The Python for loop is the way of executing a given block of code repeatedly to a 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 iterating over the code until a certain condition is true or an 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:
for loop_variable in sequence: #Write code here to execute for each iteration
You may write multiple statements to be executed in each iteration at the same indentation. See the 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:
The following code is used in the for loop example:
lst_seq = [10,20,30,40,50] for x in lst_seq: print (x)
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 displayed 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:
This is how multiple statements are used in the for loop of Python:
lst_seq = [10,20,30,40,50] for x in lst_seq: print ("The value of x = " ,x) y = x + 100 print ("The value of y = " , y)
All the statements are at the same indentation. If you use one less or more space for any line, the compiler will generate an error.
You can see the output and code on 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 strings is created. The list is used in the for loop just like the above examples. See the code and output yourself:
In the demo page, you can see this is how the list of strings is created:
str_lst = ['A','for','loop','Tutorial', 'in','Python']
After that, a for loop is used:
for currword in str_lst:
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:
This is how the break statement is used in the example:
lst_seq = [10,20,30,40,50] for x in lst_seq: print ("The current value of x = " + str(x)) if x == 30: break
Note, if you use the break statement in a 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 the current block in the for loop. See the following example to better understand this.
The continue statement is used as follows:
lst_seq = [10,20,30,40,50] for x in lst_seq: if x == 30: continue print ("The current value of x = " + str(x))
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 exited 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:
After that, a for loop is used to iterate through the array items as shown in the example below:
This is how the for loop is used with array:
from array import array num_arr=array('b',[10,20,30,40,50]) for x in num_arr: print("The array item = ", x)
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:
This is how a tuple is created and its items are displayed by using the for..in loop:
num_tuple = (10,20,30,40) for x in num_tuple: print("The tuple item = ", x)