python 元祖

# 作用 :存多个值,对比列表来说,元祖不可变(可以当做字典的key,主要是用来读)
age = (11, 22, 33)
t = (1, 2, ['a', 'b'])
# t[2] = 2 # 输出结果:TypeError: 'tuple' object does not support item assignment
t[2][0] = 'keke'
print(t) # 输出结果:(1, 2, ['keke', 'b'])
# 按索引取值
print(t[2]) # 输出结果:['keke', 'b']
# 切片
print(t[0:2]) # 输出结果:(1, 2)
# 成员运算in 和not in,for
for i in age:
  print(i)

原文地址:https://www.cnblogs.com/keqing1108/p/13262952.html