Python Range – Range() Function in Python

1. Python Range Function Tutorial

Something you’ve always seen with a for loop, python range() function is a handy feature in Python. Like we saw in our tutorial on Python Loops, range function in python provides us with a list of numbers to iterate on. Actually, it returns a range object, which we then convert to a list to iterate on.

So, let’s begin the Python Range Function Tutorial.

Range Function in Python

Python Range Function

2. Introduction to Range in Python

The range() function is an inbuilt function in Python, and it returns a range object. First, let’s see what it looks like.

Learn: Python Functions with Syntax and Examples

3. Syntax of Python range() function

The Range function in python takes from one to three arguments. Let’s see how.

a. One Parameter

For an argument n, the function returns integer values from 0 to n-1.

range(stop)

Let’s take an example to be clearer.

>>> list(range(3))
[0, 1, 2]

What happens if you provide a negative value?

>>> list(range(-3))
[]

The fact that it returned an empty list tells us that range() is internally coded to traverse to the right. And there are no integers from 0 to the right -3, because -3 falls to the left of 0.

>>> list(range(0))
[]

Learn: Python Dictionaries with Methods, Functions and Dictionary Operations

b. Two Parameters

Now, we’ll try different combinations for two parameters.

range(start,stop)

>>> list(range(1,7))
[1, 2, 3, 4, 5, 6]

Here, both arguments are positive. Also, 7 falls to the right of 1. So, it prints integers from 1 to 6 (7-1). Hence, we conclude that it prints integers from the first number to one from the second.

Now we try passing arguments where the first is greater than the second.

>>> list(range(7,1))
[] To back what we just said, it returns an empty list because 7 falls to the right of 1, and it traverses to the right from 7. Therefore, it never reaches 1.

In section a, we saw that we can’t pass a negative argument to range(). But it is indeed possible to pass a negative value or two when you pass two or more arguments. Let’s see how.

>>> list(range(-7,3))
[-7, -6, -5, -4, -3, -2, -1, 0, 1, 2]
>>> list(range(-7,-3))
[-7, -6, -5, -4]
>>> list(range(-3,-7))
[]

This, again, returns an empty list, for -3 lies to the right of -7.

>>> list(range(3,3))
[]

There are no integers between 3 and less than 3.

Learn Python Strings with String Functions and String Operations

c. Three Parameters

Finally, the range() function can also take a third parameter. This is for the interval.
range(start,stop,interval)

We’ll see this one by example.

Join DataFlair on Telegram
>>> list(range(7,1,-1))
[7, 6, 5, 4, 3, 2]

After bragging about how you can’t have the second argument smaller than the first one, now we tell you that you can. But on one condition- you must specify a negative interval. Here, we used -1 as an interval. We could’ve used -2 as well.

>>> list(range(7,1,-2))
[7, 5, 3]
>>> list(range(7,1,-8))
[7]

Here, note that the first integer, 7, is always returned, even though the interval -8 sends it beyond 1. Let’s now take a look at more examples for three parameters.

>>> list(range(1,7,1.5))

Traceback (most recent call last):

File “<pyshell#17>”, line 1, in <module>

list(range(1,7,1.5))

TypeError: ‘float’ object cannot be interpreted as an integer

Note that all three arguments must be integers only.

>>> list(range(1,7,2))
[1, 3, 5]

Let’s take another example.

>>> list(range(1,7,0))

Traceback (most recent call last):

File “<pyshell#26>”, line 1, in <module>

list(range(1,7,0))

ValueError: range() arg 3 must not be zero

It raised a value error, because the interval cannot be zero if you need to go from one number to another.

Actually, when we provide one or two arguments, the interval is assumed to be +1. This is why we were unable to have it traverse to the left. Now that you know what’s going on, it is easier to toy with range().

Let’s take one last example to make sure we’re on the right train of thoughts.

>>> list(range(12,2,2))
[]

Now this returns an empty list in python because in a positive interval of 2 means traversing to the right, but 2 falls to the left of 12.

Learn: Python Number Types and Their Conversion Functions

Python Interview Questions

4. Python Iterate Function

Now that we know how to use Python range() function, don’t you want to know where to use it? Well, one application is the for loop.

>>> for i in range(6):
         print(i*2) 

0
2
4
6
8
10
Read: Python Jobs – Python Career Opportunities

So, this was all about Python Range Function. Hope you like our explanation.

5. Conclusion

To sum this tutorial up on Python Range, range function in python is an in-built function, in Python, that lends us a sequence of integers. We can use this to iterate on using a for loop. Now you know that it’s possible to call range() in python with one, two, or three arguments. We would like you to come up with a creative use of the range() function; tell us in the comments.

Reference

3 Responses

  1. Arunkumar H says:

    Nice explanation Thanks.

  2. Harish says:

    in python iterate function we took range(6), which means it allots i with 0,1,2,3,4,5 when print (i*2) executes we have to get 0,2,4,6,8,10 isn’t it.please explain me why is it 2,4,6,8,10

    • DataFlair Team says:

      Hi Harish,
      Thanks for pointing that out! It indeed is 0,2,4,6,8,10. We have made the corrections, now you can learn easily Python range functions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.