Can you explain the concept of generators in Python and provide an example?
Generators are a way to create iterators in Python. They allow you to iterate over a sequence of values without creating the entire sequence in memory at once. This is especially useful for large datasets. Here's a simple generator example that generates squares of numbers:
def square_generator(n): for i in range(n): yield i ** 2 # Using the generator squares = square_generator(5) for num in squares: print(num)