Level 5 : Fibonacci sequence iterator.


class FibonacciIterator:
    def __init__(self, n):
        self.a, self.b = 0, 1
        self.n = n
        self.count = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.count >= self.n:
            raise StopIteration
        self.count += 1
        self.a, self.b = self.b, self.a + self.b
        return self.a

it = iter(FibonacciIterator(5))
print(list(it))

Author: SandhyaR | Tags: iterator | 1736+ Python FAQs | Share
lambdas | math-copysign | turtle | sys | attribute | factorial-function | built-in | command-line-parsing | integer | operator-overloading | operations | zip | loops | datetime | input-function | __init_subclass__ | round-function | enumerate() | destructor | syntaxerror | inheritance | while-else-loop | fabs-function | global-keyword | operators | print | escape-operator | version-check | immutability | feature | nested-loop | sum-function | matrix | counter-function | version | typeerror | iterable | syntax-error | set-operation | built-in-function | frozenset | shallow-copy | data-structures | random-function | matplotlib | async | global | copy | function-and-scope | list-slicing | staticmethod | set | pep | match | head-and-tail | min-function | import-method | variable | implementation | decimal | name-mangling | arithmetic-operation | data-structure | bitwise-operator | condition | keyword | odd-number | scientific-notation | loop | control-statement | map-function | heap-memory | design-principle | dictionary | int | asyncio | indentation | generator | numeric | string-concatenation | touples | input | data-structure | assert | sum() | oop | max-min | list-comprehension | sorted | constructor | counter | string-formatting | encapsulation | math | recurssion | for | while-loop | split | context-manager | static-method | zfill-function | join | mutable | built-in-function | mathematical-function | sorting | collections | floating-point-precision | stack | operator-and | binary-operation | mysql | round | numpy | invalid-list | list-operation | tuple-unpacking | string-method | update | string-method | control-flow | deep-copy | exception-handling | virtual-environment | list-comprehension | syntax | python_version | filter | tuple | input-function | init | boolean | default-mutable-argument | while-and-for-loop | exception | choice | comprehension | binary | operator-or | threading | exception-handling | global-variable | call | range | logical-operator | goto-function | pseudocode | assignment-operator | complex | inherit | expression | with-statement | mapping-function | string-iteration | thread | format | conditional-statement | object-attribute | list | complex-number | boolean-and-non-boolean | class-and-object | multiprocessing | raise | __future__ | basic | indexing | multithreading | control-flow | array | global-variable | list-vs-tuple | slice | expand-tab | data-type | string-slicing | file | type_conversion | parameter | __all__-attribute | namedtuple | dictionaries | arithmetic-expression | python-repl | conversion | polymorphism | mathematics | application | identity-operator | bitwise | package-management | pandas | slicing | python-django | list-join | memory | installation | classes | immutable-data-type | else-statement | method | generators-in-python | function-argument | python-module | return-statement | string-formatting | random | magic-method | def | logical-operation | operator | higher-order-function | methods | software-design | pickling-&-unpickling | print-statement | docstring | generators | string-slicing | module | functional-programming | zip-function | multiple-inheritance | postfix | class-and-inheritance | class | formal-and-actual-parameter | bitwise-operator | identity-operator | filter-function | syntax-error | loop-control | rollback | comparison | decorator | objects | file-handling | encoding | lists | precedence-and-associativity | class-and-instance-variable | append | regex | statement | logical-operator | error-handling | datatype | list-method | collection | python-history | string-manipulation | debugging | virtual-environment | id-function | oops | memory-management | identifier | benefit | min-max-function | type-conversion | center | gil | overflow | matrix-list-comprehension | lambda-function | operand | file-handling | error | file_handling | conditional-statement | namespace | asyncio-module | interpreter | exponentiation | dicts | lambda | yield | descriptors | library | recursion | comment | sys-module | precedence | compute | data_structure | data-type | access-modifier | list-indexing | functions | swapping | regular-expression | type-conversion | python | list-mutation | string | function | iterator ...