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)
相关推荐
谢亮_vipxieliang1 分钟前
PHP 8 新特性:构造器属性提升与实战
开发语言·后端·php
shaibdoio3 分钟前
瞬维AI落地经验:AI Agent工具调用准确率怎么提
数据库·人工智能·oracle
头发够用的程序员5 分钟前
TensorRT 自定义算子插件实战(三):手搓 2×2 最大池化 customMaxpool
人工智能·pytorch·python·深度学习·神经网络·边缘计算·jetson
鬼手点金11 分钟前
opencode-crawl4ai使用建议
java·开发语言·人工智能·python·机器学习
海天一色y18 分钟前
深入理解对偶单纯形法:原理推导与 Python/MATLAB 编程实现
python·matlab·最优化算法·对偶单纯形法
NPE~19 分钟前
CMDB科普:配置管理数据库——运维相关
运维·数据库·科普·cmdb·配置管理数据库
Highcharts.js24 分钟前
动态数据可视化架构:Highcharts + PHP + MySQL 从数据库到数据渲染
数据库·mysql·php·开发文档·实时数据·highcharts·图表开发
专业程序开发源25 分钟前
springboot校园志愿者管理系统28562-计算机课程设计、毕业设计
java·spring boot·后端·python·django·php·课程设计
谢亮_vipxieliang30 分钟前
Go select 多路复用:从语法到实战的完整指南
开发语言·后端·golang
计算机毕业编程指导师37 分钟前
计算机毕设怎么做?Python+Hadoop的跨平台消费者评论情感分析与数据可视化系统实战 源码 毕业设计 选题推荐 毕设选题 数据分析
大数据·hadoop·python·计算机·spark·课程设计·消费者评论