Ad

Why Does A Variable Given Data Type "range" Still Show As "list" When The "type" Function Is Used On It?

- 1 answer

While studying data types in Python, I encountered a data type range and used a variable to define it. However using type function to know about this still tells that it's a list data types.

Am I missing something here? Please guide. Thank you so much.

x = range(3)
print(type(x))

Output is as shown below:

C:\Python27>python.exe learn.py
<type 'list'>
Ad

Answer

It seems you are mixing up Python 3 and 2. These are two major versions of Python. Python 3000 introduced many intentionally backwards incompatible changes including in the workings of the range function.

In Python 2, the range function immediately expanded out to a list list_range = list(range(3))

In Python 3 it is just a mapping to the range data type.

Check the official docs here [https://docs.python.org/3.0/whatsnew/3.0.html]

It is something to do with memory.

Ad
source: stackoverflow.com
Ad