Skip to content

Commit a586bdf

Browse files
committed
function
1 parent dfc028a commit a586bdf

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed

函数/args_fun.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#函数除了正常定义的必选参数外,还可以使用默认参数、可变参数和关键字参数,
2+
3+
4+
#x2
5+
def power1(x):
6+
return x * x
7+
8+
9+
#x3 x4 x5
10+
def power2(x, n):
11+
s = 1
12+
while n > 0:
13+
n = n - 1
14+
s = s * x
15+
return s
16+
17+
18+
power2(5, 2) #25
19+
20+
#默认参数 n是默认参数
21+
22+
23+
def power3(x, n=2):
24+
s = 1
25+
while n > 0:
26+
n = n - 1
27+
s = s * x
28+
return s
29+
30+
31+
power3(5) #25
32+
power3(5, 2) #25
33+
34+
#可变参数
35+
36+
37+
# a2 + b2 +c2
38+
def calc(*numbers):
39+
sum = 0
40+
for num in numbers:
41+
sum = sum + num * num
42+
return sum
43+
44+
45+
#calc 可以传入任意个参数
46+
calc(1, 2, 3, 4) #30
47+
calc(1, 2) #5
48+
calc() # 0
49+
50+
#传入一个list或tuple 在list或tuple前面加一个*号,把list或tuple的元素变成可变参数传进去
51+
nums = [1, 2, 3, 4]
52+
calc(*nums) #30
53+
54+
#关键字参数
55+
#可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple。而关键字参数允许你传入0个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个dict。
56+
57+
58+
#函数person除了必选参数name和age外,还接受关键字参数kw。在调用该函数时,可以只传入必选参数:
59+
def person(name, age, **kw):
60+
print('name:', name, 'age:', age, 'other:', kw)
61+
62+
63+
person('colin', 90) #name: colin age: 90 other: {}
64+
65+
#也可以传入任意个数的关键字参数
66+
person(
67+
'Bob', 35, city='Beijing') #name: Bob age: 35 other: {'city': 'Beijing'}
68+
69+
person(
70+
'Adam', 45, gender='M', job='Engineer'
71+
) #name: Adam age: 45 other: {'gender': 'M', 'job': 'Engineer'}
72+
73+
#先组装出一个dict,把该dict转换为关键字参数传进去
74+
extra = {'city': 'Beijing', 'job': 'Engineer'}
75+
person(
76+
'Jack', 24,
77+
**extra) #name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}
78+
79+
#命名关键字参数
80+
#限制关键字参数的名字 和关键字参数**kw不同,命名关键字参数需要一个特殊分隔符*,*后面的参数被视为命名关键字参数。
81+
82+
83+
def person1(name, age, *, city, job):
84+
print(name, age, city, job)
85+
86+
87+
person1('Jack', 24, city='Beijing', job='Engineer') #Jack 24 Beijing Engineer
88+
89+
#如果函数定义中已经有了一个可变参数,后面跟着的命名关键字参数就不再需要一个特殊分隔符*了:
90+
91+
92+
def person2(name, age, *args, city, job):
93+
print(name, age, args, city, job)
94+
95+
96+
def print_scores(**kw):
97+
print(' Name Score')
98+
print('------------------')
99+
for name, score in kw.items():
100+
print('%10s %d' % (name, score))
101+
print()
102+
103+
104+
stu = {'Ame': 20, 'Pbe': 30, 'ESE': 90}
105+
print_scores(**stu)
106+
107+
#
108+
# Name Score
109+
#------------------
110+
# Ame 20
111+
# Pbe 30
112+
# ESE 90
113+
114+
115+
def print_info(name, *, gender, city='Beijing', age):
116+
print('Personal Info')
117+
print('---------------')
118+
print(' Name: %s' % name)
119+
print(' Gender: %s' % gender)
120+
print(' City: %s' % city)
121+
print(' Age: %s' % age)
122+
print()
123+
124+
125+
print_info('Bob', gender='male', age=20)
126+
127+
#Personal Info
128+
#---------------
129+
# Name: Bob
130+
# Gender: male
131+
# City: Beijing
132+
# Age: 20
133+
134+
print_info('Lisa', gender='female', city='Shanghai', age=18)
135+
136+
#Personal Info
137+
#---------------
138+
# Name: Lisa
139+
# Gender: female
140+
# City: Shanghai
141+
# Age: 18

函数/def_fun.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
def my_abs(x):
2+
if x >= 0:
3+
return x
4+
else:
5+
return -x
6+
7+
8+
print(my_abs(-20)) #20
9+
10+
11+
#空函数,用pass做占位符
12+
def nop():
13+
pass
14+
15+
16+
#数据类型检查用内置函数isinstance()实现
17+
18+
19+
def my_abs_isin(x):
20+
if not isinstance(x, (int, float)):
21+
raise TypeError('bad operand tpye')
22+
if x >= 0:
23+
return x
24+
else:
25+
return -x
26+
27+
28+
print(my_abs_isin('s')) #参数错误会抛出错误
29+
#Traceback (most recent call last):
30+
# File "<stdin>", line 1, in <module>
31+
# File "<stdin>", line 3, in my_abs_isin
32+
#TypeError: bad operand tpye
33+
34+
#返回多个值
35+
#在游戏中经常需要从一个点移动到另一个点,给出坐标、位移和角度,就可以计算出新的新的坐标:
36+
37+
import math
38+
39+
40+
def move(x, y, step, angle=0):
41+
nx = x + step * math.cos(angle)
42+
ny = y - step * math.sin(angle)
43+
return nx, ny
44+
45+
46+
x, y = move(100, 100, 60, math.pi / 6)
47+
r = move(100, 100, 60, math.pi / 6)
48+
49+
print(x, y) #151.96152422706632 70.0
50+
51+
print(r) #(151.96152422706632, 70.0) 返回的是个元组

函数/recur.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#一个函数在内部调用自身本身,这个函数就是递归函数。
2+
3+
4+
# 利用递归函数计算阶乘
5+
# n! = 1 x 2 x 3 x ... x n
6+
def fact(n):
7+
if n == 1:
8+
return 1
9+
return n * fact(n - 1)
10+
11+
12+
fact(5) #120
13+
14+
15+
# 利用递归函数移动汉诺塔: https://www.cnblogs.com/forever-snow/p/8316218.html https://www.cnblogs.com/haidaojiege/p/7764012.html
16+
def move(n, a, b, c):
17+
if n == 1:
18+
print('move', a, '-->', c)
19+
else:
20+
move(n - 1, a, c, b)
21+
move(1, a, b, c)
22+
move(n - 1, b, a, c)
23+
24+
25+
move(3, 'A', 'B', 'C')
26+
# move A --> C
27+
# move A --> B
28+
# move C --> B
29+
# move A --> C
30+
# move B --> A
31+
# move B --> C
32+
# move A --> C

0 commit comments

Comments
 (0)