From a30910c48db67a657c31e1567082b0dcb43c45f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20G=C3=B6r?= Date: Sun, 3 Sep 2023 14:51:41 +0300 Subject: [PATCH] next method example 2 --- python-temel/next.method.2.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 python-temel/next.method.2.py diff --git a/python-temel/next.method.2.py b/python-temel/next.method.2.py new file mode 100644 index 0000000..1cb39db --- /dev/null +++ b/python-temel/next.method.2.py @@ -0,0 +1,32 @@ +import math + +class SqrtIterable: + def __init__(self, limit): + self._limit = limit + + def __iter__(self): + return SqrtIterator(self._limit) + +class SqrtIterator: + def __init__(self, limit): + self._limit = limit + self._i = 0 + + def __iter__(self): + return self + + def __next__(self): + if self._i == self._limit: + raise StopIteration() + self._i += 1 + + return self._i, math.sqrt(self._i) + +si = SqrtIterable(25) + +for t in si: + print(t, end = ' ') +print() + +for i, x in si: + print(f'{i} --> {x}')