*元组,元素不可被修改,不能被增加或者删除
tuple
tu = (11,22,33,44)
tu.count(22),获取指定元素在元组中出现的次数
tu.index(22),索引22在元组中位置(左到右第一个出现的位置)
####################################### 深灰魔法 #######################################
1. 书写格式
一般写元组的时候,推荐在最后加入 ,
元素不可被修改,不能被增加或者删除
2. 索引
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
v = tu[0]
print(v)
111
3. 切片
v = tu[0:2]
print(v)
(111, 'alex')
4. 可以被for循环,可迭代对象
for item in tu:
print(item)
111
alex
(11, 22)
[(33, 44)]
True
33
44
5. 转换
s = "asdfasdf0"
li = ["asdf","asdfasdf"]
tu = ("asdf","asdf")
v = tuple(s) # 字符串转换为元组
print(v)
('a', 's', 'd', 'f', 'a', 's', 'd', 'f', '0')
v = tuple(li)
print(v) # 列表转换为元组
('asdf', 'asdfasdf')
v = list(tu)
print(v) # 元组转换为列表
['asdf', 'asdf']
v = "_".join(tu)
print(v) # 元组转换为字符串
asdf_asdf
li = ["asdf","asdfasdf"]
li.extend((11,22,33,))
print(li)
['asdf', 'asdfasdf', 11, 22, 33]
6.元组的一级元素不可修改/删除/增加
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
v = tu[3][0][1]
print(v)
v=tu[3]
print(v)
tu[3][0] = 567
print(tu)
44
[(33, 44)]
(111, 'alex', (11, 22), [567], True, 33, 44)