Understanding the Return Value of Python's range() Function


Python range() return value

Python range() returns an object of class type range.

Let us create a range object, store it in a variable, say my_range, and print the type of this variable.

my_range = range(4, 9)
print(type(my_range))

Let us have a brief look of the range class definition.

Python range class definition
  • range class has three readonly properties: start, stop, and step.
  • range class can be initialised with (stop), (start, stop), or (start, stop, step).
  • range class has two methods: count(), and index().

Also, this range class object has an iterator that returns integers, using which we can iterate over the values. We can use For loop to iterate over the values in the given range.

For example, consider the following program. We have used a For loop to iterate over the

my_range = range(4, 9)
for i in my_range:
    print(i)

Summary

In this tutorial of Python Ranges, we learned what type of value does range() function return, and the different properties and objects this range class object has.