Python_數字類型

#Python基礎類型

  1. Python Number 數字
    Python Number 用於存儲數值
    數字類型不允許改變,如果修改number數據類型的值將重新分配內存空間;
    刪除多個number對象時 可以用逗號分隔 刪除多個對象;
    創建數字類型的值,改變其值並查看內存地址:

>>> a = 4
>>> id(a)
4297644512
>>> a =5
>>> id(a)
4297644544
>>> 

2.Python 支持四中不同的數字類型
(1). 整型(int)整數,是正整數或者負整數
(2). 長整型(long integers)無限大小的整數,證書最後使用大寫火小寫的L表示
(3). 浮點型(floating point values)浮點型構成由整數部分和小數部分構成
(4). 複數(complex numbers)複數由實數和虛數部分構成 可以使用 a+bj,或 compler(a,b) 表示,其中 a,b部分都是浮點型
注: 長整型的取值範圍:
python2.7版本中長整型的取值範圍爲-263-1次方至263次方
python3中沒有long類型,使用int表示長整型

#!/usr/bin/env3 python3
>>> 2**63-1
9223372036854775807
>>> L = 9223372036854775807
>>> type(L)
<class 'int'>
>>> L2 = 9223372036854775808
>>> type(L2)
<class 'int'>
>>> L3 = -2**62
>>> type(L3)
<class 'int'>
>>> L4 = -2**62-1
>>> type(L4)
<class 'int'>
>>> 
#!/usr/bin/env2 python2
>>> 2**63-1
9223372036854775807L
>>> l = 2**63-1
>>> type(l)
<type 'long'>
>>> l2 = 9223372036854775807
>>> type(l2)
<type 'int'>
>>> l3 = -2**62
>>> type(l3)
<type 'int'>
>>> l4 = -2**62-1
>>> type(l4)
<type 'int'>
>>> 

創建複數:

>>> 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
>>> type(nu1)
<class 'int'>
>>> nu2 = float(nu1)
>>> type(nu2)
<class 'float'>
>>> nu3 = complex(nu2)
>>> type(nu3)
<class 'complex'>
>>> print(nu1,nu2,nu3)
89 89.0 (89+0j)
>>> nu4 = int(nu3) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert complex to int # 複數不可轉整數類型 也不可轉浮點型
>>> nu4 = int(nu2)
>>> type(nu4)
<class 'int'>
>>> nu5 = str(nu4)
>>> nu6 = str(nu3)
>>> print(nu6,nu5,type(nu6),type(nu5))
(89+0j) 89 <class 'str'> <class 'str'>
>>> 

str(x) 將對象 x 轉換爲字符串
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

>>> 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.5969913200153554
>>> random.random()
0.28790440880472545

>>> random.uniform(10,22) #在10-22間生成浮點數
10.089421761832224
>>> random.uniform(10,22)
12.82308974038482

>>> random.randint(0,3) # 在指定範圍內生成整數
2
>>> random.randint(0,3)
3
>>> random.randint(0,3)
1

>>> random.randrange(1,9,1) #從指定範圍內按指定基數遞增獲取隨機數
4
>>> random.randrange(1,9,1)
6
>>> random.randrange(1,9,1)
8
>>> random.randrange(1,9,1)
5

>>> random.choice([1,2,3,4,5]) ##從序列元素中隨機獲取元素,只能是有序類型
4
>>> random.choice([1,2,3,4,5])
1
>>> random.choice([1,2,3,4,5])
2
>>> random.choice('abcdefg')
'g'
>>> random.choice('abcdefg')
'e'

>>> a = [1,2,3,4,5]
>>> random.shuffle(a)  #將一個列表元素打亂
>>> a
[5, 1, 4, 2, 3]
>>> random.shuffle(a)
>>> a
[3, 1, 4, 5, 2]

>>> random.sample(a,3,) #從指定序列中隨機獲取N個元素,生成新對象
[5, 2, 4]
>>> random.sample(a,3,)
[3, 1, 4]
>>> random.sample(a,3,)
[1, 4, 2]
>>> random.sample(a,4)
[5, 4, 2, 3]
>>> random.sample(a,4)
[3, 5, 4, 1]
>>> 

轉自:https://www.cnblogs.com/zhangxinqi/p/7583816.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章