We are going to make this pattern and understand nested loop concept with logic.
* * * * * * * * * * * * * * *
row=5
for i in range(1,row+1):
for j in range(i):
print("*",end=" ")
print()
Genrally,
- There are two loops, inner loop and outer loop.
- Outer loop is for row.
- Inner loop is for column.
Explanation:
-
row=5, we need 5 rows. -
for i in range(1,row+1):print 1 to 5, whyrow+1. As you know range(5) would print0,1,2,3,4not 5 same as for(1,row+1)it would be(1,6)and print1,2,3,4,5. -
for j in range(i):for every row five column is printed. Example:
11 12 13 14 15
21 22 23 24 25
Dry Run Table:
row=5
i -> (1,1+row)->(1,6)
j -> (i)
| i | j->range(i) | Output |
|---|---|---|
| 1 | 0 | * |
| 2 | 0,1 | ** |
| 3 | 0,1,2 | *** |
| 4 | 0,1,2,3 | **** |
| 5 | 0,1,2,3,4 | ***** |
Top comments (0)