Python基础,,Python的流程控


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基础

评论关闭