You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# A Python generator is a special kind of function that lets you produce a sequence of values lazily — meaning it generates each value only when needed
# instead of building the entire list in memory.
# This makes generators great for:
# Handling large datasets without memory issues
# Creating infinite sequences
# Writing cleaner, more readable code compared to manual iteration logic
def count_up_to(n):
i = 1
while i <= n:
yield i
i += 1
gen = count_up_to(3)
print(next(gen)) # 1
print(next(gen)) # 2
print(next(gen)) # 3
try:
next(gen)
except StopIteration:
print("StopIteration")
# When Python sees a yield:
# It pauses the function and returns a value.
# The function’s internal state is saved (local variables, instruction pointer).
# When you call next() again, execution resumes right after the yield.