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)
相关推荐
engchina几秒前
Python PDF处理库深度对比:PyMuPDF、pypdfium2、pdfplumber、pdfminer的关系与区别
开发语言·python·pdf
拓端研究室2 分钟前
专题:2025供应链数智化与效率提升报告|附100+份报告PDF、原数据表汇总下载
开发语言·php
Lo-Y-eH7 分钟前
Openpyxl:Python操作Excel的利器
python·excel
czhc11400756639 分钟前
LINUX712 MYSQL;磁盘分区;NFS
数据库·mysql·adb
DAWN_T1713 分钟前
Transforms
pytorch·python·机器学习·jupyter·pycharm
一百天成为python专家25 分钟前
python库之jieba 库
开发语言·人工智能·python·深度学习·机器学习·pycharm·python3.11
不太可爱的大白35 分钟前
Mysql:分库分表
数据库·mysql
Go Dgg1 小时前
【Go + Gin 实现「双 Token」管理员登录】
开发语言·golang·gin
27669582921 小时前
tiktok 弹幕 逆向分析
java·python·tiktok·tiktok弹幕·tiktok弹幕逆向分析·a-bogus·x-gnarly
cylat1 小时前
Day59 经典时序预测模型3
人工智能·python·深度学习·神经网络