视频版教程 Python3零基础7天入门实战视频教程
下标索引用法和列表一样,唯一区别就是不能修改元素
实例:
# 下标索引用法和列表一样,唯一区别就是不能修改元素
t1 = ("java", "python", "c")
# t1[1] = "python22" 不能修改
print(t1[1])
print(t1[-1])
# 元组里可以嵌套元组
t7 = ((1, 2, 3), (4, 5, 6))
print(t7[1][1])
因为元组不能修改,所以方法比较少;
1,index(元素)方法,查找元组中指定元素的下标,不存在就报错
t2 = ("java", "python", "c")
print(t2.index("python"))
2,count(元素) 统计某元素在元组中的个数
t3 = ("java", "python", "c", "python", "php")
print(t3.count("python"))
3,len(元组),统计元组内一共有多少元素
t4 = ("java", "python", "c", "python", "php")
print(len(t4))
元组的遍历
t11 = ("java", "python", "c")
for ele in t11:
print(ele)
i = 0
while i < len(t11):
ele = t11[i]
print(ele)
i += 1
作业:定义一个三维元组 t12 = (((1, 2), (3, 4), (5, 6)), ((7, 8), (9, 10), (11, 12))) 通过元组遍历,打印出每个值。(提示:用循环嵌套实现)