Printing range() seems inconsistent

Following Introduction to Python course and currently on
https://www.codecademy.com/paths/analyze-data-with-python/tracks/ida-2-introduction-to-python/modules/ida-2-4-list-comprehension/lessons/lists/exercises/squares

When using list created by range, I notice that I can directly access a range as a list:
nums = range(11)
print(nums)

Prints [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

This however seems inconsistent with behavior observed before along the previos list lessons where doing seemingly the same:
list3 = range(11)
print(list3)

Would instead print: range(0, 11)

Back then I had to use:
print(list(list3))
In order to get the printed list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Am I missing something or there is some inconsistency in the way the simulation is working?

I don’t see that behaviour…

>>> nums = range(11)
>>> print(nums)
range(0, 11)
>>> print(list(nums))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

It would appear, since I’m getting a different result to you, that either you ran something different to get that original list (perhaps a list comprehension) or there’s something weird afoot.

Thanks a lot for your reply, but the behavior I see is consistent in that lesson:
Running under same “List comprehension” page is:
https://www.codecademy.com/paths/analyze-data-with-python/tracks/ida-2-introduction-to-python/modules/ida-2-4-list-comprehension/lessons/lists/exercises/size

Only code is:
nums = range(11)
print(nums)

Result is:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Running under the previous Lists lessong, page:
https://www.codecademy.com/paths/analyze-data-with-python/tracks/ida-2-introduction-to-python/modules/ida-2-3-python-lists/lessons/create-python-list/exercises/review

Only active code is: (exactly the same copy-paste)

nums = range(11)
print(nums)

Result is:
range(0, 11)

Different result for same code. Did you try in the actual pages?

I actually just tried outside in the VS Code debugger and the result I get is: range(0, 11), so I’ll go with that for now,

Has to with the different python versions, in python2, range returns a list. In python3, you get a range object, which is similar to a generator. A generator hands you the values one by one/one at a time, this way the whole list doesn’t have to be constructed in memory.

1 Like

Thanks a lot. I guess that last lesson I was taking was running on Python 2 in the background so that does answer the question. Much appreciated!

1 Like

:man_facepalming:

Sometimes I should step away from the keyboard and go make some coffee…

1 Like