17 |
17 |
18 .. 1. getting started with strings |
18 .. 1. getting started with strings |
19 .. #. getting started with lists |
19 .. #. getting started with lists |
20 .. #. basic datatypes |
20 .. #. basic datatypes |
21 |
21 |
22 .. Author : Puneeth |
22 .. Author : Amit |
23 Internal Reviewer : Amit |
23 Internal Reviewer : Anoop Jacob Thomas <anoop@fossee.in> |
24 External Reviewer : |
24 External Reviewer : |
25 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
25 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
26 |
26 |
|
27 .. #[[Anoop: Slides contain only outline and summary |
|
28 |
27 Script |
29 Script |
28 ------ |
30 ------ |
|
31 {{{ Show the slide containing title }}} |
|
32 |
29 Hello friends and welcome to the tutorial on getting started with |
33 Hello friends and welcome to the tutorial on getting started with |
30 lists. |
34 lists. |
31 |
|
32 {{{ Show the slide containing title }}} |
|
33 |
35 |
34 {{{ Show the slide containing the outline slide }}} |
36 {{{ Show the slide containing the outline slide }}} |
35 |
37 |
36 In this tutorial we will be getting acquainted with a python data |
38 In this tutorial we will be getting acquainted with a python data |
37 structure called lists. We will learn :: |
39 structure called lists. We will learn :: |
38 |
40 |
39 * How to create lists |
41 * How to create lists |
40 * Structure of lists |
42 * Structure of lists |
41 * Access list elements |
43 * Access list elements |
42 * Append elements to lists |
44 * Append elements to lists |
43 * Deleting elements from lists |
45 * Delete elements from lists |
44 |
46 |
45 List is a compound data type, it can contain data of other data |
47 List is a compound data type, it can contain data of other data |
46 types. List is also a sequence data type, all the elements are in |
48 types. List is also a sequence data type, all the elements are in |
47 order and there order has a meaning. |
49 order and the order has a meaning. |
|
50 |
|
51 .. #[[Anoop: "all the elements are in order and **there** order has a |
|
52 meaning." - I guess something is wrong here, I am not able to |
|
53 follow this.]] |
48 |
54 |
49 We will first create an empty list with no elements. On your IPython |
55 We will first create an empty list with no elements. On your IPython |
50 shell type :: |
56 shell type :: |
51 |
57 |
52 empty = [] |
58 empty = [] |
53 type(empty) |
59 type(empty) |
54 |
60 |
55 |
61 |
56 This is an empty list without any elements. |
62 This is an empty list without any elements. |
57 |
63 |
58 * Filled lists |
64 .. #[[Anoop: the document has to be continous, without any |
59 |
65 subheadings, removing * Filled lists]] |
60 Lets now define a list, nonempty and fill it with some random elements. |
66 |
61 |
67 Lets now see how to define a non-empty list. We do it as,:: |
62 nonempty = ['spam', 'eggs', 100, 1.234] |
68 |
|
69 nonempty = ['spam', 'eggs', 100, 1.234] |
63 |
70 |
64 Thus the simplest way of creating a list is typing out a sequence |
71 Thus the simplest way of creating a list is typing out a sequence |
65 of comma-separated values (items) between square brackets. |
72 of comma-separated values (items) between square brackets. |
66 All the list items need not have the same data type. |
73 All the list items need not be of the same data type. |
67 |
|
68 |
|
69 |
74 |
70 As we can see lists can contain different kinds of data. In the |
75 As we can see lists can contain different kinds of data. In the |
71 previous example 'spam' and 'eggs' are strings and 100 and 1.234 |
76 previous example 'spam' and 'eggs' are strings and 100 and 1.234 are |
72 integer and float. Thus we can put elements of heterogenous types in |
77 integer and float. Thus we can put elements of heterogenous types in |
73 lists. Thus list themselves can be one of the element types possible |
78 lists including list itself. |
74 in lists. Thus lists can also contain other lists. Example :: |
79 |
75 |
80 .. #[[Anoop: the sentence "Thus list themselves can be one of the |
76 list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4] |
81 element types possible in lists" is not clear, rephrase it.]] |
77 |
82 |
78 We access list elements using the number of index. The |
83 Example :: |
79 index begins from 0. So for list nonempty, nonempty[0] gives the |
84 |
80 first element, nonempty[1] the second element and so on and |
85 listinlist=[[4,2,3,4],'and', 1, 2, 3, 4] |
81 nonempty[3] the last element. :: |
86 |
|
87 We access list elements using the index. The index begins from 0. So |
|
88 for list nonempty, nonempty[0] gives the first element, nonempty[1] |
|
89 the second element and so on and nonempty[3] the last element. :: |
82 |
90 |
83 nonempty[0] |
91 nonempty[0] |
84 nonempty[1] |
92 nonempty[1] |
85 nonempty[3] |
93 nonempty[3] |
86 |
94 |
87 We can also access the elememts from the end using negative indices :: |
95 Following is an exercise that you must do. |
|
96 |
|
97 %% %% What happens when you do nonempty[-1]. |
|
98 |
|
99 Please, pause the video here. Do the exercise and then continue. |
|
100 |
|
101 .. #[[Anoop: was negative indices introduced earlier, if not may be we |
|
102 can ask them to try out nonempty[-1] and see what happens and then |
|
103 tell that it gives the last element in the list.]] |
|
104 |
|
105 As you can see you get the last element which is 1.234. |
|
106 |
|
107 |
|
108 In python negative indices are used to access elements from the end:: |
88 |
109 |
89 nonempty[-1] |
110 nonempty[-1] |
90 nonempty[-2] |
111 nonempty[-2] |
91 nonempty[-4] |
112 nonempty[-4] |
92 |
113 |
93 -1 gives the last element which is the 4th element , -2 second to last and -4 gives the fourth |
114 -1 gives the last element which is the 4th element , -2 second to last |
94 from last element which is first element. |
115 and -4 gives the fourth from last element which is first element. |
95 |
116 |
96 We can append elements to the end of a list using append command. :: |
117 We can append elements to the end of a list using append command. :: |
97 |
118 |
98 nonempty.append('onemore') |
119 nonempty.append('onemore') |
99 nonempty |
120 nonempty |
100 nonempty.append(6) |
121 nonempty.append(6) |
101 nonempty |
122 nonempty |
102 |
123 |
|
124 Following are exercises that you must do. |
|
125 |
|
126 %% %% What is the syntax to get the element 'and' |
|
127 in the list,listinlist ? |
|
128 |
|
129 |
|
130 %% %% How would you get 'and' using negative indices? |
|
131 |
|
132 Please, pause the video here. Do the exercise and then continue. |
|
133 |
|
134 The solution is on your screen |
|
135 |
|
136 |
103 As we can see non empty appends 'onemore' and 6 at the end. |
137 As we can see non empty appends 'onemore' and 6 at the end. |
104 |
138 |
105 |
|
106 |
|
107 Using len function we can check the number of elements in the list |
139 Using len function we can check the number of elements in the list |
108 nonempty. In this case it being 6 :: |
140 nonempty. In this case it 6 :: |
109 |
141 |
110 len(nonempty) |
142 len(nonempty) |
111 |
143 |
112 |
144 |
113 |
145 |
119 |
151 |
120 |
152 |
121 deletes the element at index 1, i.e the second element of the |
153 deletes the element at index 1, i.e the second element of the |
122 list, 'eggs'. The other way is removing element by content. Lets say |
154 list, 'eggs'. The other way is removing element by content. Lets say |
123 one wishes to delete 100 from nonempty list the syntax of the command |
155 one wishes to delete 100 from nonempty list the syntax of the command |
124 should be :: |
156 should be |
125 |
157 |
126 nonempty.remove(100) |
158 .. #[[Anoop: let x = [1,2,1,3] |
127 |
159 now x.remove(x[2]) |
128 but what if their were two 100's. To check that lets do a small |
160 still x is [2,1,3] so that is not the way to remove |
|
161 element by index, it removed first occurrence of 1(by |
|
162 content) and not based on index, so make necessary |
|
163 changes]] |
|
164 |
|
165 :: |
|
166 |
|
167 nonempty.remove(100) |
|
168 |
|
169 but what if there were two 100's. To check that lets do a small |
129 experiment. :: |
170 experiment. :: |
130 |
171 |
131 nonempty.append('python') |
172 nonempty.append('spam') |
132 nonempty |
173 nonempty |
133 nonempty.remove('python') |
174 nonempty.remove('spam') |
134 nonempty |
175 nonempty |
135 |
176 |
136 If we check a now we will see that the first occurence 'spam' is removed |
177 If we check now we will see that the first occurence 'spam' is removed |
137 thus remove removes the first occurence of the element in the sequence |
178 thus remove removes the first occurence of the element in the sequence |
138 and leaves others untouched. |
179 and leaves others untouched. |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 .. #[[Anoop: does it have two spams or two pythons?]] |
|
186 |
|
187 .. #[[Anoop: there are no exercises/solved problems in this script, |
|
188 add them]] |
|
189 |
|
190 Following are exercises that you must do. |
|
191 |
|
192 %% %% Remove the third element from the list, listinlist. |
|
193 |
|
194 %% %% Remove 'and' from the list, listinlist. |
|
195 |
|
196 Please, pause the video here. Do the exercise and then continue. |
|
197 |
139 |
198 |
140 |
199 |
141 {{{Slide for Summary }}} |
200 {{{Slide for Summary }}} |
142 |
201 |
143 |
202 |