python基础之元祖tuple

元祖是只读列表,不可哈希,可循环查询,可切片
*儿子不能改,孙子可更改--元祖里面单个元素不能更改---元祖内列表可更改
增:tu1+tu2
查:tu1[index] tu1[start_index:end_index] tu1.index(obj)
改:tu1[index][index] = obj --tuple中存在列表才可以更改
统计:len(tu1) tu1.count(obj)
1,循环:
ey:
tu1 = (1,2,'ee',3,[6,7,8,'ww'],4,'hh')
for tu in tu1:
print(tu)
2,查,切片:切片后仍返回元祖
ey:
tu1 = (1,2,'ee',3,[6,7,8,'ww'],4,'hh')
tu2 = tu1[2]
print(tu2)
result:'ee'
ey:
tu1 = (1,2,'ee',3,[6,7,8,'ww'],4,'hh')
tu2 = tu1[0:2]
print(tu2)
result:(1, 2, 'ee')
ey:
tu1 = (1,2,'ee',3,[6,7,8,'ww'],4,'hh')
tu2 = tu1[0:5:2]
print(tu2)
result:(1, 'ee', [6, 7, 8, 'ww'])
3,改
tu1 = (1,2,'ee',3,[6,7,8,'ww'],4,'hh')
tu1[4][2] = 'gai'
print(tu1)
result:(1, 2, 'ee', 3, [6, 7, 'gai', 'ww'], 4, 'hh')
4,增:追加
tu1 = (1,2,'ee',3,[6,7,8,'ww'],4,'hh')
tu2 = (11,23)
print(tu1+tu2)
result:(1, 2, 'ee', 3, [6, 7, 8, 'ww'], 4, 'hh', 11, 23)
5,计算长度
tu1= (1,2,3,4)
print(len(tu1))
result:4
6,统计
tu1 = (1,2,'ee',3,[6,7,8,'ww'],4,'hh')
print(tu1.count('ee'))
result:1
7,索引
ey:
tu1 = (1,2,'ee',3,[6,7,8,'ww'],4,'hh')
print(tu1.index('hh'))
result:6
上一篇:Python基础教程-List和Tuple


下一篇:Python基础(list与tuple)