wei/unit_3/motorcycles.py

30 lines
779 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
#可通过索引修改相应的元素
motorcycles[0] = 'ducati1'
print(motorcycles)
#append可在集合后面追加元素
motorcycles.append('ducati2')
print(motorcycles)
#insert可在指定索引位置添加元素
motorcycles.insert(1, 'ducati3')
print(motorcycles)
#del可删除指定索引位置的元素
del motorcycles[1]
print(motorcycles)
#pop可在删除列表元素的同时继续使用该元素可使用索引选择元素默认是-1索引
popped_motorcycles = motorcycles.pop(0)
print(motorcycles)
print(popped_motorcycles)
#remove可根据指定的元素值来删除元素如果列表中有多个相同的元素remove只删除第一个
motorcycles.remove('ducati2')
print(motorcycles)