statistics.txt
changeset 6 e1fcec83e1ab
child 7 9794cc414498
equal deleted inserted replaced
0:67604aed10e0 6:e1fcec83e1ab
       
     1 Hello welcome to the tutorial on statistics and dictionaries in Python.
       
     2 
       
     3 In the previous tutorial we saw the `for' loop and lists. Here we shall look into
       
     4 calculating mean for the same pendulum experiment and then move on to calculate
       
     5 the mean, median and mode for a very large data set.
       
     6 
       
     7 
       
     8 In []: g_list = []
       
     9 In []: for line in open('pendulum.txt'):
       
    10   ....     point = line.split()
       
    11   ....     L = float(point[0])
       
    12   ....     t = float(point[1])
       
    13   ....     g = 4 * pi * pi * L / (t * t)
       
    14   ....     g_list.append(g)
       
    15 
       
    16 In []: total = 0
       
    17 In []: for g in g_list:
       
    18  ....:     total += g
       
    19  ....:
       
    20 
       
    21 In []: g_mean = total / len(g_list)
       
    22 In []: print 'Mean: ', g_mean
       
    23 
       
    24 In []: g_mean = sum(g_list) / len(g_list)
       
    25 In []: print 'Mean: ', g_mean
       
    26 
       
    27 In []: g_mean = mean(g_list)
       
    28 In []: print 'Mean: ', g_mean
       
    29 
       
    30 
       
    31 In []: d = {'png' : 'image file',
       
    32       'txt' : 'text file', 
       
    33       'py' : 'python code'
       
    34       'java': 'bad code', 
       
    35       'cpp': 'complex code'}
       
    36 
       
    37 In []: d['txt']
       
    38 Out[]: 'text file'
       
    39 
       
    40 In []: 'py' in d
       
    41 Out[]: True
       
    42 
       
    43 In []: 'jpg' in d
       
    44 Out[]: False
       
    45 
       
    46 In []: d.keys()
       
    47 Out[]: ['cpp', 'py', 'txt', 'java', 'png']
       
    48 
       
    49 In []: d.values()
       
    50 Out[]: ['complex code', 'python code',
       
    51         'text file', 'bad code', 
       
    52         'image file']