# 元组的基本使用
info_tuple = ("zhangsan", 18, 36, "zhangsan")
# 1.取值和取索引
print(info_tuple[0])
# 已知数据内容,查看该数据在元祖中的位置
print(info_tuple.index("zhangsan"))
# 2.统计计数
print(info_tuple.count("zhangsan"))
# 统计元组中包含元素的个数
print(len(info_tuple))
#元组的迭代遍历
for my_ifo in info_tuple:
print(my_ifo)
# 格式化字符串后面的 () 本质上就是元组
info_tuple = ("小明", 18, 1.80)
print("%s 年龄是 %d 身高是 %.2f" % info_tuple)
info_str = "%s 年龄是 %d 身高是 %.2f" % info_tuple
print(info_str)

本文深入讲解了Python中元组的基本使用方法,包括取值、索引查找、元素计数、长度统计、迭代遍历及格式化字符串应用。通过具体代码示例,读者可以快速掌握元组的常用操作。
1296

被折叠的 条评论
为什么被折叠?



