Python函数进阶

文章目录

  • [1 函数多返回值](#1 函数多返回值)
  • [2 函数多种传参方式](#2 函数多种传参方式)
    • [2.1 位置参数](#2.1 位置参数)
    • [2.2 关键字参数](#2.2 关键字参数)
    • [2.3 缺省参数](#2.3 缺省参数)
    • [2.4 不定长参数](#2.4 不定长参数)
  • [3 匿名函数](#3 匿名函数)

1 函数多返回值

python 复制代码
def test_return():
    return 1,2,3
x,y,z = test_return()
print(x)
print(y)
print(z)

2 函数多种传参方式

2.1 位置参数

2.2 关键字参数

python 复制代码
def user_info(name,age,gender):
    print(name,age,gender)
user_info('小明','18','男')
user_info(name='小王',age=18,gender='男')
user_info(age=18,gender='女',name='小白')
user_info('天天',age=48,gender='男')

2.3 缺省参数

python 复制代码
def user_info(name,age,gender='男'):
    print(name,age,gender)
user_info('小天',13)
user_info('x',55,'女')

2.4 不定长参数


python 复制代码
def user_info(*args):
    print(args)
user_info(1,2,3,'xiaom')
python 复制代码
def user_info(**kwargs):
    print(kwargs)
user_info(name='小王',age=18,gender='男')

3 匿名函数

函数作为参数传递

python 复制代码
def test_func(compute):
    result = compute(1,2)
    print(type(compute))
    print(result)
def compute(x,y):
    return x+y
test_func(compute)

lambda匿名函数


python 复制代码
def test_func(compute):
    result = compute(1,2)
    print(result)
test_func(lambda x,y:x+y)
相关推荐
Gauss松鼠会1 分钟前
【openGauss】学习 gsql 命令行的使用
数据库·sql·database·opengauss
CCPC不拿奖不改名4 分钟前
python基础面试编程题汇总+个人练习(入门+结构+函数+面向对象编程)--需要自取
开发语言·人工智能·python·学习·自然语言处理·面试·职场和发展
晓13136 分钟前
第二章:Redis常见命令与Java客户端
java·数据库·redis
2501_9444241211 分钟前
Flutter for OpenHarmony游戏集合App实战之数字拼图滑动交换
android·开发语言·flutter·游戏·harmonyos
我送炭你添花11 分钟前
Pelco KBD300A 模拟器:10.报警联动规则编辑与执行
python·自动化·运维开发
·云扬·15 分钟前
MySQL索引实战指南:添加场景、联合索引要点与失效场景解析
数据库·mysql
偷星星的贼1117 分钟前
C++中的访问者模式实战
开发语言·c++·算法
填满你的记忆26 分钟前
【从零开始——Redis 进化日志|Day7】双写一致性难题:数据库与缓存如何不再“打架”?(附 Canal/读写锁实战)
java·数据库·redis·缓存·面试
小白考证进阶中27 分钟前
MySQL OCP认证可以考中文?备考难度怎么样?
数据库·mysql·dba·数据库管理·开闭原则·数据库管理员·mysql认证
莫问前路漫漫35 分钟前
Java Runtime Environment(JRE)全解析:Java 程序跨平台运行的核心基石
java·开发语言