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开发环境(JDK)?
java·开发语言
V1ncent Chen4 分钟前
SQL大师之路 11 外连接和自连接
数据库·sql·mysql·数据分析
zklgin4 分钟前
PostgreSQL常用时间函数与时间计算提取示例说明
数据库·postgresql
曾阿伦6 分钟前
SQL CRUD 用法详解:从入门到实战的完整指南
数据库·sql
biubiuibiu7 分钟前
JavaScript核心概念深度解析:位运算与短路逻辑
开发语言·javascript·ecmascript
让学习成为一种生活方式7 分钟前
Swiss-Prot 注释--生信工具079
数据库
2401_849644859 分钟前
C++代码重构实战
开发语言·c++·算法
葡萄城技术团队10 分钟前
Hurley:用 Rust 打造的高性能 HTTP 客户端 + 压测工具
开发语言·http·rust
2601_9486061816 分钟前
MySQL B+树索引高度计算与性能阈值探讨
数据库·b树·mysql
lierenvip17 分钟前
mysql用户名怎么看
数据库·mysql