Level 6 : Custom range implementation.


class CustomRange:
    def __init__(self, start, end, step=1):
        self.current = start
        self.end = end
        self.step = step

    def __iter__(self):
        return self

    def __next__(self):
        if self.current >= self.end:
            raise StopIteration
        value = self.current
        self.current += self.step
        return value

it = iter(CustomRange(2, 10, 2))
print(list(it))

Author: SandhyaR | Tags: iterator | 1859+ Python FAQs | Share
functions | reading:-read(),-readline(),-readlines() | operator-and | precedence-and-associativity | python_version | matrix-list-comprehension | objects | multiprocessing | multiple-inheritance | list-mutation | list-slicing | python-syntax-and-structure | mysql | opening-files-(open()) | package-management | map-function | print-statement | pass | numpy | operators | namespace | list,-set,-and-dictionary-comprehensions | math | module | list-operation | managing-directories-with-os-and-shutil | mathematics | using-yield-in-functions | range | conversion | swapping | variable | encoding | factorial-function | asyncio | __all__-attribute | handling-missing-values-(fillna,-dropna) | head-and-tail | statement | sorting-and-ranking | object-attribute | init | zip | __future__ | else-statement | center | heap-memory | interpreter | directory-operations | feature | class-and-object | working-with-temporary-files-using-tempfile | data-structure | dicts | conditional-statements | string-methods | loop | append | using-map()-for-transformations | advanced-file-operations | split | conditional-statement | zip-function | shallow-copy | raise | list-vs-tuple | boolean-and-non-boolean | method | list-methods-and-slicing | while-else-loop | integers | basic | sys | indentation | __init_subclass__ | string-slicing | assignment-operator | overriding-methods | error | assignment-operators | matplotlib | syntax-error | walking-through-directories-using-os.walk() | working-with-dates-and-times | list-indexing | pickling--unpickling | lists | memory | error-handling | memory-management | decimal | asyncio-module | operand | literal-characters-and-special-characters | string-manipulation | math-copysign | strings | gil | immutability | function-and-scope | input-function | recurssion | built-in | thread-safe-data-structures-(queue.queue) | thread-synchronization | file-handling | inheritance | single-inheritance | fabs-function | oops | lambdas | touples | membership-operators-(in,-not-in) | filter | generators | expand-tab | floating-point-precision | tuples | indexing:-loc-vs-iloc | library | iterators | virtual-environment | multithreading | difference-between-threads-and-processes | mutable | logical-operator | function-argument | bitwise | defining-functions | while-loop | postfix | attribute | version | data-types | string-iteration | context-manager | tuple-unpacking | loop-control | creating-and-starting-threads | datatype | collections | data-type | quantifiers-(*,-+,-?,-{}) | command-line-parsing | polymorphism | random | control-statement | built-in-function | identity-operator | type_conversion | round | indexing | binary-operation | generator | bitwise-operator | using-join()-and-is_alive()-methods | boolean-masking | python-repl | data_structure | higher-order-function | joining-and-merging-dataframes | max-min | id-function | expression | pep | type-conversion | decorator | class-decorators | method-overloading | enumerate() | software-design | lambda-function | staticmethod | built-in-function | exception-handling | frozenset | introduction-to-python | creating-custom-modules | complex | time-series-with-pandas | comparison | python-history | classes | global-variable | with-statement | descriptors | loop-control-statements | class-and-instance-variable | list-comprehensions | parsing-with-strptime | set | sets | escape-operator | built-in-modules | operator-overloading | multiple-inheritance | counter | application | while-and-for-loop | using-os-and-pathlib-modules | mathematical-function | async | assert | control-flow | encapsulation | round-function | class-and-inheritance | exponentiation | python-django | default-arguments | odd-number | file_handling | int | multiple-except-blocks | sum() | immutable-data-type | tuple | global-keyword | functional-programming | debugging | generators-in-python | logical-operators | bitwise-operator | logical-operator | syntaxerror | condition | else | binary | formatting-with-strftime | conditional-statement | syntax | installation | input-function | random-function | using-assertions-(assertequal,-assertraises) | data-structure | yield | list-comprehension | boolean | match | floats | call | virtual-environment | using-else-and-finally | exception | intering | data-type-conversions | namedtuple | regular-expressions | working-with-file-paths | using-the-threading-module | operator-or | global | string-method | threading | dictionary | sorted | design-principle | regular-expression | arithmetic-operation | access-modifier | update | zfill-function | list-join | global-variable | string | file-handling | set-operation | inherit | datetime | return-statement | slicing | functional-programming | aggregating-data-with-functools.reduce() | string-method | stack | print | error-handling | choice | keyword | min-max-function | join | sum-function | deep-copy | identifier | string-formatting | import-method | list | invalid-list | logical-operation | arbitrary-arguments-(*args,-**kwargs) | introduction-to-threads | recursion | goto-function | filtering-and-indexing | mapping-function | slice | methods | dictionaries | writing:-write(),-writelines() | list-comprehension | arithmetic-expression | implementation | function | iterator | type-conversion | class | version-check | data-structures-in-python | booleans | min-function | array | comment | overflow | scientific-notation | anchors-(^,-$) | why-use-multiprocessing-over-threading?-(gil) | python-operators | collection | iterable | nested-loop | def | docstring | oop | sys-module | numeric | pseudocode | string-formatting | complex-number | operator | data-structures | exception-handling | pandas | type-conversion-and-casting | formal-and-actual-parameter | parameter | unit-testing | identity-operator | integer | reading-and-writing-files | copy | tuple-methods | while-loops | regex | format | for | counter-function | filter-function | using-lists-as-stacks | string-slicing | precedence | key-value-pairs | input | introduction-to-multiprocessing | static-method | matrix | lambda-functions | working-with-binary-files-(rb,-wb) | thread | control-flow | destructor | dictionary-methods | magic-method | loops | turtle | raise-statement | data-type | identity-operators-(is,-is-not) | python | rollback | compute | sorting | list-method | string-concatenation | typeerror | syntax-error | file | operations | lambda | comprehension | default-mutable-argument | dataframe-and-series-objects | test-discovery | python-module | constructor | datetime.date,-datetime.time,-datetime.datetime | reading-and-writing-large-files-efficiently | dictionary-comprehensions | benefit | name-mangling ...