Suppose we have a list of letters:
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
Suppose we want to select from
We can do this using the following syntax:
startis the index of the first element that we want to include in our selection. In this case, we want to start at
b, which has index
endis the index of one more than the last index that we want to include. The last element we want is
f, which has index
endneeds to be
This example would yield:sublist = letters[1:6] print(sublist)
Notice that the element at index['b', 'c', 'd', 'e', 'f']
g) is not included in our selection.
Creating a selection from a list is called slicing.
How many elements does it contain?
beginning, so that it selects the first 4 elements of
Create a new list called
middle that contains the middle two items from