Write a class that act like squares and should print the squares of values and and cuustom sequence type.


Topic: Write a class that act like squares and should print the squares of values and and cuustom sequence type.

Solution

class Square:
    def __init__(self, n):
        self.n = n
    def __iter__(self):
        return self.show_sq(self.n)
    @staticmethod
    def show_sq(n):
         for i in range(n):
             yield i**2
    def __getitem__(self, i):
       if isinstance(i, int):
            if i < = self.n:
                print(i**2)
            else:
                raise ValueError('Index out of bound')



List all Python Programs