+ 2

squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(squares[2:6]) how does it result in [4, 9, 16, 25] I don't und2

Why does does it not start at one

19th Feb 2021, 2:49 PM
Coder Jon
3 ответов
+ 2
Indexing starts from 0 for lists , so squares[0] is 0 , squares[1] is 1 and so on , basically that 2:6 is index numbers for starting and ending point .
19th Feb 2021, 2:58 PM
Abhay
Abhay - avatar
+ 1
first list item is at index 0 2nd is at 1 3rd is at 2 and so on... so, last is at list length-1 slicing get first argument as start index, and second as last (not included), so [2:6] should return third to sixth (included, as seventh is not included)...
19th Feb 2021, 3:04 PM
visph
visph - avatar
0
these answers are correct. one tip about slicing: thankfully, when one slices by indexing from the endpoints (positive indices for the beginning of the slice and negative ones for the end), the slice indices correspond with how many items are going to be removed from the start or end of the sequence. for example “abcde”[2:-1] would chop off “ab” and “e” leaving “cd”
19th Feb 2021, 5:26 PM
Wilbur Jaywright
Wilbur Jaywright - avatar