Level 1 : What will be the output of the following code?


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

    def __iter__(self):
        return self

    def __next__(self):
        if self.current >= self.end:
            raise StopIteration
        self.current += 1
        return self.current - 1

c = Counter(1, 4)
print(list(c))

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