skill

759Currently online
135Today's Reading
39Share Today
Multilingual display

Zero basic learning python generator full solution two

2018-04-06 17:36:31

Detailed analysis of several generator cases, simple to understand, now learn to use.

Tools/Materials
1

python3.6

2

pycharm

Methods/Steps
1

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)

2

# Define the generator function def fib_generator(n): a, b = 1, 1 while a < n: yield a a, b = b, a+b

3

# 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()

4

# definition of Fibonacci ratched generator function of the second writing def it () : a, b = 1, while 1: yield a, a, b = b, a + b

5

This page is based on experience

6

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.

Matters needing attention

Each time the generator runs to the yield statement, it pauses until the next () function is called.

Recommendation