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