Python学习笔记2—模块

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习笔记2—模块相关的知识,希望对你有一定的参考价值。

模块的使用

引用模块的两种形式

形式一: import module_name

形式二: from module1 import module11   (module11是module的子模块)

例:引用精确除法模块

>>> 5/2
2
>>> from __future__ import division
>>> 5/2
2.5
>>> 5//2
2
>>> 

如过需要进行开方,乘方,对数等运算就需要用到Python中的Math模块

>>> import math
>>> dir(math)    #查看该模块提供的功能
[__doc__‘, __file__‘, __name__‘, __package__‘, acos‘, acosh‘, asin‘, asinh‘, atan‘, atan2‘, atanh‘, ceil‘, copysign‘, cos‘, cosh‘, degrees‘, e‘, erf‘, erfc‘, exp‘, expm1‘, fabs‘, factorial‘, floor‘, fmod‘, frexp‘, fsum‘, gamma‘, hypot‘, isinf‘, isnan‘, ldexp‘, lgamma‘, log‘, log10‘, log1p‘, modf‘, pi‘, pow‘, radians‘, sin‘, sinh‘, sqrt‘, tan‘, tanh‘, trunc]
>>> help(math.pow)   #查看函数的使用方法
Help on built-in function pow in module math:

pow(...)
    pow(x, y)
    
    Return x**y (x to the power of y).
(END) 

在交互模式下输入q退出交互模式

>>> math.pow(4,2)       #表示4的乘方
16.0
>>> math.pow(3,2)
9.0
>>> 4**2
16

 

以上是关于Python学习笔记2—模块的主要内容,如果未能解决你的问题,请参考以下文章

python基础学习笔记(十三)

模块类和对象(python学习笔记)

python学习笔记 - 函数,集合,包,模块

Python学习笔记十_模块第三方模块安装模块导入

Python学习笔记——基础篇第六周——模块

Python学习笔记__5章 模块