Someone in the New York Python user group asked about the range() function in Python. In this tutorial, I will explain in detail what does the range function do in Python.
The range(stop) function in Python generates a sequence of numbers starting from 0 up to, but not including, the specified stop value. This is particularly useful in for loops to iterate a specific number of times. For example, range(5) will produce the sequence 0, 1, 2, 3, 4. This method is efficient and commonly used for looping operations.
What is the range() Function in Python?
The range() function in Python generates a sequence of numbers, commonly used for looping a specific number of times in for loops. It is a built-in function that returns an immutable sequence of numbers, typically used to iterate over with the for loop.
Syntax of the range() Function
The range() function can be used in three different ways based on the arguments provided:
range(stop)range(start, stop)range(start, stop, step)
1. range(stop)
When a single argument is provided, the sequence starts from 0 and ends at stop - 1.
for i in range(5):
print(i)
Output:
0
1
2
3
4
2. range(start, stop)
When two arguments are provided, the sequence starts from start and ends at stop - 1.
for i in range(2, 6):
print(i)
Output:
2
3
4
5
Here is the exact output in the screenshot below:

3. range(start, stop, step)
When three arguments are provided, the sequence starts from start, ends at stop - 1, and increments by step.
for i in range(1, 10, 2):
print(i)
Output:
1
3
5
7
9
Check out Square Root Function in Python
range() Function Examples in Python
Let me show you some practical examples of using the range() function in Python.
Example 1: Iterate Over a List of Names
Suppose we have a list of names, and we want to print each name along with its position in the list.
names = ["John", "Emma", "Michael", "Sophia", "James"]
for i in range(len(names)):
print(f"{i + 1}. {names[i]}")
Output:
1. John
2. Emma
3. Michael
4. Sophia
5. James
Here is the output in the screenshot below:

Example 2: Generate a List of Even Numbers
We can use the range() function to generate a list of even numbers between 1 and 20.
even_numbers = list(range(2, 21, 2))
print(even_numbers)
Output:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Read COUNT() Function in Python
Example 3: Create a Countdown Timer
Let’s create a simple countdown timer from 10 to 1 using the range() function in Python.
import time
for i in range(10, 0, -1):
print(i)
time.sleep(1) # Pauses the loop for 1 second
print("Happy New Year!")
Output:
10
9
8
7
6
5
4
3
2
1
Happy New Year!
I executed the above Python code, and you can see the exact output in the screenshot below:

Benefits of Using the range() Function
- Efficiency: The
range()function is memory efficient as it generates numbers on the fly and doesn’t store them in memory. - Versatility: It can be used in various scenarios, such as iterating over lists, generating number sequences, and more.
- Readability: Using
range()makes the code more readable and easier to understand.
Conclusion
The range() function in Python is used to generate sequences of numbers and iterate over them. In this tutorial, I explained how to use the range() function in Python and also shows a few examples of how to implement the range() function in various scenarios.
You may also like:

I’m Michelle Gallagher, a Senior Python Developer at Lumenalta based in New York, United States. I have over nine years of experience in the field of Python development, machine learning, and artificial intelligence. My expertise lies in Python and its extensive ecosystem of libraries and frameworks. Throughout my career, I’ve had the pleasure of working on a variety of projects that have leveraged my skills in Python and machine learning. Read more…