亚洲免费乱码视频,日韩 欧美 国产 动漫 一区,97在线观看免费视频播国产,中文字幕亚洲图片

      1. <legend id="ppnor"></legend>

      2. 
        
        <sup id="ppnor"><input id="ppnor"></input></sup>
        <s id="ppnor"></s>

        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)開平方
            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ù)無法精確保有的數(shù)據(jù)精度。高精度使decimal可以執(zhí)行二進(jìn)制浮點(diǎn)數(shù)無法進(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)