Python 中哪些資料型別是不可變的?
在本文中,我們將解釋 Python 中的不可變資料型別。
Python 將所有內容都視為物件。當我們例項化一個物件時,會為其分配一個唯一的 ID。我們不能修改物件的型別,但可以更改其值。例如,如果我們將變數 a 設定為列表,則不能將其更改為元組/字典,但可以修改該列表中的條目。
在Python中,有兩種型別的物件。一方面,有些物件可以更改其內部狀態(物件內部的資料/內容),即可以使用預定義函式或方法對其進行更改;另一方面,有些物件不能更改其內部狀態(物件內部的資料/內容)。
在 Python 中,有 2 種類型的物件
可變物件
不可變物件。
總結一下區別,可變物件可以更改其狀態或內容,但不可變物件不能。
不可變資料型別
不可變資料型別是指在建立後不能被修改或更改的物件(例如,透過新增新元素、刪除元素或替換元素)。Python 的不可變資料型別有
整數 (Int)
浮點數 (Float)
元組 (Tuple)
複數 (Complex)
字串 (String)
凍結集 (Frozenset) [注意:集合的不可變版本]
位元組 (Bytes)
當您對不可變物件進行更改時,將更新它們在初始化期間儲存的記憶體。
整數 (Int)
在計算機程式設計中,整數是正數、負數或0的整數(...,-1、0、1,...)。整數也稱為int。
與其他程式語言一樣,在四位數或更多位數的數字中不應使用逗號,因此在程式碼中將 1,000 作為 1000。
int - 帶符號整數
long - 用於表示較高值的長整數
示例
以簡單的方式列印整數,如
print(10) print(-5) # performing math operations with integers sum_int = 20+30 print(sum_int)
輸出
執行上述程式後,將生成以下輸出:
10 -5 50
在 Python 應用程式中,整數可以以各種方式使用,並且隨著您對該語言的瞭解,您將有無數機會使用整數並瞭解有關此資料型別的更多資訊。
由於int資料型別是不可變的,因此我們不能修改或更新它。
如前所述,不可變物件在更新時會更改其記憶體地址。
示例
# input number inputNumber = 5 # printing the memory address of the int variable print('Before updating, memory address = ', id(inputNumber)) # here we are updating an int object by assigning a new value to it. # changing the same input number variable by assigning a new value to it inputNumber = 10 # printing the memory address of the int variable after updating using the id() function print('After updating, memory address = ', id(inputNumber))
輸出
執行上述程式後,將生成以下輸出:
Before updating, memory address = 140103023683616 After updating, memory address = 140103023683776
浮點數 (Float)
float 資料型別用於表示帶小數點的值。
float 類表示此值。它是以浮點形式表示的實數。使用小數點來指定它。為了表示科學計數法,可以在後面附加字元e或E,後跟正數或負數。
同樣,float資料型別在更新後也會更改其記憶體地址值。
示例
# printing the type of input numbers number_1 = 4.5 print("Type of 4.5:", type(number_1)) number_2 = -0.05 print("Type of -0.05:", type(number_2)) number_3 = 3.04e20 print("number_3: ", number_3) print("Type of 3.04e20:", type(number_3))
輸出
執行上述程式後,將生成以下輸出:
Type of 4.5: <class 'float'> Type of -0.05: <class 'float'> number_3: 3.04e+20 Type of 3.04e20: <class 'float'>
使用尾隨小數點來確保變數是浮點數而不是整數,即使它是整數。請注意帶小數點的整數之間的區別:
示例
# printing the type of input numbers number_1 = 10 print("Type of 10:", type(number_1)) number_2 = 8. print("Type of 8. :", type(number_2))
輸出
執行上述程式後,將生成以下輸出:
Type of 10: <class 'int'> Type of 8. : <class 'float'>
元組 (Tuple)
元組同樣是不可變的,這意味著我們不能在其中追加或更新任何內容。在修改元組中的任何專案時,我們會收到錯誤“元組”物件不支援專案賦值。
示例
# input tuple inputTuple = ('hello', 'tutorialspoint', 'python') # printing the original input tuple print("The original input tuple:", inputTuple) # modifying the tuple element at index 2 using indexing inputTuple[2]= 'welcome' # printing input tuple after modifying print("Input tuple after modification:", inputTuple)
輸出
執行上述程式後,將生成以下輸出:
The original input tuple: ('hello', 'tutorialspoint', 'python')
Traceback (most recent call last):
File "main.py", line 6, in <module>
inputTuple[2]= 'welcome'
TypeError: 'tuple' object does not support item assignment
我們使用了一些隨機值的元組,然後用另一個隨機值更改了列表的元素,因為元組是不可變的資料型別,所以元素的值不會更改,並且會丟擲異常。
複數 (Complex)
複數表示為實部+虛部<j>。
示例
inputNumber = 1+2j print(type(inputNumber))
輸出
<class 'complex'>
字串 (String)
因為字串是不可變的,所以我們不能在其中追加或更新任何內容。在修改字串的任何部分時,我們遇到了表明字串本質上不可變的問題。
示例
# input string inputString = 'Welcome to tutorialspoint python' # changing the string character at index 1 inputString[1] = 'a' print(inputString)
輸出
Traceback (most recent call last):
File "main.py", line 4, in <module>
inputString[1] = 'a'
TypeError: 'str' object does not support item assignment
凍結集
凍結集和集合之間的唯一區別在於它是不可變的。這意味著在建立集合後,您無法更改它。
示例
frozen_set = frozenset(("hello", "tutoraialspoint", "python")) # adding new item to frozen_set # we cannot modify it as the frozenset is immutable frozen_set.add("codes")
輸出
Traceback (most recent call last):
File "main.py", line 4, in <module>
frozen_set.add("codes")
AttributeError: 'frozenset' object has no attribute 'add'
結論
在本文中,我們學習了 Python 不可變資料型別。我們還透過示例演示了它們是如何不可變的。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP