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