python函数的定义和使用速查笔记

python函数的定义与使用

定义函数

  • 函数:组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。
  • 定义函数格式:
    • def 函数名(参数列表):
      • 函数体
  • 函数的调用:
    • 返回值=函数名(输入参数)
def Max(a, b):
    if a > b:
        return a
    else:
        return b

print(Max(3, 5))

返回值

  • 使用return返回函数计算结果
  • 可以有多个return语句,但是真正执行时最多运行1次,碰到return语句,函数结束
  • 没有return语句,函数返回值为None
  • return可以返回多个变量

传参机制

形参与实参

  • 形式参数:定义函数时的输入参数
  • 实际参数:调用函数时传递的参数
def f(x):
    x *= 2
    print("这是形式参数:x = {}".format(x)) # 这是形式参数:x = 200

x = 100
print("这是实际参数:x = {}".format(x)) # 这是实际参数:x = 100
f(x)
print("这是实际参数:x = {}".format(x)) # 这是实际参数:x = 100

值传递

  • 值传递:函数中的形式参数变化,不会导致实际参数的变化,这是由于传递的是数值单方向的。参数为不可变类型,字符串、数字、元组
def Swap(x, y):
    x, y = y, x
    print("这是swap函数")
    print("x = ", x, "y = ", y) # x = 5 y = 3
    print("id(x) = ", id(x), "id(y) = ", id(y)) # id(x) = 140293716253104 id(y) = 140293716253040\

x, y = 3, 5
print("这是主函数") # 
print("x = ", x, "y = ", y) # x = 3 y = 5
print("id(x) = ", id(x), "id(y) = ", id(y)) # id(x) = 140293716253040 id(y) = 140293716253104

Swap(x, y)

print("这是主函数")
print("x = ", x, "y = ", y) # x = 3 y = 5
print("id(x) = ", id(x), "id(y) = ", id(y)) # id(x) = 140293716253040 id(y) = 140293716253104

引用传递

  • 引用传递:函数中形式参数变化,会导致实际参数一起变化,这是由于传递的是变量。参数为可变类型,列表、字典
def F(x):
    x.append(4)
    print("这是swap函数")
    print("x = ", x) # x = [1, 2, 3, 4]
    print("id(x) = ", id(x)) # id(x) = 140293372285504

x = [1, 2, 3]
print("这是主函数")
print("x = ", x) # x = [1, 2, 3]
print("id(x) = ", id(x)) # id(x) = 140293372285504

F(x)

print("这是主函数")
print("x = ", x) # x = [1, 2, 3, 4]
print("id(x) = ", id(x)) # id(x) = 140293372285504
  • 如何传参不出错?在大多数情况下:
    • 尽量不要传递列表、字典这种可变数据
    • 即使传参,也尽量不要改动这些参数
  • 技巧:传递列表、字典这类变量时,传递一个副本,这样就算修改实参也不会发生变化

位置参数

  • 参数个数相同,顺序一一对应
def f(a, b, c, d, e):
    print("a = ", a)
    print("b = ", b)
    print("c = ", c)
    print("d = ", d)
    print("e = ", e)

f(1, 2, 3, 4, 5)

关键字参数

  • 根据形参的名字传递参数
def f(a, b, c, d, e):
    print("a = ", a)
    print("b = ", b)
    print("c = ", c)
    print("d = ", d)
    print("e = ", e)

f(e=1, d=2, a=3, b=4, c=5)
# 正确:位置参数在前,关键字参数在后
f(1, 2, c=3, d=4, c=5)

默认参数

  • 定义函数时,给一个形参默认值,所有的默认参数必须放在最后面。
    • 调用时,如果省略该参数则该参数为默认值。
def f(a, b, c=3, d=4, e=5):
    print("a = ", a)
    print("b = ", b)
    print("c = ", c)
    print("d = ", d)
    print("e = ", e)

# 后三个参数使用默认值
f(1, 2)

# c=5, 不使用默认值
f(1, 2, 5)

f(1, 2, d=4)

不定长参数(*)

  • 形式参数前加*, 表示不定长参数,可以按照位置参数的格式传递多个参数
  • 加了星号*的参数会以元组(typle)的形式导入,存放所有未命名的变量参数。
def f(a, b, *argtuple):
    print("a = ", a) # a = 1
    print("b = ", b) # b = 2
    print("argtuple = ", argtuple) # argtuple = (3, 4, 5, 6, 7, 8)

f(1, 2, 3, 4, 5, 6, 7, 8)

不定长参数(**)

  • 形式参数前加**, 表示不定长参数,可以按照位置参数的格式传递多个参数
  • 加了星号**的参数会以字典(dict)的形式导入。
def f(a, b, **args):
    print("a = ", a) # a = 1
    print("b = ", b) # b = 2
    print("args = ", args) # args = {'c1': 3, 'd': 4, 'e': 5}

f(1, 2, c1=3, d=4, e=5)

变量作用域

  • 作用域:变量的有效范围
    • 局部变量:函数内部的变量,仅作用域函数内部
    • 全局变量:函数外部的变量,作用于全局
total = 0
def f(a, b):
    total = a + b
    print("此处的total是局部变量,total =", total) # 此处的total是局部变量,total = 30
f(10, 20)
print("此处的total是全局变量,total =", total) # 此处的total是全局变量,total = 0
  • 如何在函数内部使用全局变量?
  • 使用global声明
total = 0
def f(a, b):
    global total
    total = a + b
    print("此处的total是全局变量,total = ", total) # 此处的total是全局变量,total = 30

f(10, 20)
print("此处的total是全局变量,total = ", total) # 此处的total是全局变量,total = 30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值