Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Range Function
#1
Hello Python World!

I'm not new to programming, I am intermediate to advanced C family programmer (mostly focused on OpenGL at the moment), but I'm learning Python at work, plus I play on Codingame.com (check it out if you're unfamiliar with the games!)

Anyways, the range function does not work as described by the tutorial that I'm following, nor does it seem to behave according to the way it's described in the documentation.


When I use the range() function with any parameters whatsoever, the most I get back is:

range(<parameters re-written>)

In other words, it doesn't enumerate the list in the range provided, HOWEVER, when I use it in as a loop control variable, it does appear to loop through the range that I provide.

Is there something that I'm missing about my python install? Or is there something that I just don't understand about this range() function?

Anyways, everything else seems to work just fine, not sure why this function is not behaving as expected. I searched the forum before posting, as I was expecting a bunch of topics related to this, but did not find any....

Thanks,

Jeff Cummings
Shifty
Reply
#2
As explained in the docs, in Python3:
Quote:Rather than being a function, range is actually an immutable sequence type, as documented in Ranges and Sequence Types — list, tuple, range.
You need to use list() to convert it to list and see the numbers.

Also from the docs
Quote:The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start, stop and step values, calculating individual items and subranges as needed).

(Feb-17-2019, 09:08 PM)OceanJeff40 Wrote: Anyways, the range function does not work as described by the tutorial that I'm following, nor does it seem to behave according to the way it's described in the documentation.
In python2 range() is indeed function and returns list. So your tutorial and the documentation you reference is for python2. However support for python2 will end 01.01.2020, so better find tutorial for python3

python2

>>> type(range)
<type 'builtin_function_or_method'>
>>> range(5)
[0, 1, 2, 3, 4]
>>> type(range(5))
<type 'list'>
python 3

>>> type(range)
<class 'type'>
>>> range(5)
range(0, 5)
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> type(range(5))
<class 'range'>
>>> 
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x axis range goes over the set range Pedroski55 5 3,179 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Writing a function to calculate time range in Unix Epoch Time t4keheart 2 2,997 Jul-15-2020, 01:55 AM
Last Post: t4keheart
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,024 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  Simple statistics with range function Pythonlearner2019 2 2,118 Nov-25-2019, 05:25 PM
Last Post: Pythonlearner2019
  Cannot print range function AndyArsalan 2 2,840 Sep-11-2018, 07:01 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020