python求斐波那契数列示例分享

yipeiwu_com6年前Python基础

复制代码 代码如下:

def getFibonacci(num):
 res=[0,1]
 a=0
 b=1
 for x in range(0,num):
  if x==a+b:
   res.append(x)
   a,b=b,a+b
 return res

res=getFibonacci(1000)
print(res)

#递归
a=[0,1]
qian=0
def fibna(num,qian):
 print(num)
 he=num+qian
 if he<1000:
  a.append(he)
  qian=num
  fibna(he,qian)
fibna(1,0)
print(a)

相关文章

Python读取sqlite数据库文件的方法分析

本文实例讲述了Python读取sqlite数据库文件的方法。分享给大家供大家参考,具体如下: import sqlite3 这是Python内置的,不需要pip install...

使用Python的Twisted框架编写简单的网络客户端

Protocol   和服务器一样,也是通过该类来实现。先看一个简短的例程: from twisted.internet.protocol import Protocol...

python运行时强制刷新缓冲区的方法

需求:打印一颗”*”休息1s 代码如下: #!/usr/bin/python #coding=utf-8 ''' 暂停1s输出 ''' import time def print...

django model去掉unique_together报错的解决方案

事情是这样的,我有一个存储考试的表 class Exam(models.Model): category = cached_fields.ForeignKeyField(Categ...

在Python中,不用while和for循环遍历列表的实例

如下所示: a = [1, 2, 3, 8, 9] def printlist(l, index): if index == len(l): return else:...