# Python?數字
Python 數字數據類型用于存儲數值。
數據類型是不允許改變的,這就意味著如果改變數字數據類型得值,將重新分配內存空間。
以下實例在變量賦值時數字對象將被創建:
~~~
var1 = 1
var2 = 10
~~~
您也可以使用del語句刪除一些數字對象引用。
del語句的語法是:
~~~
del var1[,var2[,var3[....,varN]]]]
~~~
您可以通過使用del語句刪除單個或多個對象,例如:
~~~
del var
del var_a, var_b
~~~
Python 支持四種不同的數值類型:
* 整型(Int)?- 通常被稱為是整型或整數,是正或負整數,不帶小數點。
* 長整型(long integers)?- 無限大小的整數,整數最后是一個大寫或小寫的L。
* 浮點型(floating point real values)?- 浮點型由整數部分與小數部分組成,浮點型也可以使用科學計數法表示(2.5e2 = 2.5 x 102?= 250)
* 復數( (complex numbers))?- 復數的虛部以字母J 或 j結尾 。如:2+3i
| int | long | float | complex |
|--|--|--|--|
| 10 | 51924361L | 0.0 | 3.14j |
| 100 | -0x19323L | 15.20 | 45.j |
| -786 | 0122L | -21.9 | 9.322e-36j |
| 080 | 0xDEFABCECBDAECBFBAEl | 32.3+e18 | .876j |
| -0490 | 535633629843L | -90. | -.6545+0J |
| -0x260 | -052318172735L | -32.54e100 | 3e+26J |
| 0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
* 長整型也可以使用小寫"L",但是還是建議您使用大寫"L",避免與數字"1"混淆。Python使用"L"來顯示長整型。
* Python還支持復數,復數由實數部分和虛數部分構成,可以用a + bj,或者complex(a,b)表示, 復數的實部a和虛部b都是浮點型
## Python數字類型轉換
~~~
int(x [,base ]) 將x轉換為一個整數
long(x [,base ]) 將x轉換為一個長整數
float(x ) 將x轉換到一個浮點數
complex(real [,imag ]) 創建一個復數
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 ) 將一個整數轉換為一個八進制字符串
~~~
## Python數學函數
| 函數 | 返回值 ( 描述 ) |
|--|--|--|
| [abs(x)](http://www.runoob.com/python/func-number-abs.html) | 返回數字的絕對值,如abs(-10) 返回 10 |
| [ceil(x)](http://www.runoob.com/python/func-number-ceil.html) | 返回數字的上入整數,如math.ceil(4.1) 返回 5 |
| [cmp(x, y)](http://www.runoob.com/python/func-number-cmp.html) | 如果 x y 返回 1 |
| [exp(x)](http://www.runoob.com/python/func-number-exp.html) | 返回e的x次冪(ex),如math.exp(1) 返回2.718281828459045 |
| [fabs(x)](http://www.runoob.com/python/func-number-fabs.html) | 返回數字的絕對值,如math.fabs(-10) 返回10.0 |
| [floor(x)](http://www.runoob.com/python/func-number-floor.html) | 返回數字的下舍整數,如math.floor(4.9)返回 4 |
| [log(x)](http://www.runoob.com/python/func-number-log.html) | 如math.log(math.e)返回1.0,math.log(100,10)返回2.0 |
| [log10(x)](http://www.runoob.com/python/func-number-log10.html) | 返回以10為基數的x的對數,如math.log10(100)返回 2.0 |
| [max(x1, x2,...)](http://www.runoob.com/python/func-number-max.html) | 返回給定參數的最大值,參數可以為序列。 |
| [min(x1, x2,...)](http://www.runoob.com/python/func-number-min.html) | 返回給定參數的最小值,參數可以為序列。 |
| [modf(x)](http://www.runoob.com/python/func-number-modf.html) | 返回x的整數部分與小數部分,兩部分的數值符號與x相同,整數部分以浮點型表示。 |
| [pow(x, y)](http://www.runoob.com/python/func-number-pow.html) | x**y 運算后的值。 |
| [round(x [,n])](http://www.runoob.com/python/func-number-round.html) | 返回浮點數x的四舍五入值,如給出n值,則代表舍入到小數點后的位數。 |
| [sqrt(x)](http://www.runoob.com/python/func-number-sqrt.html) | 返回數字x的平方根,數字可以為負數,返回類型為實數,如math.sqrt(4)返回 2+0j |
## Python隨機數函數
隨機數可以用于數學,游戲,安全等領域中,還經常被嵌入到算法中,用以提高算法效率,并提高程序的安全性。
Python包含以下常用隨機數函數:
| 函數 | 描述 |
|--|--|
| [choice(seq)](http://www.runoob.com/python/func-number-choice.html) | 從序列的元素中隨機挑選一個元素,比如random.choice(range(10)),從0到9中隨機挑選一個整數。 |
| [randrange ([start,] stop [,step])](http://www.runoob.com/python/func-number-randrange.html) | 從指定范圍內,按指定基數遞增的集合中獲取一個隨機數,基數缺省值為1 |
| [random()](http://www.runoob.com/python/func-number-random.html) | 隨機生成下一個實數,它在[0,1)范圍內。 |
| [seed([x])](http://www.runoob.com/python/func-number-seed.html) | 改變隨機數生成器的種子seed。如果你不了解其原理,你不必特別去設定seed,Python會幫你選擇seed。 |
| [shuffle(lst)](http://www.runoob.com/python/func-number-shuffle.html) | 將序列的所有元素隨機排序 |
| [uniform(x, y)](http://www.runoob.com/python/func-number-uniform.html) | 隨機生成下一個實數,它在[x,y]范圍內。 |
## Python三角函數
Python包括以下三角函數:
| 函數 | 描述 |
|--|--|
| [acos(x)](http://www.runoob.com/python/func-number-acos.html) | 返回x的反余弦弧度值。 |
| [asin(x)](http://www.runoob.com/python/func-number-asin.html) | 返回x的反正弦弧度值。 | ? |
| [atan(x)](http://www.runoob.com/python/func-number-atan.html) | 返回x的反正切弧度值。 |
| [atan2(y, x)](http://www.runoob.com/python/func-number-atan2.html) | 返回給定的 X 及 Y 坐標值的反正切值。 |
| [cos(x)](http://www.runoob.com/python/func-number-cos.html) | 返回x的弧度的余弦值。 |
| [hypot(x, y)](http://www.runoob.com/python/func-number-hypot.html) | 返回歐幾里德范數 sqrt(x*x + y*y)。 |
| [sin(x)](http://www.runoob.com/python/func-number-sin.html) | 返回的x弧度的正弦值。 |
| [tan(x)](http://www.runoob.com/python/func-number-tan.html) | 返回x弧度的正切值。 |
| [degrees(x)](http://www.runoob.com/python/func-number-degrees.html) | 將弧度轉換為角度,如degrees(math.pi/2) , 返回90.0 |
| [radians(x)](http://www.runoob.com/python/func-number-radians.html) | 將角度轉換為弧度 |
* * *
## Python數學常量