That doesn't follow.
Beginners need to learn list slicing to get anywhere with Python, and by the time they've got through [1], [0:10], [2:], [:5], [:-1], [0:10:2] and so on then [:] is just another use in the same pattern.
That doesn't follow.
Beginners need to learn list slicing to get anywhere with Python, and by the time they've got through [1], [0:10], [2:], [:5], [:-1], [0:10:2] and so on then [:] is just another use in the same pattern.
Followup: The tutorial didn't seem to mention what [x:y:z] does, so I checked:
>>> range(15)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> range(15)[0:10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(15)[0:10:2]
[0, 2, 4, 6, 8]
>>> range(15)[0:10:3]
[0, 3, 6, 9]
So the third argument is the number of elements to skip (minus one) between each item in the new sequence. Neat.These are "extended slices" and the third argument is the step. This link also explores deletion and the __getitem__ method:
One can also now pass slice objects to the __getitem__ methods of the built-in sequences:
>>> range(10).__getitem__(slice(0, 5, 2))
[0, 2, 4]
Or use slice objects directly in subscripts:
>>> range(10)[slice(0, 5, 2)]
[0, 2, 4]
them = things[:]
looks different from
them = (list) things;
which in C-like languages usually does not make a copy of things.
Using slices may avoid fallacies like "I know that int(x) is something like a cast of x to int, so list(x) is like a cast of x to list, which does nothing when x is a list".