定义
def add(a, b):
return a + b
调用
def add(a, b):
return a + b
add(1, 10)
默认参数
def add(a, b = 1):
return a + b
add(10)
关键子参数
def add(a, b):
return a + b
add(b = 10, a = 1)
不定参数
def add(*args):
total = 0
for i in args:
total += i
return total
add(1, 2, 3, 4)
多返回值
def check(d):
return d > 0, d == 0, d < 0
gt, eq, lt = check(10)
print(gt, eq, lt)
拆包/解构
# 元组
one, two = (1, 2)
# -> 1, 2
# 数组
one, two = [1, 2]
# -> 1, 2
# 字典
m = { 'name': 'c', 'age': 12 }
name, age = m
# -> 'name', 'age'
name, age = m.keys()
# -> 'name', 'age'
name, age = m.values()
# -> 'c', 12
网友评论