diff options
author | Santosh G. Vattam | 2010-03-30 19:17:12 +0530 |
---|---|---|
committer | Santosh G. Vattam | 2010-03-30 19:17:12 +0530 |
commit | da45223d32d5c942ce46afbc54d39af8d66cfa9c (patch) | |
tree | b8c628efe731f47dd27ad303d94c762afb2c13da /statistics.txt | |
parent | fd65842b0be72d2c2f502fbcb34ba3c77a8ac528 (diff) | |
download | st-scripts-da45223d32d5c942ce46afbc54d39af8d66cfa9c.tar.gz st-scripts-da45223d32d5c942ce46afbc54d39af8d66cfa9c.tar.bz2 st-scripts-da45223d32d5c942ce46afbc54d39af8d66cfa9c.zip |
Minor edits to statistics.txt
Diffstat (limited to 'statistics.txt')
-rw-r--r-- | statistics.txt | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/statistics.txt b/statistics.txt index c9f4241..796943b 100644 --- a/statistics.txt +++ b/statistics.txt @@ -2,10 +2,15 @@ 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. +the mean, median and standard deviation for a very large data set. +Let's start with calculating the mean acceleration due to gravity based on the data from pendulum.txt. +We first create an empty list `g_list' to which we shall append the values of `g'. In []: g_list = [] + +For each pair of `L' and `t' values in the file `pendulum.txt' we calculate the +value of `g' and append it to the list `g_list' In []: for line in open('pendulum.txt'): .... point = line.split() .... L = float(point[0]) @@ -13,40 +18,60 @@ In []: for line in open('pendulum.txt'): .... g = 4 * pi * pi * L / (t * t) .... g_list.append(g) +We proceed to calculate the mean of the value of `g' from the list `g_list'. +Here we shall show three ways of calculating the mean. +Firstly, we calculate the sum `total' of the values in `g_list'. In []: total = 0 In []: for g in g_list: ....: total += g ....: +Once we have the total we calculate by dividing the `total' by the length of `g_list' + In []: g_mean = total / len(g_list) In []: print 'Mean: ', g_mean +The second method is slightly simpler. Python provides a built-in function called "sum()" that computes the sum of all the elements in a list. In []: g_mean = sum(g_list) / len(g_list) In []: print 'Mean: ', g_mean +The third method is the simplest. Python provides a built-in function `mean' that +calculates the mean of all the elements in a list. In []: g_mean = mean(g_list) In []: print 'Mean: ', g_mean +Python provides support for dictionaries. Dictionaries are key value pairs. Lists are indexed by integers while dictionaries are indexed by strings. For example: +In []: d = {'png' : 'image', + 'txt' : 'text', + 'py' : 'python'} +is a dictionary. The first element in the pair is called the `key' and the second +is called the `value'. The key always has to be a string while the value can be +of any type. -In []: d = {'png' : 'image file', - 'txt' : 'text file', - 'py' : 'python code' - 'java': 'bad code', - 'cpp': 'complex code'} - +Dictionaries are indexed using their keys as shown In []: d['txt'] -Out[]: 'text file' +Out[]: 'text' +In []: d['png'] +Out[]: 'image' + +The dictionaries can be searched for the presence of a certain key by typing In []: 'py' in d Out[]: True In []: 'jpg' in d Out[]: False +Please note the values cannot be searched in a dictionaries. In []: d.keys() -Out[]: ['cpp', 'py', 'txt', 'java', 'png'] +Out[]: ['py', 'txt', 'png'] +is used to obtain the list of all keys in a dictionary In []: d.values() -Out[]: ['complex code', 'python code', - 'text file', 'bad code', - 'image file'] +Out[]: ['python', 'text', 'image'] +is used to obtain the list of all values in a dictionary + +In []: d +Out[]: {'png': 'image', 'py': 'python', 'txt': 'text'} +Please observe that dictionaries do not preserve the order in which the items +were entered. The order of the elements in a dictionary should not be relied upon. |