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)
相关推荐
liliangcsdn4 分钟前
因子权重矩阵处理-因子权重收缩Shrinkage算法的探索
开发语言·python·机器学习
Databend5 分钟前
只看 PASS 会骗你,6 条 Agent Trace 里的 Coding Agent 评测真相
大数据·数据库·agent
用户83562907805111 分钟前
使用 Python 在 Excel 中添加和编辑形状
后端·python
Wang's Blog22 分钟前
Java框架快速入门: Spring Security+OAuth2之密码验证规则与自定义注解
java·数据库·spring
2601_9623004723 分钟前
python在运维上可以干什么,请举几个具体的例子
运维·python·系统管理·网络监控·自动化脚本
小刘在重生~36 分钟前
Java 异常体系完整笔记
java·笔记·python
用户83562907805139 分钟前
使用 Python 为 PDF 添加和管理超链接
后端·python
千千寰宇40 分钟前
[Python/测试] pytest:简洁、可扩展的 Python 测试框架
python·软件质量保障与测试
少陽君41 分钟前
Python 并发编程:IO 等待用线程/asyncio,CPU 计算才用进程
数据库·python·php
aramae1 小时前
模拟实现strstr函数(C语言)
c语言·开发语言·算法