本小节深入探讨Python中的序列切片语法。
切片-slicing可以获取序列的子序列(列表,字符串…):

1
2
3
4
5
6
7
8
numbers = [x for x in range(10)]
print("numbers:",numbers)
print("numbers[3:9]:",numbers[3:9])
print("numbers[3:]:",numbers[3:])
print("numbers[:9]:",numbers[:9])
print("numbers[-6:-1]:",numbers[-6:-1])
print("numbers[1:9:2]:",numbers[1:9:2])
print("numbers[-1:1:-2]:",numbers[-1:1:-2])