数据类型比较
按存储模型分类
- 标量类型:数值,字符串
- 容器类型:列表,元组,字典
按更新模型分类:
- 可变类型:列表,字典
- 不可变类型:数字,字符串,元组
按访问模型分类
- 直接访问:数字
- 顺序访问:字符串。列表,元组
- 映射访问:字典
容器的意思是还可以有其他对象,数字字符串是标量类型,不能包含其它的元素了
>>> pystr = 'Python' >>> pystr[0] 'P' >>> pystr[0] = 'p' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment
>>> alist [1, 'tom', 2, 'alice', 3, [20, 30]] >>> blist = alist >>> alist[0] = 100 >>> alist [100, 'tom', 2, 'alice', 3, [20, 30]] >>> blist [100, 'tom', 2, 'alice', 3, [20, 30]] >>> clist = alist[:] >>> clist [100, 'tom', 2, 'alice', 3, [20, 30]] >>> clist.append('lee') >>> clist [100, 'tom', 2, 'alice', 3, [20, 30], 'lee'] >>> alist [100, 'tom', 2, 'alice', 3, [20, 30]]
字符串,元组,列表:可以切片