Python第六章08:元组操作练习题

# 元组定义操作练习题

"""
定义一个元组,内容是:('周杰伦',11,['football','music']),
    记录一个学生的信息(姓名、年龄、爱好)

请通元组(tuple)的功能,对其进行如下操作:
1.查询其年龄所在的下标位置
2.查询学生的姓名
3.删除学生爱好中的football
4.增加爱好:coding
"""
my_tuple = ('周杰伦',11,['football','music'])
# 1.查询其年龄所在的下标位置
index = my_tuple.index(11)
print(f"在元组my_tuple中,年龄所在下标是:{index}")
#2.查询学生的姓名
name = my_tuple[0]
print(f"在元组my_tuple中,学生姓名是:{name}")
# 3.删除学生爱好中的football   ,  不能用 列表.pop[下标]删除
del my_tuple[-1][0]
print(f"在元组my_tuple中,删除football后,元组内容是:{my_tuple}")
# 4.增加爱好:coding
list = my_tuple[-1]
# lsit = list.append("coding")
list.insert(-1,"coding")
print(f"在元组my_tuple中,加入元素coding后,元组内容是:{my_tuple}")

运行结果:

Python第六章08:元组操作练习题_第1张图片

你可能感兴趣的:(python,开发语言)