Python-100-Days/Day01-15/code/Day06/function5.py

53 lines
873 B
Python
Raw Normal View History

2018-03-06 00:03:44 +08:00
"""
函数的参数
2019-05-03 21:17:36 +08:00
- 位置参数
- 可变参数
- 关键字参数
- 命名关键字参数
2018-03-06 00:03:44 +08:00
Version: 0.1
Author: 骆昊
Date: 2018-03-05
"""
# 参数默认值
def f1(a, b=5, c=10):
2019-05-03 21:17:36 +08:00
return a + b * 2 + c * 3
2018-03-06 00:03:44 +08:00
print(f1(1, 2, 3))
print(f1(100, 200))
print(f1(100))
print(f1(c=2, b=3, a=1))
# 可变参数
def f2(*args):
2019-05-03 21:17:36 +08:00
sum = 0
for num in args:
sum += num
return sum
2018-03-06 00:03:44 +08:00
print(f2(1, 2, 3))
print(f2(1, 2, 3, 4, 5))
print(f2())
# 关键字参数
def f3(**kw):
2019-05-03 21:17:36 +08:00
if 'name' in kw:
print('欢迎你%s!' % kw['name'])
elif 'tel' in kw:
print('你的联系电话是: %s!' % kw['tel'])
else:
print('没找到你的个人信息!')
2018-03-06 00:03:44 +08:00
param = {'name': '骆昊', 'age': 38}
f3(**param)
f3(name='骆昊', age=38, tel='13866778899')
f3(user='骆昊', age=38, tel='13866778899')
f3(user='骆昊', age=38, mobile='13866778899')