HomepageTeaching PageResearch PageResources Page

Homework: List Slices and Methods

Read through sections 10.5 and 10.6 of the book and do the examples. What's the value of t in the assignment statement in the paragraph at the end?

You can even use slices without any variables, though it gets a bit weird-looking:

>>> ["Ash", "Misty", "Brock", "May"][1:3]
['Misty', 'Brock']
>>>
        

Similarly, you can use the methods with non-variable lists in the arguments.

>>> t0 = ['Bulbasaur', 'Ivysaur', 'Venusaur']
>>> t0.extend(['Squirtle', 'Wartortle', 'Blastoise'])
>>> t0
['Bulbasaur', 'Ivysaur', 'Venusaur', 'Squirtle', 'Wartortle', 'Blastoise']
        

However, you might not be very happy if you use a non-variable instead of t0, only because you won't have a reference to the extended list!

Naturally, we can use slice expressions as arguments as well!

>>> t0 = ['Bulbasaur', 'Ivysaur', 'Venusaur']
>>> t0.extend(['Squirtle', 'Wartortle', 'Blastoise'])
>>> t0
['Bulbasaur', 'Ivysaur', 'Venusaur', 'Squirtle', 'Wartortle', 'Blastoise']
>>> t1 = ['Charmander', 'Charmeleon', 'Big Fire Dragon']
>>> t0.extend(t1[0:2])
>>> t0
['Bulbasaur', 'Ivysaur', 'Venusaur', 'Squirtle', 'Wartortle', 'Blastoise', 'Charmander', 'Charmeleon']