Generators
Generators in Python are a simple way of creating iterators. They are written like regular functions but use the yield statement instead of return. Generators allow you to generate items only one at a time and only when you ask for it, which is memory efficient for large data sets.
A generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
def countdown(n):
ㅤwhile n > 0:
ㅤㅤyield n
ㅤㅤn -= 1
for i in countdown(5):
ㅤprint(i)
# Output:
# 5
# 4
# 3
# 2
# 1
Generator expressions are a high performance, memory efficient generalization of list comprehensions and generators.
squares = (x**2 for x in range(10))
forsquare in squares:
ㅤprint(square)
# Output: 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
def fibonacci():
ㅤa, b = 0, 1
ㅤwhile True:
ㅤㅤyield a
ㅤㅤa, b = b, a + b
def square(nums):
ㅤfor num in nums:
ㅤㅤyield num ** 2
fib_squares = square(fibonacci())
for iin range(10):
ㅤprint(next(fib_squares))
# Output: 0, 1, 1, 4, 9, 25, 64, 169, 441, 1156
Generators are a powerful feature in Python that provide an efficient way to work with large or infinite sequences of data. They are widely used in data processing, file I/O operations, and in scenarios where memory efficiency is crucial.