❮ Built-in Functions
Example
Create a sequence of numbers from 0 to 5, and print each item in the sequence:
x = range[6]
for n in x:
print[n]
Try it Yourself »
Definition and Usage
The range[]
function returns a
sequence of numbers, starting from 0 by default, and increments by 1 [by default], and stops before a specified number.
Syntax
Parameter Values
start | Optional. An integer number specifying at which position to start. Default is 0 |
stop | Required. An integer number specifying at which position to stop [not included]. |
step | Optional. An integer number specifying the incrementation. Default is 1 |
More Examples
Example
Create a sequence of numbers from 3 to 5, and print each item in the sequence:
x = range[3, 6]
for n in x:
print[n]
Try it Yourself »
Example
Create a sequence of numbers from 3 to 19, but increment by 2 instead of 1:
x = range[3, 20, 2]
for n in x:
print[n]
Try it Yourself »
❮ Built-in Functions
The range[]
function returns a sequence of numbers between the give range.
Example
# create a sequence of numbers from 0 to 3
numbers = range[4]
# iterating through the sequence of numbers
for i in numbers:
print[i]
# Output:
# 0
# 1
# 2
# 3
Note: range[]
returns an immutable sequence of numbers that can be easily converted to lists, tuples, sets etc.
Syntax of range[]
The range[]
function can take a maximum of three arguments:
range[start, stop, step]
The start
and step
parameters in range[]
are optional.
Now, let's see how range[]
works with different number of
arguments.
Example 1: range[] with Stop Argument
If we pass a single argument to range[]
, it means we are passing the stop
argument.
In this case, range[]
returns a sequence of numbers starting from 0 up to the number [but not including the number].
# numbers from 0 to 3 [4 is not included]
numbers = range[4]
print[list[numbers]] # [0, 1, 2, 3]
# if 0 or negative number is passed, we get an empty sequence
numbers = range[-4]
print[list[numbers]] # []
Example 2: range[] with Start and Stop Arguments
If we pass two arguments to range[]
, it means we are passing start
and stop
arguments.
In this case, range[]
returns a sequence of numbers starting from start
[inclusive] up to stop
[exclusive].
# numbers from 2 to 4 [5 is not included]
numbers = range[2, 5]
print[list[numbers]] # [2, 3, 4]
# numbers from -2 to 3 [4 is not included]
numbers = range[-2, 4]
print[list[numbers]] # [-2, -1, 0, 1, 2, 3]
# returns an empty sequence of numbers
numbers = range[4, 2]
print[list[numbers]] # []
Example 3: range[] with Start, Stop and Step Arguments
If we pass all three arguments,
- the first argument is
start
- the second argument is
stop
- the third argument is
step
The step
argument specifies the incrementation between two numbers in the sequence.
# numbers from 2 to 10 with increment 3 between numbers
numbers = range[2, 10, 3]
print[list[numbers]] # [2, 5, 8]
# numbers from 4 to -1 with increment of -1
numbers = range[4, -1, -1]
print[list[numbers]] # [4, 3, 2, 1, 0]
# numbers from 1 to 4 with increment of 1
# range[0, 5, 1] is equivalent to range[5]
numbers = range[0, 5, 1]
print[list[numbers]] # [0, 1, 2, 3, 4]
Note: The default value of start
is 0,
and the default value of step
is 1. That's why range[0, 5, 1]
is equivalent to range[5]
.
range[] in for Loop
The range[]
function is commonly used in a for loop to iterate the loop a certain number of times. For example,
# iterate the loop 5 times
for i in range[5]:
print[i, 'Hello']
0 Hello 1 Hello 2 Hello 3 Hello 4 Hello
The Python range[] function returns the sequence of the given number between the given range. The most common use of it is to iterate sequence type [Python range[] List, string, etc. ] with for and while loop using Python.
Syntax of range[]
Syntax: range[start, stop, step]
Parameter:
- start: integer starting from which the sequence of integers is to be returned
- stop: integer before which the sequence of integers is to be returned. The range of integers ends at stop – 1.
- step: integer value which determines the increment between each integer in the sequence
Example of Python range[]
Python3
for
i
in
range
[
0
,
10
,
2
]:
print
[i, end
=
" "
]
print
[]
Output:
0 2 4 6 8
What is the use of the range function in Python
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.range[] takes mainly three arguments.
- range[stop] takes one argument.
- range[start, stop] takes two arguments.
- range[start, stop, step] takes three arguments.
Python range[stop]
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.
Example: Demonstration of Python range[stop]
Python3
for
i
in
range
[
6
]:
print
[i, end
=
" "
]
print
[]
Output:
0 1 2 3 4 5
Python 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 don’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].
Example: Demonstration of Python range[start, stop]
Python3
for
i
in
range
[
5
,
20
]:
print
[i, end
=
" "
]
Output:
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Python 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 an even number between 0 to 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].
Example: Demonstration of Python range[start, stop, step]
Python3
for
i
in
range
[
0
,
10
,
2
]:
print
[i, end
=
" "
]
print
[]
Output:
0 2 4 6 8
Python range[] with Examples
Example 1: Incrementing the range using a positive step
If a user wants to increment, then the user needs steps to be a positive number.
Python3
for
i
in
range
[
0
,
30
,
4
]:
print
[i, end
=
" "
]
print
[]
Output :
0 4 8 12 16 20 24 28
Example 2: Python range[] using negative step
If a user wants to decrement, then the user needs steps to be a negative number.
Python3
for
i
in
range
[
25
,
2
,
-
2
]:
print
[i, end
=
" "
]
print
[]
Output :
25 23 21 19 17 15 13 11 9 7 5 3 1
Example 3: Python range[] with float
Python range[] function doesn’t support the float numbers. i.e. user cannot use floating-point or non-integer numbers in any of its argument. Users can use only integer numbers.
Python3
for
i
in
range
[
3.3
]:
print
[i]
Output :
for i in range[3.3]: TypeError: 'float' object cannot be interpreted as an integer
Example 4: Concatenation of two range[] functions using itertools
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
print
[
"Concatenating the result"
]
res
=
chain[
range
[
5
],
range
[
10
,
20
,
2
]]
for
i
in
res:
print
[i, end
=
" "
]
Output:
Concatenating the result 0 1 2 3 4 10 12 14 16 18
Example 5: Accessing range[] with an index value
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: 0 Last element: 9 Fifth element: 4
Some Important points to remember about the Python range[] function:
- range[] function only works with the integers i.e. whole numbers.
- All arguments must be integers. Users can not pass a string or float number or any other type in a start, stop and step argument of a range[].
- All three arguments can be positive or negative.
- The step value must not be zero. If a step is zero python raises a ValueError exception.
- range[] is a type in Python
- Users can access items in a range[] by index, just as users do with a list: