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