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)
相关推荐
Shorasul5 分钟前
Go语言goroutine调度原理_Go语言GMP调度模型教程【高效】
jvm·数据库·python
咸鱼翻身小阿橙6 分钟前
QT-P3
开发语言·qt·计算机视觉
Absurd5877 分钟前
Navicat导出JSON数据为空如何解决_过滤条件与权限排查
jvm·数据库·python
最好的期待,未来可期7 分钟前
MySQL语法的高级用法CASE WHEN
数据库·mysql
m0_716430079 分钟前
SQL如何高效统计分类下的多项指标_善用CASE WHEN与SUM聚合
jvm·数据库·python
m0_5887584810 分钟前
PHP源码运行受主板供电影响吗_供电相数重要性说明【技巧】
jvm·数据库·python
qq_4138474012 分钟前
如何处理MongoDB跨分片事务报错_4.2+分布式事务的限制与两阶段提交延迟
jvm·数据库·python
InfinteJustice13 分钟前
HTML函数在超频CPU上更流畅吗_超频对HTML函数影响【技巧】
jvm·数据库·python
心易行者13 分钟前
代码写好了,然后呢?——手把手教你把Python脚本变成能赚钱的Web应用
开发语言·前端·python
AKA__Zas15 分钟前
初识 事务
java·开发语言·数据库·sql