Loops are used to repeat a block of code multiple times.
Python has mainly 2 types of loops:
for loop
while loop
1.for Loop
Used when the number of iterations is known.
Example
input:
for i in range(5):
print(i)
output:
0
1
2
3
4
2.while loop
Used when the condition should run until it becomes false.
example
input:
i = 1
while i <= 5:
print(i)
i += 1
output:
1
2
3
4
5
Top comments (0)