数据结构之元组(Python)
数据结构之元组
python将不能修改的值称为不变的,而不可变的列表称为元组,以小括号()表示
如果需要存储的一组值在程序的整个生命周期内都不变,可使用元组
1. 创建元组
# 创建空元组
object_tuple = tuple () # 或者 object_tuple = ()
# 打印元组
print (type (object_tuple))
print (object_tuple)

2. 查找元素
object_tuple = ("钢铁侠", "黑寡妇", "绿巨人")
# 打印元组
print (object_tuple)
# 查找指定元组元素
targe = "黑寡妇"
if targe in object_tuple:
idx = object_tuple.index (targe)
print ("元素:{} 下标索引值:{}".format (targe, idx))


浙公网安备 33010602011771号