【Python基础】可变数据类型和不可变数据类型

文章探讨了Python中数据类型的可变与不可变特性,解释了变量作为对象引用的概念。可变类型如字典、列表和集合在修改时不会改变内存地址,而不可变类型如整型、浮点型、字符串和元组在修改后会创建新的内存地址。通过示例代码展示了不同类型在内存中的行为差异。
摘要由CSDN通过智能技术生成

        python中可变数据类型有:字典dict, 列表list, 集合set;不可变数据类型有:整型int,浮点型float,字符串string和元组tuple。

        在python当中,一切皆对象。所以python变量也是对象。变量:对象的引用。变量存储的是对象的地址,变量的变指的是对象地址的变化,而不是地址指向内存空间的值。举例:如上图,定义变量temp_var = 12345,程序会在内存当中开辟一块空间存储整型数据12345,然后把该空间的地址存储到temp_var中,所以实际上temp_var是12345的引用。


    

        可变与不可变最本质的区别在于内存中存储的地址所指向的值是否可以被修改。可变数据类型:变量存储的内存地址所指向的值发生了改变,内存地址不发生变化;不可变数据类型:变量存储的内存地址所指向的值发生了改变,内存地址发生变化。

        字典,列表和集合数据类型验证,定义并初始化temp_var_one变量,并利用temp_var_one定义和初始化temp_var_two,两者引用相同,指向的是同一个内存空间。内存空间存储的是字典/列表/集合,字典/列表/集合值的变化不会引起变量存储的内存空间地址变化。所以字典/列表/集合就是可变数据类型。

# 字典数据类型验证
temp_var_one = {"a": 1, "b": 2, "c": 3}
temp_var_two = temp_var_one
print(id(temp_var_one))  # id为1202100539648
print(id(temp_var_two))  # id为1202100539648
temp_var_two.update({"d": 4})
print(id(temp_var_one))  # id为1202100539648
print(id(temp_var_two))  # id为1202100539648
# 列表数据类型验证
temp_var_one = [1, 2, 3]
temp_var_two = temp_var_one
print(id(temp_var_one))  # id为2323147827968
print(id(temp_var_two))  # id为2323147827968
temp_var_two.append(4)
print(id(temp_var_one))  # id为2323147827968
print(id(temp_var_two))  # id为2323147827968
# 集合数据类型验证
temp_var_one = {1, 2, 3}
temp_var_two = temp_var_one
print(id(temp_var_one))  # id为1724139533248
print(id(temp_var_two))  # id为1724139533248
temp_var_two.add(4)
print(id(temp_var_one))  # id为1724139533248
print(id(temp_var_two))  # id为1724139533248

        整型,浮点型,字符串,元组数据类型验证,定义并初始化temp_var_one变量,并利用temp_var_one定义和初始化temp_var_two,两者引用相同,指向的是同一个内存空间。内存空间存储的是整型/浮点型/字符串/元组,整型/浮点型/字符串/元组的变化引起变量存储的内存空间地址变化。所以整型/浮点型/字符串/元组就是可变数据类型。

# 整型数据类型验证
temp_var_one = 3
temp_var_two = temp_var_one
print(id(temp_var_one))  # id为2623080130928
print(id(temp_var_two))  # id为2623080130928
temp_var_two = 2
print(id(temp_var_one))  # id为2623080130928
print(id(temp_var_two))  # id为2623080130896
# 浮点型数据类型验证
temp_var_one = 3.14
temp_var_two = temp_var_one
print(id(temp_var_one))  # id为2016210395920
print(id(temp_var_two))  # id为2016210395920
temp_var_two = 3.14159
print(id(temp_var_one))  # id为2016210395920
print(id(temp_var_two))  # id为2016209169264
# 浮点型数据类型验证
temp_var_one = "olivier"
temp_var_two = temp_var_one
print(id(temp_var_one))  # id为1912564360624
print(id(temp_var_two))  # id为1912564360624
temp_var_two = "olivier_"
print(id(temp_var_one))  # id为1912564360624
print(id(temp_var_two))  # id为1912564662128
# 元组数据类型验证
temp_var_one = (1, 2, 3)
temp_var_two = temp_var_one
print(id(temp_var_one))  # id为2376541232448
print(id(temp_var_two))  # id为2376541232448
temp_var_two += (4,)
print(id(temp_var_one))  # id为2376541232448
print(id(temp_var_two))  # id为2376541265808

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值