1. 程式人生 > >【再回首Python之美】【模組 math】math模組的基本使用

【再回首Python之美】【模組 math】math模組的基本使用

學好數理化,走遍天下都不怕,所以一定要把Python的math模組學好。

math簡介

  math提供兩個數學常量和眾多數學函式。這倆常量和眾多數學函式都是哪些呢?通過dir(math)一看便知。

使用math模組之前,一定要把模組包括進來,告訴編譯器我要開始使用math提供的變數和方法了。

import math

math的兩個常量

print u"圓周率  math.pi\t= ",math.pi #3.14159265359
print u"自然常數 math.e\t= ",math.e  #2.71828182846

math的眾多函式

>>> dir(math)
['__doc__', '__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']
>>> 

math官方手冊地址

點選進入下面官方math手冊頁面,裡面包含所有的math 屬性,工作當中用到時可以查詢使用,很是方便。

示例程式碼

#coding:utf-8
#ex_module_math.py
self_file = __file__ #save current file absulote path

import math

print "\n======dump math type======"
print type(math)    #<type 'module'>

print "\n======dump math attr list======"
print dir(math)
#['__doc__', '__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']

print "\n======dump math attr dict======"
print math.__dict__
#{'pow': <built-in function pow>,……,'gamma': <built-in function gamma>}

print "\n===normal introduce to function==="
print u"向上取整 math.ceil(1.001) = ",math.ceil(1.001)  #2.0
print u"向上取整 math.ceil(1.999) = ",math.ceil(1.999)  #2.0

print u"向下取整 math.floor(1.001) = ",math.floor(1.001)    #1.0
print u"向下取整 math.floor(1.999) = ",math.floor(1.999)    #1.0

print u"指數運算 math.pow(3,2) = ",math.pow(3,2)    #9.0
print u"指數運算 math.pow(4,2) = ",math.pow(4,2)    #16.0
print u"指數運算 math.pow(5,2) = ",math.pow(5,2)    #25.0

e = math.e
print u"基底為e的對數運算 math.log(e) = ",math.log(e)           #1.0
print u"基底為e的對數運算 math.log(e * e) = ",math.log(e * e)   #2.0
print u"基底為2的對數運算 math.log(4,2) = ",math.log(4,2)       #2.0
print u"基底為2的對數運算 math.log(8,2) = ",math.log(8,2)       #3.0

print u"平方根 math.sqrt(4) = ",math.sqrt(4)   #2.0
print u"平方根 math.sqrt(9) = ",math.sqrt(9)   #3.0
print u"平方根 math.sqrt(16) = ",math.sqrt(16) #4.0

print u"絕對值 math.fabs(+1) = ",math.fabs(+1) #1.0
print u"絕對值 math.fabs(-1) = ",math.fabs(-1) #1.0
print u"絕對值 math.fabs(+2) = ",math.fabs(+2) #2.0
print u"絕對值 math.fabs(-2) = ",math.fabs(-2) #2.0



print u"\n======Angular conversion角度轉換======"
#常識 0度(deg)   == 0弧度(rad)
#常識 90度(deg)  == 1.5707964弧度(rad)
#常識 180度(deg) == 3.1415927弧度(rad)

def deg2rad(deg):   #角度轉弧度
    return math.radians(deg)

def rad2deg(rad):   #弧度轉角度
    return math.degrees(rad)

print "deg2rad(0)\t=",deg2rad(0)        #0.0
print "deg2rad(90)\t=",deg2rad(90)      #1.57079632679
print "deg2rad(180)\t=",deg2rad(180)    #3.14159265359

print u"\n======三角函式======"
#三角函式入參為弧度rad,計算角度的時候,需要先將角度轉弧度後計算
print "math.sin(deg2rad(0))\t= ",math.sin(deg2rad(0))   #0.0
print "math.sin(deg2rad(30))\t= ",math.sin(deg2rad(30)) #0.5
print "math.sin(deg2rad(90))\t= ",math.sin(deg2rad(90)) #1.0

print "math.cos(deg2rad(0))\t= ",math.cos(deg2rad(0))    #1.0
print "math.cos(deg2rad(60))\t= ",math.cos(deg2rad(60))  #0.5
print "math.cos(deg2rad(180))\t= ",math.cos(deg2rad(180))#-1.0

print "math.tan(deg2rad(0))\t= ",math.tan(deg2rad(0))   #0.0
print "math.asin(deg2rad(0))\t= ",math.asin(deg2rad(0)) #0.0
print "math.acos(deg2rad(0))\t= ",math.acos(deg2rad(0)) #1.57079632679
print "math.atan(deg2rad(0))\t= ",math.atan(deg2rad(0)) #0.0

print "\n======math's const variable========"
print u"圓周率  math.pi\t= ",math.pi #3.14159265359
print u"自然常數 math.e\t= ",math.e  #2.71828182846

print "\nexit %s" % self_file

編譯執行


(end)