Python Forum
range object - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: range object (/thread-14664.html)



range object - nikosxatz - Dec-11-2018

1. Is range object a collection (container) or just a sequence?
2. Is range object a generator?
Thanks


RE: range object - wavic - Dec-11-2018

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.


RE: range object - buran - Dec-11-2018

>>> 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
>>>



RE: range object - DeaD_EyE - Dec-11-2018

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.