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