python 肆--模块,cmath复数

来源:互联网 发布:淘宝全球买手是真的吗 编辑:程序博客网 时间:2024/06/05 23:39

上一节,一个人的年龄是22。9,她年龄还没到23,只是取22,则需要floor函数

模块:导入到python以增强其功能的扩展,暂时这么理解

需要import命令来导入math模块,利用math模块中的floor函数

用import导入模块,按照“模块。函数”的格式用这个模块的函数、

>>> import math>>> math.floor (22.9)22.0>>> 

把年龄转换为整数22,用int函数

>>> int(math.floor(22.9))22

math.ceil(22.9)23.0
 int(22.9)#把参数转化成整数时会自动向下取整22




类型对象type object



>>> long(math.floor(22.9))22L>>> float(math.floor(22.9))22.0

如果不从不同模块导入多个同名函数,则可以用一下格式:不用每次调用函数时都写上模块的名字

>>> from math import sqrt>>> sqrt(9)3.0
>>> foo = math.sqrt#变量来引用函数>>> foo(9)3.0

sqrt只能处理浮点数,也就是实数,如求负一的平方根:

>>> from math import sqrt>>> sqrt(-1)Traceback (most recent call last):  File "<pyshell#13>", line 1, in <module>    sqrt(-1)ValueError: math domain error


数学概念里有复数,虚数,则需另外一个模块了cmath(complex math,复数):

>>> import cmath>>> cmath.sqrt(-1)1j

>>> (1 + 3j) * (9 + 4j)(-3+31j)

时光机__future__,导入那些未来成为标准python组成部分的新特性

原创粉丝点击