Python 基礎 - 數據類型

標準數據類型

Python3 中有六個標準的數據類型
1 Number(數字)
2 String(字符串)
3 List (列表)
4 Tuple (元組)
5 Sets (集合)
6 Dictionary(字典)

首先呢,我們先來介紹 數字

int(整數)

數字2 就是一個整數的例子,而長整數就是大一點的整數。

  • 在32位元的機器上,整數的位數為32位,取值的範圍為 -2**31 ~ 2**31-1-2147483648 ~ 2147483648
  • 在64位元的機器上,整數的位數為64位,取值的範圍為 -2**63 ~ 2**63-1-9223372036854775808L ~ 9223372036854775807L

在 Python2 裡,分整數長整數,請看下面就是一個64位系統所打印出來的

Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> type(2**32)
<type 'int'>
>>> type(2**62)
<type 'int'>
>>> type(2**63)
<type 'long'>
>>> type(2**64)
<type 'long'>

而在 Python3 裡,不管多大的數字,都只會顯示 整數,已經沒有 長整數的概念了,但其他語言仍然還是有 長整數的概念

Python 3.5.2 (default, Oct 11 2016, 05:05:28)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> type(2**32)
<class 'int'>
>>> type(2**64)
<class 'int'>
>>> type(2**128)
<class 'int'>
>>> type(2**256)
<class 'int'>
>>> type(2**512)
<class 'int'>
>>> type(2**1024)
<class 'int'>

請注意,如果是其他語言的話,存超過限制的話,還是會發生錯誤,而在Python裡,則會自動幫你做轉換

float(浮點數)

一般人認知的浮點數就是有小數點的數字(廣義),其實不完全正確的,只是浮點數的表示型態是小數,但小數不止包括浮點,有點類似於C語言中的Double類型,占8個字節(64bit),其中52bit表示為底,11bit表示指數,剩下的1bit表示符號。

3.2352.3E-4是浮點數的例子, E標記是表示 10的冪數。所以52.3E-4表示 52.3* 10^4

Python 3.5.2 (default, Oct 11 2016, 05:05:28)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 52.3E4
523000.0
>>> 52.3* 10**4
523000.0

complex(複數) → 不常用

(-5+4j)和(2.3-4.6j)都是複數的例子,其中 -5,4都為實數, j 為虛數,複數什麼情況會用到呢?大都是用在 量子力學空氣動力學物理相關的等,都是應用在工程相關方面的,一般我們會比較少用到。

boolean(布林值) 或 (布爾值) → 常用,必會

它是只有兩種值的原始類型,通常是TrueFalse 或是 01,請看範例圖

參考資料:

posted @ 2016-12-08 23:38  丹尼爾  阅读(187)  评论(0)    收藏  举报