Genrators in python

technical talkiess
0

 Genratorals program :=

def gen(n):

    # yield real time memory allocate kar denga .... isse hamri ram bachengi

    yield n


# genrator ban gaya aur iske upper aap aap next method run kar sakte hai

number = gen(1)

# print(number.__next__())

# print(number.__next__())


vishal = 'vishal'

itr = vishal.__iter__()

# print(itr.__next__())

# print(itr.__next__())

# print(itr.__next__())

# print(itr.__next__())

# for i in itr:

    # print(i.__next__())



    # FActorial program

def fact(n):

    num = 1

    if n<0:

        return 0

    elif n==0:

        return 1

    else:

        for i in range(1,n+1):

          num *= i

        #   print(num)

          yield num

        # print(f'The factorial of {n} is {num}')

   

    # return num


facto = fact(5)

print(facto.__next__())

print(facto.__next__())

print(facto.__next__())

print(facto.__next__())

print(facto.__next__())





Tags

Post a Comment

0Comments

Thanks you for commenting your questions. I will see question and respond you.

Post a Comment (0)