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)
相关推荐
Irene1991几秒前
Python 中的 round() 函数不是严格的“四舍五入“,而是采用银行家舍入法(Bankers‘ Rounding)
python
ZC跨境爬虫几秒前
3D 地球卫星轨道可视化平台开发 Day9(AI阈值调控+小众卫星识别+低Token测试模式实战)
人工智能·python·3d·信息可视化·json
钮钴禄·爱因斯晨3 分钟前
聚焦操作系统中的PV操作
数据库·算法·系统架构·c#
2301_813599553 分钟前
CSS中relative与absolute的区别_详解相对与绝对定位应用场景
jvm·数据库·python
0xDevNull8 分钟前
Java 深度解析:for 循环 vs Stream.forEach 及性能优化指南
java·开发语言·性能优化
研☆香11 分钟前
聊一聊如何分析js中的数据结构
开发语言·javascript·数据结构
qq_3721542312 分钟前
c++怎么在写入文件流时通过peek预读功能实现复杂的逻辑判断【实战】
jvm·数据库·python
-凌凌漆-15 分钟前
【Qt】 QSerialPort::flush()介绍
开发语言·qt
徐子元竟然被占了!!18 分钟前
IS-IS协议
开发语言·网络·php
m0_5145205721 分钟前
CSS如何给按钮添加按下缩小的动画_利用-active配合transform
jvm·数据库·python