-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_05.py
More file actions
104 lines (83 loc) · 2.18 KB
/
demo_05.py
File metadata and controls
104 lines (83 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
def printInfo(name, age=18, score=100):
# 函数参数
# 带有默认值得参数一定位于参数列表的最后面
"""
默认值参数
:param name:
:param age:
:param score:
:return:
"""
# ctrl +d 复制当前行到下一行
# ctrl +y 删除当前行
print("name:{}".format(name))
print("age:{}".format(age))
print("score:{}".format(score))
# printInfo("xiaoming", 19)
# 不定长参数
def func(a, b, *args, **kwargs):
print("a:{}".format(a))
print("b:{}".format(b))
print("args:{}".format(args))
for key, value in kwargs.items():
print("key:{}, value:{}".format(key, value))
# 格式化 alt+enter
# func(1, 2, 3, 4, 5, m=6, n=7)
# c = (3, 4, 5)
# d = {"m": 6, "n": 7}
# func(1, 2, *c, **d) # func(1,2 3,4,5,m=6,n=7)
# func(1, 2, c, d) # func(1,2, (1,2,3),{"m": 6, "n": 7})
# func(1,2, (1,2,3),{"m": 6, "n": 7})
# 引用传参
def add(a):
"""
Python中函数参数是引用传递(注意不是值传递)。
对于不可变类型,因变量不能修改,所以运算不会影响到变量自身;
而对于可变类型来说,函数体中的运算有可能会更改传入的参数变量。
:param a:
:return:
"""
a += a
# a_init = 1
# add(a_init)
# print(a_init)
# a_init = [1, 2]
# add(a_init)
# print(a_init)
# 递归函数
def calNum(n):
"""
计算N的阶乘
n! = n*(n-1)!
:param n:
:return:
"""
"""
归纳出来计算的逻辑,n! = n*(n-1)!
"""
if n >= 1:
result = n * calNum(n - 1)
else:
result = 1
return result
# print(calNum(4))
# 匿名函数 lambda
def operate_parms(a, b, opt):
"""
匿名函数
:param a:
:param b:
:param opt:
:return:
"""
print("result={}".format(opt(a, b)))
# operate_parms(1, 2, lambda x, y: x + y)
# def opt(a,b):
# return a+b
# operate_parms(1,2, opt)
# 作业:
# 1,编程实现9*9乘法表
# 2. 用函数实现一个判断,用户输入一个年份,判断是否是闰年
# 3. 用函数实现输入某年某月某日,判断一下这一天是这一年的第几天,需要考虑闰年
name = input("我是谁?")
print("name是:{}".format(name))