Solution Review: Yield Odd Numbers From 1 to n

This lesson discusses how you can create generators to return a list of odd numbers.

We'll cover the following


In line 3 of the following code, you can see that the generator checks for each integer in the sequence 1 to n to see if the value is not divisible by 2 (i.e., it’s odd). If it’s odd, it returns that value using the yield keyword.

Lines 6 and 7 simply have a loop that prints the odd numbers from 1 to 8.

Get hands-on with 1000+ tech skills courses.