Nested Loop in Python
Nested loop means number of any loop within a another loop. In nested loop the inner loop execute n number of times depends on each iteration of outer loop.
The syntax of nested loop given below:
for variable in sequence:
block of statement
for variable in sequence:
block of statement
other statement
Example 1:
Program to display the following pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Ans:
for i in range(1,6):
for j in range(1,i+1):
print(j,end=" ")
print("\n")
Example 2:
Program to display the pattern:
@ @ @ @ @
@ @ @ @
@ @ @
@ @
@
Ans:
n=int(input("Enter row"))
for row in range(n,0,-1):
for col in range(1,row+1):
print("@",end=" ")
print()
The syntax of nested loop given below:
for variable in sequence:
block of statement
for variable in sequence:
block of statement
other statement
Example 1:
Program to display the following pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Ans:
for i in range(1,6):
for j in range(1,i+1):
print(j,end=" ")
print("\n")
Example 2:
Program to display the pattern:
@ @ @ @ @
@ @ @ @
@ @ @
@ @
@
Ans:
n=int(input("Enter row"))
for row in range(n,0,-1):
for col in range(1,row+1):
print("@",end=" ")
print()
Comments
Post a Comment
If you have any doubt, Please let me know..