[Python] List 的用法
index與count方法
- index(內容) : 找到內容所在的索引值
- count(內容) : 計算該內容的次數
score = [90, 81, 70, 68, 92, 70, 77, 70]
print("第一個70分的學生是編號%d號" %(int(score.index(70))+1))
print("70分的學生共有%d位" % score.count(70))
in與not in
animal = ["dog", "cat", "elephone", "bird"]
if "dog" in animal:
print("dog已在串列中")
else:
animal.append("dog")
if "snake" not in animal:
animal.append("snake")
print("已新增snake")
else:
print("snake已在串列中")
留言
張貼留言