Page Contents
Range function in Python is used to to generate a sequence of numbers . Actually Python treats range function as a datatype which represents an immutable sequence of numbers . Range function in Python is commonly used for looping a specific number of times in for loops .
Syntax of Range function in Python
range(start , stop , step)
Parameters of Range function in Python
- start = Optional , An integer number which will specify the starting position . Default is
0
. - stop = An integer number which will specify the stopping position ( This number will not be included) .
- step = Optional , An integer number which will specify the incrementation . Default is
1
.
Return type of Range function in Python
It returns a range-object
(Sequence of numbers)
Range function in Python examples
Example 1: Creating a range-object in Python
# range function
x = range(5)
print(type(x))
<class 'range'>
In the above code first we have declared a variable x
as range(5)
. Then we have printed the type of the variable . We can see that type of the variable is range
.
Example 2: Iterate a range-object in Python
# range function with for loop
x = range(5)
for number in x:
print(number)
0 1 2 3 4
- In the above code we have applied
<a href="https://www.codingconception.com/python/python-for-loop/">for-loop</a>
withrange
function . At first we have declared a variablex
asrange(5)
. - We have passed only one parameter with
range
function so , it is the stop parameter . As default start and step value is0
and1
so Python will treatrange(5)
asrange(0,5,1)
. - Then we have iterated every number of the
range
(x
) usingfor-loop
and printed the number . - So , first
0
(start) is printed , then 0+1 (step) =1
is printed , then 1+1 =2
is printed , then 2+1 =3
is printed , then 3+1 =4
is printed . - After that if we increment
4
by1
then it will be5
. But as5
(stop) is not included so it will not be printed .
Example 3: Using all parameters of a Range function in Python
# range function with all the parameter
x = range(5,11,2)
for number in x:
print(number)
5 7 9
- In the above code we have declared a variable
x
asrange(5 ,11 , 2)
whose start parameter is5
, stop parameter is11
and step parameter is2
. - as start parameter of
range
function is5
so at first5
is printed . - After that 5+2(step) =
7
is printed , then 7+2 =9
is printed . - After that if we increment
9
by2
then it will be11
. But as11
(stop) is not included so it will not be printed .
How to use range function with other Python datatype
We can also use range
function with other Python datatype such as string
, list
, tuple
, set
and dictionary
. In this article we will use range
function with Python list
. To use range
function with Python list
we need to use len
function .
Example 1: Range function in Python with list
# range function with python list
x = ["Rajkumar","Sourav","Sudipta"]
for name in range(len(x)):
print(x[name])
Rajkumar Sourav Sudipta
- In the above code we have declared a variable
x
as["Rajkumar","Sourav","Sudipta"]
which has 3 elements .Aslen()
function returns length of any datatype solen(x)
will return3
. - As
len(x)
returns3
sorange(len(x))
will be treated asrange(3)
whose start parameter is0
, stop parameter is3
and step parameter is1
. - At first
name
variable will be assigned as0
sox[name]
will bex[0]
which is equal to the 0th element ofx
. As 0th element ofx
is"Rajkumar"
so"Rajkumar"
is printed . - After that 1st element
x[1]
("Sourav"
) is printed then 2nd elementx[2]
("Sudipta"
) is printed .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .