☞ It is used to iterate over a sequence (such as a list, tuple, string, or range) or any other iterable object.
☞ It executes a block of code repeatedly for each item in the sequence.
Syntax :
for <control_variable> in <sequence/item/range>:
# statements of for
else: #optional
#statements of else
#To demonstrate range(<stop>) function
for i in range(5):
print(i)
0 1 2 3 4
#To demonstrate range(<start>, <stop>) function
for i in range(1, 5):
print(i)
1 2 3 4
#To demonstrate range(<start>, <stop>, <step>) function
for i in range(1, 5, 2):
print(i)
1 3
#To print numbers in reverse order
for i in range(5, 0, -1):
print(i)
5 4 3 2 1
#To print numbers in reverse order skipping 3 steps
for i in range(10, 0, -3):
print(i)
10 7 4 1