[Python] Shorthand for slice operation with negative numbers

Just remember the following formula: [start_index: stop_index: step]
start_index is the starting position
of the slice stop_index is the ending position of the slice (not included)
step can be omitted , the default value is 1, and the step value cannot be 0, otherwise ValueError will be reported

For example, it a = [1,2,3,4,5]
a[-2:]means start_index=-2, and the value is taken to the end, step=1, so the result is [4,5]
a[:-2]stop_index=-2, start_index is the default 0, step=1, so the result is[1,2,3]

Reference: Comprehensive and thorough understanding of Python slicing operation [Original]
[:-1] and [::-1] in python

Guess you like

Origin blog.csdn.net/weixin_38705903/article/details/107143949