Home > python > Python Range

Python range

Python range is a built in function available in python. While going through the different articles or books on python you might have come across this function. But the question is what it does? Why it is used. In a single statement we can say that this function will generate a list of numbers. The generated list can be used according to the requirements of the users i.e. to iterate over with a loop etc. you can have many number of cases to use this function to get a list of numbers. E.g. you want to perform a task repeatedly for X number of times. You can generate a list using python range function and then loop through this list to perform the required task.

Syntax

The basic signature for python range function is:

range (Start, Stop, Step)

all three parameters are of integer type.

Start: It represents the starting point of the list. It is optional argument and the default value for this argument is 0.

Stop: It is the end point of the sequence generated. It is the only required argument in the function. It does not have any default value.

Step: this is the 3rd and the last argument in the function. Just like start it is also the optional argument and its default value is 1. Though the user can change this value according to its requirements.

Since python is 0 indexed. So by default every list generated by range function will start from 0 and will end at stop-1. i.e. 1 less than the value for stop argument. Though it will include the starting point in the list.

Python range () Function Examples

list = range (8) # single argument. Stop value for the function.

for value in list:

print value

Output:

0

1

2

3

4

5

6

7

list = range (0,8) # two arguments. Start and Stop values for the function.

for value in list:

    print value

Output:

0

1

2

3

4

5

6

7

list = range (0,8, 1) # three argument. Start, Stop and step values for the function.

for value in list:

    print value

Output:

0

1

2

3

4

5

6

7

We can see in the above examples that all three calls of the range function generate the same list. This is because we have provided the default values for the optional arguments.

Let’s have a look at some other examples:

list = range (10,100,15)

for value in list:

    print(value)

Output:

10

25

40

55

70

85

Following errors can occur if we pass arguments any values other than integers.

list = range(1.1,100,15.6)


for value in list:

    print(value)

Traceback (most recent call last):

  File “main.py”, line 4, in <module>

    list = range(1.1,100,15.6)

TypeError: range() integer start argument expected, got float.

list = range(1,10.9,15.6)


for value in list:

    print(value)

Traceback (most recent call last):

  File “main.py”, line 4, in <module>

    list = range(1,10.9,15.6)

TypeError: range() integer end argument expected, got float.

list = range(1,100,15.6)


for value in list:

    print(value)

this will give us the following output.

Traceback (most recent call last):

  File “main.py”, line 4, in <module>

    list = range(1,100,15.6)

TypeError: range() integer step argument expected, got float.

print range(6) 

Output:

[0, 1, 2, 3, 4, 5]

python range

As we have seen in the above examples that we can’t use floating point values in the python range function. But python provides us different other simple functions to generate a list of floating point values. frange () function is one of them. Just like the range () it also follows the same syntax but allows us to use floating point values.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.