View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Often times we want to create a list containing a continuous value like, in a range of 100-200. Let’s discuss how to create a list using the
range[]
function.
Will this work ?
My_list
=
[
range
[
10
,
20
,
1
]]
print
[My_list]
Output :
As we can see in the output, the result is not exactly what we were expecting because Python does not unpack the result of the range[] function.
Code #1: We can use argument-unpacking operator i.e. *.
My_list
=
[
*
range
[
10
,
21
,
1
]]
print
[My_list]
Output :
As we can see in the output, the argument-unpacking operator has successfully unpacked the result of the range function.
Code #2 : We can use the
extend[]
function to unpack the result of range function.My_list
=
[]
start, end
=
10
,
20
if
start < end:
My_list.extend[
range
[start, end]]
My_list.append[end]
print
[My_list]
Output
:
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