切片类似 php的截取(字符串/数组截取)
常用于list, tupple,字符串的操作
L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']
# 取前3个元素, 索引从0开始,不包括索引3
L[0:3] => ['Michael', 'Sarah', 'Tracy']
# 从索引1开始,取2个元素
L[1:3] => ['Sarah', 'Tracy']
# 前3个
L[:3] => ['Michael', 'Sarah', 'Tracy']
# 取最后两个
L[-2:] => ['Bob', 'Jack']
# 取倒数第二个
L[-2: -1] => ['Bob']
# 前4个,每2个取一个
L[:4:2] => ['Michael', 'Tracy']
# 复制
L[:] => ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']
# 字符串也可以使用切片, 结果也是字符串
'ABCEDF'[:3] => 'ABC'
# tupple也可以使用切片, 结果也是tupple
(0,1,2,3,4,5)[:3] => (0,1,2)