元组:固定长度、不可变的对象序列。 创建方式:1、用逗号间隔 2、用tuple函数转换为元组
a = 4, 5, 6, 7
b = (4, 5, 6), 7
c = tuple([[1, 2, 4], 'cool', True])
print(a)
print(b)
print(c)
# 运行结果
(4, 5, 6, 7)
((4, 5, 6), 7)
([1, 2, 4], 'cool', True)
Process finished with exit code 0
元组的元素可以用括号[ ]的形式获取。***注意***元组创建后,各个位置的对象无法修改,但是对象本身如果可变,那么可以修改,比如列表可修改。
print(b[0])
c[0].append([1, 1])
print(c)
c[1] = '123'
print(c)
# 运行结果
(4, 5, 6)
([1, 2, 4, [1, 1]], 'cool', True)
Traceback (most recent call last):
File "F:\Pythonworkspace\002\test_shan.py", line 25, in <module>
c[1] = '123'
TypeError: 'tuple' object does not support item assignment
Process finished with exit code 1