Final Exam - Solutions

Each problem below is worth 6 points. Overall the Python problems are worth 30 points and half of the Final exam grade.

Problem 1

Create a function that will take three integers as arguments and calculate their average.


In [1]:
def my_average(x,y,z):
    value = (x+y+z)/3.0
    return value

Problem 2

Create a for loop, that will print only the female names from the list below:

name_list = ["Jack","James","Alice",
            "Jimmy","John","Andrea",
            "Joshua","Jonathan","Anastasia"]

In [2]:
name_list = ["Jack","James","Alice",
            "Jimmy","John","Andrea",
            "Joshua","Jonathan","Anastasia"]
for i in range(len(name_list)):
    if i%3==2:
        print(name_list[i])


Alice
Andrea
Anastasia

Problem 3

Solve the problem above using a while loop.


In [3]:
i=2
while i<len(name_list):
    print(name_list[i])
    i = i + 3


Alice
Andrea
Anastasia

Problem 4

Get all time stock data using pandas_datareader on Alibaba ("BABA") from Google finance. Show the plot of the Alibaba stock's Opening price. Calculate its 5 descriptive measures (mean, median, mode, range, standard deviation).


In [4]:
import pandas_datareader as web
import matplotlib.pyplot as plt

data = web.DataReader("BABA","google")
plt.plot(data['Open'])
plt.show()

mean = data['Open'].mean()
mode = data['Open'].mode()
median = data['Open'].median()

range_max_min = data['Open'].max()-data['Open'].min()
std = data['Open'].std()

print(mean)
print(mode[0])
print(median)

print(range_max_min)
print(std)


92.3219448276
83.3
87.25
100.55
19.5469697302

Problem 5

Define a function called converter that will take a string as an argument. This string should be some 3 digit amount followed USD or EUR (e.g. "115 USD" or "115 EUR"). The function will then convert this amount to AMD and return a string (e.g. "55011 AMD" or "65052 AMD")


In [5]:
def converter(amount):
    if amount[-3:]=="EUR":
        new_amount = int(amount[:3])*560
        value = str(new_amount)+" AMD"
    elif amount[-3:]=="USD":
        new_amount = int(amount[:3])*480
        value = str(new_amount)+" AMD"
    else:
        value = "Wrong Input !"
    return value

test = converter("115 EUR")
print(test)


64400 AMD