happyhacking

在python中,数据类型分为可变数据类型和不可变数据类型,不可变数据类型包括string,int,float,tuple,可变数据类型包括list,dict。

所谓的可变与不可变,举例如下:

>>> a = "test"
>>> print a[0]
t
>>> a[0] = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: \'str\' object does not support item assignment
>>> a = [1, 2, 34]
>>> print  a[0]
1
>>> a[0] = 4
>>> a
[4, 2, 34]
>>>


因为字符串是不可变对象,所以使用字符串的内置方法replace()等并非改变原字符串,而是新创建了一个新的字符串。

>>> a = "hello world"
>>> a.replace("world", "python")
\'hello python\'
>>> a
\'hello world\'
>>> a = a.replace("world", "python")
>>> a
\'hello python\'
>>>




分类:

技术点:

相关文章: