# HG changeset patch # User Santosh G. Vattam # Date 1269940512 -19800 # Node ID e1fcec83e1abee270d0aaed50573c4973475e9c1 # Parent 67604aed10e0fb4f208b42f9dd7843846a10a2fd Added statistics.txt. diff -r 67604aed10e0 -r e1fcec83e1ab statistics.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/statistics.txt Tue Mar 30 14:45:12 2010 +0530 @@ -0,0 +1,52 @@ +Hello welcome to the tutorial on statistics and dictionaries in Python. + +In the previous tutorial we saw the `for' loop and lists. Here we shall look into +calculating mean for the same pendulum experiment and then move on to calculate +the mean, median and mode for a very large data set. + + +In []: g_list = [] +In []: for line in open('pendulum.txt'): + .... point = line.split() + .... L = float(point[0]) + .... t = float(point[1]) + .... g = 4 * pi * pi * L / (t * t) + .... g_list.append(g) + +In []: total = 0 +In []: for g in g_list: + ....: total += g + ....: + +In []: g_mean = total / len(g_list) +In []: print 'Mean: ', g_mean + +In []: g_mean = sum(g_list) / len(g_list) +In []: print 'Mean: ', g_mean + +In []: g_mean = mean(g_list) +In []: print 'Mean: ', g_mean + + +In []: d = {'png' : 'image file', + 'txt' : 'text file', + 'py' : 'python code' + 'java': 'bad code', + 'cpp': 'complex code'} + +In []: d['txt'] +Out[]: 'text file' + +In []: 'py' in d +Out[]: True + +In []: 'jpg' in d +Out[]: False + +In []: d.keys() +Out[]: ['cpp', 'py', 'txt', 'java', 'png'] + +In []: d.values() +Out[]: ['complex code', 'python code', + 'text file', 'bad code', + 'image file']