Pre-Tutorial Exercises

If you've arrived early for the tutorial, please feel free to attempt the following exercises to warm-up.


In [1]:
# 1. Basic Python data structures
# I have a list of dictionaries as such:

names = [{'name': 'Eric',
          'surname': 'Ma'},
         {'name': 'Jeffrey',
          'surname': 'Elmer'},
         {'name': 'Mike',
          'surname': 'Lee'},
         {'name': 'Jennifer',
          'surname': 'Elmer'}]

# Write a function that takes in a list of dictionaries and a query surname, 
# and searches it for all individuals with a given surname.

def find_persons_with_surname(persons, query_surname):
    # Assert that the persons parameter is a list. 
    # This is a good defensive programming practice.
    assert isinstance(persons, list)   
    
    results = []
    for name in names:
        if name['surname'] == query_surname:
            results.append(name)
    
    return results

In [2]:
# Test your result below.
results = find_persons_with_surname(names, 'Lee')
assert len(results) == 1

results = find_persons_with_surname(names, 'Elmer')
assert len(results) == 2

In [3]:
results = find_persons_with_surname(names, 'Ma')
assert len(results) == 3


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-3-89c1f093e9b0> in <module>()
      1 results = find_persons_with_surname(names, 'Ma')
----> 2 assert len(results) == 3

AssertionError: 

In [4]:
import random
lst = list()
#random.seed(9001)
for i in range(100):
    #temp = random.randint(0,10000)
    #random.seed(temp)
    lst.append(random.randint(0,5))
print(lst)


[4, 4, 2, 4, 1, 5, 4, 3, 4, 2, 1, 4, 1, 3, 4, 0, 1, 2, 4, 0, 4, 1, 0, 3, 0, 4, 4, 4, 2, 0, 3, 2, 4, 1, 2, 2, 0, 4, 0, 1, 4, 1, 2, 5, 1, 4, 2, 2, 5, 1, 3, 1, 4, 3, 5, 2, 0, 1, 4, 1, 3, 0, 1, 1, 3, 2, 3, 5, 5, 5, 4, 3, 3, 2, 0, 0, 5, 1, 0, 0, 0, 4, 1, 1, 3, 5, 3, 4, 5, 5, 2, 2, 4, 0, 2, 1, 0, 0, 2, 5]

In [5]:
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

In [6]:
lst = list()
for i in range(20):
    lst.append(random.uniform(0, 1))

In [7]:
print(lst)


[0.7525874388092904, 0.9592356789130781, 0.2634430571309443, 0.006256586106564432, 0.07512778474837056, 0.6292901645822183, 0.8503088859834467, 0.9581808562837656, 0.6662849523140008, 0.2296232973486816, 0.018280048391809856, 0.9789996381731564, 0.8670347282231149, 0.7802258855370047, 0.2818568839659219, 0.27084522698031666, 0.6595103467375983, 0.7546670749704983, 0.604656791352812, 0.3504964357719832]

In [8]:
plt.plot(range(20),lst)
plt.show()



In [9]:
t = np.linspace(0,1, 20)

In [10]:
plt.plot(t,lst,'w', label='Ratings')
plt.show()



In [11]:
plt.plot(range(20),lst)
plt.show()



In [ ]: