列表是可以修改的,而不可变的列表被称为元组 。
** 可以通过set()筛选重复的元素
set(newList)
定义
* 用圆括号来标识。定义元组后,使用索引来访问其元素,就像访问列表元素一样
修改变量
* 不能修改元组的元素,但可以给存储元组的变量赋值
修改元素:
dimensions = (200, 50)
dimensions[0] = 250
Traceback (most recent call last):
File "dimensions.py", line 3, in <module>
dimensions[0] = 250
TypeError: 'tuple' object does not support item assignment
修改变量:
dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)
Original dimensions:
200
50
Modified dimensions:
400
100