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