Python中的變量
變量(Variable
)在計算機編程中於關聯的標識符配對的內存存儲位置
,在使用時含相關類型的值,這個值可以修改。 這裏的標識符就是變量的名字。
在Python當變量被使用的時候,首先在內存裏將會產生兩個動作
- 第一:開闢一個指定地址的空間
- 第二:賦予指定的變量值
在Python語言中,變量在指定的同時,必須強制賦予初始值,否則解釋器報錯。
Python變量值類型
在所有編程語言中的變量值都是分類型的,Python語言的變量值的類型在賦值後才被隱性確定。例如A=0 那麼0就是整數類型的值; A = “OK”,那麼OK就是字符串類型的值,A = True,那麼True則是波爾類型的值。
Python語言的基本變量類型包括:字符串(String)、數字(Numeric)、列表(List)、字典(Dict)、元組(Tuple)
五大類。
- name = “yankerp” ## String
- price = 100 ## numeric
- names = [“yankerp”, “list”, “wangwu”] ## list
- name_data = {name1 : “yankerp”, name2 : “wangwu”} ## dict
- name = (“yankerp”, “zhangsan”) ## tuple
引用
在Python當變量被使用的時候,首先在內存裏將會產生兩個動作
- 第一:開闢一個指定地址的空間
- 第二:賦予指定的變量值
>>> a = 100
>>> b = a
>>> c = 1314
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:492206240
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:492206240
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:3649264
----------------------------------------------------------
A:492206240
B:492206240
C:3649264
輸出結果A與B的內存地址是一個地址,當使用變量A和B時,使用的是同一個對象。
C爲新開闢的一塊新的內存地址並賦予指定的變量值。
>>> a = 100
>>> b = a
>>> c = b
>>> d = c
>>> e = d
>>> f = e
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:492206240
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:492206240
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:492206240
>>> print(f"This is the ID of D:{id(d)}")
This is the ID of D:492206240
>>> print(f"This is the ID of E:{id(e)}")
This is the ID of E:492206240
>>> print(f"This is the ID of F:{id(f)}")
This is the ID of F:492206240
----------------------------------------------
492206240
不同變量名不同變量值
>>> a = 1000
>>> b = 2000
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:3649264
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:3646576
>>> a = 1000
>>> b = a
>>> c = b
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:3646576
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:3646576
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:3646576
>>> a = a + 100
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:31166256
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:3646576
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:3646576
A 爲不可變類型對象,A = 1000 首先會在內存中查找有沒有1000這個值的內存位置,如果沒有那麼將會在內存中劃分一塊內存空間區域,並賦予指定的變量值與變量名進行關聯。 這時候1000的值對應內存地址爲:31166256
,當B = A時相當於B = 31166256
C = B 時相當於 C = 31166256
所以 A B C 都爲同一個對象。
列表變量類型可變對象
>>> a = [1, 2, 3]
>>> b = a
>>> c = b
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:36806152
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:36806152
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:36806152
>>> a.append(4)
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:36806152
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:36806152
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:36806152
>>> print(a)
[1, 2, 3, 4]
>>> print(b)
[1, 2, 3, 4]
>>> print(c)
[1, 2, 3, 4]
對於列表可變對象,A = [1, 2, 3]當A值修改時,系統會自動在A–>對應內存地址中擴容,所以不需要修改地址。當A append後 B 和 C 的內存地址不變,隨着A的增加而增加。
不可變對象
>>> a = 1
>>> b = a
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:492203072
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:492203072
>>> a += 1
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:492203104
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:492203072
可變類型及不可變類型:
可變類型:
- 列表:List
- 字典:dict
不可變類型:
- 數值類型:int, long, bool, float
- 字符串:str
- 元組:tuple
緩存數據機制
在文章上面提到:A 爲不可變類型對象,A = 1000 首先會在內存中查找有沒有1000
這個值的內存位置,如果沒有那麼將會在內存中劃分一塊內存空間區域,並賦予指定的變量值與變量名進行關聯。
>>> name1 = "yankerp"
>>> name2 = "yankerp"
>>> print(f"This is the ID of name1:{id(name1)}")
This is the ID of name1:38801456
>>> print(f"This is the ID of name2:{id(name2)}")
This is the ID of name2:38801456
當name1 = “Yankerp”時,內存ID:38801456 當name2 = "Yankerp"相當於:name2 = 38801456 用的爲同一個地址。
>>> print(f"This is the ID of name3:{id(name3)}")
This is the ID of name3:38801456
它們都是使用的同一塊地址
緩存數據機制對於可變類型
>>> name1 = [1,2,3]
>>> name2 = [1,2,3]
>>> print(f"This is the ID of name1:{id(name1)}")
This is the ID of name1:38772232
>>> print(f"This is the ID of name2:{id(name2)}")
This is the ID of name2:38784264