
- Python 基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python 與 C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境搭建
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策
- Python - if 語句
- Python - if else
- Python - 巢狀 if
- Python - match-case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - while 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式 & 模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅限關鍵字引數
- Python - 位置引數
- Python - 僅限位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表元素
- Python - 修改列表元素
- Python - 新增列表元素
- Python - 刪除列表元素
- Python - 迴圈遍歷列表
- Python - 列表推導式
- Python - 列表排序
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組元素
- Python - 更新元組
- Python - 解包元組
- Python - 迴圈遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合元素
- Python - 新增集合元素
- Python - 刪除集合元素
- Python - 迴圈遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典元素
- Python - 修改字典元素
- Python - 新增字典元素
- Python - 刪除字典元素
- Python - 字典檢視物件
- Python - 迴圈遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列元素
- Python - 新增陣列元素
- Python - 刪除陣列元素
- Python - 迴圈遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類 & 物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝器類
- Python - 列舉
- Python - 反射
- Python 錯誤 & 異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 塊
- Python - try-finally 塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀 try 塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 執行緒連線
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路程式設計
- Python - 網路程式設計
- Python - 套接字程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python - 日期 & 時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUIs
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - 猴子補丁
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
Python - 資料型別
Python 資料型別
Python 資料型別實際上是類,定義的變數是它們的例項或物件。由於 Python 是動態型別的,因此變數的資料型別是在執行時根據分配的值確定的。
一般來說,資料型別用於定義變數的型別。它表示我們將在變數中儲存的資料型別,並確定可以在其上執行的操作。
每種程式語言都有自己對資料項的分類。使用這些資料型別,我們可以儲存不同型別的資料值。
Python 中的資料型別
Python 支援以下內建資料型別 -
-
數值資料型別
- int (整數)
- float (浮點數)
- complex (複數)
- 字串資料型別
-
序列資料型別
- list (列表)
- tuple (元組)
- range (範圍)
-
二進位制資料型別
- bytes (位元組)
- bytearray (位元組陣列)
- memoryview (記憶體檢視)
- 字典資料型別
-
集合資料型別
- set (集合)
- frozenset (凍結集合)
- 布林資料型別
- None 型別

1. Python 數值資料型別
Python 數值資料型別儲存數值。當您為其分配值時,會建立數字物件。例如 -
var1 = 1 # int data type var2 = True # bool data type var3 = 10.023 # float data type var4 = 10+3j # complex data type
Python 支援四種不同的數值型別,並且它們各自在 Python 庫中都有內建類,分別稱為 int、bool、float 和 complex -
- int (帶符號整數)
- float (浮點實數)
- complex (複數)
複數由兩個部分組成 - **實部** 和 **虛部**。它們由 '+' 或 '-' 符號分隔。虛部字尾為 'j',表示虛數單位。-1 的平方根($\sqrt{-1}$)被定義為虛數。Python 中的複數表示為 x+yj,其中 x 是實部,y 是虛部。因此,5+6j 是一個複數。
>>> type(5+6j) <class 'complex'>
以下是一些數字的例子:
int (整數) | 浮點數 | complex (複數) |
---|---|---|
10 | 0.0 | 3.14j |
0O777 | 15.20 | 45.j |
-786 | -21.9 | 9.322e-36j |
080 | 32.3+e18 | .876j |
0x17 | -90. | -.6545+0J |
-0x260 | -32.54e100 | 3e+26J |
0x69 | 70.2-E12 | 4.53e-7j |
數值資料型別的示例
以下是一個示例,展示了整數、浮點數和複數的使用方法
# integer variable. a=100 print("The type of variable having value", a, " is ", type(a)) # float variable. c=20.345 print("The type of variable having value", c, " is ", type(c)) # complex variable. d=10+3j print("The type of variable having value", d, " is ", type(d))
2. Python 字串資料型別
Python 字串是由一個或多個 Unicode 字元組成的序列,用單引號、雙引號或三引號(也稱為反引號)括起來。Python 字串是不可變的,這意味著當您對字串執行操作時,您始終會生成一個相同型別的新字串物件,而不是修改現有的字串。
只要字元序列相同,使用單引號、雙引號或三引號都沒有關係。因此,以下字串表示方式是等價的。
>>> 'TutorialsPoint' 'TutorialsPoint' >>> "TutorialsPoint" 'TutorialsPoint' >>> '''TutorialsPoint''' 'TutorialsPoint'
Python 中的字串是 **str** 類的物件。可以使用 **type()** 函式進行驗證。
>>> type("Welcome To TutorialsPoint") <class 'str'>
字串是一種非數值資料型別。顯然,我們不能對其執行算術運算。但是,可以執行諸如 **切片** 和 **連線** 等操作。Python 的 str 類定義了許多用於字串處理的有用方法。可以使用切片運算子([ ] 和 [:])獲取字串的子集,索引從字串開頭的 0 開始,到結尾的 -1 結束。
加號 (+) 是 Python 中的字串連線運算子,星號 (*) 是重複運算子。
字串資料型別的示例
str = 'Hello World!' print (str) # Prints complete string print (str[0]) # Prints first character of the string print (str[2:5]) # Prints characters starting from 3rd to 5th print (str[2:]) # Prints string starting from 3rd character print (str * 2) # Prints string two times print (str + "TEST") # Prints concatenated string
這將產生以下結果:
Hello World! H llo llo World! Hello World!Hello World! Hello World!TEST
3. Python 序列資料型別
序列是一種集合資料型別。它是專案的有序集合。序列中的專案具有從 0 開始的位置索引。它在概念上類似於 C 或 C++ 中的陣列。Python 中定義了以下三種序列資料型別。
- 列表資料型別
- 元組資料型別
- 範圍資料型別
Python 序列是有界且可迭代的 - 每當我們在 Python 中說可迭代時,它都意味著序列資料型別(例如,列表)。
(a) Python 列表資料型別
Python 列表是最通用的複合資料型別。Python 列表包含用逗號分隔並在方括號 ([ ]) 內括起來的專案。在某種程度上,Python 列表類似於 C 中的陣列。它們之間的一個區別是,屬於 Python 列表的所有專案可以是不同的資料型別,而 C 陣列只能儲存與特定資料型別相關的元素。
>>> [2023, "Python", 3.11, 5+6j, 1.23E-4]
Python 中的列表是 **list** 類的物件。我們可以用 type() 函式檢查它。
>>> type([2023, "Python", 3.11, 5+6j, 1.23E-4]) <class 'list'>
如前所述,列表中的專案可以是任何資料型別。這意味著列表物件也可以是另一個列表中的專案。在這種情況下,它成為巢狀列表。
>>> [['One', 'Two', 'Three'], [1,2,3], [1.0, 2.0, 3.0]]
列表可以包含簡單的數字、字串、元組、字典、集合或使用者定義類的物件。
可以使用切片運算子([ ] 和 [:])訪問 Python 列表中儲存的值,索引從列表開頭的 0 開始,一直到結尾的 -1。加號 (+) 是列表連線運算子,星號 (*) 是重複運算子。
列表資料型別的示例
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tinylist = [123, 'john'] print (list) # Prints complete list print (list[0]) # Prints first element of the list print (list[1:3]) # Prints elements starting from 2nd till 3rd print (list[2:]) # Prints elements starting from 3rd element print (tinylist * 2) # Prints list two times print (list + tinylist) # Prints concatenated lists
這將產生以下結果:
['abcd', 786, 2.23, 'john', 70.2] abcd [786, 2.23] [2.23, 'john', 70.2] [123, 'john', 123, 'john'] ['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
(b) Python 元組資料型別
Python 元組是另一種類似於列表的序列資料型別。Python 元組由用逗號分隔的若干個值組成。但是,與列表不同,元組用括號 (...) 括起來。
元組也是一個序列,因此元組中的每個專案都有一個索引,指向它在集合中的位置。索引從 0 開始。
>>> (2023, "Python", 3.11, 5+6j, 1.23E-4)
在 Python 中,元組是 **tuple** 類的物件。我們可以用 type() 函式檢查它。
>>> type((2023, "Python", 3.11, 5+6j, 1.23E-4)) <class 'tuple'>
與列表一樣,元組中的專案也可以是列表、元組本身或任何其他 Python 類的物件。
>>> (['One', 'Two', 'Three'], 1,2.0,3, (1.0, 2.0, 3.0))
要形成元組,括號的使用是可選的。用逗號分隔的資料項,沒有任何封閉符號,預設情況下被視為元組。
>>> 2023, "Python", 3.11, 5+6j, 1.23E-4 (2023, 'Python', 3.11, (5+6j), 0.000123)
元組資料型別的示例
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john') print (tuple) # Prints the complete tuple print (tuple[0]) # Prints first element of the tuple print (tuple[1:3]) # Prints elements of the tuple starting from 2nd till 3rd print (tuple[2:]) # Prints elements of the tuple starting from 3rd element print (tinytuple * 2) # Prints the contents of the tuple twice print (tuple + tinytuple) # Prints concatenated tuples
這將產生以下結果:
('abcd', 786, 2.23, 'john', 70.2) abcd (786, 2.23) (2.23, 'john', 70.2) (123, 'john', 123, 'john') ('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
列表和元組之間的主要區別在於:列表用括號 ( [ ] ) 括起來,其元素和大小可以更改,即列表是可變的,而元組用括號 ( ( ) ) 括起來,並且不能更新(不可變)。元組可以被認為是 **只讀** 列表。
以下程式碼在元組中是無效的,因為我們嘗試更新元組,這是不允許的。類似的情況也可能發生在列表中:
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tuple[2] = 1000 # Invalid syntax with tuple list[2] = 1000 # Valid syntax with list
(c) Python 範圍資料型別
Python 範圍是數字的不可變序列,通常用於迭代特定數量的專案。
它由 **Range** 類表示。此類的建構函式接受一個從 0 開始並遞增到 1 直到到達指定數字的數字序列。以下是函式的語法:
range(start, stop, step)
以下是使用引數的描述:
**start**:指定起始位置的整數(可選,預設值:0)
**stop**:指定結束位置的整數(必填)
**step**:指定增量的整數(可選,預設值:1)
範圍資料型別的示例
以下程式使用 for 迴圈列印從 0 到 4 的數字:
for i in range(5): print(i)
這將產生以下結果:
0 1 2 3 4
現在讓我們修改上面的程式,列印從 2 開始而不是從 0 開始的數字:
for i in range(2, 5): print(i)
這將產生以下結果:
2 3 4
再次,讓我們修改程式,列印從 1 開始但增量為 2 而不是 1 的數字
for i in range(1, 5, 2): print(i)
這將產生以下結果:
1 3
4. Python 二進位制資料型別
Python 中的二進位制資料型別是一種將資料表示為一系列二進位制數字(0 和 1)的方式。它就像計算機用來有效儲存和處理資訊的特殊語言。
這種型別的資料通常用於處理檔案、影像或任何可以使用僅兩個可能值表示的內容。因此,二進位制序列資料型別不使用常規數字或字母,而是使用 0 和 1 的組合來表示資訊。
Python 提供三種不同的方式來表示二進位制資料。它們如下:
- bytes (位元組)
- bytearray (位元組陣列)
- memoryview (記憶體檢視)
讓我們分別討論這些資料型別:
(a) Python 位元組資料型別
Python 中的位元組資料型別表示一系列位元組。每個位元組都是 0 到 255 之間的整數值。它通常用於儲存二進位制資料,例如影像、檔案或網路資料包。
我們可以使用內建的 **bytes()** 函式 或在數字序列前加上 **b** 來在 Python 中建立位元組。
位元組資料型別的示例
在以下示例中,我們使用內建的 bytes() 函式顯式指定表示 ASCII 值的數字序列:
# Using bytes() function to create bytes b1 = bytes([65, 66, 67, 68, 69]) print(b1)
獲得的結果如下:
b'ABCDE'
在這裡,我們在字串前使用 "b" 字首來自動建立一個位元組物件:
# Using prefix 'b' to create bytes b2 = b'Hello' print(b2)
以下是上述程式碼的輸出:
b'Hello'
(b) Python 位元組陣列資料型別
Python 中的位元組陣列資料型別與位元組資料型別非常相似,但有一個關鍵區別:它是可變的,這意味著您可以在建立後修改其中儲存的值。
您可以使用多種方法建立位元組陣列,包括傳遞表示位元組值的整數的可迭代物件、對字串進行編碼或轉換現有的位元組或位元組陣列物件。為此,我們使用 bytearray() 函式。
位元組陣列資料型別的示例
在下面的示例中,我們透過傳遞表示位元組值的整數的可迭代物件來建立一個位元組陣列:
# Creating a bytearray from an iterable of integers value = bytearray([72, 101, 108, 108, 111]) print(value)
獲得的輸出如下所示:
bytearray(b'Hello')
現在,我們透過使用 "UTF-8" 編碼對字串進行編碼來建立一個位元組陣列:
# Creating a bytearray by encoding a string val = bytearray("Hello", 'utf-8') print(val)
產生的結果如下:
bytearray(b'Hello')
(c) Python 記憶檢視資料型別
在 Python 中,記憶檢視是一個內建物件,它提供對原始物件記憶體的檢視,通常是支援緩衝區協議的物件,例如位元組陣列 (bytearray) 和位元組 (bytes)。它允許您訪問原始物件的基礎資料,而無需複製它,從而為大型資料集提供高效的記憶體訪問。
您可以使用多種方法建立記憶檢視。這些方法包括使用 memoryview() 建構函式、切片位元組或位元組陣列物件、從陣列物件中提取或使用內建函式(如 open())從檔案中讀取。
記憶檢視資料型別的示例
在給定的示例中,我們透過將支援的物件傳遞給 memoryview() 建構函式來直接建立一個記憶檢視物件。支援的物件通常包括位元組陣列 (bytearray)、位元組 (bytes) 和其他支援緩衝區協議的物件:
data = bytearray(b'Hello, world!') view = memoryview(data) print(view)
以下是上述程式碼的輸出:
<memory at 0x00000186FFAA3580>
如果您有陣列物件,您可以使用緩衝區介面建立記憶檢視,如下所示:
import array arr = array.array('i', [1, 2, 3, 4, 5]) view = memoryview(arr) print(view)
獲得的輸出如下所示:
<memory at 0x0000017963CD3580>
您還可以透過切片位元組或位元組陣列物件來建立記憶檢視:
data = b'Hello, world!' # Creating a view of the last part of the data view = memoryview(data[7:]) print(view)
獲得的結果如下:
<memory at 0x00000200D9AA3580>
5. Python 字典資料型別
Python 字典是一種雜湊表型別。字典鍵幾乎可以是任何 Python 型別,但通常是數字或字串。另一方面,值可以是任何任意 Python 物件。
Python 字典類似於 Perl 中的關聯陣列或雜湊,由 **鍵:值** 對組成。這些對用逗號分隔,並放在花括號 {} 內。為了建立鍵和值之間的對映,在兩者之間放置分號 ':' 符號。
>>> {1:'one', 2:'two', 3:'three'}
在 Python 中,字典是內建 **dict** 類的物件。我們可以用 type() 函式檢查它。
>>> type({1:'one', 2:'two', 3:'three'}) <class 'dict'>
字典用花括號 ({ }) 括起來,可以使用方括號 ([] ) 為值賦值和訪問值。
字典資料型別的示例
dict = {} dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name': 'john','code':6734, 'dept': 'sales'} print (dict['one']) # Prints value for 'one' key print (dict[2]) # Prints value for 2 key print (tinydict) # Prints complete dictionary print (tinydict.keys()) # Prints all the keys print (tinydict.values()) # Prints all the values
這將產生以下結果:
This is one This is two {'dept': 'sales', 'code': 6734, 'name': 'john'} ['dept', 'code', 'name'] ['sales', 6734, 'john']
Python 的字典不是序列。它是一個專案的集合,但每個專案(鍵:值對)不像字串、列表或元組那樣由位置索引標識。因此,無法對字典執行切片操作。字典是可變物件,因此可以使用 dict 類中定義的相應功能執行新增、修改或刪除操作。這些操作將在後續章節中解釋。
6. Python 集合資料型別
集合是 Python 對數學中定義的集合的實現。Python 中的集合是一種集合,但不像字串、列表或元組那樣是索引或有序集合。一個物件不能在集合中出現多次,而在列表和元組中,同一個物件可以出現多次。
集合中用逗號分隔的專案放在花括號 {} 內。集合中的專案可以是不同的資料型別。
>>> {2023, "Python", 3.11, 5+6j, 1.23E-4} {(5+6j), 3.11, 0.000123, 'Python', 2023}
請注意,集合中的專案可能不遵循輸入的順序。Python 會最佳化專案的排列,以便根據數學中定義的集合執行操作。
Python 的集合(Set)是內建 **set** 類的物件,可以使用 type() 函式進行檢查。
>>> type({2023, "Python", 3.11, 5+6j, 1.23E-4}) <class 'set'>
集合只能儲存 **不可變** 物件,例如數字(int、float、complex 或 bool)、字串或元組。如果嘗試將列表或字典放入集合中,Python 會引發 **TypeError** 錯誤。
>>> {['One', 'Two', 'Three'], 1,2,3, (1.0, 2.0, 3.0)} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list'
**雜湊** 是計算機科學中的一種機制,它能夠更快地在計算機記憶體中搜索物件。**只有不可變物件是可雜湊的**。
即使集合不允許可變項,集合本身也是可變的。因此,可以使用內建 set 類中的方法對集合物件執行新增/刪除/更新操作。Python 還有一組運算子來執行集合操作。這些方法和運算子將在後面的章節中進行解釋。
集合示例
set1 = {123, 452, 5, 6} set2 = {'Java', 'Python', 'JavaScript'} print(set1) print(set2)
這將生成以下輸出:
{123, 452, 5, 6} {'Python', 'JavaScript', 'Java'}
7. Python 布林資料型別
Python **布林** 型別是內建資料型別之一,表示兩個值之一:**True** 或 **False**。Python 的 **bool()** 函式允許您評估任何表示式的值,並根據表示式返回 True 或 False。
布林數只有兩個可能的值,分別由關鍵字 **True** 和 **False** 表示。它們分別對應於整數 1 和 0。
>>> type (True) <class 'bool'> >>> type(False) <class 'bool'>
布林資料型別示例
以下程式列印布林變數 a 和 b 的值:
a = True # display the value of a print(a) # display the data type of a print(type(a))
這將產生以下結果:
true <class 'bool'>
以下是另一個程式,它評估表示式並列印返回值:
# Returns false as a is not equal to b a = 2 b = 4 print(bool(a==b)) # Following also prints the same print(a==b) # Returns False as a is None a = None print(bool(a)) # Returns false as a is an empty sequence a = () print(bool(a)) # Returns false as a is 0 a = 0.0 print(bool(a)) # Returns false as a is 10 a = 10 print(bool(a))
這將產生以下結果:
False False False False False True
8. Python None 型別
Python 的 None 型別由 "nonetype" 表示。它是一個自身資料型別的物件。nonetype 表示空值型別或值的缺失。
None 型別示例
在以下示例中,我們將 None 賦值給變數 x 並列印其型別,該型別將為 nonetyoe:
# Declaring a variable # And, assigning a Null value (None) x = None # Printing its value and type print("x = ", x) print("type of x = ", type(x))
這將產生以下結果:
x = None type of x = <class 'NoneType'>
獲取資料型別
要獲取 Python 中的資料型別,可以使用 type() 函式。type() 是一個內建函式,它返回給定物件的類。
示例
在以下示例中,我們獲取值和變數的型別:
# Getting type of values print(type(123)) print(type(9.99)) # Getting type of variables a = 10 b = 2.12 c = "Hello" d = (10, 20, 30) e = [10, 20, 30] print(type(a)) print(type(b)) print(type(c)) print(type(d)) print(type(e))
這將產生以下結果:
<class 'int'> <class 'float'> <class 'int'> <class 'float'> <class 'str'> <class 'tuple'> <class 'list'>
設定資料型別
在 Python 中,在宣告變數或物件時,不需要設定資料型別。資料型別會根據分配的值自動設定。
示例
以下示例演示瞭如何根據給定值設定變數的資料型別:
# Declaring a variable # And, assigning an integer value x = 10 # Printing its value and type print("x = ", x) print("type of x = ", type(x)) # Now, assigning string value to # the same variable x = "Hello World!" # Printing its value and type print("x = ", x) print("type of x = ", type(x))
這將產生以下結果:
x = 10 type of x = <class 'int'> x = Hello World! type of x = <class 'str'>
基本資料型別和非基本資料型別
以上解釋的資料型別也可以分為基本資料型別和非基本資料型別。
1. 基本型別
基本資料型別是用於建立複雜資料型別(有時稱為複雜資料結構)的基本資料型別。主要有四種基本資料型別,分別是:
- 整數
- 浮點數
- 布林值,以及
- 字串
2. 非基本型別
非基本資料型別儲存值或值的集合。主要有四種非基本型別,分別是:
- 列表
- 元組
- 字典,以及
- 集合
Python 資料型別轉換
有時,您可能需要在內建資料型別之間執行轉換。要將資料在不同的 Python 資料型別之間進行轉換,只需使用型別名稱作為函式即可。
閱讀: Python 型別轉換
示例
以下是一個示例,它分別將不同的值轉換為整數、浮點數和字串值:
print("Conversion to integer data type") a = int(1) # a will be 1 b = int(2.2) # b will be 2 c = int("3.3") # c will be 3 print (a) print (b) print (c) print("Conversion to floating point number") a = float(1) # a will be 1.0 b = float(2.2) # b will be 2.2 c = float("3.3") # c will be 3.3 print (a) print (b) print (c) print("Conversion to string") a = str(1) # a will be "1" b = str(2.2) # b will be "2.2" c = str("3.3") # c will be "3.3" print (a) print (b) print (c)
這將產生以下結果:
Conversion to integer data type 1 2 3 Conversion to floating point number 1.0 2.2 3.3 Conversion to string 1 2.2 3.3
資料型別轉換函式
有幾個 內建函式 可以執行從一種資料型別到另一種資料型別的轉換。這些函式返回一個表示轉換值的新物件。
序號 | 函式及描述 |
---|---|
1 | Python int() 函式
將 x 轉換為整數。如果 x 是字串,則 base 指定基數。 |
2 | Python long() 函式
將 x 轉換為長整數。如果 x 是字串,則 base 指定基數。此函式已被棄用。 |
3 | Python float() 函式
將 x 轉換為浮點數。 |
4 | Python complex() 函式
建立一個複數。 |
5 | Python str() 函式
將物件 x 轉換為字串表示形式。 |
6 | Python repr() 函式
將物件 x 轉換為表示式字串。 |
7 | Python eval() 函式
評估字串並返回一個物件。 |
8 | Python tuple() 函式
將 s 轉換為元組。 |
9 | Python list() 函式
將 s 轉換為列表。 |
10 | Python set() 函式
將 s 轉換為集合。 |
11 | Python dict() 函式
建立一個字典。d 必須是 (鍵,值) 元組的序列。 |
12 | Python frozenset() 函式
將 s 轉換為凍結集。 |
13 | Python chr() 函式
將整數轉換為字元。 |
14 | Python unichr() 函式
將整數轉換為 Unicode 字元。 |
15 | Python ord() 函式
將單個字元轉換為其整數值。 |
16 | Python hex() 函式
將整數轉換為十六進位制字串。 |
17 | Python oct() 函式
將整數轉換為八進位制字串。 |