元组
- 元组和列表相似,不同点元组定义后不能改变,列表可以做改变。
- 元组用小括号,列表用中括号。
#tuple
#元组定义
students = ("张三","李四","王五")
列表常用操作
- 打印元素
#tuple
#元组定义
students = ("张三","李四","王五")
print(students[0])
print(students[1])
print(students[2])
上述代码运行结果:
- 遍历元素
#tuple
#元组定义
students = ("张三","李四","王五")
for i in students:
print(i)
'''
用while循环也可以
i = 0
while i < len(students):
print(students[i])
i+=1
'''
上述代码运行结果:
- 统计
#tuple
#元组定义
students = ("张三","李四","王五","李四")
print("叫李四的人有%d个"%students.count("李四"))
上述代码运行结果:
- 查索引
#tuple
#元组定义
students = ("张三","李四","王五","李四")
print("王五的索引是%d"%students.index("王五"))
上述代码运行结果: