List value problem, python

there is a rookie problem. The code is as follows:

arr = [1,2,3,4,5,6,7,8,9,10]

print(arr[1:5])
print(arr[1:5:2])
print(arr[1:5:-2])

:
[2, 3, 4, 5]
[2, 4]
[]
  1. does the second parameter 5 also represent the index, and if so, is it intercepted before the index? Can you understand it that way?
  2. you can take a value when the step size is 2. Why is it empty when-2
  3. what is the underlying principle of intercepting the value of the index here in the
  4. python list, and why it leads to the above result
< hr > < hr >

Thank you. I understand. The code can see the answers that have been adopted.
if it is easy to understand, it is the explanation of dodopy . Post it down:

:
1.0
2.
3.
4.step1

arr = [1,2,3,4,5,6,7,8,9,10]
-sharp 2index141[2,3,4,5]
print(arr[1:5])
-sharp 2index142[2,4]
print(arr[1:5:2])
-sharp 2index142[]
print(arr[1:5:-2])
-sharp 
-sharp 142[10, 8]
print(arr[-1:-5:-2]

Feb.28,2021

-sharp 
def index(l, start=None, end=None, step=1):
    result = []
    if step == 0:
        -sharp raise 
        return
    elif step > 0:
        if start is None: start = 0
        if end is None: end = len(l)
        while start < end:
            result.append(l[start])
            start += step
    else:
        if start is None: start = len(l) - 1
        if end is None: end = -1
        while start > end:
            result.append(l[start])
            start += step
    return result
    
    
l = list(range(10))
print(l[::2], index(l, step=2))
print(l[::-2], index(l, step=-2))
print(l[:5:2], index(l, end=5, step=2))
print(l[:6:-3], index(l, end=6, step=-3))
print(l[6::2], index(l, start=6, step=2))
print(l[2::-2], index(l, start=2, step=-2))
print(l[2:7], index(l, start=2, end=7))
print(l[7:2:-1], index(l, start=7, end=2, step=-1))

of course, the bottom layer of list slices is implemented in c, which is much more efficient than what I wrote


clarify several messages:
1. The index of the list starts at 0
2. The range of slices conforms to the principle of closing left and opening right
3. A negative number indicates the order from right to left
4.step value defaults to 1

arr = [1,2,3,4,5,6,7,8,9,10]
-sharp 2index141[2,3,4,5]
print(arr[1:5])
-sharp 2index142[2,4]
print(arr[1:5:2])
-sharp 2index142[]
print(arr[1:5:-2])
-sharp 
-sharp 142[10, 8]
print(arr[-1:-5:-2]
When

step is negative, start is greater than end,
everything else is correct.
https://stackoverflow.com/que.


this is the operation of list slices
aa= [1 br > aa [1:5:2] refers to slices with an interval of 2.
AA [1: 5] refers to slices with an interval of-2. Negative numbers can only be used when slicing in reverse, so the result is empty.
attach a picture and knock on it for yourself.

clipboard.png

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38445-2b951.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38445-2b951.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?