33 Windows users just double click on the file to open it. Please be careful not to edit the file. |
33 Windows users just double click on the file to open it. Please be careful not to edit the file. |
34 |
34 |
35 The first column is the length of the pendulum and the second column is the time. We read the file line-by-line, collect the data into lists and plot them. |
35 The first column is the length of the pendulum and the second column is the time. We read the file line-by-line, collect the data into lists and plot them. |
36 |
36 |
37 Let's begin with initializing three empty lists for length, time-period and square of the time-period. |
37 Let's begin with initializing three empty lists for length, time-period and square of the time-period. |
38 L = [] |
38 l = [] |
39 t = [] |
39 t = [] |
40 tsq = [] |
40 tsq = [] |
41 |
41 |
42 Initializing an empty list is done as shown above using just a pair of square brackets without any content in them. |
42 Initializing an empty list is done as shown above using just a pair of square brackets without any content in them. |
43 |
43 |
53 point = line.split() |
53 point = line.split() |
54 the split function returns a list of elements from the 'line' variable split over spaces. In this case it will have two elements, first is length and second is time. |
54 the split function returns a list of elements from the 'line' variable split over spaces. In this case it will have two elements, first is length and second is time. |
55 |
55 |
56 Note the indentation here. Everything inside the 'for' loop has to be indented by 4 spaces. |
56 Note the indentation here. Everything inside the 'for' loop has to be indented by 4 spaces. |
57 Then we append the length and time values to the appropriate lists. Since we cannot perform mathematical operations on strings, we need to convert the strings to floats, before appending to the lists. |
57 Then we append the length and time values to the appropriate lists. Since we cannot perform mathematical operations on strings, we need to convert the strings to floats, before appending to the lists. |
58 L.append(float(point[0])) |
58 l.append(float(point[0])) |
59 append is a function used to append a single element to a list. |
59 append is a function used to append a single element to a list. |
60 t.append(float(point[1])) |
60 t.append(float(point[1])) |
61 |
61 |
62 Now we have the time and length values in two lists. Now to get the square of the time values, we shall write one more 'for' loop which will iterate through list 't' |
62 Now we have the time and length values in two lists. Now to get the square of the time values, we shall write one more 'for' loop which will iterate through list 't' |
63 |
63 |
64 for time in t: |
64 for time in t: |
65 tsq.append(time*time) |
65 tsq.append(time*time) |
66 |
66 |
67 Let us now verify if L, t and tsq have the same number of elements. Type |
67 Let us now verify if l, t and tsq have the same number of elements. Type |
68 print len(L), len(t), len(tsq) |
68 print len(l), len(t), len(tsq) |
69 |
69 |
70 Now we have verified that all three have the same dimensions. Lists l and tsq have the required data. Let's now plot them, as we did earlier. |
70 Now we have verified that all three have the same dimensions. lists l and tsq have the required data. Let's now plot them, as we did earlier. |
71 plot(l, tsq, 'o') |
71 plot(l, tsq, 'o') |
72 |
72 |
73 So here is the required plot. |
73 So here is the required plot. |
74 In this way, you can plot data from files. Hope this information was helpful. Thank you. |
74 In this way, you can plot data from files. Hope this information was helpful. Thank you. |