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