Python Range – Range() Function in Python

Free Python courses with 57 real-time projects - Learn Python

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

What is 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.

Syntax of Python range() function

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

1. 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))

Output

[0, 1, 2]

What happens if you provide a negative value?

>>> list(range(-3))

Output

[]

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))

Output

[]

2. Two Parameters

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

range(start,stop)

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

Output

[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))

Output

[]

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))

Output

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

Output

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

Output

[]

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

>>> list(range(3,3))

Output

[]

There are no integers between 3 and less than 3.

3. 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.

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

Output

[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))

Output

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

Output

[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))

Output

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))

Output

[1, 3, 5]

Let’s take another example.

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

Output

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))

Output

[]

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.

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) 

Output

0
2
4
6
8
10

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

Python Interview Questions on Range() Function

  1. What is Python range() function?
  2. What does range() do in Python?
  3. How do you find the range in Python?
  4. How does range() work in Python?
  5. What is the syntax  for range() function in Python?

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.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

4 Responses

  1. Arunkumar H says:

    Nice explanation Thanks.

    • DataFlair Team says:

      It’s a real pleasure for us to understand that our hard work has been of great help to you. Don’t forget to take a look at python projects. They can offer exciting opportunities for exploring and learning.

  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 *