range


So what does range really return?

Instead of returning the list of numbers (as it used to do in Python 2), now it returns a range object that provides "the opportunity to go over the specific series of numbers" without actually creating the list of numbers. Getting an object instead of the whole list has a number of advantages. One is space. In the next example we'll see how much memory is needed for the object returned by the range function and how much would it take to have the corresponding list of numbers in memory. For now let's see how can we use it:


examples/functional/range.py
rng = range(3, 9, 2)
print(rng)
print(type(rng).__name__)

print()

for num in rng:
    print(num)

print()

for num in rng:
    print(num)

print()

print(rng[2])
print(len(rng))

range(3, 9, 2)
range

3
5
7

3
5
7

7
3