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