語法:[start:stop:step]
step代表切片步長;切片區間為[start,stop),包含start但不包含stop
1.step > 0,從左往右切片
2.step <0,從右往左切片
3.start、stop、step 為空值時的理解:
start、stop默認為列表的頭和尾,並且根據step的正負進行顛倒;step的默認值為1
4.start、stop為負,無論step正負,start、stop代表的是列表從左到右的倒數第幾個元素
st = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] print(st[2:6:2]) print(st[6:2:-2]) print(st[::1]) print(st[::-1]) # 倒序輸出 print(st[-1]) 輸出結果: ['c', 'e'] ['g', 'e'] ['a', 'b', 'c', 'd', 'e', 'f', 'g'] ['g', 'f', 'e', 'd', 'c', 'b', 'a'] g