元组类型:有序,不可变的,又叫只读列表 names = ("alex","jack","eric")
1.元组本身不可变,但如果元组中有可变元素,如列表,那么元组中的这个可变元素是可以改变的
2.相关操作 切片
>>> n2 = (1,2,3,4,5,6,7,8) >>> n2[2:5] (3, 4, 5) >>> n2 = (1,2,3,['a','b'],5) >>> n2[3] ['a', 'b'] >>> n2[3][1] 'b' >>> n2[3][1] = 'B' >>> n2 (1, 2, 3, ['a', 'B'], 5)
枚举enumerate 可用在列表和元组
#列表 >>> lis = ['Tony', 'Kelly', 'eric', 'rain', 'seven'] >>> for i,j in enumerate(lis): ... print(i,j) ... 0 Tony 1 Kelly 2 eric 3 rain 4 seven #元组 >>> names = [ (1,'alex'),(2,'reic'),(3,'rain')] >>> for i,j in enumerate(names): ... print(j[0],j[1]) ... 1 alex 2 reic 3 rain
3.用处:用于表示出不能更改的已有变量