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