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