tup1 = ('Google', 'atguigu', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d"; # 不需要括号也可以
tup1[1]
tup1[1:5]
元组不允许更新
del tup[0]
表达式 结果 描述
len((1, 2, 3)) 3 计算元素个数
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) 连接
('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') 复制
3 in (1, 2, 3) True 元素是否存在
for x in (1, 2, 3): print (x,) 1 2 3 迭代(生成器)
方法 描述 实例
len(tuple) 计算元组元素个数 len(tuple1)
max(tuple) 返回元组中元素最大值 max(tuple2)
min(tuple) 返回元组中元素最小值 min(tuple2)
tuple(seq) 将列表转换为元组 list1= ['Google', 'Taobao', 'Runoob', 'Baidu']
tuple1=tuple(list1)
tuple1 = ('Google', 'Taobao', 'Runoob', 'Baidu')