python学习笔记——列表
程序员文章站
2022-03-08 17:04:49
...
切片:python中处理列表的部分元素。python到达第二个索引前面的元素后停止。即要输出前面3个元素,需要指定索引0~3([0:3])
players=['charles','martina','michael','florence','eli']
print(players[0:3])
print(players[1:4])
print(players[:3])#从第一个到第三个元素
print(players[2:])#从第三个到最后一个
print(players[-3:])#从倒数第三个到最后一个
切片遍历
players=['charles','martina','michael','florence','eli']
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())
复制列表
my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("my favorite food are:")
print(my_foods)
print("\nMyfrend's favorite food arre:")
print(friend_foods)