-1

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.

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44

1 Answers1

1

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

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
  • thank you for the answer, but is the behavior correct? I mean, based on the documents and tutorials I saw (i.e. Python for everybody) the command (range(4)) should generate a list like [0, 1, 2, 3] – qualquercoisa May 09 '23 at 01:35
  • yes the behavior's correct. I have added more explanation to my answer. – Talha Tayyab May 09 '23 at 01:39
  • 4
    @qualquercoisa: You're almost certainly looking at *wildly* out-of-date docs & tutorials for Python 2, a version of the language that has been end-of-life and unsupported now for over two years. `range` in Python 3 is a dedicated class (similar to, but more powerful than, Python 2's `xrange`), not a simple function that returns a `list` of `int`s. – ShadowRanger May 09 '23 at 01:41
  • @GodIsOne your explanation now is very clear, thank you for the update in the answer and for sharing the documentation. – qualquercoisa May 09 '23 at 01:50
  • @ShadowRanger, thank you also! I'm watching a tutorial that is about Python 3 actually but as you said it is probably outdated. https://youtu.be/ljExWqnWQvo?t=473 – qualquercoisa May 09 '23 at 01:53
  • 1
    @qualquercoisa: Yeah, the course materials claim to be for Python 3, but I'm guessing those specific slides were made up in the Py2 era, and poorly ported forward (they added parens to `print` calls, but forgot `range` doesn't mean what it used to mean). – ShadowRanger May 09 '23 at 02:05
  • @GodIsOne, additional question: so when I run "range(4)" it does generates the range I asked for, but it is just not listed (printed visually) as a list because I didn't use the command list. When I add the "list" function it builds a list based on the values that "range" generates and the "print" only makes it visible. Is my understand correct? – qualquercoisa May 09 '23 at 02:06
  • @qualquercoisa: Kind of. It depends what you mean by "generates the range I asked for". It generates a range object, but that's not actually a container of all the integers from 0-3. Instead, it just stored the parameters of the range (i.e. the start, stop and step arguments you gave it) and it will generate the actual values on the fly later, as needed. When you pass the range to `list` (or any of the other container types shown in this answer), those classes iterate over the range to get the specific values, which the range iterator calculates one by one. – Blckknght May 09 '23 at 02:21