'''元组(tuple):元素不可被修改,不能被增加或删除'''
tu = (111, "alex", (11, 22), [33, 44], True, 33, 44,)
'''一般写元组的时候,推荐在最后加入'''
'''1、索引'''
v = tu[1]
print(v) # 结果:alex
'''2、切片'''
v = tu[0:3]
print(v) # 结果:(111, 'alex', (11, 22)
'''3、for循环(可迭代对象)'''
for item in tu:
print(item)
#结果:
# 111
# alex
# (11, 22)
# [33, 44]
# True
# 33
# 44
'''4、元组、字符串、列表间转换'''
s = "abcdefg"
li = ["opq", 123]
tu = ("efg", "abc")
v = tuple(s) # 字符串转元组
print(v) # 结果:('a', 'b', 'c', 'd', 'e', 'f', 'g')
v = tuple(li) # 列表转元组
print(v) # 结果:('opq', 123)
v = list(tu) # 元组转列表
print(v) # 结果:[123, 456, 'abc']
v = "".join(tu) # 元组转字符串(只支持元组全是字母)
print(v) # 结果:efgabc
'''5、元组是有序的'''
tu = (111, "alex", (11, 22), [(33, 44)], True, 33, 44,)
v1 = tu[2][0]
print(v1) # 结果:11
v2 = tu[3][0]
print(v2) # 结果:(33, 44)
'''6、元素的一级元素不可以修改、删除、增加'''
tu = (111, "alex", (11, 22), [(33, 44)], True, 33, 44,)
tu[3][0] = 520
print(tu) # 结果:(111, 'alex', (11, 22), [520], True, 33, 44)
'''7、获取指定元素在元组中出现的次数'''
tu = (111, 33, "alex", (11, 22), [(33, 44)], True, 33, 44,)
v = tu.count(33)
print(v) # 结果:2
tu.index(44)
print(v) # 结果:2