Exercises

Tuples and lists

  1. Create a tuple called mytuple, with the following strings: "sausage", "eggs" and "bacon"
  2. check it's type using ``type()```
  3. coerce it to a list using list() and assign to mylist

In [ ]:
mytuple = ("sausage", "eggs", "bacon")
type(mytuple)
mylist = list(mytuple)

Attempt to append the string "spam" to mylist and mytuple using append.


In [ ]:
mylist.append("spam")
mytuple.append("spam")

List objects have a sort() function, use that for sorting the list alphabetically (e.g. mylist.sort() ). What is now the first item of the list?

Next, remove the first item from the list, investigate the contents and remove then last item from the list.


In [ ]:
mylist.sort()
print(mylist[0])
del mylist[-1]
print(mylist)

Slicing

Using range() create a list that has the numbers from 50 to 0 with a step of -2. Note that in Python 3 range() returns an iterator (we'll discuss iterators more later on), list(range(args)) returns an actual list.


In [ ]:
numbers = list(range(50, 0, -2))

Using slicing syntax, select

  • the last 4 items from the list
  • the items from index 10 to index 13
  • the first 5 items from the list

In [ ]:
print(numbers[-4])
print(numbers[10:14])
print(numbers[:5])

Read up on the stride syntax . Then using it select

  • every third value in the list
  • the values with an odd-numbered index in the list

In [ ]:
print(numbers[::3])
print(numbers[1::2])

Multidimensional lists

Create a two dimensional list of (x,y) value pairs, i.e. arbitrary long list whose elements are two element lists.

Are you able to use slicing for extracting only the y values? (Answer is no, but try it in any case)


In [ ]:
coordinates = [[0.1, 4.5], [-1.2, 3], [2.2, -5], [3.4, 1.6]]

Dictionaries

Create a dictionary whose keys are the fruits “pineapple”, “strawberry”, and “banana”. As values use numbers representing e.g. prices.

Add “orange” to the dictionary and then remove “banana” from the dictionary. Investigate the contents of dictionary and pay attention to the order of key-value pairs.


In [ ]:
fruits = {'banana' : 5, 'strawberry' : 7, 'pineapple' : 3}
print(fruits)
fruits['orange'] = 4.5
print(fruits)
del fruits['banana']
print(fruits)

Bonus exercises

Create a new “fruits” dictionary where the values are also dictionaries containing key-value pairs for color and weight, e.g.

fruits['apple'] = {'color':'green', 'weight': 120}

Change the color of apple from green to red


In [ ]:
apple = {'color' : 'green', 'weight' : 120}
orange = {'color' : 'orange', 'weight' : 110}
fruits = {'apple' : apple, 'orange' : orange}
print(fruits)
fruits['apple']['color'] = 'red'
print(fruits)

It is often useful idiom to create empty lists or dictionaries and add contents little by little.

Create first an empty dictionary for a mid-term grades of students. Then, add a key-value pairs where the keys are student names and the values are empty lists.

Finally, add values to the lists and investigate the contents of the dictionary.


In [ ]:
grades = {}
grades['Jussi'] = []
grades['Jyry'] = []
print(grades)
grades['Jussi'].append(2)
grades['Jyry'].append(5)
print(grades)
grades['Jussi'].append(3)
grades['Jyry'].append(4)
print(grades)