Level 1 : Implementing an iterator that returns prime numbers.


class PrimeIterator:
    def __init__(self, n):
        self.n = n
        self.num = 1

    def __iter__(self):
        return self

    def __next__(self):
        self.num += 1
        while any(self.num % i == 0 for i in range(2, int(self.num ** 0.5) + 1)):
            self.num += 1
        if self.num > self.n:
            raise StopIteration
        return self.num

it = iter(PrimeIterator(10))
print(list(it))

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