For Loop explained with examples
A loop is a used for iterating over a set of statements repeatedly. In Python we have three types of loops for, while and do-while. In this guide, we will learn for loop and the other two loops are covered in the separate tutorials.
Syntax of For loop in Python
for <variable> in <sequence>: # body_of_loop that has set of statements # which requires repeated execution
Here <variable> is a variable that is used for iterating over a <sequence>. On every iteration it takes the next value from <sequence> until the end of sequence is reached.
Lets take few examples of for loop to understand the usage.
Python – For loop example
The following example shows the use of for loop to iterate over a list of numbers. In the body of for loop we are calculating the square of each number present in list and displaying the same.
# Program to print squares of all numbers present in a list # List of integer numbers numbers = [1, 2, 4, 6, 11, 20] # variable to store the square of each num temporary sq = 0 # iterating over the given list for val in numbers: # calculating square of each number sq = val * val # displaying the squares print(sq)
1
4
16
36
121
400
Function range()
In the above example, we have iterated over a list using for loop. However we can also use a range() function in for loop to iterate over numbers defined by range().
range(n): generates a set of whole numbers starting from 0 to (n-1).
For example:
range(8) is equivalent to [0, 1, 2, 3, 4, 5, 6, 7]
range(start, stop): generates a set of whole numbers starting from start to stop-1.
For example:
range(5, 9) is equivalent to [5, 6, 7, 8]
range(start, stop, step_size): The default step_size is 1 which is why when we didn’t specify the step_size, the numbers generated are having difference of 1. However by specifying step_size we can generate numbers having the difference of step_size.
For example:
range(1, 10, 2) is equivalent to [1, 3, 5, 7, 9]
Lets use the range() function in for loop:
Python for loop example using range() function
Here we are using range() function to calculate and display the sum of first 5 natural numbers.
# Program to print the sum of first 5 natural numbers
# variable to store the sum
sum = 0
# iterating over natural numbers using range()
for val in range(1, 6):
# calculating sum
sum = sum + val
# displaying sum of first 5 natural numbers
print(sum)
Output:
15
For loop with else block
Unlike Java, In Python we can have an optional ‘else’ block associated with the loop. The ‘else’ block executes only when the loop has completed all the iterations. Lets take an example:
for val in range(5):
print(val)
else:
print("The loop has completed execution")
Output:
0
1
2
3
4
The loop has completed execution
Note: The else block only executes when the loop is finished
Nested For loop in Python
When a for loop is present inside another for loop then it is called a nested for loop. Lets take an example of nested for loop.
for num1 in range(3):
for num2 in range(10, 14):
print(num1, ",", num2)
Output:
0 , 10
0 , 11
0 , 12
0 , 13
1 , 10
1 , 11
1 , 12
1 , 13
2 , 10
2 , 11
2 , 12
2 , 13
Leave Comment