Python nested loops | Just enough Python

preview_player
Показать описание
Loops can be nested within each other. For example, here is how you print each number ( from 1 to 10 ) as many number of times.

for i in range(1,11) :
for count in range(0,i) :
print ( i, end=" ")
print ()

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10

You can do the same with while loops as well. You can nest for loop inside a while loop and vice-versa. Here is a re-write of the same program as above nesting a while loop inside a for loop.

for i in range(1,11) :
count = i
while count > 0 :
print ( i, end=" ")
count = count - 1
print ()
Рекомендации по теме