Does the Python range generator generate all values ​​or give them gradually?

When you do something like this:

for i in range(5):
      print i

What does Python do? Does it first generate an array with [0,1,2,3,4] and then iterate over each element printing it? Similar to:

for i in [0,1,2,3,4]:
  print i

Or does it print each number as it is created? Sort of:

Generate 0 assign 0 for i print i

Create 1 -> assign 1 to i -> print i

Create 2 -> Assign 2 to me -> print i

Create 3 -> Assign 3 to me -> print i

Generate 4 -> assign 4 to i -> print i

Update

I added a tag for Python 2.7. I did not think that my question is about the version, but it seems to be so!

I am working with Python 2.7 now, so my question relates to this. But I find the information comparing the Python 2 range to the Python 3 range very valuable.

+4
2

Python 2, range() , for.

Python 3 , , . for , .

Python 2 xrange(), - , Python 3 range() . Python 2 xrange Python 2's Python 3. , , .

, Python 3, , :

>>> list(range(...))
+6

. ,

print range(4)
0
source

Source: https://habr.com/ru/post/1598440/


All Articles