# 元组(tuple)
#主要操作:1.in和not in;2.比较、串联、切片和索引;3.min()和max();4.可以放不同类型的元素
# 创建元组(以下两种创建方法一样)
tuple1 = (1, 2, 'a')
print(tuple1) # output: (1, 2, 'a')
tuple2 = 1, 2, 'a' # output: (1, 2, 'a')
print(tuple2)
# 利用索引访问元组
print(tuple1[0]) # output:1
print(tuple1[-1]) # output:a
# 元组元素不可更改
try:
tuple2[1] = 0
except:
print('Cannot change value of tuple by index')
# output:Cannot change value of tuple by index
# 元组是不可更改的,但是其元素是可变对象
tuple3 = (1, [1, 2])
print(tuple3) # output:(1, [1, 2])
tuple3[1].append(3)
print(tuple3) # output: (1, [1, 2, 3])
# 元组一旦创建便不可扩展,但是两个元组可以串联在一起
tuple2_3 = tuple2 + tuple3
print(tuple2_3) #output: (1, 2, 'a', 1, [1, 2, 3])
#对元组进行切片
a=(0,1,2,3,4,5,6,7,8,9)
print(a[1:]) #output: (1, 2, 3, 4, 5, 6, 7, 8, 9)
print(a[1:3]) #output: (1, 2)
print(a[1:6:2]) #output: (1, 3, 5, 7, 9)
print(a[:-1])#output: (0, 1, 2, 3, 4, 5, 6, 7, 8)
#求元组的最大最小值
print('max: ',max(a),'min: ',min(a)) #output:max: 9 min: 0
#包含于或非包含于
if(10 not in a):
print('10 is not in tuple a')
else:
print('10 is in tuple a')
# output: 10 is not in tuple a