Level 4 : What is the output of the following code?


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

    def __iter__(self):
        return self

    def __next__(self):
        if self.current < self.end:
            val = self.current
            self.current += 1
            return val * 2
        else:
            raise StopIteration

it = SimpleIterator(0, 3)
output_list = []
for num in it:
    output_list.append(num)
print(output_list)

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