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