元组的数据不能被修改,修改会直接报错,元组的元素不可变,但可以包含可变元素。tuple写在小括号内,元素之间用逗号隔开。只有一个元素时,末尾需要逗号,不然会被识别为运算式,导致tuple可能被识别为int。元组的删除会直接删除元组的定义,删除后再去输出会发现报错说没有定义。
元组的增删
tup0 = () print("tup0 = ()类型:", type(tup0)) tup00 = (0) print("tup00 = (0)类型:", type(tup00)) tup000 = (0,) print("tup000 = (0,)类型:", type(tup000)) tup1 = (2, 4, 6) tup2 = (1, 3, 5) tup = tup1 + tup2 print("tup[0: 7]:", tup[0: 7])#左闭右开 tup += ("abc", "斟茶冰冰yozi") print('tup + ("abc", "斟茶冰冰yozi"):', tup) #del tup 会直接删除整个元组的定义