CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day06/function5.py
Views: 729
1
"""
2
函数的参数
3
- 位置参数
4
- 可变参数
5
- 关键字参数
6
- 命名关键字参数
7
8
Version: 0.1
9
Author: 骆昊
10
Date: 2018-03-05
11
"""
12
13
14
# 参数默认值
15
def f1(a, b=5, c=10):
16
return a + b * 2 + c * 3
17
18
19
print(f1(1, 2, 3))
20
print(f1(100, 200))
21
print(f1(100))
22
print(f1(c=2, b=3, a=1))
23
24
25
# 可变参数
26
def f2(*args):
27
sum = 0
28
for num in args:
29
sum += num
30
return sum
31
32
33
print(f2(1, 2, 3))
34
print(f2(1, 2, 3, 4, 5))
35
print(f2())
36
37
38
# 关键字参数
39
def f3(**kw):
40
if 'name' in kw:
41
print('欢迎你%s!' % kw['name'])
42
elif 'tel' in kw:
43
print('你的联系电话是: %s!' % kw['tel'])
44
else:
45
print('没找到你的个人信息!')
46
47
48
param = {'name': '骆昊', 'age': 38}
49
f3(**param)
50
f3(name='骆昊', age=38, tel='13866778899')
51
f3(user='骆昊', age=38, tel='13866778899')
52
f3(user='骆昊', age=38, mobile='13866778899')
53
54