Review Python Lists So Far (Exercises)
00:00
Here are a couple of review exercises that you can work on to train the skills that you’ve heard about in the past lessons. Start by creating a list named food
with two elements, "rice"
and "beans"
, both of them strings.
00:14
Then you want to append the string "broccoli"
to food
using the .append()
method. Then also add the strings "bread"
, and "pizza"
to the food
list, but this time using .extend()
.
00:25
And then you should print the first two items in the food
list using print()
and slice notation. And then also print the last item in food
using print()
and index notation.
00:35 So this is just to get you to train a little bit the concepts that you’ve learned, and I’ve got some more for you.
00:42
Next, you can create a list called breakfast
, but create it from the string "eggs, fruit, orange juice"
. So you have one string, "eggs, fruit, orange juice"
.
00:53
And then use the .split()
method to create a list from those. That should only contain three items. Second, you should verify that the breakfast
list has only three items.
01:05
And then finally, also create a new list called lengths
. Here you should use a list comprehension to calculate the lengths of each of the strings in the breakfast
list and then put them into the new lengths
list.
01:18 So remember, in the list comprehension, you can do this in one line. So this is a nice way to train using this very Pythonic way of doing calculations on elements in a collection.
01:30 Okay, go train these concepts a bit. Putting in some coding time always helps to fortify your memory and figure out if you understood everything that you’ve heard about.
Become a Member to join the conversation.