元祖

print("====================元祖的创建开始=======================")
# 直接创建
t = (2,)
print(type(t))
t = ("abc", [2, 3], 3)
print(type(t))
t = "abc", [2, 3], 3
print(type(t))
# 列表转换
list = ["abc", [2, 3], 3]
t = tuple(list)
print(t)

print("====================元祖的创建结束=======================")
print("====================元祖常用操作开始=======================")
# 元祖不支持 增删改
t = (1, 2, 3)
# 通过索引获取
print(t[0])
# 切片操作(不改变原来的)
print(t[::-1])
print(t)
# 遍历
for ele in t:
    print(ele, end="\t")
print()

for index in range(len(t)):
    print(index, t[index])
print("====================元祖常用操作结束=======================")
print("====================元祖的拼接和拆包开始=======================")
# 拼接
print(t * 2)
print((1, 2, 3) + (4, 5, 6))
# 拆包
a, b = (10, 20)
print(a, b)

print("====================元祖的拼接和拆包结束=======================")

你可能感兴趣的:(元祖)