定义:与列表相比,只不过[]换成了(),主要用来读
l = ['a','b','c','d','e','f'] t = ('a','b','c','d','e','f') l[0] =1 print(l)
t[0]
print(t[0])
#t[0] =1
print(t)
#报错
按索引取值(只能读不能改)
t = ('a','b','c','d','e','f') print(t[0]) print(t[1])
切片([:])
t = ('a','b','c','d','e','f') print(t[0:4])#索引0到4范围的数值都要(不包括4) print(t[:4])#索引为4之后的数值都不要(包括4) print(t[4:])#索引为4之前的数值都不要(包括4)
长度(len)
t = ('a','b','c','d','e','f') print(len(t)) print(t.__len__())
成员运算(in,not,in)
t = ('a','b','c','d','e','f') if 'a' not in t: print('ok') if t: print('OK')
循环
t = ('a','b','c','d','e','f')
for i in t:
print(i) i=0 result = len(t) while i < result: print(t[i]) i += 1