Skip to content

Commit 585b097

Browse files
committed
update Day06
1 parent 76a92c1 commit 585b097

9 files changed

+644
-37
lines changed

Day06/function1.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
3+
函数的定义和使用 - 计算组合数C(7,3)
4+
5+
Version: 0.1
6+
Author: 骆昊
7+
Date: 2018-03-05
8+
9+
"""
10+
11+
12+
# 将求阶乘的功能封装成一个函数
13+
def factorial(n):
14+
result = 1
15+
for num in range(1, n + 1):
16+
result *= num
17+
return result
18+
19+
20+
print(factorial(7) // factorial(3) // factorial(4))

Day06/function2.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
3+
函数的定义和使用 - 求最大公约数和最小公倍数
4+
5+
Version: 0.1
6+
Author: 骆昊
7+
Date: 2018-03-05
8+
9+
"""
10+
11+
12+
def gcd(x, y):
13+
if x > y:
14+
(x, y) = (y, x)
15+
for factor in range(x, 1, -1):
16+
if x % factor == 0 and y % factor == 0:
17+
return factor
18+
return 1
19+
20+
21+
def lcm(x, y):
22+
return x * y // gcd(x, y)
23+
24+
25+
print(gcd(15, 27))
26+
print(lcm(15, 27))

Day06/function3.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
3+
Python的内置函数
4+
- 数学相关: abs / divmod / pow / round / min / max / sum
5+
- 序列相关: len / range / next / filter / map / sorted / slice / reversed
6+
- 类型转换: chr / ord / str / bool / int / float / complex / bin / oct / hex
7+
- 数据结构: dict / list / set / tuple
8+
- 其他函数: all / any / id / input / open / print / type
9+
10+
Version: 0.1
11+
Author: 骆昊
12+
Date: 2018-03-05
13+
14+
"""
15+
16+
17+
def myfilter(mystr):
18+
return len(mystr) == 6
19+
20+
21+
# help()
22+
print(chr(0x9a86))
23+
print(hex(ord('骆')))
24+
print(abs(-1.2345))
25+
print(round(-1.2345))
26+
print(pow(1.2345, 5))
27+
fruits = ['orange', 'peach', 'durian', 'watermelon']
28+
print(fruits[slice(1, 3)])
29+
fruits2 = list(filter(myfilter, fruits))
30+
print(fruits)
31+
print(fruits2)

Day06/function4.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
3+
Python常用模块
4+
- 运行时服务相关模块: copy / pickle / sys / ...
5+
- 数学相关模块: decimal / math / random / ...
6+
- 字符串处理模块: codecs / re / ...
7+
- 文件处理相关模块: shutil / gzip / ...
8+
- 操作系统服务相关模块: datetime / os / time / logging / io / ...
9+
- 进程和线程相关模块: multiprocessing / threading / queue
10+
- 网络应用相关模块: ftplib / http / smtplib / urllib / ...
11+
- Web编程相关模块: cgi / webbrowser
12+
- 数据处理和编码模块: base64 / csv / html.parser / json / xml / ...
13+
14+
Version: 0.1
15+
Author: 骆昊
16+
Date: 2018-03-05
17+
18+
"""
19+
20+
import time
21+
import shutil
22+
import os
23+
24+
seconds = time.time()
25+
print(seconds)
26+
localtime = time.localtime(seconds)
27+
print(localtime)
28+
print(localtime.tm_year)
29+
print(localtime.tm_mon)
30+
print(localtime.tm_mday)
31+
asctime = time.asctime(localtime)
32+
print(asctime)
33+
strtime = time.strftime('%Y-%m-%d %H:%M:%S', localtime)
34+
print(strtime)
35+
mydate = time.strptime('2018-1-1', '%Y-%m-%d')
36+
print(mydate)
37+
38+
shutil.copy('/Users/Hao/hello.py', '/Users/Hao/Desktop/first.py')
39+
os.system('ls -l')
40+
os.chdir('/Users/Hao')
41+
os.system('ls -l')
42+
os.mkdir('test')

Day06/function5.py

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

Day06/function6.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
3+
作用域问题
4+
5+
Version: 0.1
6+
Author: 骆昊
7+
Date: 2018-03-05
8+
9+
"""
10+
11+
12+
# 局部作用域
13+
def foo1():
14+
a = 5
15+
16+
17+
foo1()
18+
# print(a) # NameError
19+
20+
# 全局作用域
21+
b = 10
22+
23+
24+
def foo2():
25+
print(b)
26+
27+
28+
foo2()
29+
30+
31+
def foo3():
32+
b = 100 # 局部变量
33+
print(b)
34+
35+
36+
foo3()
37+
print(b)
38+
39+
40+
def foo4():
41+
global b
42+
b = 200 # 全局变量
43+
print(b)
44+
45+
46+
foo4()
47+
print(b)

0 commit comments

Comments
 (0)