Lists · practice
Find Items by Position
A
Like
snacks[0] asks for the first item. snacks[1] asks for the second. The brackets after the list name mean “get the item at this index.”
Count backward with negative indexes
A negative index starts from the end:
Index -1 is the last item, and -2 is the item before it. This is useful when the list length might change and you still want its final item.
An index must exist
A four-item list has indexes 0 through 3. Asking for snacks[4] causes an
What does this code print?
levels = ["beginner", "intermediate", "advanced"]
print(levels[0])
print(levels[-1])Store selected items
An indexed item is a
In the exercise, use the same pattern to select the first and last snacks.
Task
Use indexes to finish the program:
Store the first item from
snacksinfirst_snack.Store the last item in
last_snackusing a negative. Print both selected values, one per line.