Generators in Python
Concept Generators are used to create iterators, but with a different approach. Generators are simple functions that return an iterable set of items, one at a time, in a special way. When an iteration over a set of items starts using the for a statement, the generator is run. Once the generator's function code reaches a "yield" statement, the generator yields its execution back to the for loop, returning a new value from the set. The generator function can generate as many values (possibly infinite) as it wants, yielding each one in its turn. Code def gen_fun(): yield 1 yield 2 yield 3 yield 4 yield 5 for i in gen_fun(): print(i) >>> 1 2 3 4 5 Screenshot : Quiz Level 1 Quiz Level 2 Request Me