99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀python基礎(chǔ)教程之數(shù)字處理(math)模塊詳解
python基礎(chǔ)教程之數(shù)字處理(math)模塊詳解
2018-07-29
收藏

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

這篇文章主要介紹了pythonr的數(shù)字處理模塊知識(math),需要的朋友可以參考下

1.math簡介

代碼如下:
>>> import math
>>>dir(math)          #這句可查看所有函數(shù)名列表
>>>help(math)         #查看具體定義及函數(shù)0原型

2.常用函數(shù)

代碼如下:
ceil(x) 取頂
floor(x) 取底
fabs(x) 取絕對值
factorial (x) 階乘
hypot(x,y)  sqrt(x*x+y*y)
pow(x,y) x的y次方
sqrt(x) 開平方
log(x)
log10(x)
trunc(x)  截斷取整數(shù)部分
isnan (x)  判斷是否NaN(not a number)
degree (x) 弧度轉(zhuǎn)角度
radians(x) 角度轉(zhuǎn)弧度

另外該模塊定義了兩個常量:

代碼如下:
e = 2.718281828459045
pi = 3.141592653589793

random

1.簡介

random是用于生成隨機數(shù),我們可以利用它隨機生成數(shù)字或者選擇字符串

代碼如下:
import random

2.常用函數(shù)

random.random()
用于生成一個隨機浮點數(shù):range[0.0,1.0)

復(fù)制代碼代碼如下:

>>> import random
>>> random.random()
0.999410896951364
random.uniform(a,b)

用于生成一個指定范圍內(nèi)的隨機浮點數(shù),a,b為上下限

只要a!=b,就會生成介于兩者之間的一個浮點數(shù),若a=b,則生成的浮點數(shù)就是a

代碼如下:
>>> random.uniform(10,20)
13.224754825064881
>>> random.uniform(20,10)
14.104410713376437
>>> random.uniform(10,10)
10.0

random.randint(a,b)
用于生成一個指定范圍內(nèi)的整數(shù),a為下限,b為上限,生成的隨機整數(shù)a<=n<=b;

若a=b,則n=a;若a>b,報錯

代碼如下:
>>> random.uniform(10,10)
10.0
>>> random.randint(10,20)
15
>>> random.randint(10,10)
10
>>> random.randint(20,10)
Traceback (most recent call last):
……
ValueError: empty range for randrange() (20,11, -9)

random.randrange([start], stop, [,step])
從指定范圍內(nèi),按指定基數(shù)遞增的集合中獲取一個隨機數(shù),基數(shù)缺省值為1

代碼如下:
>>> random.randrange(10,100,5)
95
>>> random.randrange(10,100,5)
45

random.choice(sequence)
從序列中獲取一個隨機元素,參數(shù)sequence表示一個有序類型,并不是一種特定類型,泛指list,tuple,字符串等

代碼如下:
>>> random.choice([1,2,3,4])
1
>>> random.choice([1,2,3,4])
3
>>> random.choice('hello')
'e'

random.shuffle(x[, random])
用于將一個列表中的元素打亂

代碼如下:
>>> 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)
從指定序列中隨機獲取k個元素作為一個片段返回,sample函數(shù)不會修改原有序列

代碼如下:
>>> 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.簡介

默認,浮點數(shù)學缺乏精確性

decimal 模塊提供了一個 Decimal 數(shù)據(jù)類型用于浮點數(shù)計算。相比內(nèi)置的二進制浮點數(shù)實現(xiàn) float這個類型有助于

金融應(yīng)用和其它需要精確十進制表達的場合,
控制精度,
控制舍入以適應(yīng)法律或者規(guī)定要求,
確保十進制數(shù)位精度,或者用戶希望計算結(jié)果與手算相符的場合。
Decimal 重現(xiàn)了手工的數(shù)學運算,這就確保了二進制浮點數(shù)無法精確保有的數(shù)據(jù)精度。 高精度使 Decimal 可以執(zhí)行二進制浮點數(shù)無法進行的模運算和等值測試。

2.使用

代碼如下:
>>> from decimal import Decimal
>>> Decimal('0.1') / Decimal('0.3')
Decimal('0.3333333333333333333333333333')

>>> from decimal import getcontext
>>> getcontext().prec = 4 #設(shè)置全局精度
>>> Decimal('0.1') / Decimal('0.3')
Decimal('0.3333')   

fractions
分數(shù)類型

構(gòu)造

復(fù)制代碼代碼如下:

>>> from fractions import Fraction
>>> Fraction(16, -10)  #分子分母
Fraction(-8, 5)
>>> Fraction(123)   #分子
Fraction(123, 1)

>>> Fraction('3/7')   #字符串分數(shù)
Fraction(3, 7)

>>> Fraction('-.125')  #字符串浮點數(shù)
Fraction(-1, 8)

>>> Fraction(2.25)  #浮點數(shù)
Fraction(9, 4)

>>> from decimal import Decimal
>>> Fraction(Decimal('1.1')) #Decimal
Fraction(11, 10)

計算

代碼如下:

>>> from fractions import Fraction
>>> 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)

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }