python的变量

本文详细介绍了Python中变量的动态类型分配、内存地址跟踪以及变量修改对内存位置的影响,通过实例展示了Python如何自动判断变量类型并追踪其内存地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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不用声明变量类型 根据赋值变化
>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值