Generators use yield to produce values one at a time without building the whole list in memory. Ideal for reading large files or processing big datasets. Use a for loop to iterate over generator values.
Generators
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
gen = fibonacci()
for _ in range(10):
print(next(gen))
# Generator expression
squares = (x**2 for x in range(1000000)) # lazy — no memory used