For loop is used when a loop has to be several number of times.
a = [ ‘ He ‘ , ‘ loves ‘ , ‘ Python ‘ ]
for i in a:
print(i)
When we execute this program, it will give the output as :-
He
loves
python.
Also we can have a range in for loop in which the loop has to be executed for number of times.
Range
Range is used to set a bound for which the loop has to be executed.
for i in range(1, 4):
print(i)
The output will be
1
2
3
4
Continue
Continue statement is used when the loop has to be executed from the first.
while true:
a = int(input(” Enter the number “))
if a < 0:
continue
elif a = 0:
break
else:
print(” The square is %d ” % (n * n))
Thus when the number which we enter is less then zero, then the loop will be again executed from the first. The break statement is used to come out of the loop.
Else in For
We can also use else statement in for loop.
for i in range(1,5):
print(i, end=’ ‘)
else:
print(” Out of Range “)
The output of this program will be
1 2 3 4 5
Out of Range.