What are generators in python
Generators are used to create sequence of values such as integers, floats, Booleans, strings,etc. We
can say it as a function that generates iterator. However, main difference between them is that
normal function returns whereas generators uses yield. Generators also don’t store values , they just
iterate once. Whenever you call generator, it wont execute immediately unless and until the next()
method is called and its execution stops when yield statement has been reached. Once yield is
encountered, control is transferred to yield value. Normally, generator functions are implemented
with a loop having a suitable terminating condition.
python code for Generators In Python Geeks For Geeks
Def friend(): number = 1 print("Jimit") yield number number = 2 print("Harsh") yield number number = 3 print("Manthan") yield number obj = Friend() # Return a generator object. Function execution is not started yet next(obj) # Function execution started. # Output: Jimit # 1 next(obj) # Output: Harsh # 2 next(obj) # Output: Manthan # 3 next(obj) # Give error. As nothing to yield.
In this article we saw Generators In Python Geeks For Geeks so about this section you have any query then free to ask me
Name of intern who share this Task :
I am Jimit Tushar Mehta has completed my BE in computer engineering in 2020. I am sending the task assigned document along with my github link of python project.