tuple和list都用于存储多个数据内容,tuple使用小括号()进行定义
tuple中元素的值不可以被修改
vartuple = 1,'1' print(vartuple,type(vartuple)) vartuple = (1,) print(vartuple,type(vartuple)) vartuple = (1,'a') + (1,'a') print(vartuple) vartuple = (2,) * 3 print(vartuple) (1, '1') <class 'tuple'> (1,) <class 'tuple'> (1, 'a', 1, 'a') (2, 2, 2)
vart = (1,2,3,4,5.6) res = vart[::] print(res) res = vart[2::-1] print(res) res = vart[:4:2] print(res) (1, 2, 3, 4, 5.6) (3, 2, 1) (1, 3)
vart = 'a','b','c','a','b','c','a','b','c' res = len(vart) print(res) res = vart.count('a') print(res) res = vart.index('a',4,7) print(res) res = 'a' in vart print(res) 9 3 6 True