#匹配
位置匹配
关键字匹配
默认值(调用时省略传值)
用一个元组
def avg(*scores):#可以不传tuple元组,只要是语法兼容都可传
return sum(scores)/len(scores)
result=avg((1,2,3))
print(result)
def avg(*scores):
return sum(scores)/len(scores)
a=(1,2,3)
result=avg(*a)
print (result)#在定义参数时,里面加星号表明可以接受任意数量tuple,但是不能直接传一个tuple,若想传的话,必须把tuple解开,解开的方法就是前面加星号
#*args 任意数量的参数
def display_employee(**employee):
print (employee)
display_employee(name=‘tom’,age=22,job=‘kaifa’)#单星号是一个元组,双星号是一个字典表
#字典表的定义两种表达方式d={‘name’:‘tom’,‘age’:20} d2=dict(name=‘tom’,age=20,job=‘kaifa’)
**kwargs 关键字参数
lambda表达式 只是表达式,不是语句
定义匿名函数 基本格式lambda 参数1,: 函数
def avg (a,b):
return a+b
f=lambda name: print (name)#相当于定义了一个 def hello(name) : print(name)
f(‘tom’)
f2=lambda x,y: x+y
print (f2(5,3)
def hello_chinese(name):
print (‘你好:’,name)
def hello_english(name):
print (‘hello:’,name)
while True:
name=input(‘请输入您的姓名:\n’)
if name==‘stop’:
break
language= input(‘请选择语言:\n c=>中文\n e=>英文\nj=>riwen’)
if language==‘c’:
hello_chinese(name)
elif language==‘j’:
(lambda name:print(‘hhh’,name))
else:
hello_english(name)
def hello_chinese(name):
print (‘你好:’,name)
def hello_english(name):
print (‘hello:’,name)
while True:
name=input(‘请输入您的姓名:\n’)
if name==‘stop’:
break
language= input(‘请选择语言:\n c=>中文\n e=>英文\nj=>riwen’)
if language==‘c’:
hello_chinese(name)
elif language==‘j’:
(lambda name:print(‘hhh’,name))
else:
hello_english(name)
def hello(action,name):#名义上执行的是hello,实际是action,参数是name
action(name)
hello(hello_english,‘tom’)
l=list(range(1,21))
result=[]
#使用循环
for n in l:
if n%20:
result.append(n)
#print (result)#会一个一个添加
print (result)
#使用推导
result=[x for x in l if x%20]
print (result)
#使用推导 大多采用推导的方式
for x in l:
res=[x+5 for x in l]
print (res)
#循环
for x in l :
res.append(x+5)
print (res)
#map(函数,可迭代对象)、
#filter (函数,可迭代对象)
def add_number(x):
return x+5
res=list(map(add_number,l))
print (res)
res=list(map((lambda n:n**2),l))
print (res)