LernerPython

Iterators and generators

Understand how “for” loops work — and how to implement your own iterable objects

What you should know

Course Content

Course length

7 hours

Number of lessons

81

Training materials

Jupyter notebooks + PDFs

Coding exercises

15

LEARN MORE

Python programmers use “for” loops all of the time. But how do these loops work? How is it that strings, lists, tuples, dicts, and files all work with loops, but give different values with each iteration? And how can we make our own classes iterable?

This course answers all of these questions, introducing Python’s “iterator protocol” and how you can use it to your benefit. First, we’ll look at how you can add iteration to your own classes. Then we’ll look at generators and generator functions, which allow us to create iterators using function syntax. The course ends with an introduction to generator comprehensions, which produce generators using code that’s similar to a list comprehension.

  • Python’s iterator protocol
  • How to make a class iterable
  • What you can do with an iterable class
  • Styles of iterable classes, and their trade-offs
  • Generators and generator functions
  • Generator comprehensions

As with all courses on LernerPython.com, this is taught by Reuven Lerner.