1 Hello welcome to the tutorial on statistics and dictionaries in Python. |
1 Hello welcome to the tutorial on statistics and dictionaries in Python. |
2 |
2 |
3 In the previous tutorial we saw the `for' loop and lists. Here we shall look into |
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 |
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. |
5 the mean, median and standard deviation for a very large data set. |
6 |
6 |
|
7 Let's start with calculating the mean acceleration due to gravity based on the data from pendulum.txt. |
7 |
8 |
|
9 We first create an empty list `g_list' to which we shall append the values of `g'. |
8 In []: g_list = [] |
10 In []: g_list = [] |
|
11 |
|
12 For each pair of `L' and `t' values in the file `pendulum.txt' we calculate the |
|
13 value of `g' and append it to the list `g_list' |
9 In []: for line in open('pendulum.txt'): |
14 In []: for line in open('pendulum.txt'): |
10 .... point = line.split() |
15 .... point = line.split() |
11 .... L = float(point[0]) |
16 .... L = float(point[0]) |
12 .... t = float(point[1]) |
17 .... t = float(point[1]) |
13 .... g = 4 * pi * pi * L / (t * t) |
18 .... g = 4 * pi * pi * L / (t * t) |
14 .... g_list.append(g) |
19 .... g_list.append(g) |
15 |
20 |
|
21 We proceed to calculate the mean of the value of `g' from the list `g_list'. |
|
22 Here we shall show three ways of calculating the mean. |
|
23 Firstly, we calculate the sum `total' of the values in `g_list'. |
16 In []: total = 0 |
24 In []: total = 0 |
17 In []: for g in g_list: |
25 In []: for g in g_list: |
18 ....: total += g |
26 ....: total += g |
19 ....: |
27 ....: |
20 |
28 |
|
29 Once we have the total we calculate by dividing the `total' by the length of `g_list' |
|
30 |
21 In []: g_mean = total / len(g_list) |
31 In []: g_mean = total / len(g_list) |
22 In []: print 'Mean: ', g_mean |
32 In []: print 'Mean: ', g_mean |
23 |
33 |
|
34 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. |
24 In []: g_mean = sum(g_list) / len(g_list) |
35 In []: g_mean = sum(g_list) / len(g_list) |
25 In []: print 'Mean: ', g_mean |
36 In []: print 'Mean: ', g_mean |
26 |
37 |
|
38 The third method is the simplest. Python provides a built-in function `mean' that |
|
39 calculates the mean of all the elements in a list. |
27 In []: g_mean = mean(g_list) |
40 In []: g_mean = mean(g_list) |
28 In []: print 'Mean: ', g_mean |
41 In []: print 'Mean: ', g_mean |
29 |
42 |
|
43 Python provides support for dictionaries. Dictionaries are key value pairs. Lists are indexed by integers while dictionaries are indexed by strings. For example: |
|
44 In []: d = {'png' : 'image', |
|
45 'txt' : 'text', |
|
46 'py' : 'python'} |
|
47 is a dictionary. The first element in the pair is called the `key' and the second |
|
48 is called the `value'. The key always has to be a string while the value can be |
|
49 of any type. |
30 |
50 |
31 In []: d = {'png' : 'image file', |
51 Dictionaries are indexed using their keys as shown |
32 'txt' : 'text file', |
52 In []: d['txt'] |
33 'py' : 'python code' |
53 Out[]: 'text' |
34 'java': 'bad code', |
|
35 'cpp': 'complex code'} |
|
36 |
54 |
37 In []: d['txt'] |
55 In []: d['png'] |
38 Out[]: 'text file' |
56 Out[]: 'image' |
39 |
57 |
|
58 The dictionaries can be searched for the presence of a certain key by typing |
40 In []: 'py' in d |
59 In []: 'py' in d |
41 Out[]: True |
60 Out[]: True |
42 |
61 |
43 In []: 'jpg' in d |
62 In []: 'jpg' in d |
44 Out[]: False |
63 Out[]: False |
|
64 Please note the values cannot be searched in a dictionaries. |
45 |
65 |
46 In []: d.keys() |
66 In []: d.keys() |
47 Out[]: ['cpp', 'py', 'txt', 'java', 'png'] |
67 Out[]: ['py', 'txt', 'png'] |
|
68 is used to obtain the list of all keys in a dictionary |
48 |
69 |
49 In []: d.values() |
70 In []: d.values() |
50 Out[]: ['complex code', 'python code', |
71 Out[]: ['python', 'text', 'image'] |
51 'text file', 'bad code', |
72 is used to obtain the list of all values in a dictionary |
52 'image file'] |
73 |
|
74 In []: d |
|
75 Out[]: {'png': 'image', 'py': 'python', 'txt': 'text'} |
|
76 Please observe that dictionaries do not preserve the order in which the items |
|
77 were entered. The order of the elements in a dictionary should not be relied upon. |