python 函数

多返回值

复制代码
def test_return():
    return 1,"hello",True
num, str1, isTrue = test_return()

print(f"num={num} str1={str1} isTrue={isTrue}")

传参数

复制代码
def test_return():
    return 1,"hello",True
num, str1, isTrue = test_return()

print(f"num={num} str1={str1} isTrue={isTrue}")



#关键字传参 键值传值
def user_info(name,age,sex):
    print(f"name={name} age={age} sex={sex}")


#位置传值
user_info("ljs",18,"男")

#关键字传值 可以不按参数顺序
user_info(name="lyl",age=55,sex="男")


#关键字和位置混合使用
user_info("ldh",sex="男",age=68)

#设置默认参数 但是要在后边
def class_info(name, size=10, isNew = True):
        print(f"name={name} size={size} isNew={isNew}")

class_info("view",20,False)

#不定长
#位置不定长 参数是元组形式
def test_arges(*args):
     print(f"test_arges={args} type={type(args)}")
     for arg in args:
          print(f"arg={arg}")

#关键字不定长 参数是keyvalue形式
def test_keyWord(**args):
     print(f"test_keyWord={args} type={type(args)}")

test_arges(1,2,"name","xx")
test_keyWord(name="ldh",age=68,sex="男",high=172)

函数作为参数传递

复制代码
def test_func(compute):
     result = compute(66,711)
     print(f"compute 类型={compute}")
     print(f"计算结果:{result}")


def sum(a,b):
     return a + b

test_func(sum)

匿名函数

复制代码
def test_func(compute):
     result = compute(66,711)
     print(f"compute 类型={compute}")
     print(f"计算结果:{result}")


def sum(a,b):
     return a + b

test_func(sum)


test_func(lambda x,y: x * y)
test_func(lambda x,y: x / y)
相关推荐
Wellijohn13 分钟前
Python 容器选型实测:list.insert(0) 慢 925 倍,in 慢 6000 倍
python
新*16 分钟前
标题全变成一级?靠编号规律找回章节边界
python·正则表达式·pdf解析·文本处理
炸膛坦客1 小时前
单片机/C/C++八股:(三十七)指针数组 和 数组指针
c语言·开发语言·c++
夜不会漫长1 小时前
C++:C++11特性
开发语言·c++·算法
apcipot_rain3 小时前
C++转java日记(13)
java·开发语言
高洁013 小时前
多源数据融合:把零散信号拼成一致图景
人工智能·python·机器学习·transformer·迁移学习
郝学胜-神的一滴3 小时前
Effective Python 条款41:试Mix‑In组合式开发
开发语言·pytorch·python·pycharm·numpy
吃我一窝jio4 小时前
Python与Redis集合深度解析
开发语言·redis·笔记·python
星恒随风4 小时前
从0实现C++ AI大模型接入SDK(五):DeepSeekProvider的实现——策略模式、HTTP请求与SSE流式响应
linux·开发语言·c++·人工智能
饮水桶_T5 小时前
Git笔记
开发语言·笔记·算法