How do you create a range of objects in python?

  1. Home
  2. Python Built-in Functions
  3. Python range() function

(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!


Updated on Jan 07, 2020


The range() function is used to generate a sequence of numbers over time. At its simplest, it accepts an integer and returns a range object (a type of iterable). In Python 2, the range() returns a list which is not very efficient to handle large data.

The syntax of the range() function is as follows:

Syntax:

range([start,] stop [, step]) -> range object

PARAMETERDESCRIPTION
start (optional) Starting point of the sequence. It defaults to 0.
stop (required) Endpoint of the sequence. This item will not be included in the sequence.
step (optional) Step size of the sequence. It defaults to 1.

Let's now look at a couple of examples to understand how range() works:

Example 1:

>>> 
>>> range(5)
range(0, 5)
>>> 
>>> list(range(5)) # list() call is not required in Python 2
[0, 1, 2, 3, 4]
>>>

Try it out:

When range() is called with a single argument it generates a sequence of numbers from 0 upto the argument specified (but not including it). That's why the number 5 is not included in the sequence.

Example 2:

>>>
>>> range(5, 10)
range(5, 10)
>>> 
>>> list(range(5, 10))
[5, 6, 7, 8, 9]
>>>

Try it out:

Here range() is called with two arguments, 5 and 10. As a result, it will generate a sequence of numbers from 5 up to 10 (but not including 10).

You can also specify negative numbers:

>>> 
>>> list(range(-2, 2))
[-2, -1, 0, 1]
>>> 
>>> list(range(-100, -95))
[-100, -99, -98, -97, -96]
>>>

Try it out:

Example 3:

>>> 
>>> range(1, 20, 3)
range(1, 20, 3)
>>> 
>>> 
>>> list(range(1, 20, 3))
[1, 4, 7, 10, 13, 16, 19]
>>>

Try it out:

Here the range() function is called with a step argument of 3, so it will return every third element from 1 to 20 (off course not including 20).

You can also use the step argument to count backwards.

>>> 
>>> list(range(20, 10, -1))
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11]
>>> 
>>> list(range(20, 10, -5))
[20, 15]
>>>

Try it out:

The range() function is commonly used with for loop to repeat an action certain number of times. For example, in the following listing, we use range() to execute the loop body 5 times.

>>> 
>>> for i in range(5):
...     print(i)
... 
0
1
2
3
4
>>>

Try it out:

This code is functionally equivalent to the following:

>>> 
>>> for i in [0, 1, 2, 3, 4]:
...     print(i)
... 
0
1
2
3
4
>>>

However, in the actual code, you should always use range() because it is concise, flexible and performs better.


Other Tutorials (Sponsors)

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!


What does range () do in Python?

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

How do you range data in Python?

The Python range() function returns the sequence of the given number between the given range..
range(stop) takes one argument..
range(start, stop) takes two arguments..
range(start, stop, step) takes three arguments..

How do you make a range inclusive in Python?

To create a range in Python, use the range() function. ... .
Python range is inclusive because it starts with the first argument of the range() method, but it does not end with the second argument of the range() method; it ends with the end – 1 index..

What a range () function does give an example?

One of the most common uses of the range() function is for iterating over a series of values in a for loop. This is particularly useful if you want to access each of the values in a list or array, or, for example, only every other value. In this example, the range() function is generating a sequence from 0 to 4 .