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