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