generators

Python Generators

Python Generators

Generators are a simple way of creating iterators. They are written using the yield keyword instead of return.

Using Generators

Here is an example of a generator that yields the first n squares:

      
        def squares(n):
            for i in range(n):
                yield i * i

        for square in squares(5):
            print(square)
      
    

In this example, the squares function generates squares from 0 to n-1 using the yield keyword.

Activity

Try It Yourself!

Write a generator that yields Fibonacci numbers up to a given limit.

Quick Quiz

Quick Quiz

  1. What is the main difference between a generator and an iterator?
  2. What keyword is used to create a generator?

Answers: Generators use the yield keyword and are more memory-efficient than iterators. They produce values one at a time and don't store them in memory.