Homework 2 (solutions)

Problem 1

Construct a function, which will ask the user to input several numbers separated by commas and will calculate their average. (e.g. if the user inputs 3,5,7 the function must result in 5).


In [1]:
inputted_numbers = input("Please, input some numbers seprated by a comma: ")
def simple_mean(inputted_numbers):
    return float( sum(inputted_numbers) ) / float( len(inputted_numbers) )
print simple_mean(inputted_numbers)


Please, input some numbers seprated by a comma: 10, 12.5, 15, 17.5, 20, 22.5, 25
17.5

Problem 2

Upgrade the function above to also ask the user the number of occurrences to calculate the average for. (e.g. if the user inputs 1,3,5,7,9 and as the second argument to function inputs 2, the function must result in (7+9)/2 = 8. If the user inputs the same numbers as a first argument, but inputs 3 as the 2nd, then the function must result in (5+7+9)/3=7)


In [4]:
inputted_numbers = input("Please, input some numbers seprated by a comma: ")
occurancy = input("Please, input the number of occurancies to calculate the mean for: ")
def rolling_mean(inputted_numbers,occurancy):
    return float( sum(inputted_numbers[-occurancy:]) ) / float( len(inputted_numbers[-occurancy:]) )
print rolling_mean(inputted_numbers, occurancy)


Please, input some numbers seprated by a comma: 10, 12.5, 15, 17.5, 20, 22.5, 25
Please, input the number of occurancies to calculate the mean for: 5
20.0

Problem 3

Construct a function, which will generate a random number between 1 and 100 (both included). If this number is between 50 and 100 (not included) the function returns "Win", if it is between 1 and 50 (both included) the function returns "Loss" and if it is exactly 100 then the function returns "Draw". (Hint: to generate a random number, one should first import a package called random (i.e. import random) and then use the function randint from there (e.g. random.randint(x,y), where x=1 and y=100 in our case)


In [5]:
import random
number=random.randint(1,100)
def generator(number):
    if 100 > number > 50:
        return "Win"
    elif 50 >= number >= 1:
        return "Loss"
    else:
        return "Draw"
print(number)
print generator(number)


70
Win

Problem 4

Create a list of 3 stocks (choose whatever stocks you want, e.g. ["IBM","AAPL","MSFT"]). Create a for loop that will iterate over the list, get the data of relevant stock and print the first 7 rows of the data.


In [6]:
import pandas_datareader.data as web
stock_list = ["IBM","AAPL","MSFT"]
for stock in stock_list:
    data = web.DataReader(stock,"google")
    print( data.head(7) )


              Open    High     Low   Close   Volume
Date                                               
2010-01-04  131.18  132.97  130.85  132.45  6155846
2010-01-05  131.68  131.85  130.10  130.85  6842471
2010-01-06  130.68  131.49  129.81  130.00  5605290
2010-01-07  129.87  130.25  128.91  129.55  5840569
2010-01-08  129.07  130.92  129.05  130.85  4197105
2010-01-11  131.06  131.06  128.67  129.48  5731177
2010-01-12  129.03  131.33  129.00  130.51  8083354
             Open   High    Low  Close     Volume
Date                                             
2010-01-04  30.49  30.64  30.34  30.57  123432050
2010-01-05  30.66  30.80  30.46  30.63  150476004
2010-01-06  30.63  30.75  30.11  30.14  138039594
2010-01-07  30.25  30.29  29.86  30.08  119282324
2010-01-08  30.04  30.29  29.87  30.28  111969081
2010-01-11  30.40  30.43  29.78  30.02  115557365
2010-01-12  29.88  29.97  29.49  29.67  148614774
             Open   High    Low  Close    Volume
Date                                            
2010-01-04  30.62  31.10  30.59  30.95  38414185
2010-01-05  30.85  31.10  30.64  30.96  49758862
2010-01-06  30.88  31.08  30.52  30.77  58182332
2010-01-07  30.63  30.70  30.19  30.45  50564285
2010-01-08  30.28  30.88  30.24  30.66  51201289
2010-01-11  30.71  30.76  30.12  30.27  68754648
2010-01-12  30.15  30.40  29.91  30.07  65913228

Problem 5

Upgrade the for loop above. Now, instead of printing the data, the for loop should iterate over the list, get the data and plot it.


In [8]:
import pandas_datareader.data as web
import matplotlib.pyplot as plt
stock_list = ["IBM","AAPL","MSFT"]
for stock in stock_list:
    data = web.DataReader(stock,"google")
    plt.plot(data["Open"])
plt.show()