Python day26

@浙大疏锦行 Python day26

内容:

  • 函数的定义
python 复制代码
def 函数名(参数):

        函数逻辑

        return 返回值
  • 局部变量和全局变量
python 复制代码
y = 2 # 全局变量,可以在任意位置访问,
z = 3
def fun()
    x = 1 # 局部变量,只能在内部访问
    z = 1 # 局部变量优先级更高
    print(x)
    print(z)
  • 参数类型
python 复制代码
# 位置参数
def fun(x , y)
    return x + y
# 默认参数
def fun(x , y, z = 1, w = None)
    return x + y + 1
# 不定参数
# *args: 列表,参数列表
#  **kwargs: 字典,一个个的 key-word ,但是kwargs必须放在参数最后
def fun(x, y, *args, **kwargs):
    return x + y
  • 传参方式
python 复制代码
# 传参方式
def fun(x, y)
    return x + y
# 默认传参
fun(1,2)
# 指定参数
fun(x = 1, y = 2)
# 不定参数
def fun(x, y, *args, **kwargs)
    return x + y
# 对于 *args
fun(1, y =2 ,3, 4, 5)
# 对于 **kwargs
fun(1, 2, 3, 4, 5, k1=6, k2=7)
  • 传参顺序
  • 一般按照定义顺序,不过*args和**kwargs必须放在最后
相关推荐
花酒锄作田14 小时前
Pydantic校验配置文件
python
hboot15 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi1 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi1 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 天前
海天线算法的前世今生
python·计算机视觉
韩师傅2 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L2 天前
LangGraph的MessageState and HumanMessage
python