0
Slices
Code: squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(squares[-2:6]) print(squares[3:8]) print(squares[0:1]) Result: [] [9, 16, 25, 36, 49] [0] As you see when I change the slice value from [2:6] to [-2,6] then as a result empty output occurs ( [] ).why the value between -2 to 6 squares are not listed. Secondly, code: squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(squares[-2:9]) print(squares[3:8]) print(squares[0:1]) Result: [64] [9, 16, 25, 36, 49] [0] In this I change slices to [-2,9] but in this single output is listed. Why such thing occurs.
2 Antworten
+ 3
-2 means the second number from the end.
And if the ending index of the slice is equal to or less than the beginning index, it returns no item.
+ 3
Here are some examples
https://code.sololearn.com/cXNzv56rgiyL