|
1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. By the end of this tutorial, you will be able to |
|
5 |
|
6 .. Create Lists. |
|
7 .. Access List elements. |
|
8 .. Append elemets to list |
|
9 .. Delete list elemets |
|
10 |
|
11 .. 1. getting started with ipython |
|
12 |
|
13 |
|
14 |
|
15 .. Prerequisites |
|
16 .. ------------- |
|
17 |
|
18 .. 1. getting started with strings |
|
19 .. #. getting started with lists |
|
20 .. #. basic datatypes |
|
21 |
|
22 .. Author : Amit |
|
23 Internal Reviewer : Anoop Jacob Thomas <anoop@fossee.in> |
|
24 External Reviewer : |
|
25 Language Reviewer : Bhanukiran |
|
26 Checklist OK? : <12-11-2010, Anand, OK> [2010-10-05] |
|
27 |
|
28 .. #[[Anoop: Slides contain only outline and summary |
|
29 |
|
30 Script |
|
31 ------ |
|
32 {{{ Show the slide containing title }}} |
|
33 |
|
34 Hello friends and welcome to the tutorial on getting started with |
|
35 lists. |
|
36 |
|
37 {{{ Show the slide containing the outline slide }}} |
|
38 |
|
39 In this tutorial we will be getting acquainted with a python data |
|
40 structure called lists. We will learn :: |
|
41 |
|
42 * How to create lists |
|
43 * Structure of lists |
|
44 * Access list elements |
|
45 * Append elements to lists |
|
46 * Delete elements from lists |
|
47 |
|
48 List is a compound data type, it can contain data of mutually |
|
49 different datatypes. List is also a sequence data type, all the |
|
50 elements are arranged in a given order. |
|
51 |
|
52 .. #[[Anoop: "all the elements are in order and **there** order has a |
|
53 meaning." - I guess something is wrong here, I am not able to |
|
54 follow this.]] |
|
55 |
|
56 We will first create an empty list with no elements. On your IPython |
|
57 shell type :: |
|
58 |
|
59 empty = [] |
|
60 type(empty) |
|
61 |
|
62 |
|
63 This is an empty list without any elements. |
|
64 |
|
65 .. #[[Anoop: the document has to be continous, without any |
|
66 subheadings, removing * Filled lists]] |
|
67 |
|
68 Lets now see how to define a non-empty list. We do it as,:: |
|
69 |
|
70 nonempty = ['spam', 'eggs', 100, 1.234] |
|
71 |
|
72 Thus the simplest way of creating a list is typing out a sequence |
|
73 of comma-separated values (or items) between two square brackets. |
|
74 |
|
75 As we can see lists can contain different kinds of data. In the |
|
76 previous example 'spam' and 'eggs' are strings whereas 100 and 1.234 are |
|
77 integer and float respectively. Thus we can put elements of different types in |
|
78 lists including lists itself. This property makes lists heterogeneous |
|
79 data structures. |
|
80 |
|
81 .. #[[Anoop: the sentence "Thus list themselves can be one of the |
|
82 element types possible in lists" is not clear, rephrase it.]] |
|
83 |
|
84 Example :: |
|
85 |
|
86 listinlist=[[4,2,3,4],'and', 1, 2, 3, 4] |
|
87 |
|
88 We access an element of a list using its corresponding index. Index of |
|
89 the first element of a list is 0. So for the list nonempty, nonempty[0] |
|
90 gives the first element, nonempty[1] the second element and so on and |
|
91 nonempty[3] the last element. :: |
|
92 |
|
93 nonempty[0] |
|
94 nonempty[1] |
|
95 nonempty[3] |
|
96 |
|
97 Following is an exercise that you must do. |
|
98 |
|
99 %% %% What happens when you do nonempty[-1]. |
|
100 |
|
101 Please, pause the video here. Do the exercise and then continue. |
|
102 |
|
103 .. #[[Anoop: was negative indices introduced earlier, if not may be we |
|
104 can ask them to try out nonempty[-1] and see what happens and then |
|
105 tell that it gives the last element in the list.]] |
|
106 |
|
107 As you can see you get the last element which is 1.234. |
|
108 |
|
109 |
|
110 In python negative indices are used to access elements from the end:: |
|
111 |
|
112 nonempty[-1] |
|
113 nonempty[-2] |
|
114 nonempty[-4] |
|
115 |
|
116 -1 gives the last element which is the 4th element , -2 second to last |
|
117 and -4 gives the fourth from the last which, in this case, is the first element. |
|
118 |
|
119 We can append elements to the end of a list using the method append. :: |
|
120 |
|
121 nonempty.append('onemore') |
|
122 nonempty |
|
123 nonempty.append(6) |
|
124 nonempty |
|
125 |
|
126 Following are exercises that you must do. |
|
127 |
|
128 %% %% What is the syntax to get the element 'and' |
|
129 in the list,listinlist ? |
|
130 |
|
131 |
|
132 %% %% How would you get 'and' using negative indices? |
|
133 |
|
134 Please, pause the video here. Do the exercise and then continue. |
|
135 |
|
136 The solution is on your screen |
|
137 |
|
138 |
|
139 As we can see nonempty is appended with 'onemore' and 6 at the end. |
|
140 |
|
141 Using len function we can check the number of elements in the list |
|
142 nonempty. In this case it is 6 :: |
|
143 |
|
144 len(nonempty) |
|
145 |
|
146 |
|
147 |
|
148 Just like we can append elements to a list we can also remove them. |
|
149 There are two ways of doing it. One is by using index. :: |
|
150 |
|
151 del(nonempty[1]) |
|
152 |
|
153 |
|
154 |
|
155 deletes the element at index 1, i.e the second element of the |
|
156 list, 'eggs'. The other way is removing element by content. Lets say |
|
157 one wishes to delete 100 from nonempty list the syntax of the command |
|
158 would be |
|
159 |
|
160 .. #[[Anoop: let x = [1,2,1,3] |
|
161 now x.remove(x[2]) |
|
162 still x is [2,1,3] so that is not the way to remove |
|
163 element by index, it removed first occurrence of 1(by |
|
164 content) and not based on index, so make necessary |
|
165 changes]] |
|
166 |
|
167 :: |
|
168 |
|
169 nonempty.remove(100) |
|
170 |
|
171 but what if there were two 100's. To check that lets do a small |
|
172 experiment. :: |
|
173 |
|
174 nonempty.append('spam') |
|
175 nonempty |
|
176 nonempty.remove('spam') |
|
177 nonempty |
|
178 |
|
179 If we check now we will see that the first occurence 'spam' is removed |
|
180 and therefore `remove` removes the first occurence of the element in the sequence |
|
181 and leaves others untouched. |
|
182 |
|
183 One should remember this that while del removes by index number, |
|
184 `remove` removes on the basis of content being passed on. For instance |
|
185 if :: |
|
186 |
|
187 k = [1,2,1,3] |
|
188 del([k[2]) |
|
189 |
|
190 gives us [1,2,3]. :: |
|
191 |
|
192 k.remove(x[2]) |
|
193 |
|
194 will give us [2,1,3]. Since it deletes the first occurence of what is |
|
195 returned by x[2] which is 1. |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 .. #[[Anoop: does it have two spams or two pythons?]] |
|
204 |
|
205 .. #[[Anoop: there are no exercises/solved problems in this script, |
|
206 add them]] |
|
207 |
|
208 Following are exercises that you must do. |
|
209 |
|
210 %% %% Remove the third element from the list, listinlist. |
|
211 |
|
212 %% %% Remove 'and' from the list, listinlist. |
|
213 |
|
214 Please, pause the video here. Do the exercise and then continue. |
|
215 |
|
216 |
|
217 |
|
218 {{{Slide for Summary }}} |
|
219 |
|
220 |
|
221 In this tutorial we came across a sequence data type called lists. :: |
|
222 |
|
223 * We learned how to create lists. |
|
224 * How to access lists. |
|
225 * Append elements to list. |
|
226 * Delete Element from list. |
|
227 * And Checking list length. |
|
228 |
|
229 |
|
230 |
|
231 {{{ show Sponsored by Fossee Slide }}} |
|
232 |
|
233 This tutorial was created as a part of FOSSEE project. |
|
234 |
|
235 I hope you found this tutorial useful. |
|
236 |
|
237 Thank You |
|
238 |
|
239 .. |
|
240 * Author : Amit Sethi |
|
241 * First Reviewer : |
|
242 * Second Reviewer : Nishanth |