元组就是一个特殊的"列表"
t = (1, 2, 3, '1', (1,)) print(t, type(t))
t = (1, 2, 3, '1', [1, 2, 3, 4]) t[0] = 11111 # 不能改 t[1] = 22222 # 不能改 print(t) t[-1][1] = 22222 print(t)
print(tuple([1, 2, 34])) print(tuple('hello')) print(tuple({'a1': 123, "k2": 222})) # 把字典的键存为元组
# 4.内置方法 # 优先掌握的操作: # 1.按索引取值(正向取+反向取)只能取 # t = ('aaa', 'bbb', 111) # print(t[0]) # print(t[-1]) # 2.切片 # print(t[0:3]) # print(t[2:0:-1]) # 3.长度 # t = ('aaa', 'bbb', 111) # print(len(t)) # 4.成员运算 in 和not in # t = ('aaa', 'bbb', 111) # print('aaa' in t) # 5.循环 # t = ('aaa', 'bbb', 111) # for i in t: # print(i) # 内置方法: # t = (1, 2, 3, 3, 3, 3, 3) # print(t.index(1)) # 找到并返回起始索引,找不到报错 # print(t.count(3)) # 计数