Generators in Python in Hindi

Python

Generators in Python in Hindi – Generators iterator Hote Hai, Jinhe Aap Keval Ek Bar Iterate Kar Sakte Hai, Waise Generators Ka Istemal Ram Ko Bachane Ke Liye Hota Hai |

Read More – Python File Handling

What Is Generators In Python In Hindi – Generator ek Function Hota Hai, Jise Ham Normal Function Ki Tarah Hi Defined Karte Hai, Lekin Jab Hame Value Ki Jarurat Hoti Hai To Ham Value Genereate Kar Dete Hai yield Keyword Se, Generator Function Create Karne Ke Liye Function Ke Body Me yield Ka Istemal karne Se Generator Function Ban Jata Hai |

Generators in Python in Hindi

Jab Generator Function Return Me Object Return Karta Hai Jab Ham Ise iterate, Karte Hai |

Example:

def gen_function():
    yield 1
    yield 2
    yield 3

obj = gen_function()
print(obj)

Output:

<generator object gen_function at 0x00655C70>

Example 2:

def gen_function():
    yield 1
    yield 2
    yield 3

for x in gen_function():
    print(x)

Generator Ki Value Ko Ham For Loop Se Bhi Print Kar Sakte Hai |

Output:

1
2
3

Example 3:

def revstr(mystr):
    length = len(mystr)
    for i in range(length - 1,-1,-1):
        yield mystr[i]

for char in revstr("MasterPrograming"):
     print(char)

Output:

g
n
i
m
a
r
g
o
r
P
r
e
t
s
a
M

Jaise Ham Lambda Function Ko Create Karte Hai And Waise Hi Ham Generators Function Ka Istemal Karte Hai Or Fir Dekhte Hai Ki Generators Function Kaise Kaam Karta Hai Or Generators Memory Ko Kaise Kaam Istemal Karte Hai |

Example 4:

print(f"Run Time is : {n1-n2}")

Output:

5
15
25
20
Run Time is : 0.0

Also Read This Post :

Leave a Reply

Your email address will not be published.