• Tuple(元组)与列表类似,不同之处在于元组的 元素不能修改

    • 元组 表示多个元素组成的序列

    • 元组 在 Python 开发中,有特定的应用场景

  • 用于存储 一串 信息,数据 之间使用 , 分隔

  • 元组用 () 定义

  • 元组的 索引 从 0 开始

    • 索引 就是数据在 元组 中的位置编号

info_tuple = ("zhangsan", 18, 1.75)print(info_tuple[0])

元组中 只包含一个元素 时,需要 在元素后面添加逗号

info_tuple = (50, )
info = ("zhangsan", 18)print("%s 的年龄是 %d" % info)

 

元组和列表之间的转换

使用 list 函数可以把元组转换成列表

 

list(元组)

使用 tuple 函数可以把列表转换成元组

tuple(列表)
# 元组转换成列表info_tuple = ("zhangsan", 18, 1.75)
temp = list(info_tuple)print(temp)# 列表转换成元组name_list = ["zhangsan","lisi"]
temp2 = tuple(name_list)print(temp2)