Skip to content

Commit bf1a5eb

Browse files
committed
模块的使用
1 parent a63bd2f commit bf1a5eb

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

exercise/calc.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def add(a, b):
2+
return a + b
3+
4+
5+
def div(a, b):
6+
return a / b
Loading
Loading

exercise/module_test.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 模块
2+
# 导入模块的几种方式
3+
# 方式一:import 模块名称 [as 别名]
4+
# 方式二:from 模块名称 import 函数/变量/类
5+
import math
6+
from math import pi, pow
7+
8+
# 导入自定义模块
9+
import calc
10+
from calc import div
11+
12+
print(id(math))
13+
print(type(math))
14+
print(math)
15+
print(math.pi)
16+
17+
# 查看math模块提供的属性、类和函数
18+
print(dir(math))
19+
print(math.pow(2, 3), type(math.pow(2, 3)))
20+
print(math.ceil(9.001))
21+
print(math.floor(9.9999))
22+
23+
print(pi)
24+
print(pow(2, 3))
25+
26+
# 使用自定义模块
27+
print(calc.add(1, 2))
28+
print(div(1, 2))

0 commit comments

Comments
 (0)