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