Search⌘ K

Generators in Python

Explore how generator functions in Python allow you to write iterators that are shorter and easier to maintain. This lesson helps you understand using the yield keyword to create generator objects, allowing iteration with less code and more clarity.

We'll cover the following...

In this lesson, you’ll learn writing iterators faster using fewer lines of code.

Simplified iterators

Previously, we implemented a basic iterator from scratch. This is what the class looks like in its simplified version:

class Counter:
  def __init__(self):
    self.n =1

  def __iter__(self):
 
...