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