码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础

时间:2018-03-24 23:53:12      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:fibonacci   html   href   else   ret   字符   迭代   tin   iter   

Python的流程控制

if else ,  for ,  range() ,  break continue ,  pass

 Python 的 for 语句依据任意序列(链表或字符串)中的子项,按它们在序列中的顺序来进行迭代。

 与循环一起使用时,else 子句与 try 语句的 else 子句比与 if 语句的具有更多的共同点:try 语句的 else 子句在未出现异常时运行,循环的 else 子句在未出现 break 时运行。

 定义函数

def fibs(n):
"""Print a Fibonacci series up to n.""" a,b
= 0,1 while a < n: print (a) a,b = b,a+b --- def fibs(max):
"""Print a Fibonacci series of the first n.""" n,a,b
= 0,0,1 while n < max: print (a) a,b = b,a+b n = n + 1 return done
‘‘‘
a, b = b, a+b

be equal to

t = (b, a+b) # t is a tuple
a = t[0]
b = t[1]
‘‘‘
def fib2(n): # return Fibonacci series up to n
    """Return a list containing the Fibonacci series up to n."""
    result = []
    a, b = 0, 1while a < n:
        result.append(a)    # see below
        a, b = b, a+b
    return result

 

 

 

 

 

 

 

参考链接1

参考链接2

Python基础

标签:fibonacci   html   href   else   ret   字符   迭代   tin   iter   

原文地址:https://www.cnblogs.com/yexuesong/p/8641901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!