#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))
时间: 2024-10-12 09:52:31