Lists#

Exercise 1: Explain this error#

my_list = [1, 2, 3, 4]
print(my_list[1.0])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 2
      1 my_list = [1, 2, 3, 4]
----> 2 print(my_list[1.0])

TypeError: list indices must be integers or slices, not float

Exercise 2: Print the first, the last and second to last item in the list#

Remember that you can use negative indices.

ta = [-0.19,-0.177,-0.308,-0.289,-0.193,-0.057,0.077,0.026,0.029,0.068,0.256, 0.342, 0.591, 0.792]
ta[0], ta[-2], ta[-1] 
# your solution here
(-0.19, 0.591, 0.792)

Exercise 3: Slice indexing#

Use slice indexing to print

  1. only the odd elements of the list (odd elements are the first, third, fifth etc. elements)

  2. only the even elements of the list (even elements are the second, fourth, sixth etc. elements)

  3. the list elements in reverse order

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# your solution here

Exercise 4: Manipulating lists#

Add the item 10 to the end of the list

Bonus: Add the item 0 to the beginning of the list

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# your solution here