51.基础语法-函数

函数的实参与形参

  • 括号中的ab是形参,56是实参
def hanshu(a, b):
    return a + b

print(hanshu(5, 6))

参数分类

  1. 普通参数&位置参数(一一对应,不能缺也不能多)
def hanshu_pt(a, b, c):
    pass
  1. 默认参数
def hanshu_mr(a, b, c=100):
    pass
  1. 关键字参数:调用函数,并输入参数时带上关键字名字
def hanshu_gjz(a, b, c):
    pass

hanshu_gjz(c=100, b=100, a=100)

参数特殊用法

  • *args 参数打包成元组给函数调用
  • **kwargs 参数打包成字典给参数调用
#args接了一个1
#*args接了一个元组(2,3,4)
#**kwargs接了一个字典{"a":5, "b":6}

def func(1,2,3,4,a=5,b=6):
    pass
原文地址:https://www.cnblogs.com/TK-tank/p/12345179.html