Loops are used to execute a block of code repeatedly. For example, If you want to print the numbers from 1 to 5, you can do it very easily. But, when printing the numbers from 1 to 1000, writing so much code could be hectic. This can be done easily using loops.
The for loop
This loop iterates over sequences such as strings, lists, tuples, sets and dictionaries.
Example 1: iterating over a string
name = 'Python'
for i in name:
print(i)
Output:
P
y
t
h
o
n
Example 2: iterating over a list
colors = ["Red", "Green", "Blue", "Yellow"]
for color in colors:
print(color)
Output:
Red
Green
Blue
Yellow
You can also use for
loop inside a for
loop. For example:
colors = ["Red", "Green", "Blue", "Yellow"]
for color in colors:
print(color)
for i in color:
print(i)
Using range() in Loops
If you want to use for
loop for a specific number of times, then you can use the range()
function.
Syntax:
range(start, stop, step)
Example 1:
for n in range(5):
print(n)
Output:
0
1
2
3
4
Here loop starts from 0 by default and increments at each iteration.
Example 2:
for n in range(5, 10):
print(n)
Output:
5
6
7
8
9
Here loop starts from 5 and increments each time up to (but not including) 10.
Example 3:
for n in range(5, 10, 2): # 5 is the start, 10 is the stop (not included), and 2 is the step value.
print(n)
Output:
5
7
The while loop
The while
loop executes a block of code as long as a specified condition is True
.
For example:
count = 1
while count < 5:
print(count)
count = count + 1
Output:
1
2
3
4
Infinite Loop
In an infinite loop, if the condition never becomes False
, the loop will keep running forever and never stop on its own.
Example of breaking an infinite loop:
while True:
number = int(input("Enter a positive number: "))
print(number)
if number <= 0:
break
The break Statement
The break
statement stops the loop right away and moves to the code that comes after the loop.
For example:
for i in range(10):
if i == 5:
break
print(i)
Output:
0
1
2
3
4
The continue Statement
The continue
statement skips the remaining part of a loop and moves on to the next iteration of the loop.
For example:
for i in range(10):
if i == 5:
print("Skipping iteration")
continue
print(i)
Output of the above code:
0
1
2
3
4
Skipping iteration
6
7
8
9
Do-While Loop Equivalent
Python does not have a built-in do-while
loop, but similar behavior can be achieved with a while
loop.
For example:
while True:
number = int(input("Enter a positive number: "))
print(number)
if number <= 0:
break
The else Statement with Loops
In Python, you can use an else
block with both for
and while
loops. The else
block runs only if the loop completes normally (i.e., it doesn’t exit using a break
).
Example with for
loop:
for i in range(5):
print(i)
else:
print("Loop finished without break")
Output:
0
1
2
3
4
Loop finished without break
Example with break:
for i in range(5):
if i == 3:
break
print(i)
else:
print("Loop finished without break")
Output:
0
1
2
In this case, the else
block is not executed because the loop was stopped with break
.