culTest

    One of the 50 questions in this topic

    How much do you know about Python?

    What is a generator in Python?

    Answers

    A function that generates random numbers
    A function that returns an iterator that produces values ​​on demandCorrect
    A class to automatically generate objects
    A tool to generate Python code

    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).