Generators are useful when we want to produce a large sequence of values but we don't want to store them in memory at once
yield keyword helps python to execute generator .
Key Features of Generators
- Memory Efficient – They do not store all values in memory; instead, they generate values one by one.
- Lazy Evaluation – Values are produced only when needed.
- Automatic State Retention – The function retains its state between yield calls.
- Iterable – Generators can be iterated using a for loop or the next() function.
Comments
Post a Comment