# Python之路-day5

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

#斐波拉契数列

deffib(max):

n,a,b =,,1

whilen < max:

print(b)

a,b = b,a + b

n = n +1

return'done'

print(fib(6))

done

deffib(max):

n,a,b =,,1

whilen < max:

yieldb

a,b = b,a + b

n = n +1

return'done'

print(fib(6))

deffib(max):

n,a,b =,,1

whilen < max:

yieldb

a,b = b,a + b

n = n +1

return'done'

print(fib(6))

forninfib(6):

print(n)

# 期待输出:# [1]# [1, 1]# [1, 2, 1]# [1, 3, 3, 1]# [1, 4, 6, 4, 1]# [1, 5, 10, 10, 5, 1]# [1, 6, 15, 20, 15, 6, 1]# [1, 7, 21, 35, 35, 21, 7, 1]# [1, 8, 28, 56, 70, 56, 28, 8, 1]# [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

#杨辉三角

deftriangles(max):

L=[1]

n=

whilen < max:

yieldL

L=[L[i]+L[i+1]foriinrange(len(L)-1)]

L.insert(,1)

L.append(1)

n+=1

print(triangles(8))

forLintriangles(8):

print(L)

L=[L[i]+L[i+1]foriinrange(len(L)-1)]

range(len(L)-1)

L.insert(,1)

L.append(1)

[1]

[1, 1]

[1, 2, 1]

[1, 3, 3, 1]

[1, 4, 6, 4, 1]

[1, 5, 10, 10, 5, 1]

[1, 6, 15, 20, 15, 6, 1]

[1, 7, 21, 35, 35, 21, 7, 1]

• 发表于:
• 原文链接http://kuaibao.qq.com/s/20171226G0AM7P00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2018-07-06

2018-07-03

2018-06-19

2022-01-28

2022-01-28

2022-01-28