Nested Loop
Python programming language allows to use one loop inside another loop. Loop defined within another loop is know as nested loops. Nested loops are the loops that are nested inside an existing loop, that is, nested loops are the body of another loop.
Syntax
for iterator-var in sequence:
for var in sequence:
statement(s) for inner loop
# inner loop closed
statement(s) for outer loop
# outer loop closed
Other statement(s)
Example
for i in range(1,9,2):
for j in range(i):
print(i,end='')
print()
Output
1
3 3 3
5 5 5 5 5
7 7 7 7 7 7 7
Note : The range() function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter.
Example
for x in range(3,31,3):
print(x) # table of 3
Output
3
6
9
12
15
18
21
24
27
30
No comments:
Post a Comment