函数参数:
位置参数、关键字参数,可变长度参数*args,**kw
def foo(who):
print('Hello')
foo('World')
默认参数:必须在位置参数后面
def taxMe(cost,rate=0.0825):
return cost+(cost+rate)
taxMe(100)
taxMe(100,0.05)
可变长度非关键字参数*args(元组)
def func(a,b,*args):
suma=0
suma+=a+b
for i in args:
suma+=i
return suma
print(func(1,2,3,4,5))
可变长度关键字参数**kws(字典)
def func(a,b,**kw):
suma=0
suma+=a+b
for v in kw.values():
suma+=v
return suma
print(func(1,2,c=6,d=7,e=8))
lambda匿名函数:不需要定义函数名,没有命名空间
zz = lambda : True
zz()
map函数,将函数作用于序列中的每个元素上,返回序列
list(map(lambda x:x+1,[1,2,3]))
list(map(lambda x:x*2,[1,2,3]))
list(map(lambda x,y:x*y,[1,2,3],[4,5,6]))
list(map(lambda x,y:(x+y,x-y),[1,3,5],[2,4,6]))
filter函数,过滤序列,返回序列中的非零元素组成的序列
list(filter(lambda x:x%2,[2,3,4]))
reduce函数:
from functools import reduce
reduce(lambda x,y:x+y,[1,2,3,4,5])