Python range() Function
The range()
function generates a sequence of numbers starting from 0
(by default), increments by 1
(by default), and stops before a specified number.
This function is particularly useful for iterating over a sequence of numbers in for loops:
for x in range(3):
print('Hello world!')
output
Hello world!
Hello world!
Hello world!
Syntax
range(start, stop, step)
range() Parameters
Python range()
function parameters:
Parameter | Condition | Description |
---|---|---|
start | Optional | A number specifying start position. Default is 0 . |
stop | Required | A number specifying end position. |
step | Optional | A number specifying the increment. Default is 1 . |
When generating the sequence of numbers:
- The
start
number is included. - The
end
number is excluded.
range() Return Value
Python range()
function returns
Examples
Example 1: Basic Usage with range(stop)
The default behaviour of range()
with just one argument is to generate a sequence of numbers starting from 0
up to the specified number.
For example, generate a sequence of numbers from 0
to 5
(excluded):
for x in range(5):
print(x)
output
0
1
2
3
4
Example 2: Specifying Start and Stop with range(start, stop)
A range starts from 0
by default.
However, you can start the range at another number by specifying a start
parameter.
For example, generate a sequence of numbers from 5
to 10
(excluded):
for x in range(5, 10):
print(x)
output
5
6
7
8
9
Example 3: Generate a negative sequence of numbers with range()
Using range()
you can generate a range of negative numbers:
For example, generate a sequence of numbers from -5
to 0
(excluded):
for x in range(-5, 0):
print(x)
output
-5
-4
-3
-2
-1
Example 4: Specifying Start, Stop, and Step with range(start, stop, step)
The range increments by 1
by default.
However, you can specify a different increment by adding a step parameter.
for x in range(1, 10, 2):
print(x)
output
1
3
5
7
9
Example 5: Reversing a Range
By specifying a negative step
, you can generate a sequence of number in reverse order (i.e. decrementing):
for x in range(10, 0, -2):
print(x)
output
10
8
6
4
2
Example 6: Construct a List Using range() function
You can construct a list of numbers from the range using list()
constructor.
my_list = list(range(10))
print(my_list)
output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Another example that uses List Comprehension is the following:
squares = [i**2 for i in range(10)]
print(squares)
output
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Example 7: Range for Float Numbers
The range()
function works ONLY with integers. It does not support the float type.
BUT, you can use arange()
function from NumPy
module to generate the range for float numbers.
import numpy
for i in numpy.arange(0, 2.75, 0.5):
print(i)
output
0.0
0.5
1.0
1.5
2.0
2.5
Example 8: Indexing and Slicing a Range
You can access items in a range()
by index or even slice it (in the sam way you would with a list).
For example
# example with indexing
print(range(10)[3])
# example with slicing (converting before to list)
print(list(range(10)[2:6]))
output
3
[2, 3, 4, 5]