python基礎(chǔ)教程之?dāng)?shù)字處理(math)模塊詳解

字號(hào):


    這篇文章主要介紹了pythonr的數(shù)字處理模塊知識(shí)(math),需要的朋友可以參考下
    1.math簡(jiǎn)介
    代碼如下:
    >>>importmath
    >>>dir(math)#這句可查看所有函數(shù)名列表
    >>>help(math)#查看具體定義及函數(shù)0原型
    2.常用函數(shù)
    代碼如下:
    ceil(x)取頂
    floor(x)取底
    fabs(x)取絕對(duì)值
    factorial(x)階乘
    hypot(x,y)sqrt(x*x+y*y)
    pow(x,y)x的y次方
    sqrt(x)開(kāi)平方
    log(x)
    log10(x)
    trunc(x)截?cái)嗳≌麛?shù)部分
    isnan(x)判斷是否NaN(notanumber)
    degree(x)弧度轉(zhuǎn)角度
    radians(x)角度轉(zhuǎn)弧度
    另外該模塊定義了兩個(gè)常量:
    代碼如下:
    e=2.718281828459045
    pi=3.141592653589793
    random
    1.簡(jiǎn)介
    random是用于生成隨機(jī)數(shù),我們可以利用它隨機(jī)生成數(shù)字或者選擇字符串
    代碼如下:
    importrandom
    2.常用函數(shù)
    random.random()
    用于生成一個(gè)隨機(jī)浮點(diǎn)數(shù):range[0.0,1.0)
    代碼如下:
    >>>importrandom
    >>>random.random()
    0.999410896951364
    random.uniform(a,b)
    用于生成一個(gè)指定范圍內(nèi)的隨機(jī)浮點(diǎn)數(shù),a,b為上下限
    只要a!=b,就會(huì)生成介于兩者之間的一個(gè)浮點(diǎn)數(shù),若a=b,則生成的浮點(diǎn)數(shù)就是a
    代碼如下:
    >>>random.uniform(10,20)
    13.224754825064881
    >>>random.uniform(20,10)
    14.104410713376437
    >>>random.uniform(10,10)
    10.0
    random.randint(a,b)
    用于生成一個(gè)指定范圍內(nèi)的整數(shù),a為下限,b為上限,生成的隨機(jī)整數(shù)a<=n<=b;
    若a=b,則n=a;若a>b,報(bào)錯(cuò)
    代碼如下:
    >>>random.uniform(10,10)
    10.0
    >>>random.randint(10,20)
    15
    >>>random.randint(10,10)
    10
    >>>random.randint(20,10)
    Traceback(mostrecentcalllast):
    ……
    ValueError:emptyrangeforrandrange()(20,11,-9)
    random.randrange([start],stop,[,step])
    從指定范圍內(nèi),按指定基數(shù)遞增的集合中獲取一個(gè)隨機(jī)數(shù),基數(shù)缺省值為1
    代碼如下:
    >>>random.randrange(10,100,5)
    95
    >>>random.randrange(10,100,5)
    45
    random.choice(sequence)
    從序列中獲取一個(gè)隨機(jī)元素,參數(shù)sequence表示一個(gè)有序類型,并不是一種特定類型,泛指list,tuple,字符串等
    代碼如下:
    >>>random.choice([1,2,3,4])
    1
    >>>random.choice([1,2,3,4])
    3
    >>>random.choice('hello')
    'e'
    random.shuffle(x[,random])
    用于將一個(gè)列表中的元素打亂
    代碼如下:
    >>>a=[1,2,3,4,5]
    >>>random.shuffle(a)
    >>>a
    [4,5,2,1,3]
    >>>random.shuffle(a)
    >>>a
    [3,2,5,1,4]
    random.sample(sequence,k)
    從指定序列中隨機(jī)獲取k個(gè)元素作為一個(gè)片段返回,sample函數(shù)不會(huì)修改原有序列
    代碼如下:
    >>>a=[1,2,3,4,5]
    >>>random.sample(a,3)
    [1,4,5]
    >>>random.sample(a,3)
    [1,2,5]
    >>>a
    [1,2,3,4,5]
    decimal
    1.簡(jiǎn)介
    默認(rèn),浮點(diǎn)數(shù)學(xué)缺乏精確性
    decimal模塊提供了一個(gè)Decimal數(shù)據(jù)類型用于浮點(diǎn)數(shù)計(jì)算。相比內(nèi)置的二進(jìn)制浮點(diǎn)數(shù)實(shí)現(xiàn)float這個(gè)類型有助于
    金融應(yīng)用和其它需要精確十進(jìn)制表達(dá)的場(chǎng)合,
    控制精度,
    控制舍入以適應(yīng)法律或者規(guī)定要求,
    確保十進(jìn)制數(shù)位精度,或者用戶希望計(jì)算結(jié)果與手算相符的場(chǎng)合。
    Decimal重現(xiàn)了手工的數(shù)學(xué)運(yùn)算,這就確保了二進(jìn)制浮點(diǎn)數(shù)無(wú)法精確保有的數(shù)據(jù)精度。高精度使Decimal可以執(zhí)行二進(jìn)制浮點(diǎn)數(shù)無(wú)法進(jìn)行的模運(yùn)算和等值測(cè)試。
    2.使用
    代碼如下:
    >>>fromdecimalimportDecimal
    >>>Decimal('0.1')/Decimal('0.3')
    Decimal('0.3333333333333333333333333333')
    >>>fromdecimalimportgetcontext
    >>>getcontext().prec=4#設(shè)置全局精度
    >>>Decimal('0.1')/Decimal('0.3')
    Decimal('0.3333')
    fractions
    分?jǐn)?shù)類型
    構(gòu)造
    代碼如下:
    >>>fromfractionsimportFraction
    >>>Fraction(16,-10)#分子分母
    Fraction(-8,5)
    >>>Fraction(123)#分子
    Fraction(123,1)
    >>>Fraction('3/7')#字符串分?jǐn)?shù)
    Fraction(3,7)
    >>>Fraction('-.125')#字符串浮點(diǎn)數(shù)
    Fraction(-1,8)
    >>>Fraction(2.25)#浮點(diǎn)數(shù)
    Fraction(9,4)
    >>>fromdecimalimportDecimal
    >>>Fraction(Decimal('1.1'))#Decimal
    Fraction(11,10)
    計(jì)算
    代碼如下:
    >>>fromfractionsimportFraction
    >>>a=Fraction(1,2)
    >>>a
    Fraction(1,2)
    >>>b=Fraction('1/3')
    >>>b
    Fraction(1,3)
    >>>a+b
    Fraction(5,6)
    >>>a-b
    Fraction(1,6)