Level 1 : Factorial sequence.


import math

class FactorialIterator:
    def __init__(self, n):
        self.n = n
        self.current = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.current > self.n:
            raise StopIteration
        value = math.factorial(self.current)
        self.current += 1
        return value

it = iter(FactorialIterator(4))
print(list(it))

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