Python Quest

Track 12 · Lesson 2 of 12

Generators

Write iterators the short way with yield: lazy values, paused state, early stops and infinite sequences sliced with islice.

About 45 minIntermediate4 exercises

You will be able to

  • Write a generator function with yield and explain what calling it returns
  • Describe how a generator pauses and resumes, keeping its local variables
  • Produce infinite sequences safely and take what you need with itertools.islice
  • Decide when a generator is the right tool and when a list is simpler

Included with a Formula Quest membership

It has 4 graded exercises with feedback, hints and solutions. Python Quest is included in Formula Quest memberships that cover Python; membership and billing are managed on Formula Quest.

View Formula Quest memberships

Tracks 1 and 2, the first lesson of every track, the playground and the dataset library are free.