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.

18th Sep 2021, 1:09 PM
Virendra Agarkar
Virendra Agarkar - avatar
2 ответов
+ 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.
18th Sep 2021, 2:13 PM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
+ 3
18th Sep 2021, 2:15 PM
David Ashton
David Ashton - avatar