Python For Loop
This tutorial will explain how to use Python for loop, which is one type of Python loop statement with basic syntax by example for better understanding.
What is a for loop in Python? A for loop in Python repeats a part of code ‘n’ number of times over a sequence of items. We use for loop whenever we have a block of code that we want to repeat a specific number of times. We are using for loop with an iterable object like list, tuple, or a set of characters in a string.
Python for loop Syntax
for <iterating_var> in <sequence>:
Body of for loop
- sequence: It contains expressions like string, list, dictionary, sets, tuple, etc.
- iterating_var: It is a variable that takes the item’s value inside the sequence on each iteration.
The sequence is evaluated first when the program control reaches Python for a loop. Then the first item in the sequence is assigned to the iterating variable of the loop, and then a block of code under the loop is executed. After one iteration, the second item of the sequence is assigned to an iterating variable, and the block of code under the loop is again executed.
This process continues until the last item of the sequence is assigned to the iterating variable, and the block of code is executed until the entire sequence is exhausted. Then, the first statement, if any, after the loop is executed. The body of the Python for loop is determined through indentation, and all statements under the loop are equally indented.
Example (1)
for a in 'web':
print(a)
print('For loop stop')
Output of example 1
W
e
b
For loop stop
In the above example, ‘a’ is iterating_value and ‘ web’ (w,e,b) is a sequence of loop. ‘print(a)’ is a code of loop body. For first iteration, a=w & output shows as (a=w) , for second iteration, a=e & output shows as (a=e) and for third iteration, a=b & output shows as (a=b) by executing print statement. The fourth iteration is not possible because the last character of the sequence list is ‘b’. After for loop, the print statement is available as “print(‘For loop stop’)” so after completion of for loop, the output shows as ‘For loop stop’.
Iterating on sequence using for loop
We can use the for loop to iterate on a set, list, tuple, and dictionary.
Syntax
for 'iterating_var' in sequence:
Body of for loop
- sequence: set, list, tuple, and dictionary
Example (2)
#Use set as sequence
print('set values')
for a in {1,2,3}:
print(a)
print('tuple values')
#Use tuple as sequence
for a in (4,5,6):
print(a)
print('list values')
#Use list as sequence
for a in [7,8,9]:
print(a)
print('dictionary values')
for a in {'A':'a','B':'b','C':'c'}:
print(a)
Output of example 2
set values
1
2
3
tuple values
4
5
6
list values
7
8
9
dictionary values
B
A
C
In the above example, we have used set, tuple, list and dictionary as a sequence of loop so iterating variable “a= (1,2,3),(4,5,6),(7,8,9),(B,A,C)” for respective sequence and output shows the print statement result against iterating values as “(1,2,3),(4,5,6),(7,8,9),(B,A,C)” of respective sequence.
Iterating on indices of a sequence using for loop
We can use the for loop to iterate on indices of sequence (set, list, tuple, and dictionary) by using the len() and range() functions. The len() function returns the length of the given sequence, and then applying the range() function to that, it returns the sequence indices on a range object to iterate.
Syntax
for 'iterating_var' in range(len(sequence)):
Body of for loop
- sequence: set, list, tuple, and dictionary
Example (3)
#Use set as sequence
print('set values')
set1={1,2}
for a in range(len(set1)):
print(set1)
print('tuple values')
#Use tuple as sequence
num=(3,4)
for a in range(len(num)):
print(num[a])
print('dictionary values')
alpha= {'A':'a','B':'b'}
for a in range(len(alpha)):
print(a)
print('list values')
#Use list as sequence
sub=['Math','Science']
for a in range(len(sub)):
print(sub[a])
Output of example 3
set values
{1, 2}
{1, 2}
tuple values
3
4
dictionary values
0
1
list values
Math
Science
In the above example, we have used indices of set, tuple, list, and dictionary as a loop sequence by using the len() and range() functions. Here, the length of all sequences is ‘2’. Hence ‘range(2)’ means ‘(0,1)’ and for the first iteration, iterating variable ‘a=0’ and for the second iteration, iterating variable ‘a=1’. Hence, the output shows the print statement result against iterating values a=0 and 1 of the respective sequence.
Using Python for loop with range function
We can use the for loop to iterate on the range function.
Syntax
for 'iterating_var' in range(argument_value):
Body of for loop
- argument_value = one argument, two arguments, and three arguments.
Using range(n)
It shows a sequence of numbers. It is called with one argument like n and creates a sequence of numbers from 0 to n-1. E.g., range(4) means (0,1,2,3). We can use the list function to convert the range object into a list object to know the iterating values. Eg. list(range(3)) = [0, 1, 2]
Example (4)
for i in range(2):
print(i)
print(i+1)
Output of example 4
0
1
1
2
In the above example, ‘i’ is iterating_value and ‘ range(2)’ means (0,1) is a sequence list of loop. ‘print(i)’ and ‘print(i+1)’ are code of loop body. For first iteration, i=0 & output shows as (i=0,i+1=1) and for second iteration, i=1 & output shows as (i=1,i+1=2) after executing print statement.
Using range(n1,n2)
It is called with two arguments like (n1,n2) and creates a sequence of numbers from first to the second-1. Eg. range(2,7) means (2,3,4,5,6).
Example (5)
for i in range(1,4):
print(i)
Output of example (5)
1
2
3
In the above example, ‘i’ is iterating_value and ‘ range(1,4)’ means (1,2,3) is a sequence list of loop. ‘print(i)’ is code of loop body. For first iteration, i=1 & output shows as (i=1) , for second iteration, i=2 & output shows as (i=2) and for third iteration, i=3 & output shows as (i=3) after executing print statement.
Using range(n1,n2, Interval)
It is called with three arguments (n1,n2, Interval) and creates a sequence of numbers from first to second-1 with Interval. Eg. range (2,14,3) means (2, 2+3=5, 5+3=8, 8+3=11). Interval can be negative. Eg. range (10,2,-2) means (10, 10-2=8, 8-2=6, 6-2=4).
Note: If we take (n1,n2,Interval) values like (n1=10,n2=2 ,interval=2) or (n1=2,n2=10 ,interval=-2), the output returns blank.
Example (6)
for i in range(1,4,2):
print(i)
Output of example 6
1
3
In the above example, ‘i’ is iterating_value and ‘ range(1,4,2)’ means (1,1+2=3) is a sequence list of loop. ‘print(i)’ is code of loop body. For first iteration, i=1 & output shows as (i=1) and for second iteration, i=3 & output shows as (i=3) after executing print statement.
Conclusion
In this tutorial, you have learned how to use python for loop, you also learned how to use iterate on sequence using for loop and we have explained how to use range function with for loop that takes one and two arguments and you have practiced each concept by giving 6 different examples.
Hopefully, it was clear and concise.
If you have an inquiry or doubt don’t hesitate to leave them in the comment section. we are waiting for your feedback.
Related Articles
Python Loop Statements – Comprehensive Guide
Python Tutorials List:
- Python tutorials list: access all python tutorials.