
Last Updated on 21 March 2025
Loops allow us to repeat a block of code multiple times that makes them very essential for many tasks. For example, iterating over collections of data, automating repetitive tasks, etc. In Python, we primarily work with two types of loops: for loops and while loops.
In this article, we’ll learn about both types of loops, and see how they work with practical examples to make the concept clearer.
What is a Loop?
In programming, a loop is a sequence of instructions that is continually repeated until a certain condition is met. Instead we write the same code over and over, loops allow us to execute the same block of code multiple times with minimal effort.
For Loop in Python
A for loop is used to iterate over a sequence (like a list, tuple, dictionary, string, or range) and execute a block of code for each item in that sequence.
Syntax for For Loop
for item in iterable:
# Block of code to execute
Here:
itemis the variable that represents each element in the iterable.iterablecan be any object that can return its members one at a time, permitting it to be iterated over in aforloop.
Example 1: Iterating Over a List
Let’s say you have a list of numbers, and you want to print each number. You can use a for loop like this:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
Output
1 2 3 4 5
In this example, the loop goes through each number in the list numbers and prints it.
Example 2: Using a Range
Python provides a built-in function range() to generate a sequence of numbers. You can use it in a for loop to repeat a block of code a specific number of times.
for i in range(1, 6):
print(i)
Output
1 2 3 4 5
Here, range(1, 6) generates a sequence of numbers from 1 to 5, and the loop prints each number.
While Loop in Python
A while loop is used to execute a block of code as long as a specified condition is True. The loop will keep running until the condition becomes False.
Syntax of While Loop:
while condition:
# Block of code to execute
Here, condition is an expression that evaluates to True or False. The loop continues as long as the condition remains True.
Example 1: Counting from 1 to 5
Let’s use a while loop to print numbers from 1 to 5:
count = 1
while count <= 5:
print(count)
count += 1
Output
1 2 3 4 5
In this example, the loop keeps running as long as count is less than or equal to 5. Each time the loop runs, count is incremented by 1.
Example 2: Infinite Loop
A while loop can lead to an infinite loop if the condition never becomes False. For example:
while True:
print("This loop will run forever!")
This code will continuously print “This loop will run forever!” unless you manually stop it (e.g., by pressing Ctrl+C).
Break, Continue, and Else in Loops
Python provides additional control flow tools that can be used inside loops to modify their behavior. Let’s look at these briefly:
Break
The break statement is used to exit a loop early, even if the loop condition is still True.
for num in range(1, 6):
if num == 4:
break
print(num)
Output
1 2 3
In this example, the loop stops when num reaches 4 because of the break statement.
Continue
The continue statement is used to skip the current iteration and move to the next one. The loop continues running without executing the rest of the code inside the loop for that iteration.
for num in range(1, 6):
if num == 3:
continue
print(num)
Output
1 2 4 5
Here, the number 3 is skipped because of the continue statement.
Else
You can also use an else block with loops. The else block will execute only if the loop completes without encountering a break statement.
for num in range(1, 6):
print(num)
else:
print("Loop finished without break.")
Output
1 2 3 4 5 Loop finished without break.
The else block runs after the loop finishes unless a break occurs.