Powers of 2 Python Iterable Object || Iterators || Python
In this we are going to see a basic program on iterable object to print powers of 2 in Python Programming Language.


class Power:
    def __init__(self, p=0):
        self.p = p

    def __iter__(self):
        self.n = 0
        return self

    def __next__(self):
        if self.n <= self.p:
            res = 2 ** self.n
            self.n += 1
            return res
        else:
            raise StopIteration


for i in Power(4):
    print(i)

# Coded by Saahil

#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post