1.变量的属性
在Python中,创建一个变量会给这个变量分配三种属性:
id ,代表该变量在内存中的地址;
type,代表该变量的类型;
value,该变量的值;
x = 10
print(id(x))
print(type(x))
print(x) ---
1689518832
<class 'int'>
10
2.变量的比较
- 身份的比较
is 关键字用来判断变量的身份,即 id;
- 值的比较
== 用来判断变量的值是否相等,即value;
C:\Users\Administrator>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> x=10
>>> y=10
>>>
>>> id(x)
1711080176
>>> id(y)
1711080176
>>>
>>> x is y
True
>>>
>>> x == y
True
>>>
>>> x=300
>>> y=300
>>>
>>> id(x)
5525392
>>> id(y)
11496656
>>>
>>> x is y
False
>>> x == y
True
>>>
- 总结
- is 同,则value一定相等;
- value同,则is不一定相等;