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)
相关推荐
belldeep2 小时前
如何阅读、学习 Tcc (Tiny C Compiler) 源代码?如何解析 Tcc 源代码?
c语言·开发语言
LuckyTHP2 小时前
java 使用zxing生成条形码(可自定义文字位置、边框样式)
java·开发语言·python
mahuifa3 小时前
(7)python开发经验
python·qt·pyside6·开发经验
学地理的小胖砸5 小时前
【Python 操作 MySQL 数据库】
数据库·python·mysql
安迪小宝5 小时前
6 任务路由与负载均衡
运维·python·celery
Blossom.1185 小时前
使用Python实现简单的人工智能聊天机器人
开发语言·人工智能·python·低代码·数据挖掘·机器人·云计算
da-peng-song5 小时前
ArcGIS Desktop使用入门(二)常用工具条——数据框工具(旋转视图)
开发语言·javascript·arcgis
galaxy_strive5 小时前
qtc++ qdebug日志生成
开发语言·c++·qt
TNTLWT5 小时前
Qt功能区:简介与安装
开发语言·qt
lisw055 小时前
Python高级进阶:Vim与Vi使用指南
python·vim·excel