Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error in range() function – Python 3.11.3

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading