The range()
function in Python is a handy built-in feature that makes it easy to work with sequences of numbers. For example, instead of writing 0, 1, 2, 3, and 4, you can just tell Python to give you a range from 0 to 4. You can even customize how the numbers in your range are spaced, like counting by twos or starting at a certain number and stopping at another. In this article, you’ll learn all about the Python range()
function, see how to use it in different ways, and explore practical examples.
Table of Contents:
What is the Python range() Function?
The range() function in Python is a built-in function that generates a sequence of numbers within a specified range. You can use it to count a series one by one or to skip numbers by a step. It’s very helpful when you’re writing loops in your code, like when you want to do something five times or loop through a list by number. You can use range() with one, two, or three arguments to define the start, stop, and step of your sequence. It’s commonly used in loops to control the loop how many times a block of code has to run or to make indices for iteration purposes in lists and other data structures.
Syntax:
range(start, stop, step)
Parameters:
- start: The first number to start the sequence.
- stop: The number right after the last number in the sequence.
- step: The gap between each number in the sequence.
Return: creates an object that behaves like a list of numbers.
Elevate Your Python Skills – Start Your Journey Now!
Enroll now to gain in-demand Python expertise and stand out in the job market.

How to Use the range() Function in Python?
The range() function in Python can be used in three different ways: by providing just one number to count up from 0 to that number, by giving a start and a stop number to create a range in between, or by including a third number to control the step size of counting. Let’s take a look at how each of these variations works.
1. range(stop) in Python
When you call the range() function with just one argument, it returns a range object that behaves like a sequence of numbers starting from 0 up to the number you provide, without including the stop number.

Syntax:
range(stop)
Example:
Output:

Explanation: Here, the loop starts at 0 and goes up to 5 (since 6 is not included). It prints 0, 1, 2, 3, 4, and 5 one by one.
2. range(start,stop) in Python
When you call the range() function with two arguments, it will give you a list of numbers starting from the first number you provide, and going up to the second number, without including the stop number.

Syntax:
range(start, stop)
Example:
Output:

Explanation: Here, the loop starts at 0 and ends at 5 (since 6 is not included). Each number is printed on a new line.
3. range(start, stop, step) in Python
When you call the range() function with three arguments, it will give you a list of numbers starting from the first number you provide to the second number, without including the stop number and counting by the step size you specify.

Syntax:
range(start, stop, step) Example:
Output:

Explanation: Here, the loop starts at 0 and stops before 10, with a step of 2. So it prints every second number: 0, 2, 4, 6, and 8.
Incrementing the Range() using a Positive Step in Python
When you want your loop to count up (increment), make sure you use a positive step value in range()
. By default, the step is 1, so range(start, stop)
will automatically count up by 1. If you want to skip some numbers while counting up, you can specify a larger positive step, like 2 or 3.
Example
Output

Explanation: Here, the loop starts at 1 and stops before 20, with a step of 3. So it prints every third number starting from 1: 1, 4, 7, 10, 13, 16, and 19.
Decrementing the Range using a Negative Step in Python
When you want your loop to count down (decrement), make sure you use a negative step value in range()
. By default, the step is positive, so to count down, you need to set it to a negative number like -1. If you want to skip some numbers while counting down, you can specify a larger negative step, like -2 or -3.
Example:
Output:

Explanation: Here, the loop starts at 25 and stops before 10, with a step of -2. So it prints every second number going down from 25: 25, 23, 21, 19, 17, 15, 13, and 11.
Get 100% Hike!
Master Most in Demand Skills Now!
Python range() with Float Values
The range() function in Python doesn’t work with floating-point or non-integer numbers; it only accepts integer numbers for its arguments
Example:
Output:

Explanation: Here, the range() function only accepts integers. Since 5.3 is a floating-point number, this code will produce an error.
Effective Ways to Use range() in Python
1. Concatenating Two range() Objects using itertools chain() methods
You can join the results of two range() functions by using the chain() method from the itertools module. The chain() method takes multiple sequences and combines them, printing all the elements one after another.
Example:
Output:

Explanation: Here, the program combines two range() sequences using the chain() method from the itertools module. It first prints numbers from 0 to 5, then prints numbers starting from 1 and going up to 19 in step 3.
2. Accessing range() Elements by Index in Python
The range() function returns an object containing a sequence of numbers that you can access by their index. You can use both positive and negative indices to get the numbers in the sequence.
Example:
Output:

Explanation: Here, the program prints specific elements from the sequence of numbers produced by range (20). It shows the first element, the last element, and the eighth element.
3. range() function with List in Python
In this example, we’re making a list and using range() to go through and print each element in the list
Example:
Output:

Explanation: Here, the program creates a list of course names and then uses the range() function to loop through each index, printing out the course names one by one.
Using range() in Nested Loops in Python
You can use range() in nested loops to create patterns or handle data with multiple dimensions, like tables.
Example:
Output:

Explanation: Here, the program has nested loops that generate pairs of numbers. The outer loop (i) runs from 1 to 3, and for each value of i, the inner loop (j) also goes from 1 to 3. It prints out each pair of (i, j) numbers line by line.
Best Practices for Using range() Function in Python
1. Use range() instead of list: When you’re iterating through numbers, use range()
function directly in your for
loop to save memory. Avoid turning it into a list unless necessary.
2. Be careful with the stop value: Remember, the stop value isn’t included. If you want to include the end value, make sure to adjust it accordingly.
3. Use meaningful start, stop, and step values: Always choose clear and appropriate values for start
, stop
, and step
to avoid confusion and bugs in your code.
4. Avoid using floats: The range()
function works only with integers. If you need to work with floating-point numbers, consider using numpy.arange()
or a custom loop.
5. Use a negative step when counting down: When you want to create a decreasing sequence, don’t forget to specify a negative step
value.
Use Cases of range() Function in Python
1. Looping a specific number of times
If you need to do something repeatedly, like processing a list of items, you can use range() with for to loop the right number of times.
2. Generating sequences of numbers
The range() can quickly generate numbers you need for calculations, like a sequence of test scores or years.
3. Accessing items by index
When working with lists, you can use range(len(list)) to get each index and access items by their index.
4. Creating tables or patterns
Using nested range() loops, you can create multiplication tables, matrices, or other repetitive structures.
5. Controlling steps in loops
You can use a step value in the range() loop to skip numbers, reverse a list, or work with every other item in the loop.
Learn Python for Free – Build Real Skills Today!
Sign up for our free course and start mastering Python

Conclusion
In this article, we explored the Python range function in detail and how it can be used to create efficient loops and number sequences in your code. We learned about its three different variations using one, two, or three arguments and looked at practical examples to show how they work in real-world scenarios. By understanding how to use the range function properly and knowing how to avoid common pitfalls, you can write cleaner and faster Python code. Mastering range is a simple yet powerful step in becoming a more effective Python programmer.
Take your skills to the next level by enrolling in the Python Course today and gaining hands-on experience. Also, prepare for job interviews with Python interview questions prepared by industry experts.
Python range() Function – FAQs
Q1. What does range(1, 10, 2) return?
It returns a sequence of numbers starting from 1 up to (but not including) 10, counting by 2. So it produces: 1, 3, 5, 7, 9.
Q2. What is range() in a for loop?
In a for loop, range() is used to generate a sequence of numbers that the loop can iterate over. For example, for i in range(5): will loop 5 times.
Q3. How do I create a list of numbers from 1 to 100 in Python?
Use list(range(1, 101)) to create a list of numbers from 1 to 100.
Q4 Can range() be used with a negative step?
Yes, if you use a negative step (like range (10, 0, -1)), it counts down instead of up
Q5. Does range() include the stop value?
No, the stop value is not included. For example, range(5) includes 0, 1, 2, 3, and 4 — it stops before 5.