数列 :1,1,2,3,5,8,,,,前两个相加得到新的
提供三种
第一种
a=input("希望输出第几位斐波那契数:")
import pandas as pd
fbo=pd.Series(0)
fbo[0]=0
fbo[1]=1
for i in range(int(a)):
fbo[i+2]=fbo[i]+fbo[1+i]
if i==int(a)-3:
print(fbo[i+2])
希望输出第几位斐波那契数:15
377
第二种
def fbo(n):
if n ==1:
return [1]
if n==2:
return [1,1]
fbo1=[1,1]
for i in range(2,n):
fbo1.append(fbo1[-1]+fbo1[-2])
return fbo1
fbo(10)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
第三种(参考python100例)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def fib(n):
if n == 1:
return [1]
if n == 2:
return [1, 1]
fibs = [1, 1]
for i in range(2, n):
fibs.append(fibs[-1] + fibs[-2])
return fibs
fib(10)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
其他小练习
京东秋招笔试python实现
https://blog.csdn.net/m0_46665608/article/details/108160109
https://blog.csdn.net/m0_46665608/article/details/108110670
量化交易策略
https://blog.csdn.net/m0_46665608/article/details/108037432
参考资料
1.https://www.runoob.com/python/python-100-examples.html