python之Number,pythonnumber

來源:互聯網
上載者:User

python之Number,pythonnumber

1、Python number數字

Python Number 資料類型用於儲存數值。

資料類型是不允許改變的,這就意味著如果改變 Number 資料類型的值,將重新分配記憶體空間。

建立一個number資料值,改變資料的值,查看記憶體位址已發生改變:

>>> num = 123>>> id(num)8743872>>> num = 456>>> id(num)13991590095640

使用del語句刪除number對象,可以刪除多個用','逗號分隔:

>>> num = 123>>> num1 =888>>> del num,num1

2、python支援四種不同的資料類型:

  • 整形(int)-通常被稱為整型或整數,是正數或者負整數。
  • 長整型(long integers)-無限大小的整數,整數最後使用大寫或小寫L表示。
  • 浮點型(floating point real values)-浮點型由整數部分和小數部分組成。
  • 複數(complex numbers)-複數由實數部分和虛數部分構成,可以使用a+bj,或者complex(a,b)表示,複數的a和b部分都是浮點型.
長整型的取值範圍:python2.7版本中長整型的取值範圍為-2**63-1次方至2**63次方python3中沒有long類型,使用int表示長整型In [1]: 2**63-1Out[1]: 9223372036854775807LIn [2]: lo1 = 9223372036854775807In [3]: type(lo1)Out[3]: intIn [4]: lo2 = 9223372036854775808In [5]: type(lo2)Out[5]: longIn [6]: log8 = -2**62In [7]: type(log8)Out[8]: intIn [9]: log8 = -2**63-1In [10]: type(log8)Out[11]: long建立複數:>>> complex1 = 1.2+3.4j>>> type(complex1)<class 'complex'>>>> complex2 = complex(0.3,3.2)>>> print(complex1,complex2)(1.2+3.4j) (0.3+3.2j)

3、python number類型轉換

內建的函數可以執行資料類型之間的轉換。這些函數返回一個新的對象,表示轉換的值

>>> nu1 = 89>>> nu2 = float(nu1)  #轉換浮點型>>> type(nu2)<class 'float'>>>> nu3 =complex(nu2)  #轉複數>>> type(nu3)<class 'complex'>>>> print(nu3)(89+0j)>>> nu4 = int(nu2)  #轉整數>>> type(nu4)<class 'int'>>>> nu5 =str(nu4) #轉字元>>> type(nu5)<class 'str'>
str(x )                 將對象 x 轉換為字串  repr(x )                將對象 x 轉換為運算式字串  eval(str )              用來計算在字串中的有效Python運算式,並返回一個對象  tuple(s )               將序列 s 轉換為一個元組  list(s )                將序列 s 轉換為一個列表  chr(x )                 將一個整數轉換為一個字元  unichr(x )              將一個整數轉換為Unicode字元  ord(x )                 將一個字元轉換為它的整數值  hex(x )                 將一個整數轉換為一個十六進位字串  oct(x )                 將一個整數轉換為一個八進位字串

4、python數字內建函數,數文書處理模組math

>>> import math   #數文書處理模組>>> help(math.ceil)  #查看協助>>> dir(math)   #列印所有方法['__doc__', '__file__', '__loader__', '__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', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']>>> nu1 = 12.34>>> math.ceil(nu1)   #取上入整數13>>> math.exp(nu1)  #返回e的nu1次冪,e為定義的常量228661.9520568098>>> math.fabs(nu1) #返回絕對值12.34>>> math.floor(nu1) #返回數位下舍整數部分12>>> math.modf(nu1) #返回小數部分與整數部分(0.33999999999999986, 12.0)>>> math.sqrt(nu1) #返回平方根3.5128336140500593>>> math.e    #模組定義的常量e2.718281828459045>>> math.pi   #模組定義的常量pi3.141592653589793內建函數:>>> abs(11.2)   #返回絕對值11.2>>> max(12,24)  #最大值24>>> min(12,24)  #最小值12>>> pow(2,4)   #2**4冪次方16>>> round(1.245,3)  #傳回值的四捨五入值,3為定義到小數第幾位1.245>>> round(1.245)   #預設為01

5、python隨機數模組random

>>> import random    #匯入模組>>> random.random()   #擷取0到1之間的隨機數0.1781419039493949>>> random.random()0.914421842727102>>> random.uniform(10,20)  #產生10,20之間的浮點數19.774883012515218>>> random.uniform(10,20)11.654111952867027>>> random.randint(10,20)  #產生指定範圍內的整數18>>> random.randint(10,20)11>>> random.randrange(1,100,8)  #從指定範圍內按指定基數遞增擷取隨機數33>>> random.randrange(1,100,8)17>>> random.randrange(1,100,8)33>>> random.choice([1,2,3,4,5]) #從序列元素中隨機擷取元素,只能是有序類型2>>> random.choice([1,2,3,4,5])1>>> random.choice([1,2,3,4,5])2>>> random.choice('abcd')'d'>>> random.choice('abcd')'a'>>> random.choice('abcd')'c'>>> a = [1,2,3,4,5]  #將一個列表元素打亂>>> random.shuffle(a)>>> a[1, 2, 3, 5, 4]>>> random.shuffle(a)>>> a[2, 5, 4, 3, 1]>>> random.sample(a,2)  #從指定序列中隨機擷取N個元素,產生新對象[5, 2]>>> random.sample(a,2)[5, 4]>>> random.sample(a,3)[3, 4, 1]>>> random.sample(a,5)[5, 1, 4, 2, 3]

2017-09-2412:27:23

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.