Generator in Python
By
Mridusmita Baruah
Dept of CSE
GCU,Guwahati
What is Generator in Python
Basic Generator Function�
Example :�
This example demonstrates how to create a simple generator that yields numbers from 1 to 5. The 'yield' statement allows the function to return a value and pause its state, making it efficient for iteration.
def simple_generator():
for i in range(1, 6): # Iterate from 1 to 5
yield i # Yield the current value of I
# Using the generator
gen = simple_generator()
# Iterating through the generator
for value in gen:
print(value)