元祖创建使用圆括号括起来,中间元素使用逗号隔开
如:tuple1 = (1,2,3,4)
tuple2 = () 空元祖
1 #!/usr/bin/env python 2 #-*-coding:utf-8-*- 3 4 5 6 tuple1 = (1,2,3,4,5,6,8,2,6) 7 tuple2 = tuple1[2:5] #截取第三位至第5位元素,包头不包尾 8 print(tuple2) 9 10 #len元祖长度 11 print('元祖长度为:%d'%len(tuple1)) 12 13 #max最大值,min最小值 14 print(max(tuple1)) 15 print(min(tuple1)) 16 17 ##元祖只有一个元素时,需要在元素后面加逗号 18 tuple3 = ('1',) 19 print(tuple3) 20 21 #index索引 22 print(tuple1.index(2)) #返回第三个元素 23 24 #count统计
25 print(tuple1.count(6))
tuple1 = ((1,2,3),('a','b','c')) #单层循环
for i,j,k in tuple1:
print(i,j,k)