Python - Loop Through a Range
Last Updated :
23 Jul, 2025
Looping through a range is an important operation in Python. In this article, we will explore the different ways to loop through a range in Python, demonstrating how we customize start, end, and step values, as well as alternative methods for more advanced use cases like looping through floating-point ranges or infinite sequences.
Example of Using range()
The most simple way to loop through a range in Python is by using the range() function in a for loop. By default, the range() function generates numbers starting from 0 up to, but not including, the specified endpoint.
Python
# Loop from 0 to 4
for i in range(5):
print(i, end=" ")
Explanation:
- Here only one argument is provided so the range begins at
0. - The range ends at
n-1 i.e. 4, where n is the argument provided.
Let's see some more methods and see how we can loop through a range in Python.
Using Start and Step Value in range()
In Python, we can loop through a range using the start parameter by leveraging the range() function. The range() function can take up to three parameters: start, stop, and step. Here’s the general syntax for using the start parameter in range():
range(start, stop, step)
- start: The value at which the range starts (inclusive).
- stop: The value at which the range stops (exclusive).
- step: The increment or decrement for each iteration (default is 1).
Example:
Python
# Loop with a step value of 2
for i in range(0, 10, 2):
print(i, end=" ")
Explanation:
- The step value(2) determines how much the range increments after each iteration.
- It supports both positive and negative step values for ascending or descending loops.
Note: Python also allows looping in reverse order by setting a negative step value in the range() function.
#Loop from 10 to 1 in reverse
for i in range(10, 0, -1):
print(i, end=" ") # Output: 10 9 8 7 6 5 4 3 2 1
Using enumerate() and range()
The enumerate() function, when combined with range(), provides both the index and the value during iteration.
Python
# Loop with index and value
for idx, val in enumerate(range(5, 10)):
print(f"Index: {idx}, Value: {val}")
OutputIndex: 0, Value: 5
Index: 1, Value: 6
Index: 2, Value: 7
Index: 3, Value: 8
Index: 4, Value: 9
Explanation:
enumerate() returns a tuple containing the index and the corresponding value in the range.- It works seamlessly with ranges of any size.
Looping Through Floating-Point Ranges
Since the range() function only supports integers, creating a range of floating-point numbers requires external libraries or custom solutions. Here we will use numpy.arrange().
Python
import numpy as np
# Loop through floating-point numbers
for i in np.arange(0.5, 2.5, 0.5):
print(i, end=" ")
Explanation:
Here, NumPy allows iteration over fractional ranges.- It Supports precise step values for scientific or mathematical applications.
The itertools.count() function generates an infinite sequence of numbers, starting from a given value and stepping by a defined amount. It is suitable for dynamic or unpredictable iteration lengths, such as streaming data.
Python
from itertools import count
# Infinite range with manual termination
for i in count(1, 3):
if i > 10:
break
print(i, end=" ")
Explanation:
- Here, it automatically continues until a stopping condition is provided.
- It supports both positive and negative step values.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice