變量類型-Numbers
-
del
num=1 mum=2 del num,mum print(mum)
-
Python math 模塊、cmath 模塊
Python 中數學運算常用的函數基本都在 math 模塊、cmath 模塊中。
Python math 模塊提供了許多對浮點數的數學運算函數。
Python cmath 模塊包含了一些用於複數運算的函數。
cmath 模塊的函數跟 math 模塊函數基本一致,區別是 cmath 模塊運算的是複數,math 模塊運算的是數學運算。
要使用 math 或 cmath 函數必須先導入:
import math #查看 math 查看包中的內容: dir(math)
數學函數
import math,cmath import random print(dir(math)) print(dir(cmath)) a=-12.57 b=1.9 print(math.fabs(a)) print(math.floor(a)) # 截斷小數位 print(math.trunc(a)) print(math.ceil(a)) print(math.radians(a)) rana=random.choice(range(10)) print(rana) ranb=random.randrange(0,500,5) print(ranb) ranc=random.random() print(ranc) arr=[1,2,3,4,5,6,7] #將序列的所有元素隨機排序 random.shuffle(arr) print(arr)
>>> range(1,5) # 代表從1到5(不包含5) [1, 2, 3, 4] >>> range(1,5,2) # 代表從1到5,間隔2(不包含5) [1, 3] >>> range(5) # 代表從0到5(不包含5) [0, 1, 2, 3, 4]
cmp(x, y) 函數在 python3.x 中不可用,可用以下函數替代:
s='a' n=98 print(ord(s)) # convert char to int print(chr(n)) # convert int to char
1、abs()是一個內置函數,而fabs()在math模塊中定義的。
2、fabs()函數只適用於float和integer類型,而 abs() 也適用於複數。 -
decimal(查看引用計數的個數)
import decimal decimal.getcontext().prec=3 m=2.0 n=3.0 d=decimal.Decimal(m)/decimal.Decimal(n) print(d)
-
分數計算
from fractions import Fraction f=Fraction(3,5) #3/5 f=f+1 #3/5+1=8/5 f=f+Fraction(1,2) print(f)
-
bool
x,y=3>2,3>5 print(x,y) # True False print(bool('')) #False
-
None
obj=None print(obj)
-
type():判斷數據類型