Home »
Python
Loops in Python
Last Updated : April 22, 2025
There are three types of loops in python:
- Condition Controlled Loops
- Range Controlled Loops
- Collection Controlled Loops
Condition Controlled Loops
In Condition controlled Loops, there is condition(expression) that controls the loop. In python there is 1 loop falls under this category.
1. Python while Loop
Python while loop is used to repeat a block of code as long as the given condition stays true.
Here’s an example:
a=1
while a<=10:
print(a)
a=a+1
Output
1
2
3
4
5
6
7
8
9
10
2. Python do while Loop (Not Present in Python)
In Python Do While is NOT present, but we can convert while loop to work as do while loop.
a=1
while True:
print(a)
a=a+1
if a>10:
break
Output
1
2
3
4
5
6
7
8
9
10
Range Controlled Loops
For loop is used for implementation of range() method.
There are three ways to use Range():
1. Range with 1 Parameter [range(end)] [start=0, step=1]
Range with 1 Parameter [range(end)] generates a sequence of numbers starting from 0 up to, but not including, the specified end value, with a default step of 1.
The following example demonstrates:
for i in range(10):
print(i,end="\t")
Output
0 1 2 3 4 5 6 7 8 9
2. Range with 2 Parameter [range(start,end)] [step=1]
Range with 2 Parameters [range(start, end)] generates a sequence of numbers starting from the specified start value, up to but not including the end value, with a default step of 1.
The following example demonstrates:
for i in range(1,10):
print(i,end="\t")
Output
0 1 2 3 4 5 6 7 8 9
3. Range with 3 Parameter [range(start,end,step)]
Range with 3 Parameters [range(start, end, step)] generates a sequence of numbers starting from the specified start value, up to but not including the end value, with the specified step value to determine the increment.
The following example demonstrates:
for i in range(1,10,3):
print(i,end="\t")
Output
1 4 7
Collection Controlled Loops
Collection controlled loops are also implemented with the help of for loop.We need a collection object as a source. Python have various collection class like: list, tuple, set, and dictionary.
1. Python Collection Controlled Loops With List
Python Collection Controlled Loops with List allow you to iterate over each item in a list using a loop. The loop runs once for each element in the list, making it easy to process or manipulate list items.
The following example demonstrates:
# with list
data = [12,45,67,23,15]
for item in data:
print(item)
Output
12
45
67
23
15
2. Python Collection Controlled Loops With Tuple
Python Collection Controlled Loops with Tuple allow you to iterate over each element in a tuple using a loop. Just like lists, tuples are ordered collections, and you can easily access each item in the tuple during the loop.
The following example demonstrates:
# with tuple
data = (12,45,67,23,15)
for item in data:
print(item)
Output
12
45
67
23
15
3. Python Collection Controlled Loops With Set
Python Collection Controlled Loops with Set allow you to iterate over each element in a set. Since sets are unordered collections, the elements may not appear in the order they were added.
The following example demonstrates:
# with set
data = {12,45,67,23,15}
for item in data:
print(item)
Output
12
45
67
23
15
4. Python Collection Controlled Loops With dict (Dictionary)
Python Collection Controlled Loops with dict (Dictionary) allow you to iterate over the keys, values, or both from a dictionary. You can use a loop to process the dictionary's contents easily.
The following example demonstrates:
# with dictionaies
data = {"item1":12,"item2":45,"item3":67,"item4":23,"item5":15}
for item in data:
print("data["+item+"] = "+str(data[item]))
Output
data[item3] = 67
data[item2] = 45
data[item1] = 12
data[item5] = 15
data[item4] = 23
Python Loops Exercise
Select the correct option to complete each statement about loops in Python.
- The for loop is used to iterate over a ___ such as a list, tuple, or string.
- The while loop continues to execute as long as the ___ is true.
- To iterate over a range of numbers, we commonly use the ___ function with a for loop.
Advertisement
Advertisement