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