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)
相关推荐
belldeep1 天前
python:genesis-world, Genesis-Embodied-AI
人工智能·python·ai·genesis
布莱克6051 天前
C/C++ 中变量的声明与定义:区别、联系与常见误区
c语言·开发语言·c++·声明·定义
LabVIEW开发1 天前
LabVIEW 局部变量为何引发竞争条件?四种同步机制
开发语言·labview
benchmark_cc1 天前
A 股、港股、美股如何建立统一的标的主数据格式?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
维克兜率天1 天前
【维克】趋势型CTA:期货市场的“追涨杀跌“
人工智能·python
Zenova EdgeOS1 天前
Go 工业边缘错误处理实战:用 errors 构建可追溯错误链
开发语言·数据库·golang
取名真是1 天前
Python 算法知识点从基础到进阶
开发语言·python·算法
梦想不只是梦与想1 天前
PyTorch、CPU、GPU、CUDA
人工智能·pytorch·python·深度学习·机器学习·cuda
数字融合1 天前
透明化视频孪生危化安全管控解决方案
人工智能·python·安全
FRing031 天前
vue2干货总结3(computed计算属性、watch监听器)
开发语言·前端·vue.js·vue2