导入模块的2种方法

模块其实就是计算机中的一个py文件,可以把一个模块导入到当前py程序中,以增强其功能。导入模块使用import命令。

例如i

mport math

math.sqrt(9)

In [7]: math
math

In [7]: math.sqrt(9)
Out[7]: 3.0

使用import math。这必须先写上模块名,然后写函数名。例如math.sqrt()

另外一个方法,from math import sqrt。这种方法导入模块之后,就可以直接使用函数了。

In [15]: from math import pow

In [16]: pow(2,4)
Out[16]: 16.0

原文地址:https://www.cnblogs.com/gdlinux/p/11336968.html