元組
- 元組和列表相似,不同點(diǎn)元組定義后不能改變,列表可以做改變。
- 元組用小括號(hào),列表用中括號(hào)。
#tuple
#元組定義
students = ("張三","李四","王五")
列表常用操作
- 打印元素
#tuple
#元組定義
students = ("張三","李四","王五")
print(students[0])
print(students[1])
print(students[2])
上述代碼運(yùn)行結(jié)果:
上述代碼運(yùn)行結(jié)果
- 遍歷元素
#tuple
#元組定義
students = ("張三","李四","王五")
for i in students:
print(i)
'''
用while循環(huán)也可以
i = 0
while i < len(students):
print(students[i])
i+=1
'''
上述代碼運(yùn)行結(jié)果:
上述代碼運(yùn)行結(jié)果
- 統(tǒng)計(jì)
#tuple
#元組定義
students = ("張三","李四","王五","李四")
print("叫李四的人有%d個(gè)"%students.count("李四"))
上述代碼運(yùn)行結(jié)果:
上述代碼運(yùn)行結(jié)果
- 查索引
#tuple
#元組定義
students = ("張三","李四","王五","李四")
print("王五的索引是%d"%students.index("王五"))
上述代碼運(yùn)行結(jié)果:
上述代碼運(yùn)行結(jié)果
歡迎關(guān)注