Skip to content

Range function in Python

Range function in Python

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> with range function . At first we have declared a variable x as range(5) .
  • We have passed only one parameter with range function so , it is the stop parameter . As default start and step value is 0 and 1 so Python will treat range(5) as range(0,5,1) .
  • Then we have iterated every number of the range(x) using for-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 by 1 then it will be 5 . But as 5 (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 as range(5 ,11 , 2) whose start parameter is 5 , stop parameter is 11 and step parameter is 2 .
  • as start parameter of range function is 5 so at first 5 is printed .
  • After that 5+2(step) =7 is printed , then 7+2 = 9 is printed .
  • After that if we increment 9 by 2 then it will be 11 . But as 11 (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 .As len() function returns length of any datatype so len(x) will return 3 .
  • As len(x) returns 3 so range(len(x)) will be treated as range(3) whose start parameter is 0 , stop parameter is 3 and step parameter is 1 .
  • At first name variable will be assigned as 0 so x[name] will be x[0] which is equal to the 0th element of x . As 0th element of x is "Rajkumar" so "Rajkumar" is printed .
  • After that 1st element x[1] ("Sourav") is printed then 2nd element x[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 .

Leave a Reply