1. Is range object a collection (container) or just a sequence?
2. Is range object a generator?
Thanks
2. Is range object a generator?
Thanks
range object
|
1. Is range object a collection (container) or just a sequence?
2. Is range object a generator? Thanks
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.
Dec-11-2018, 11:43 AM
>>> 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
Dec-11-2018, 12:44 PM
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! |
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
matplotlib x axis range goes over the set range | Pedroski55 | 5 | 6,262 |
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 | 9,934 |
Jan-24-2020, 06:19 AM Last Post: KiNeMs |