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