The for loop in Python
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
Where a sequence can be a list, tuple, array, etc.
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)
Output:
20
30
40
50
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)
Output:
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.
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:
str_lst = ['A','for','loop','Tutorial', 'in','Python']
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.
Output:
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.
See the following example where a list of five numbers is created.
As the current value reaches the value of 30 in the for loop, the loop will exit.
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
Output:
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.
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.
See an example below of using the continue statement:
lst_seq = [10,20,30,40,50] for x in lst_seq: if x == 30: continue print ("The current value of x = " + str(x))
Output:
The current value of x = 20
The current value of x = 40
The current value of x = 50
In the output, you can see the value of 30 is omitted as the continue statement executed when the value of variable reached 30.
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:
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)
Output:
The array item = 20
The array item = 30
The array item = 40
The array item = 50
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)
Output:
The tuple item = 20
The tuple item = 30
The tuple item = 40