iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 9
0
自我挑戰組

Python 學習筆記系列 第 9

Day9 Python 基礎 - 數據類型

標準數據類型

Python3 中有六個標準的數據類型:

  • Number(數字)
  • String(字符串)
  • List (列表)
  • Tuple (元組)
  • Sets (集合)
  • 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,請看範例圖

參考資料:

加油…頭過身就過了!!!


上一篇
Day8 Python 基礎 - pyc 是什麼
下一篇
Day10 Python 基礎 - bytes數據類型
系列文
Python 學習筆記29
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
huangsb
iT邦好手 1 級 ‧ 2016-12-11 22:59:01

真是可惜,2的31次方居然無法正常顯示,只能怪it幫幫忙的 Markdown 支援只做了一半,居然不支援 inline html。

我好弱 iT邦新手 4 級 ‧ 2016-12-12 01:40:15 檢舉

@huangsb 請問你是怎麼打的?

huangsb iT邦好手 1 級 ‧ 2016-12-12 11:40:16 檢舉

真正的 markdown 有支援 inline html,像 2 的 31 次方,應該用 sup tag 將 32 包住(例如:<sup>32</sup>),因為 markdown 語法沒有對應到 sup tag,可惜的是這裡的所有 html tag 都會被濾掉,因此也無法顯示。

我好弱 iT邦新手 4 級 ‧ 2016-12-12 11:44:31 檢舉

哦哦! 這邊的 markdown 好像只有簡單的功能而已,上次我要弄個表格也不行,科科

1
huangsb
iT邦好手 1 級 ‧ 2016-12-12 14:14:33

簡單的表格是可以的,範例如下:

範例一:

First Header  | Second Header
------------- | -------------
Content Cell  | Content Cell
Content Cell  | Content Cell

結果:

First Header Second Header
Content Cell Content Cell
Content Cell Content Cell

範例二:

| Right | Center | Left  |
| ----: | :----: | :---- |
| 10    | 10     | 10    |
| 1000  | 1000   | 1000  |

結果:

Right Center Left
10 10 10
1000 1000 1000
我好弱 iT邦新手 4 級 ‧ 2016-12-12 23:15:19 檢舉

感謝大大

我要留言

立即登入留言