ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Python语言例题集(002)

2026/9/20 9:06:56 拓冰建站 浏览量
Python语言例题集(002)

#!/usr/bin/python3

#使用del语句删除元素
motorcycles=[‘honda’,‘yamaha’,‘suzuki’]
print(motorcycles)
del motorcycles[0]
print(motorcycles)
motorcycles=[‘honda’,‘yamaha’,‘suzuki’]
print(motorcycles)
del motorcycles[1]
print(motorcycles)
#使用方法pop()删除元素
motorcycles=[‘honda’,‘yamaha’,‘suzuki’]
print(motorcycles)
popedMotorcycle=motorcycles.pop()
print(motorcycles)
print(popedMotorcycle)
#删除列表中任何位置处的元素
motorcycles=[‘honda’,‘yamaha’,‘suzuki’]
firstOwnde=motorcycles.pop(0)
print(f"The first motorcyle I owned was a
{firstOwnde.title()}.")
#根据值删除元素
motorcycles=[‘honda’,‘yamaha’,‘suzuki’,‘ducati’]
print(motorcycles)
motorcycles.remove(‘ducati’)
print(motorcycles)