I’m new in programming and I’m facing an error in Python that I didn’t find a way to correct.
I’m trying to use the function range(). According to documentation if I try the follow command it should print as follows:
print(range(4))
[1, 2, 3, 4]
However when I execute it in console or in a script .py the output is as follows:
print(range(4))
range(0, 4)
>>>
I tried to find something online without success. I check the documentation, used ‘help(range)’ and it should work according to the documentation and examples I saw online. I tried also console, pycharm, visual studio, always the same.
>Solution :
print(range(4))
prints a range
object.
You should print:
print(list(range(1,5)))
#[1, 2, 3, 4]
Just to make you understand;
if you put range in set you will get:
set(range(4))
#{0, 1, 2, 3}
If you put range in tuple you will get:
tuple(range(4))
#(0, 1, 2, 3)
Similarly, if you put range in list you will get:
list(range(4))
#[0, 1, 2, 3]
Link to doc: https://docs.python.org/3/library/stdtypes.html#range