Last Updated : 11 Jul, 2025
The Python range() function returns a sequence of numbers, in a given range. The most common use of it is to iterate sequences on a sequence of numbers using Python loops.
Example
In the given example, we are printing the number from 0 to 4.
Python
for i in range(5):
print(i, end=" ")
print()
Output:
0 1 2 3 4Syntax of Python range() function
What is the use of the range function in PythonSyntax: range(start, stop, step)
Parameter :
- start: [ optional ] start value of the sequence
- stop: next value after the end value of the sequence
- step: [ optional ] integer value, denoting the difference between any two numbers in the sequence
Return : Returns an object that represents a sequence of numbers
In simple terms, range() allows the user to generate a series of numbers within a given range. Depending on how many arguments the user is passing to the function, the user can decide where that series of numbers will begin and end, as well as how big the difference will be between one number and the next. Python range() function takes can be initialized in 3 ways.
When the user call range() with one argument, the user will get a series of numbers that starts at 0 and includes every whole number up to, but not including, the number that the user has provided as the stop.
Python range visualization Example of Python range (stop)In this example, we are printing the number from 0 to 5. We are using the range function in which we are passing the stopping of the loop.
Python3
# printing first 6
# whole number
for i in range(6):
print(i, end=" ")
print()
Output:
0 1 2 3 4 5Python range (start, stop)
When the user call range() with two arguments, the user gets to decide not only where the series of numbers stops but also where it starts, so the user doesn’t have to start at 0 all the time. Users can use range() to generate a series of numbers from X to Y using range(X, Y).
Python range visualization Example of Python range (start, stop)In this example, we are printing the number from 5 to 19. We are using the range function in which we are passing the starting and stopping points of the loop.
Python3
# printing a natural
# number from 5 to 20
for i in range(5, 20):
print(i, end=" ")
Output:
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19Python range (start, stop, step)
When the user call range() with three arguments, the user can choose not only where the series of numbers will start and stop, but also how big the difference will be between one number and the next. If the user doesn’t provide a step, then range() will automatically behave as if the step is 1. In this example, we are printing even numbers between 0 and 10, so we choose our starting point from 0(start = 0) and stop the series at 10(stop = 10). For printing an even number the difference between one number and the next must be 2 (step = 2) after providing a step we get the following output (0, 2, 4, 8).
Python range visualization Example of Python range (start, stop, step)In this example, we are printing the number from 0 to 9 with the jump of 2. We are using the range function in which we are passing the starting and stopping points with the jump of the iterator.
Python3
for i in range(0, 10, 2):
print(i, end=" ")
print()
Output:
0 2 4 6 8Incrementing the Range using a Positive Step
If a user wants to increment, then the user needs steps to be a positive number.
Python3
# incremented by 4
for i in range(0, 30, 4):
print(i, end=" ")
print()
Output :
0 4 8 12 16 20 24 28Python range() using Negative Step
If a user wants to decrement, then the user needs steps to be a negative number.
Python3
# incremented by -2
for i in range(25, 2, -2):
print(i, end=" ")
print()
Output :
25 23 21 19 17 15 13 11 9 7 5 3Python range() with Float Values
Python range() function doesn’t support float numbers. i.e. user cannot use floating-point or non-integer numbers in any of its arguments. Users can use only integer numbers.
Python3
# using a float number
for i in range(3.3):
print(i)
Output :
for i in range(3.3):Python range() with More Examples Concatenation of two range() functions using itertools chain() method
TypeError: 'float' object cannot be interpreted as an integer
The result from two range() functions can be concatenated by using the chain() method of itertools module. The chain() method is used to print all the values in iterable targets one after another mentioned in its arguments.
Python3
from itertools import chain
# Using chain method
print("Concatenating the result")
res = chain(range(5), range(10, 20, 2))
for i in res:
print(i, end=" ")
Output :
Concatenating the resultAccessing range() with an Index Value
0 1 2 3 4 10 12 14 16 18
A sequence of numbers is returned by the range() function as its object that can be accessed by its index value. Both positive and negative indexing is supported by its object.
Python3
ele = range(10)[0]
print("First element:", ele)
ele = range(10)[-1]
print("\nLast element:", ele)
ele = range(10)[4]
print("\nFifth element:", ele)
Output :
First element: 0range() function with List in Python
Last element: 9
Fifth element: 4
In this example, we are creating a list and we are printing list elements with the range() in Python.
Python
fruits = ["apple", "banana", "cherry", "date"]
for i in range(len(fruits)):
print(fruits[i])
Output :
appleSome Important points to remember about the Python range() function
banana
cherry
date
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4