About Lesson
Nested for loop
A nested for
loop is a loop inside another loop. It allows you to iterate over multiple sequences or dimensions.
Example:
Python
>>> for i in range(1,6):
... for j in range(1,i+1):
... print("*",end=" ")
... print("")
...
*
* *
* * *
* * * *
* * * * *
>>> for i in range(1,6):
... for j in range(1,i+1):
... print(j,end=" ")
... print("")
...
1
1 2
1 2 3
1 2 3 4
Nested while loop
Nested while
loops are loops inside another while
loop. They are useful for performing multiple iterations based on different conditions.
Example:
Python
# Nested while loop
i=1
... while(i<=rows):
... print("*",end=" ")
...
>>>
>>> def rightangel(rows):
... i=1
... while(i<=rows):
... j=1
... while j<=i:
... print("*",end=" ")
... j=j+1
...
...
>>> def rightangel(rows):
... i=1
... while(i<=rows):
... j=1
... while j<=i:
... print("*",end=" ")
... j=j+1
... print("")
... i=i+1
...
>>> rightangel(5)
*
* *
* * *
* * * *
* * * * *
>>> rightangel(10)
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
>>> rightangel(20)
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *