One of the 50 questions in this topic
How much do you know about Python?A generator is a special function that returns an iterator that produces values on demand using yield instead of return. Unlike lists that store all values in memory, generators calculate values only when they are needed, making them more efficient for large or infinite sequences. They can be created with generator functions or generator expressions (similar to list comprehensions but with parentheses).