师从黑马程序员
函数中多个返回值的接收

python
def test_return():
return 1,"hello",3
x,y,z=test_return()
print(x)
print(y)
print(z)
多种参数的使用
函数参数种类

位置参数

关键字参数

python
def user_info(name,age,gender):
print(f"姓名是{name},年龄是:{age},性别是:{gender}")
user_info(name='小王',age=11,gender='女')
user_info(age=0,gender='女',name='潇潇')#关键字参数可以不按照参数的定义顺序传参
user_info('甜甜',gender='女',age=9)#关键字参数和位置参数可以混用
缺省参数

默认参数一定要放在最后面
python
def user_info(name,age,gender='男'):
print(f"姓名是{name},年龄是:{age},性别是:{gender}")
user_info('小甜',13)
user_info('小甜',13,gender='女')
不定长参数

位置传递的不定长

形式参数会作为元组存在,接收不定长数量的参数传入
python
def user_info(*args):
print(f"args参数的类型是:{type(args)},内容是:{args}")
user_info(1,2,3,'小明','男孩')
关键字传递的不定长

python
def user_info(**Kwargs):
print(f"args参数的类型是:{type(Kwargs)},内容是:{Kwargs}")
user_info(name='小王',age=11,gender="男孩")
函数作为参数传递
函数可以接受数据作为参数传入:各种容器和另一个函数内

python
def test_func(compute):
result=compute(1,2)
print(f"compute参数的类型是:{type(compute)}")
print(f"计算结果:{result}")
def compute(x,y):
return x+y
test_func(compute)
注:将函数传入的作用在于:传入计算逻辑,而非传入数据
lambda匿名函数

匿名函数与普通函数的比较

python
def test_func(compute):
result=compute(1,2)
print(f"计算结果:{result}")
test_func(lambda x,y:x+y)
test_func(lambda x,y:x+y)
若有侵权,请联系作者