This exercise follows directly from the previous. If you haven't already done so, create a list of ten cookie recipes.
>>> recipes = ['molasses', 'ginger', 'chocolate', 'samoa',
... 'no bake', 'mint', 'sugar', 'peanut', 'oatmeal', 'bacon']
Note
When inputting the commmand above, after typing 'samoa',, I press the "Enter" key on the keyboard. The Python console automatically indents the next line, because it understands that I haven't finished typing in the rest of the list.
With our recipes in a list, lets start sampling the list in a couple different ways through the next several exercises.
You can choose to look at part of the list, such as only the first three items or the middle five items, for example. Looking at a section of a list is called slicing. I imagine it to be like slicing a stick of butter with a warm knife.
In Python, slicing uses the 'crosshairs' notation, the left and right brackets ([ and ]), except instead of targeting a specific position, it's targeting all positions located between a starting position, such as 0, and before an ending position, such as 4. The start and end positions are separated by a :, a colon. In practice, the command recipes[0:4] keeps the items in position 0, 1, 2, and 3, and tosses out the rest. Since "molasses", "ginger", "chocolate", and "samoa" are in positions 0 through 3, they form the slice.
Here are more ways of slicing our recipe list. After typing each command after the >>>, press the Enter key to see Python's output. [1]
>>> recipes[1:4]
['ginger', 'chocolate', 'samoa']
>>> recipes[0:]
['molasses', 'ginger', 'chocolate', 'samoa', 'no bake', 'mint', 'sugar', 'peanut', 'oatmeal', 'bacon']
>>> recipes[:4]
['molasses', 'ginger', 'chocolate', 'samoa']
>>> recipes[0:len(recipes)/2]
['molasses', 'ginger', 'chocolate', 'samoa', 'no bake']
>>> recipes[:]
['molasses', 'ginger', 'chocolate', 'samoa', 'no bake', 'mint', 'sugar', 'peanut', 'oatmeal', 'bacon']
The last line, recipes[:], should look a bit strange to you. The command instructs Python to give you all the items that are between the start and the end. That is, the command gives the whole list back to us. This may sound dumb, but it's actually very useful when working with data, because it's a quick way to make a copy of your raw data, like this: newList = oldList[:]. Then you can mess with the copy without accidentally disturbing the original data. It would be embarassing to lose one of our cookie recipes.
Instead of playing with slices, recall that we're supposed to be picking three cookie flavors for our community taste test. We could sample our recipe list by just taking a slice of any three sequential flavors. Lets sample the last three:
We created a new variable, samples, and stuffed into it a slice of our recipes list. But how many items are in our sample?
Peek inside samples to see what the items are:
So, we apparently got the last three items from our original recipe list, but how? Lets dissect the command recipes[len(recipes) - 3:]:
[1] | Remember, only type what follows the >>> on this page. You do not type "molasses", "ginger", or anything that isn't following the `>>>`. Let the computer do the work, not you. |