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