元组(tuple)是不可更改的(list)
元组定义
temp=(1)
#此时temp的类型为int
temp=(1,)
#此时temp的类型为tuple
temp=1,
#此时temp的类型也为tuple
对元组进行修改就需要利用切片的特性
temp=(1,2,3,5)
temp = temp[:2]+(4,)+temp[2:]
本文介绍了Python中元组的基本概念及定义方法,展示了如何通过切片特性来修改元组中的元素,并给出了具体的代码示例。
temp=(1)
#此时temp的类型为int
temp=(1,)
#此时temp的类型为tuple
temp=1,
#此时temp的类型也为tuple
671
2820

被折叠的 条评论
为什么被折叠?