元组(tuple)
作者:互联网
# tuple
# 元组元素不能被修改,不能被增加或者删除
# 一般写元组的时候,推荐最后加入“,”
tu = ("yoki", '22', "male","yuqi",)
# 索引取值
v = tu[0]
print(v)
# 切片
v = tu[0:2]
print(v)
# 可以被for循环,可迭代对象
for item in tu:
print(item)
# 转换
# 字符串转换成元组
s = "yoki"
v = tuple(s)
print(v)
# 列表转换成列表
li = ["yoki", "22", "yuqi", "male"]
v2 = tuple(li)
print(v2)
#元组转换成列表
tu = ("yoki", '22', "male","yuqi",)
v3 = list(tu)
print(v3)
v = "_".join(tu)
print(v) # yoki_22_male_yuqi
# 元组有序
tu = ("yoki", "yuqi", ["yoki", "22", "yuqi", "male", ("yoki", '22', "male","yuqi",)], )
print(tu[2][4][1][1])
###############!!!!!!!!!!!!!
# # 元组的一级元素不能被修改
tu = ("yoki", "yuqi", ["yoki", "yuqi", ("yoki", '22', "male","yuqi",)], )
tu[2][1] = "yq"
print(tu)
tu = ("yoki", '22', "male","yuqi",)
print(tu.index("22"))
print(tu.count("yoki"))
标签:22,tuple,tu,yuqi,元组,print,yoki,male 来源: https://www.cnblogs.com/yuqi-yoki/p/16491075.html