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)
相关推荐
java_logo2 分钟前
Docker 部署 Hue:轻松搭建数据库与数据仓库 SQL 查询平台
数据库·数据仓库·sql·docker·hue·部署教程·轩辕镜像
Davina_yu5 分钟前
1、count(*)、count(1) 和 count(单个字段) 的区别
数据库
锦衣夜行?6 分钟前
Oracle 数据库安装失败清理残留步骤
数据库·oracle
时间售卖铺19 分钟前
MySQL 表的约束
运维·数据库·mysql·云原生
技术卷20 分钟前
Windows 下自定义 MCP 踩坑:stdin 默认继承导致 CPython 启动阻塞
python·mcp·zcode
小oo呆23 分钟前
【学习心得】Python多进程
python·操作系统
一条泥憨鱼24 分钟前
【Redis | 黑马点评】基于Redis的消息队列 | stream消息队列 源码解读
java·数据库·redis·缓存·lua
CodeGL25 分钟前
9、Python基础之IO编程
开发语言·数据库·python
每天一道题26 分钟前
Redis 缓存为什么会出问题?穿透、击穿、雪崩与缓存一致性
数据库·redis·缓存
HySpark27 分钟前
ERes2Net声纹识别短音频匹配不稳定:从音频时长到有效音素的排查实践
python·语音识别·声纹识别·vad·eres2net