Python-列表和元祖的区别

列表定义:

list = [1,3,"ceshi"]

print(list)   #输出:[1,3,"ceshi"]

print(list[2]) #输出:ceshi

元祖定义:

tuple=(1,3,"ceshi)

print(tuple)   #输出:(1,3,"ceshi)

print(tuple[2]) #输出:ceshi

区别:

list[2]=4  #修改列表成功

print(list[2])  #输出:4

tuple[2]=4  #修改元祖失败  TypeError: 'tuple' object does not support item assignment

print(tuple[2)]  #输出:ceshi

你可能感兴趣的:(Python,python)