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)
相关推荐
磁场转动100万匹9 小时前
PyTorch 手写数字识别实战:从数据加载到模型训练(零基础详解版)
人工智能·pytorch·python
青 春 记 忆16 小时前
零基础入门python30:Flask个人账本从空目录运行与阶段验收
python·flask·后端开发
Nontee16 小时前
MySQL 插入冲突了怎么办?两种处理方式入门笔记
数据库·笔记·mysql
ltl16 小时前
Parquet 文件格式:row group、编码与谓词下推索引
数据库
l12586516 小时前
# LangGraph Memory机制深度解析:短期记忆与长期记忆的工程实践
前端·人工智能·python·langchain·bootstrap
ltl16 小时前
PostgreSQL Join 路径:优化器如何选中 Nested Loop
数据库
ltl17 小时前
RocksDB 生产嵌入:Flink、TiKV 与 Kafka Streams 怎么用
数据库
m0_5474866617 小时前
《JavaScript核心原理 》全套PPT课件2026
开发语言·javascript·ecmascript
MC皮蛋侠客17 小时前
Tauri 2.x 系列(五):调用系统 API——官方插件、Rust crate 与原生能力
开发语言·后端·rust
金銀銅鐵17 小时前
斐波那契数列的个位数出现的周期是多少?
python·数学