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