What list range() function used?

The range function has three different versions:

range(start, stop)
range(start, stop, step)

Where does the function take the original list?
If I have more than one list created in the program. Which list will the function choose?

range produces a list:

print range(5) # outputs: [0, 1, 2, 3, 4]

so i am bit confused by your question, because it doesn’t take a list, it produces a list

1 Like

range() creates ONLY ONE list - [0, 1, 2, 3, 4, …], of different lengths?

range() produces a single list, but the list produced depends on the arguments you supply.

range(0, 3) will generate a different list then range(4,9)

1 Like

Thank you! Now I realized that the function creates a definite arithmetic sequence, and does not allocate a part of the existing list.

In Python 2, that is. In Python 3 it returns an iterator from which we can create a list.


In Python 2 we still treat range() as an iterator in most usage cases, though from time to time we might wish to generate a sequence that can be accessed later.

range(start=>inclusive, stop=>exclusive, step=>with a direction)

threes = range(3, 100, 3)


[3, 6, 9, 12, .., 90, 93, 96, 99]
1 Like