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)
相关推荐
深蓝电商API2 分钟前
百度百科词条关联关系爬取
爬虫·python
Bert.Cai2 分钟前
Python type函数详解
开发语言·python
逆境不可逃3 分钟前
【从零入门23种设计模式16】行为型之迭代器模式
java·开发语言·数据结构·算法·设计模式·职场和发展·迭代器模式
李昊哲小课3 分钟前
Python OS模块详细教程
服务器·人工智能·python·microsoft·机器学习
geovindu3 分钟前
python: Singleton Pattern
开发语言·python·单例模式·设计模式
每日IO5 分钟前
AI“重构”生活 —— Mind+ V2 模型训练挑战赛
人工智能·python·mind+·开源硬件·模型训练·二哈识图·行空板
Ronin3059 分钟前
【Qt常用控件】按钮类控件
开发语言·qt·常用控件·按钮类控件
慧都小项9 分钟前
JAVA开发工具IntelliJ IDEA v2026更新前瞻:更优的交互视觉,编程体验升级
java·开发语言·intellij-idea
wsad053210 分钟前
在Windows上创建Python虚拟环境并在PyCharm中使用
windows·python·pycharm
高铭杰11 分钟前
Postgresql源码(157)Redo系列MultiXact Redo (RM_MULTIXACT_ID = 6)
数据库·postgresql