1.變量的屬性
在Python中,創建一個變量會給這個變量分配三種屬性:
id ,代表該變量在內存中的地址;
type,代表該變量的類型;
value,該變量的值;
1 x = 10 2 print(id(x)) 3 print(type(x)) 4 print(x) 5 6 --- 7 1689518832 8 <class 'int'> 9 10
2.變量的比較
- 身份的比較
is 關鍵字用來判斷變量的身份,即 id;
- 值的比較
== 用來判斷變量的值是否相等,即value;
1 C:\Users\Administrator>python 2 Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AM 3 D64)] on win32 4 Type "help", "copyright", "credits" or "license" for more information. 5 >>> 6 >>> x=10 7 >>> y=10 8 >>> 9 >>> id(x) 10 1711080176 11 >>> id(y) 12 1711080176 13 >>> 14 >>> x is y 15 True 16 >>> 17 >>> x == y 18 True 19 >>> 20 >>> x=300 21 >>> y=300 22 >>> 23 >>> id(x) 24 5525392 25 >>> id(y) 26 11496656 27 >>> 28 >>> x is y 29 False 30 >>> x == y 31 True 32 >>>
- 總結
- is 同,則value一定相等;
- value同,則is不一定相等;