元組是不可變對象。python
t = tuple() t = () t = tuple(range(1,7,2)) t = (1,2,3,4,5,1) t = (1,) t = (1,)*5 t = (1,2,3)*6
支持下表索引code
正索引對象
負索引索引
tuple[index] t = (1,2,3) t[1] = 6 # 報錯 u = (1,[2,3,4],5) u[1][1] = 10 # 能夠改變
index(value) : 經過 value 從指定區間查詢字符串
count(value) : 返回元組中匹配 value 的次數io
len() : 返回元素個數class
namedtuple(typename, field_names, verbose=False, rename=False)import
命名元組,返回一個元組的子類,並定義了字段im
field_names : 能夠是以空格或逗號分隔的字符串命名
from collection import namedtuple Point = namedtuple('_Point', ['x','y']) p1 = Point(11,22) p1 # _Point(x=1,y=2) p1.x # 1 pa.y # 2 Student = namedtuple('Student','name age') tom = Student('tom', 20) jerry = Student('jerry', 18) tom.name