元组
是对列表的二次加工,书写格式为括号(),里面放元素
元组的一级元素不可被修改,且不能被增加和删除
一般写元组的时候,推荐在最后加入逗号, 能加则加
创建元组
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
索引
tu = (111,"alex",(11,22),[(33,44)],True,33,44,) # 索引找值 v = tu[1] print(v)
切片
tu = (111,"alex",(11,22),[(33,44)],True,33,44,) # 切片,区间内找值 v = tu[0:3] print(v)
可以被for循环(可迭代对象)
tu = (111,"alex",(11,22),[(33,44)],True,33,44,) for item in tu: print(item) 111 alex (11,22) [(33,44)] True 44
有序的,排列的时候有顺序
- 转换(字符串,列表,元祖之间的转换)
s = "asd" li = ["asdf",123] tu = (123,321,"asdf") v = tuple(s) print(v) v1 = tuple(li) print(v1) v2 = list(tu) print(v2) # 输出结果 ('a', 's', 'd') ('asdf', 123) [123, 321, 'asdf']
元组运算符
与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。