May 2019
Beginner
528 pages
29h 51m
English
You can slice sequences to create new sequences of the same type containing subsets of the original elements. Slice operations can modify mutable sequences—those that do not modify a sequence work identically for lists, tuples and strings.
Let’s create a slice consisting of the elements at indices 2 through 5 of a list:
In [1]: numbers = [2, 3, 5, 7, 11, 13, 17, 19]In [2]: numbers[2:6]Out[2]: [5, 7, 11, 13]
The slice copies elements from the starting index to the left of the colon (2) up to, but not including, the ending index to the right of the colon (6). The original list is not modified.
If you omit the starting index, ...
Read now
Unlock full access