Python 基礎(chǔ)教程
Python Number 數(shù)據(jù)類型用于存儲數(shù)值。
數(shù)據(jù)類型是不允許改變的,這就意味著如果改變 Number 數(shù)據(jù)類型的值,將重新分配內(nèi)存空間。
以下實例在變量賦值時 Number 對象將被創(chuàng)建:
var1 = 1 var2 = 10
您也可以使用del語句刪除一些 Number 對象引用。
del語句的語法是:
del var1[,var2[,var3[....,varN]]]]
您可以通過使用del語句刪除單個或多個對象,例如:
del var del var_a, var_b
Python 支持四種不同的數(shù)值類型:
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 |
int(x [,base ]) 將x轉(zhuǎn)換為一個整數(shù) long(x [,base ]) 將x轉(zhuǎn)換為一個長整數(shù) float(x ) 將x轉(zhuǎn)換到一個浮點數(shù) complex(real [,imag ]) 創(chuàng)建一個復(fù)數(shù) str(x ) 將對象 x 轉(zhuǎn)換為字符串 repr(x ) 將對象 x 轉(zhuǎn)換為表達式字符串 eval(str ) 用來計算在字符串中的有效Python表達式,并返回一個對象 tuple(s ) 將序列 s 轉(zhuǎn)換為一個元組 list(s ) 將序列 s 轉(zhuǎn)換為一個列表 chr(x ) 將一個整數(shù)轉(zhuǎn)換為一個字符 unichr(x ) 將一個整數(shù)轉(zhuǎn)換為Unicode字符 ord(x ) 將一個字符轉(zhuǎn)換為它的整數(shù)值 hex(x ) 將一個整數(shù)轉(zhuǎn)換為一個十六進制字符串 oct(x ) 將一個整數(shù)轉(zhuǎn)換為一個八進制字符串
Python 中數(shù)學(xué)運算常用的函數(shù)基本都在 math 模塊、cmath 模塊中。
Python math 模塊提供了許多對浮點數(shù)的數(shù)學(xué)運算函數(shù)。
Python cmath 模塊包含了一些用于復(fù)數(shù)運算的函數(shù)。
cmath 模塊的函數(shù)跟 math 模塊函數(shù)基本一致,區(qū)別是 cmath 模塊運算的是復(fù)數(shù),math 模塊運算的是數(shù)學(xué)運算。
要使用 math 或 cmath 函數(shù)必須先導(dǎo)入:
import math
查看 math 查看包中的內(nèi)容:
>>> import math >>> dir(math) ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc'] >>>
下文會介紹各個函數(shù)的具體應(yīng)用。
查看 cmath 查看包中的內(nèi)容
>>> import cmath >>> dir(cmath) ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cos', 'cosh', 'e', 'exp', 'inf', 'infj', 'isclose', 'isfinite', 'isinf', 'isnan', 'log', 'log10', 'nan', 'nanj', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau'] >>>
實例
>>> import cmath >>> cmath.sqrt(-1) 1j >>> cmath.sqrt(9) (3+0j) >>> cmath.sin(1) (0.8414709848078965+0j) >>> cmath.log10(100) (2+0j) >>>
函數(shù) | 返回值 ( 描述 ) |
---|---|
abs(x) | 返回數(shù)字的絕對值,如abs(-10) 返回 10 |
ceil(x) | 返回數(shù)字的上入整數(shù),如math.ceil(4.1) 返回 5 |
cmp(x, y) | 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1 |
exp(x) | 返回e的x次冪(ex),如math.exp(1) 返回2.718281828459045 |
fabs(x) | 返回數(shù)字的絕對值,如math.fabs(-10) 返回10.0 |
floor(x) | 返回數(shù)字的下舍整數(shù),如math.floor(4.9)返回 4 |
log(x) | 如math.log(math.e)返回1.0,math.log(100,10)返回2.0 |
log10(x) | 返回以10為基數(shù)的x的對數(shù),如math.log10(100)返回 2.0 |
max(x1, x2,...) | 返回給定參數(shù)的最大值,參數(shù)可以為序列。 |
min(x1, x2,...) | 返回給定參數(shù)的最小值,參數(shù)可以為序列。 |
modf(x) | 返回x的整數(shù)部分與小數(shù)部分,兩部分的數(shù)值符號與x相同,整數(shù)部分以浮點型表示。 |
pow(x, y) | x**y 運算后的值。 |
round(x [,n]) | 返回浮點數(shù)x的四舍五入值,如給出n值,則代表舍入到小數(shù)點后的位數(shù)。 |
sqrt(x) | 返回數(shù)字x的平方根 |
隨機數(shù)可以用于數(shù)學(xué),游戲,安全等領(lǐng)域中,還經(jīng)常被嵌入到算法中,用以提高算法效率,并提高程序的安全性。
Python包含以下常用隨機數(shù)函數(shù):
函數(shù) | 描述 |
---|---|
choice(seq) | 從序列的元素中隨機挑選一個元素,比如random.choice(range(10)),從0到9中隨機挑選一個整數(shù)。 |
randrange ([start,] stop [,step]) | 從指定范圍內(nèi),按指定基數(shù)遞增的集合中獲取一個隨機數(shù),基數(shù)默認(rèn)值為 1 |
random() | 隨機生成下一個實數(shù),它在[0,1)范圍內(nèi)。 |
seed([x]) | 改變隨機數(shù)生成器的種子seed。如果你不了解其原理,你不必特別去設(shè)定seed,Python會幫你選擇seed。 |
shuffle(lst) | 將序列的所有元素隨機排序 |
uniform(x, y) | 隨機生成下一個實數(shù),它在[x,y]范圍內(nèi)。 |
Python包括以下三角函數(shù):
函數(shù) | 描述 |
---|---|
acos(x) | 返回x的反余弦弧度值。 |
asin(x) | 返回x的反正弦弧度值。 |
atan(x) | 返回x的反正切弧度值。 |
atan2(y, x) | 返回給定的 X 及 Y 坐標(biāo)值的反正切值。 |
cos(x) | 返回x的弧度的余弦值。 |
hypot(x, y) | 返回歐幾里德范數(shù) sqrt(x*x + y*y)。 |
sin(x) | 返回的x弧度的正弦值。 |
tan(x) | 返回x弧度的正切值。 |
degrees(x) | 將弧度轉(zhuǎn)換為角度,如degrees(math.pi/2) , 返回90.0 |
radians(x) | 將角度轉(zhuǎn)換為弧度 |
常量 | 描述 |
---|---|
pi | 數(shù)學(xué)常量 pi(圓周率,一般以π來表示) |
e | 數(shù)學(xué)常量 e,e即自然常數(shù)(自然常數(shù))。 |