In Python sequences (lists and tuples are both sequences), you can use slicing operations: sequence[start : end : step] The first two are easy to understand, they are the position of the start index and the position of the end index. (Python provides two indexes: 0 from left to right... index-1 and -1 from right to left... -index). The key is the meaning of this step.
Let me introduce to you the meaning of step (step length):
>>>s = ‘abcdefgh’ >>>s[::-1] ’hgfedcba’ >>>s[::2] ’aceg’
>>>s = 'abcdefgh' >>>s[::-1] 'hgfedcba' >>>s[::2] 'aceg'
Related recommendations: "Python Video Tutorial"
Actually, step here represents the step size of the slice (step cannot be 0, the default is 1):
If step > 0, it means from left to right Make slices. At this time, start must be less than end to have a result, otherwise it will be empty. For example: the result of s[0,: 5: 2] is 'ace'
If step
Then, s[::-1] means slicing from right to left with a step size of 1; s[: :2] means slicing from left to right with a step size of 2
The above is the detailed content of What does python step size mean?. For more information, please follow other related articles on the PHP Chinese website!