1. 程式人生 > >學習筆記之Python資料型別-Number

學習筆記之Python資料型別-Number

Number

Python 2 中的數字,支援int, float, bool, complex, long這些資料型別,其中long(即長整型)很少用。

int

>>> v1 = 10
>>> type(v1)   #type()方法可以獲取變數的資料型別
<type 'int'>
>>> v2 = 11111
>>> type(v2)
<type 'long'>

float

float即浮點數,也就是小數。包括使用科學計數法表示的小數。一般對於很大或者很小的浮點數,用科學計數法表示,10用e表示,例如1.23*10^9, 用1.23e9表示。
>>> v3 = 3.14
>>> type(v3)
<type 'float'>
>>> v4 = 1.23e9
>>> v4
1230000000.0
>>> type(v4)
<type 'float'>

Bool

布林值,只有兩種可能,True, False. 只要非零/非空/非none,即為FALSE。

Complex

複數,複數的虛部用j表示,小寫大寫均可。可通過x.real返回實部,x.imag返回虛部,例如:
>>> a =  3 + 4j
>>> a
(3+4j)
>>> a.real
3.0
>>> a.imag
4.0