diff --git a/generators.html b/generators.html index c9008e4..17ac5c5 100644 --- a/generators.html +++ b/generators.html @@ -360,12 +360,15 @@ def plural(noun):
 >>> from fibonacci import fib
->>> for n in fib(1000):  
+>>> for n in fib(1000):      
 ...     print(n, end=' ')    
-0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
+0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 +>>> list(fib(1000)) +[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
  1. You can use a generator like fib() in a for loop directly. The for loop will automatically call the next() function to get values from the fib() generator and assign them to the for loop index variable (n).
  2. Each time through the for loop, n gets a new value from the yield statement in fib(), and all you have to do is print it out. Once fib() runs out of numbers (a becomes bigger than max, which in this case is 1000), then the for loop exits gracefully. +
  3. This is a useful idiom: pass a generator to the list() function, and it will iterate through the entire generator (just like the for loop in the previous example) and return a list of all the values.

A Plural Rule Generator