python的变量与c语言java不同,不需要提前定义变量的格式。python会根据赋值自动判断变量的格式。同时,变量的改变只代表变量指向地址的改变。
事例如下:
>>> x = 12
>>> print x
12
>>> x = 13
>>> print x
13
>>> help(id)
Help on built-in function id in module __builtin__:
id(...)
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)
>>> print x
13
>>> id(x)
20816480
>>> x = 13
>>> print x
13
>>> id(x)
20816480
>>> x = 12
>>> id(x)
20816492
>>> y = 12
>>> id(y)
20816492
>>> y = 13
>>> x
12
>>> y = x
>>> y
12
>>> id(x)
20816492
>>> id (y)
20816492
#python变量时指针 值相同 说明指向同一个单元
>>> x = 12
>>> y = 12.5
>>> z = "hello world~"
>>> type(x)
<type 'int'>
>>> type(y)
<type 'float'>
>>> type(z)
<type 'str'>
#python不用声明变量类型 根据赋值变化
>>>