Creating Class and inserting function in it

#python #statistical_tools

we can merge the different functions into a class. In this exp. stats is a class.

 

import math
class stats:
    def __init__(self, st):
        self.lst=st
        
    def __str__(self):
        return str(self.lst) 
        
    def summation(self):
        sum=0
        for i in range(0,len(self.lst)):
            sum=sum + self.lst[i]
        return sum
    
    def mean(self):
        p=self.summation()
        
        return (p/len(self.lst))
    
    def Median(self):
        if (len(self.lst) % 2 ==0):
            n=len(self.lst)
            sum=self.lst[int(n/2)] + self.lst[int(n/2) + 1]
            return sum/2
        else:
            n=len(self.lst)
            return self.lst[int((n-1)/2)]
    
    def cummlative(self):
        temp=[]
        temp=temp+self.lst
        for i in range(0,len(self.lst)):
            temp[i]=temp[i]+temp[i-1]
            return temp
    def Sorting(self):
        f=sorted(self.lst)
        return f
    

y=[1,2,3,4,5,3,4,5,6,7,8,9,0,10,11]
a=stats(y)
b=a.Median()
c=a.mean()
d=a.Sorting()
d={1:"a",2:b,3:c,4:d}

print(a)
print(a.summation())
print(a.mean())
print(a.Median())
print(a.cummlative())
print(a.Sorting())
for key in d:
    print(key,d[key])




Output:--



Leave a Reply