Python - 資料型別



Python 資料型別

Python 資料型別實際上是類,定義的變數是它們的例項或物件。由於 Python 是動態型別的,因此變數的資料型別是在執行時根據分配的值確定的。

一般來說,資料型別用於定義變數的型別。它表示我們將在變數中儲存的資料型別,並確定可以在其上執行的操作。

每種程式語言都有自己對資料項的分類。使用這些資料型別,我們可以儲存不同型別的資料值。

Python 中的資料型別

Python 支援以下內建資料型別 -

data_types

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、floatcomplex -

  • 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() 函式

將整數轉換為八進位制字串。

廣告