2.1 Mean calculator


In [10]:
set=raw_input("please enter numbers by comma ")
my_list=set.split(",")
n=len(my_list)
def mean(my_list):
    sum=0
    for i in range(n):
        sum=sum+float(my_list[i])
        i=i+1 
    return float(sum/n)    
print "Mean is "+str(mean(my_list))


please enter numbers by comma 1,2,3,5,6,6,6
Mean is 4.14285714286

2.2 Moving mean


In [11]:
set=raw_input("please, enter numbers by comma ")
my_list=set.split(",")
number=raw_input('please,enter a number less than the length of the list ')
m=int(number)
n=len(my_list)

def moving_mean(my_list,number):
    sum=0
    for i in range(m):
        k=n-int(i)-1
        sum=sum+float(my_list[k])
        i=i+1 
    return float(sum/m)    
print "Moving mean is "+str(moving_mean(my_list,number))


please, enter numbers by comma 1,2,3,2
please,enter a number less than the length of the list 2
Moving mean is 2.5

2.3 Lottery


In [47]:
import random
x=random.randint(1,100)
def func(x):
    if x>50  and x<100 :
        return "Win"
    elif x>=1 and x<=50:
        return "Loss"
    elif x==100:
        return "Draw"
print func(x)
print (x)


Draw
100

2.4 Stock data(variant_1 for each stock separately)


In [48]:
import pandas_datareader.data as web
%matplotlib inline

In [49]:
import matplotlib.pyplot as plt

In [50]:
data1=web.DataReader("IBM","google")

In [51]:
data2=web.DataReader("AAPL","google")

In [52]:
data3=web.DataReader("MSFT","google")

In [53]:
data_list=["IBM","AAPL","MSFT"]

2.4 Stock data


In [54]:
for i in range(0,len(data_list)):
    if data_list[int(i)]=="IBM":
        print data1.head(7)
    elif data_list[int(i)]=="AAPL":
        print  data2.head(7)
    elif data_list[int(i)] =="MSFT":
        print data3.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

2.5 stock data graph


In [55]:
for i in range(0,len(data_list)):
    if data_list[int(i)]=="IBM":
        plt.plot(data1["High"],"red")
    elif data_list[int(i)]=="AAPL":
        plt.plot(data2["High"],"blue")
    elif data_list[int(i)] =="MSFT":
        plt.plot(data3["High"],"green")



In [ ]: