开发者学堂课程【Python入门 2020年版: 生成器的练习】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/639/detail/10430
生成器的练习
一.练习一
def fibonacci(n):
num1=num2=1
count = 0
while count <= n-2:
num1, num2 = num2, num1+num2
count += 1
yield num1
F = fibonacci(12)
for x in,
二.练习2
def fibonacci(n):n:12
num1=num2=1 num1:1 num2:2
count=0
whilecount(=n-2:
num1, num2 = num2, num1+num2
count+=1
#此时并不会调用函数
yield num1
F=fibonacci(12)
print(next(iter(F)))
print (next(iter(F)))
print(next(iter(F)))
print(next(iter(F)))
print(next(iter(F)))
运行结果
count=(int)1
n= (int
)12
num1 = (int)1
num2 = (int)2
三.练习3
def fibonacci(n):
num1=num2=1
count=0
while count <= n-2:
num1, num2 = num2, num1 + num2
count +=1
#此时并不会调用函数
yield num1
F=fibonacci(12)for i in F:
print(i)