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)
相关推荐
John.Lewis4 分钟前
C++进阶(12)附加学习:STL之空间配置器(了解)
开发语言·c++·笔记
weixin_5860614612 分钟前
CSS Grid布局如何解决图片溢出网格单元_设置object-fit与网格尺寸.txt
jvm·数据库·python
计算机徐师兄27 分钟前
Python基于农村和城镇人民生活数据的可视化系统(附源码,文档说明)
python·生活·农村和城镇人民生活数据·python人民生活数据·农村和城镇人民生活数据可视化·生活数据可视化系统·python生活数据的可视化
Byron Loong28 分钟前
【网络】Python 怎么做TCP通讯
网络·python·tcp/ip
ILYT NCTR30 分钟前
爬虫学习案例3
爬虫·python·学习
Greyson132 分钟前
CSS Grid布局如何解决图片溢出网格单元_设置object-fit与网格尺寸.txt
jvm·数据库·python
234710212734 分钟前
4.16 学习笔记
开发语言·软件测试·python
2401_8836002542 分钟前
Redis如何查询特定用户的排名_利用ZREVRANK指令获取ZSet降序名次
jvm·数据库·python
014-code43 分钟前
日志规范:怎么写才不算写废话
java·开发语言·设计模式·日志
2301_777599371 小时前
如何决定是否需要创建索引_数据区分度与基数Cardinality计算
jvm·数据库·python