Python变量解析

Python变量解析

变量:可变化数据对象的程序标示符,变量指向某个数据单元,变量的变化是指向的地址变化,类似指针,所以定义变量不需要声明数据类型,不能通过变量改变变量的值,只能重新通过变化指向

C语言中的变量

变量的内容可以变,地址是不能变化的,已经申明地址不能改变

例子

>>> 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.)

>>> id(x)
34124808
>>> print x
13
>>> id(x)
34124808
>>> x=12
>>> print x
12
>>> id(x)
34124820
>>> id(y)
34124808
>>> y = x
>>> id(x)
34124820
>>> id(y)
34124820
>>> y
12
>>> x=12
>>> y=12.5
>>> z='www.bling.com'
>>> x
12
>>> y
12.5
>>> z
'www.bling.com'
>>> type(x)
<type 'int'>
>>> type(y)
<type 'float'>
>>> type(z)
<type 'str'>
程序
x = 12
y = 13
print 'x = ',x,id(x)
print 'y = ',y,id(y)
x = y
print 'x = ',x,id(x)
print 'y = ',y,id(y)

x = 14
y = 15
print 'x = ',x,id(x)
print 'y = ',y,id(y)
输出结果
x =  12 6534164
y =  13 6534152
x =  13 6534152
y =  13 6534152
x =  14 6534140
y =  15 6534128
原文地址:https://www.cnblogs.com/yangml/p/3852234.html