9--Python入门--模块

模块简单来说是一个保存了python代码的文件
很多python开源库就是模块

#例如我们调用科学计算库 numpy
import numpy as np          #as np是为了之后方便调用 
from pandas import DataFrame #只调用模块下的一个子模块
from numpy import *          #导入模块中的所有子模块
print('调用numpy产生10个标准正态随机数
',np.random.randn(10))  
View Code

输出:

调用numpy产生10个标准正态随机数
 [-0.86123492 -0.91268667  0.83897686  0.29236517 -1.30950825 -1.14943404
 -0.36781918  0.13990052  0.4298387  -0.80505142]
再次调用,结果会不一样,因为是随机的

#对于一个模块 我们可以先查看该模块下的所有子模块、变量和函数
import math
print('查看math模块下的所有子模块、变量和函数',dir(math))
View Code

输出:

查看math模块下的所有子模块、变量和函数 ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
原文地址:https://www.cnblogs.com/lizhiyan/p/9713111.html