You are on page 1of 3

4/26/2021 Py101_7 - B - Jupyter Notebook

Dictionaries - Activities

PART A
In [2]: return_dict = {'Fund A': 0.057,
'Fund B': 0.059,
'Fund C': 0.34,
'Fund D': 0.199,
'Fund E': 0.203
}

In [3]: return_dict['Fund G'] = .5675


return_dict

Out[3]: {'Fund A': 0.057,


'Fund B': 0.059,
'Fund C': 0.34,
'Fund D': 0.199,
'Fund E': 0.203,
'Fund G': 0.5675}

In [4]: #1. add the following values to the dictionary return_dict


# 'Fund F' with value 0.10124
# 'Fund X' with value 0.37951
new = {'Fund F': 0.10124, 'Fund X': 0.37951}
return_dict.update(new)
return_dict

Out[4]: {'Fund A': 0.057,


'Fund B': 0.059,
'Fund C': 0.34,
'Fund D': 0.199,
'Fund E': 0.203,
'Fund G': 0.5675,
'Fund F': 0.10124,
'Fund X': 0.37951}

PART B
In [5]: #2. create a new dictionary 'totalReturn_dict' with
# 'Total' as key
# sum of return_dict values as value
## PART A
return_dict = {'Fund A': 0.057,
'Fund B': 0.059,
'Fund C': 0.34,
'Fund D': 0.199,
'Fund E': 0.203
}

localhost:8888/notebooks/Documents/PY101/Notebooks/Py101_7 - B.ipynb 1/3


4/26/2021 Py101_7 - B - Jupyter Notebook

In [6]:
returns = {0.057 : 'Fund A',
0.059 : 'Fund B',
0.34 : 'Fund C',
0.199 : 'Fund D',
0.203 : 'Fund E'
}
#3. switch the keys and values in dictionary returns
# keys will now be the values
#values will now be the keys

In [7]: #4. square the values of the resulting dictionary in #4


#example, 'Fund A' will now have a value of 0.003249

In [8]: #5. Create a function that passes d2 and d3 dictionaries,


# calculates the market value by multiplying quantity held
#and price for each stock
#example for Apple, market value = 175 * 126.02
# returns a new dictionary where values are market values (Q x P)
# resulting dictionary should be

d2 = {'Apple': 175,
'Blackberry': 57,
'Celcuity': 231,
'Discovery': 219,
'ElectroCore': 3700,
'FedEx': 38,
'XBiotech': 732}

d3 = {'Apple': 126.02,
'Blackberry': 9.04,
'Celcuity': 14.50,
'Discovery': 43.90,
'ElectroCore': 2.06,
'FedEx': 282.88,
'XBiotech': 16.97}

In [9]: resulting_dict = {'Apple': 22053.5,


'Blackberry': 515.28,
'Celcuity': 3349.5,
'Discovery': 9614.1,
'ElectroCore': 7622.0,
'FedEx': 10749.44,
'XBiotech': 12422.04}

In [ ]:

localhost:8888/notebooks/Documents/PY101/Notebooks/Py101_7 - B.ipynb 2/3


4/26/2021 Py101_7 - B - Jupyter Notebook

localhost:8888/notebooks/Documents/PY101/Notebooks/Py101_7 - B.ipynb 3/3

You might also like