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-1 Out[1]: 9223372036854775807L In [2]: lo1 = 9223372036854775807 In [3]: type(lo1) Out[3]: int In [4]: lo2 = 9223372036854775808 In [5]: type(lo2) Out[5]: long In [6]: log8 = -2**62 In [7]: type(log8) Out[8]: int In [9]: log8 = -2**63-1 In [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 #模塊定義的常量e 2.718281828459045 >>> math.pi #模塊定義的常量pi 3.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) #默認為0 1
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™