section (Slicing)
In all sequence containers , Include list 、 Tuples 、 Strings and so on , They all support slicing operations , This is a Python The powerful weapon of language , This article will begin to learn the advanced skills of slicing .
Why slice and Range The operation does not include the last item ?
from Python The grammar knows , When slicing , such as mylist[0,2] The operation of , It doesn't include the last one [2]. Again range Iterative generation also does not include the last item , What's the reason for this ? In fact, it is C Language is like many other languages , Subscript variables start with 0 Start calculated .
Using such a convention has the following advantages :
1) It's easy to see slicing operations or range Length after operation , such as range(3) and my_list[:3], From the last number is the length .
2) It is convenient to calculate the length of the operation with the start index and the last index , such as my_list[start:stop], It can be calculated in this way :stop - start.
3) It's easy to split through an index position , such as my_list[:x] and my_list[x:] It can be divided into two , here x Don't change .
>>> l = [10,