Python基礎-python的資料型別
阿新 • • 發佈:2018-11-23
1.整型
In [14]: a = 1
In [15]: print(a)
1
In [16]: type(a) ##檢視資料型別
Out[16]: int
2.浮點型
In [17]: b = 1.2
In [18]: type(b)
Out[18]: float
3.字串型別
In [19]: c = westos ------------------------------------------------------------------------ NameError Traceback (most recent call last) <ipython-input-19-47a30bc3df16> in <module> ----> 1 c = westos NameError: name 'westos' is not defined In [20]: c = 'westos' In [21]: type(c) Out[21]: str
型別轉換
In [22]: float(a) Out[22]: 1.0 In [23]: int(b) Out[23]: 1 In [24]: float(c) ------------------------------------------------------------------------ ValueError Traceback (most recent call last) <ipython-input-24-36b90e4449a0> in <module> ----> 1 float(c) ValueError: could not convert string to float: 'westos' In [25]: str(a) Out[25]: '1' In [26]: str(b) Out[26]: '1.2' In [27]: d = '123456' In [28]: d Out[28]: '123456' In [29]: int(d) Out[29]: 123456 In [30]: float(d) Out[30]: 123456.0
如何刪除記憶體中的變數
In [31]: del a In [32]: a ------------------------------------------------------------------------ NameError Traceback (most recent call last) <ipython-input-32-3f786850e387> in <module> ----> 1 a NameError: name 'a' is not defined
bool型別
bool:只有兩個值(True False)
# 非0即真
In [34]: a = 1
In [35]: bool(a)
Out[35]: True
In [36]: bool(0)
Out[36]: False
In [37]: bool(1)
Out[37]: True
In [38]: bool(' ')
Out[38]: True
In [39]: bool('')
Out[39]: False
In [40]: bool(2412414)
Out[40]: True