Home  >  Article  >  Backend Development  >  What does python step size mean?

What does python step size mean?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-20 17:53:2531692browse

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.

What does python step size mean?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn