元组与列表相似
但是元组与字符串一样,一旦创建就不能修改
元组的元素是int型
>>> tuple1 = ( 1,11,2,22,3,33) //与列表创建相似 >>> tuple1 (1, 11, 2, 22, 3, 33) >>> tuple1[1:3] //同样也能进行切片操作 (11, 2) >>> tuple1[:4] //切片 (1, 11, 2, 22) >>> tuple2 = tuple1[:] >>> tuple2 (1, 11, 2, 22, 3, 33)
>>> tuple3 = 1,2,3,4,5,6,7,8 //由此可见,括号并不重要 >>> type(tuple3) //逗号才重要 <class 'tuple'> >>> tuple4 = () //创建空元组时,括号才是必须的 >>> type(tuple4) <class 'tuple'> >>> 8 * (8,) //有了逗号,就是元组 (8, 8, 8, 8, 8, 8, 8, 8)
>>> yy = ("花蝴蝶" ,"小蜜蜂" ,"大脑胡", "大西几" ,"密密麻麻" ,"钢铁侠义") //创建一个元组 >>> yy = yy[:2] + ("美女黑寡妇",) + yy[2:] //利用切片进行更新 >>> yy ('花蝴蝶', '小蜜蜂', '美女黑寡妇', '大脑胡', '大西几', '密密麻麻', '钢铁侠义') //更新后的元组
>>> del(yy) //del内置函数删除元组 >>> yy Traceback (most recent call last): File "<pyshell#22>", line 1, in <module> yy NameError: name 'yy' is not defined