Level 1 : What happens when we execute this iterator code?


class StepIterator:
    def __init__(self, start, step, end):
        self.current = start
        self.step = step
        self.end = end

    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 = StepIterator(2, 3, 10)
print(list(it))

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