python 元组 Tuple

python yekong 1548℃

tuple 与 list 类似,tuple不能修改,tuple写在小括号内,元素间用逗号分隔。
元组的元素不可变,但是可以包含可变对象入list.
定义一个只有1个元素的tuple,必须加逗号。


tup2 = (50,)

tupl = ('abc', 'def', 200, 21111)
print(tupl[1])
print(tupl[-1]) #访问最后一个元素
print(tupl[1:5]) #左闭右开,进行切片


tupl = ('abc', 'def', 200, 21111)
tupl2 = ('abc', 'def', 200, 21111)
tup = tupl + tupl2
print(tup)


tupl = ('abc', 'def', 200, 21111)
print(tupl)
del tupl
print('删除后')
print(tupl)

不能修改

喜欢 (1)