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)
相关推荐
麒qiqi1 分钟前
进阶 IMX6ULL 裸机开发:从 C 语言点灯到 BSP 工程化(附 SDK / 链接脚本实战)
c语言·开发语言
weixin_462446231 分钟前
Python用Flask后端解析Excel图表,Vue3+ECharts前端动态还原(附全套代码)
前端·python·flask·echats
LaughingZhu2 分钟前
Product Hunt 每日热榜 | 2026-01-20
数据库·人工智能·经验分享·神经网络·搜索引擎·chatgpt
SJLoveIT5 分钟前
sql注入攻击的防御思路总结
数据库·sql
偷星星的贼117 分钟前
如何为开源Python项目做贡献?
jvm·数据库·python
秋刀鱼程序编程8 分钟前
Java基础入门(七)---异常处理
java·开发语言·python
遇见你的雩风10 分钟前
Java---多线程(一)
java·开发语言
二十雨辰11 分钟前
[python]-基础语法
python
小白学大数据11 分钟前
基于 Python 的知网文献批量采集与可视化分析
开发语言·爬虫·python·小程序
Ulyanov12 分钟前
PyVista战场可视化实战(一):构建3D战场环境的基础
开发语言·python·3d·tkinter·gui开发