#1、
records = [('f',1,2,4),('k',3,4,5),('z','e','d')]
for key,value,*val in records:
#可迭代对象,解构
if key == 'f':
f(val)
print('val:{0},{1}'.format(*val))
print('key:{0},value:{1}'.format(key,value))
#2、参数槽
#以*分隔,{非命名参数,命名参数}
#不可使用可变位置参数
#可变关键字参数必须在命名参数之后
def func(a,b,c=3,*,f=5,k=3,j,**kw):
print(a)
print(b)
print(c)
print(f)
print(k)
print(j)
print(kw)
func(9,4,f=4,j=7,s=8)
9
4
3
4
3
7
{'s': 8}
#3、递归
lst = []
#十进制转换二进制
def to_binary(n):
r = n % 2
if n >= 2:
to_binary(n/2)
lst.append(int(r))
to_binary(10)
print(lst)
[1, 0, 1, 0]
#4、生成器(斐波那契数列)
def fib():
a,b = 0,1
while True:
a,b = b,a+b# 解包,封包
yield b
gen=fib()
def func(x):
for i in range(x):
print(next(gen))
5、斐波那契数列:
def fab(n):
#(1)
a = 1
b = 1
for i in range(n):
print(a)
a,b = b,a+b
def fab():
#(2)
a = 1
b = 1
while True:
yield a
a,b = b,a+b
gen = fab()
for i in range(10):
print(next(gen))
def fab(n):
#(3)
if n == 1 or n == 0:
return 1
return fab(n-1)+fab(n-2)
转载于:https://blog.51cto.com/calily/1829393