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)
相关推荐
SelectDB5 分钟前
科大讯飞可观测性底座:从 ES/Loki 到 Apache Doris 的可观测存储分析升级实践
数据库
青禾8376 分钟前
Linux 系统信息、权限管理与 Python 并发编程(协程与线程)完全指南
linux·python
SelectDB11 分钟前
Cisco WebEx 数据平台统一改造:基于 Apache Doris / SelectDB 替换 Trino、Pinot、Iceberg 及 Kyuubi
数据库
caimouse12 分钟前
ReactOS 图形系统分析(47):GDI 批处理 — gdibatch.c
c语言·开发语言
曲无忆12 分钟前
密码学三大核心技术解析
python·密码学
qq210846295313 分钟前
在python中什么是 self 和 cls?
开发语言·前端·python
数据技术说17 分钟前
MySQL 和 PostgreSQL 的 CDC 方案到底有什么区别?
数据库·架构
nanawinona21 分钟前
2026年手工思路量化后,工具重点会怎样变化
人工智能·python
SelectDB28 分钟前
灵犀科技统一数据服务平台:基于 Apache Doris / SelectDB 实现存储降本 60%、计算提效 10 倍
数据库