Open links in new tab
  1. Generators are special functions that allow you to produce a sequence of values lazily, meaning values are generated on demand rather than all at once in memory. In Python, they are created using the yield keyword instead of return.

    When a generator function is called, it returns a generator object (an iterator) without executing the function body immediately. Execution starts when iterated over, and pauses at each yield, resuming from the same point on the next iteration.

    Example – Basic Generator

    def count_up_to(n):
    count = 1
    while count <= n:
    yield count
    count += 1

    for num in count_up_to(5):
    print(num)
    Copied!

    This yields numbers 1 to 5, pausing after each yield and resuming where it left off.

    Key Advantages

    • Memory Efficient – Generates items one at a time without storing the entire sequence.

    • Lazy Evaluation – Values are computed only when needed.

    • Infinite Sequences – Can represent endless streams like Fibonacci numbers.

    • Pipeline Processing – Chain multiple generators for staged data processing.

  1. Generators in Python - GeeksforGeeks

    Dec 12, 2025 · A generator function is a special type of function that returns an iterator object. Instead of using return to send back a single value, generator functions use yield to produce a series of …

  2. What is a Generator? | Hendrik Erz

    I’ll leave you today with a final piece of code that will “force-drain” your generator, meaning that it will pull out every inch of data out of your generator until it reaches …

  3. Generator (computer programming) - Reference.org

    In computer science, a generator is a special type of iterator that controls the iteration of a loop. Unlike a function returning an entire array, a generator yields values one at a time, reducing memory usage and …

  4. Generator (computer programming) - Semantic Scholar

    However, instead of building an array containing all the values and returning them all at once, a generator yields the values one at a time, which requires less memory and allows the caller to get …

  5. People also ask
    Loading
    Unable to load answer
  6. Generator (computer programming) - Wikiwand

    In computer science, a generator is a routine that can be used to control the iteration behaviour of a loop. All generators are also iterators. A generator is very similar to a function that returns an array, in …

  7. Generator (computer science)

    In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop. A generator is very similar to a function that returns an array, in that a generator has …

  8. Generator (computer programming) explained

    What is Generator (computer programming)? Generator is a routine that can be used to control the iteration behaviour of a loop.

  9. Generator (computer programming) - Alchetron

    Nov 22, 2024 · In short, a generator looks like a function but behaves like an iterator. Generators can be implemented in terms of more expressive control flow constructs, such as coroutines or first-class …

  10. Generator (computer programming) - HandWiki

    Feb 6, 2024 · In computer science, a generator is a routine that can be used to control the iteration behaviour of a loop. All generators are also iterators. [1] . A generator is very similar to a function that …

By using this site you agree to the use of cookies for analytics, personalized content, and ads.Learn more about third party cookies|Microsoft Privacy Policy