示例
players = ['charles','martina','michae','florence','eli']
print(players[0:3])
该代码的第二行,意思是打印players...', 'florence']
如需要终止于末尾,也可以使用类似的语法:
players = ['charles','martina','michae','florence','eli']
print(players...[-3:]:
players = ['charles','martina','michae','florence','eli']
print(players[-3:])
输出结果:
['michae',...'florence', 'eli']
遍历切片
如需遍历列表的部分元素,可在for循环中使用切片:
players = ['charles','martina','michae','florence'...', 'eli', 'bob']
['charles', 'martina', 'michae', 'florence', 'eli', 'tom']