Generators are a type of iterable that can be used to generate a sequence of values on-the-fly, without storing them in memory all at once. They are similar to lists, but unlike lists, generators do not store all the values in memory at once. Instead, they generate values one at a time as requested. Generators are defined using a special type of function called a generator function. A generator function uses the `yield` keyword instead of `return` to produce a series of values. Here's an example:
def my_generator(): yield 1 yield 2 yield 3 # Using the generator function gen = my_generator() print(next(gen)) # Output: 1 print(next(gen)) # Output: 2 print(next(gen)) # Output: 3
1 2 3
In this example, the `my_generator()` function is a generator function that yields three values: `1`, `2`, and `3`. When we call the generator function, it returns a generator object. We can then use the `next()` function to retrieve the next value from the generator. Generators are useful when working with large datasets or when you don't need to store all the values in memory at once. They are memory-efficient because they generate values on-the-fly as requested, rather than storing them in a list or other data structure. Generators can also be used in `for` loops, as they are iterable. Here's an example:
def countdown(n): while n > 0: yield n n -= 1 # Using the generator in a for loop for num in countdown(5): print(num) # Output: 5 4 3 2 1
5 4 3 2 1
In this code, the `countdown()` generator function yields values from `n` down to `1`. We can use the generator in a `for` loop to iterate over the values and print them. Generators are a powerful tool in Python for working with large datasets or when you need to generate values on-the-fly. They provide a memory-efficient way to produce a sequence of values without storing them all at once.