range() function does not return Iterator:

In Python for data science, the range function generates a list of numbers that can be used for iteration in many situations such as in for loops. But one thing to keep in mind is that range objects are not iterators. It is a class of a list consisting of immutable objects. Iteration behavior of range is the same as that of lists. Range is unable to call the next function directly. To call the next function, we need an iterator using iter.

python for data science

From the Runtime Error shown above, we can clearly say that range is not an iterator.

As range is iterable so we can get an iterator with their help but direct calling to next is not available.

Let us look into some important properties of range:

  • Range does not generate all numbers present in it. It only returns those numbers which we get using loops.
  • The range objects are immutable, which makes them suitable for use as index of dictionaries.
  • They consist of start, stop, and step arguments.
  • We can visit the same range again and again.

To learn more about range and iterators in python for data science then you can check this and this as well.

Leave a Reply

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