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]
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).
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.
+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.