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)
相关推荐
二哈喇子!2 分钟前
数据库如何建表
数据库·sql·mysql
计算机网恋2 分钟前
思源笔记使用S3同步(阿里云OSS)
数据库·笔记·阿里云
白露与泡影3 分钟前
SpringBoot + Vue 实现 Python 在线调试器 - 技术方案文档
vue.js·spring boot·python
六毛的毛3 分钟前
hot100 python解法合集
开发语言·python
学嵌入式的小杨同学5 分钟前
【嵌入式 C 语言实战】手动实现字符串四大核心函数(strcpy/strcat/strlen/strcmp)
c语言·开发语言·前端·javascript·数据结构·数据库·算法
拾贰_C6 分钟前
[python | numpy] numpy& matplotib冲突
开发语言·python·numpy
小二·7 分钟前
Python Web 开发进阶实战:零信任架构落地 —— BeyondCorp 模型在 Flask + Vue 中的实现
前端·python·架构
PeterClerk8 分钟前
OpenCV 常用函数+ 示例图
图像处理·人工智能·python·opencv·计算机视觉
MoonPointer-Byte8 分钟前
【Python爬虫实战】用 Flet 把爬虫做成手机 App
爬虫·python·智能手机
学Linux的语莫8 分钟前
基于ollama、llamafile部署的大模型使用
linux·服务器·python·langchain·llama