# 元组[有序不变序列](不可修改)
def tuples():
# --- 元组 ---
# 创建 (类似于列表的数据存储方式,但是不能修改)
tuples = ("柳岩", 21, "女")
tuples = tuple(["a", "b", "c"]) # 将 列表 转为 元组 (注:将字典转为元组会损失数据)
tuples = tuple() # 空元组,不知何用
# × 添加 × 修改 × 删除 (注意:不许修改)
# 获取
tuples = ("柳岩", 21, "女")
strs = tuples[0] # 获取指定索引数据, '柳岩'
strs = tuples[-1] # 倒数获取数据 (1开始), '女'
tup4 = tuples[1:] # 截取, <class 'tuple'>: (21, '女')
tup5 = tuples[0:2] # <class 'tuple'>: ('柳岩', 21)
tup2 = tuples + (1, 2, 3) # 合并元组(原元组不变), <class 'tuple'>: ('柳岩', 21, '女', 1, 2, 3)
tup3 = tuples * 3 # 复制(3份)合并元组, <class 'tuple'>: ('柳岩', 21, '女', '柳岩', 21, '女', '柳岩', 21, '女')
strs = max((1, 3, 6, 2.5)) # 最大值, 6
strs = min((1, 3, 6, 2.5))
# 统计
length = len(tuples) # 元组长度
count = tuples.count(21) # 该元素的数量
# 查找
index = tuples.index(21) # 该元素出现的索引,找不到抛异常
index = tuples.index(21, 1) # 从 1-末尾
index = tuples.index(21, 0, 2) # 从 0-2
# 遍历
for i in tuples:
print("tuple:%s"%i)
# 判断
boolean = 21 in tuples
boolean = 21 not in tuples