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