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