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)
相关推荐
Boilermaker19921 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
沈浩(种子思维作者)1 小时前
真的能精准医疗吗?癌症能提前发现吗?
人工智能·python·网络安全·健康医疗·量子计算
MM_MS1 小时前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
꧁Q༒ོγ꧂2 小时前
LaTeX 语法入门指南
开发语言·latex
njsgcs2 小时前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
alonewolf_992 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
io_T_T2 小时前
迭代器 iteration、iter 与 多线程 concurrent 交叉实践(详细)
python
古城小栈2 小时前
Rust 迭代器产出的引用层数——分水岭
开发语言·rust
华研前沿标杆游学2 小时前
2026年走进洛阳格力工厂参观游学
python
Carl_奕然2 小时前
【数据挖掘】数据挖掘必会技能之:A/B测试
人工智能·python·数据挖掘·数据分析