元祖和列表类似,但是元祖中的元素不能修改
例子:
tuple = (34, 56, 'hi')
print(tuple[2])
# 尝试修改
del tuple[2]
print(tuple)
运行结果:(会报错)
hi
Traceback (most recent call last):
File "D:/PythonDemo/Demo1/src/Test2.py", line 4, in <module>
tuple[2] = 23
TypeError: 'tuple' object does not support item assignment
网友评论