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