For loop iterates over a sequence which can be list,tuple, dictionary, set, string or it can be any other iterable object. Loop goes till the last item. For loop executes all the statements under it for each item. In the following program for loop is used to print all the items in the list.
# for loop can be used to access each item
items = ["pen", "book", "bus", "college", "boy"]
for all in items:
print(all)
Output:
pen book bus college boy
Following program prints all the characters of the string using for loop.
#Accessing all characters in a string
for allcharacters in"Cricket": print(allcharacters)
Output:
C r i c k e t
Use of break and continue statement:
Break statement is used to break the loop. The loop will stop to iterate all the items. The following program shows the use of break statement. The program stops once it iterates to ‘book’.
# Use of break statement
items = ["pen", "book", "bus", "college", "boy"]
for all in items:
print(all)
if all == "book":
break
Output:
pen book
Continue statement stops at the current iteration of the item and continues with the next item. The following program shows the use of continue statement. For loop stops to iterate at the item ‘book’ and continues with the next items in the list.
# Use of continue statement
items = ["pen", "book", "bus", "college", "boy"]
for all in items:
if all == "book":
continue
print(all)
Output:
pen bus college boy
Use of range function
The ‘range’ function can be used to generate a sequence of numbers. It starts from 0. There is an increment in sequence of numbers by 1 by default. The following program will generate a sequence of numbers from 0 to 4 for the range of 5.
for number inrange(5):
print(number)
Output:
0 1 2 3 4
Use of range function between numbers
The ‘range’ function can also be used to start from a definite number by specifying the value. The following program is used to print a sequence of numbers from 2 to 4.
for number inrange(2, 5):
print(number)
OutPut:
2 3 4
Use of range function between numbers with interval
The ‘range’ function can increment the sequence by a specified value. The following program will print a sequence of number from 3 to 19 by an increment of 2.
for number inrange(3, 20, 2):
print(number)
OutPut:
3 5 7 9 11 13 15 17 19
Use of else with for loop
The ‘else’ part in the for loop get executed after the execution of loop and there is no iteration. The following program shows the use of ‘else’.
for number inrange(4):
print(number)
else:
print("Printing numbers completed")
Output:
0 1 2 3 Printing numbers completed
Nested for loop
Loop inside the loop is the nested loop. Nested loops can be of different types, like a while loop can be inside a for loop. The following program shows a nested loop.In the program inner loop is executed after each iteration of the outer loop.
color = ["green", "blue", "red"]
clothe = ["pent", "shirt", "sweater"]
for x in color:
for y in clothe:
print(x, y)
Output:
green pent green shirt green sweater blue pent blue shirt blue sweater red pent red shirt red sweater