Detailed analysis of several generator cases, simple to understand, now learn to use.
print('fib:1 1 2 3 5 8 13 21.... ') # Fibonacci ratched sequence def fib_list (n) : TMP =  a, b = 1, 1, while a < n: TMP. Append (a) a, b = b, a+b return tmp= fib_list(200)print(L)
# Define the generator function def fib_generator(n): a, b = 1, 1 while a < n: yield a a, b = b, a+b
# Call the function, generate a generator, and iterate through the generator with a for loop. g = fib_generator(200)for i in g: print(i, end=' ')print()
# definition of Fibonacci ratched generator function of the second writing def it () : a, b = 1, while 1: yield a, a, b = b, a + b
This page is based on experience
To use a generator, you first define a generator function with the keyword yield, then call the function to create a generator, and finally iterate through the loop and fetch the next value of the generator with the next () function.
Each time the generator runs to the yield statement, it pauses until the next () function is called.