Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
range object
#1
1. Is range object a collection (container) or just a sequence?
2. Is range object a generator?
Thanks
Reply
#2
Neither.
>>> obj = range(5)
>>> type(obj)
<class 'range'>
>>> 
It's a some kind of iterator.

I came across this months ago. You may find it interesting to read.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
>>> some_range = range(10)
>>> type(some_range)
<class 'range'>
>>> import collections.abc
>>> isinstance(some_range, collections.abc.Sequence)
True
>>> isinstance(some_range, collections.abc.Container)
True
>>> isinstance(some_range, collections.abc.Generator)
False
>>>
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
#4
The range object is very handy.

r = range(-100, 255, 2)

# you can reuse the range object
list(r) == list(r)
for i in r:
    for j in r:
        pass

# member testing is supported
42 in r
-100 in r

# Access an area
r[0:10] # > returns a new range object, which fit's to the selected area of the origin

# IndexAccess
r[1] # > returns the second element

# using built-in functions on range object
sum(r)
min(r)
max(r)

import math
import operator
from functools import reduce


math.factorial(10) == reduce(operator.mul, range(1,10+1))
Read the provided link posted by wavic.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x axis range goes over the set range Pedroski55 5 3,215 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,073 Jan-24-2020, 06:19 AM
Last Post: KiNeMs

Forum Jump:

User Panel Messages

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