0
Can any one explain this code?
I mean why its printing range(0,10). https://code.sololearn.com/cTWqLV29Oz5i/?ref=app
4 Answers
+ 4
Even Dead I Am The Hero.
let me put it this way. `range` is a built in class in python, also, a generator class. Now, what is displayed by a class when it is printed is defined by either the __repr__ or __str__ method on that class. So we can assume that the __repr__ method on the range class returns "range(start-value, stop-value)". Now, why the `range` class has defined its __repr__ method that way? As I mentioned above, range is a generator class, i.e. it does NOT store all the numbers in the range. Those numbers are 'generated' when you iterate on `range`. That is why, it would be a waste of precious time and resources to generate all these numbers just to print them, and for that reason, `range` is printed like that.
+ 3
Because range() is a generator class. It has to 'generate' all the numbers before printing them. There is no point in wasting time and resources on just printing all the numbers.
You can convert the result to a list and print it, like so
print(list(range(0, 10)))
# will output [0, 1, 2, .... 9, 10]
or, you can unpack the generator and pass the elements as arguments to the print() function, like so
print(*range(0, 10))
# will output 0 1 2 ... 9 10
+ 1
XXX
I mean why its giving range(0,10) as output.
0
XXX
Thank You So Much