|
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 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
26 |
|
27 .. #[[Anoop: Slides contain only outline and summary |
|
28 |
|
29 Script |
|
30 ------ |
|
31 {{{ Show the slide containing title }}} |
|
32 |
|
33 Hello friends and welcome to the tutorial on getting started with |
|
34 lists. |
|
35 |
|
36 {{{ Show the slide containing the outline slide }}} |
|
37 |
|
38 In this tutorial we will be getting acquainted with a python data |
|
39 structure called lists. We will learn :: |
|
40 |
|
41 * How to create lists |
|
42 * Structure of lists |
|
43 * Access list elements |
|
44 * Append elements to lists |
|
45 * Delete elements from lists |
|
46 |
|
47 List is a compound data type, it can contain data of other data |
|
48 types. List is also a sequence data type, all the elements are in |
|
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.]] |
|
54 |
|
55 We will first create an empty list with no elements. On your IPython |
|
56 shell type :: |
|
57 |
|
58 empty = [] |
|
59 type(empty) |
|
60 |
|
61 |
|
62 This is an empty list without any elements. |
|
63 |
|
64 .. #[[Anoop: the document has to be continous, without any |
|
65 subheadings, removing * Filled lists]] |
|
66 |
|
67 Lets now see how to define a non-empty list. We do it as,:: |
|
68 |
|
69 nonempty = ['spam', 'eggs', 100, 1.234] |
|
70 |
|
71 Thus the simplest way of creating a list is typing out a sequence |
|
72 of comma-separated values (items) between square brackets. |
|
73 All the list items need not be of the same data type. |
|
74 |
|
75 As we can see lists can contain different kinds of data. In the |
|
76 previous example 'spam' and 'eggs' are strings and 100 and 1.234 are |
|
77 integer and float. Thus we can put elements of heterogenous types in |
|
78 lists including list itself. |
|
79 |
|
80 .. #[[Anoop: the sentence "Thus list themselves can be one of the |
|
81 element types possible in lists" is not clear, rephrase it.]] |
|
82 |
|
83 Example :: |
|
84 |
|
85 listinlist=[[4,2,3,4],'and', 1, 2, 3, 4] |
|
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. :: |
|
90 |
|
91 nonempty[0] |
|
92 nonempty[1] |
|
93 nonempty[3] |
|
94 |
|
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:: |
|
109 |
|
110 nonempty[-1] |
|
111 nonempty[-2] |
|
112 nonempty[-4] |
|
113 |
|
114 -1 gives the last element which is the 4th element , -2 second to last |
|
115 and -4 gives the fourth from last element which is first element. |
|
116 |
|
117 We can append elements to the end of a list using append command. :: |
|
118 |
|
119 nonempty.append('onemore') |
|
120 nonempty |
|
121 nonempty.append(6) |
|
122 nonempty |
|
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 |
|
137 As we can see non empty appends 'onemore' and 6 at the end. |
|
138 |
|
139 Using len function we can check the number of elements in the list |
|
140 nonempty. In this case it 6 :: |
|
141 |
|
142 len(nonempty) |
|
143 |
|
144 |
|
145 |
|
146 Just like we can append elements to a list we can also remove them. |
|
147 There are two ways of doing it. One is by using index. :: |
|
148 |
|
149 del(nonempty[1]) |
|
150 |
|
151 |
|
152 |
|
153 deletes the element at index 1, 'eggs' which is the second element of |
|
154 the list. The other way is removing element by content. Lets say one |
|
155 wishes to delete 100 from nonempty list the syntax of the command |
|
156 should be |
|
157 |
|
158 .. #[[Anoop: let x = [1,2,1,3] |
|
159 now x.remove(x[2]) |
|
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 |
|
170 experiment. :: |
|
171 |
|
172 nonempty.append('spam') |
|
173 nonempty |
|
174 nonempty.remove('spam') |
|
175 nonempty |
|
176 |
|
177 If we check now we will see that the first occurence 'spam' is removed |
|
178 thus remove removes the first occurence of the element in the sequence |
|
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 |
|
198 |
|
199 |
|
200 {{{Slide for Summary }}} |
|
201 |
|
202 |
|
203 In this tutorial we came across a sequence data type called lists. :: |
|
204 |
|
205 * We learned how to create lists. |
|
206 * How to access lists. |
|
207 * Append elements to list. |
|
208 * Delete Element from list. |
|
209 * And Checking list length. |
|
210 |
|
211 |
|
212 |
|
213 {{{ show Sponsored by Fossee Slide }}} |
|
214 |
|
215 This tutorial was created as a part of FOSSEE project. |
|
216 |
|
217 I hope you found this tutorial useful. |
|
218 |
|
219 Thank You |
|
220 |
|
221 .. |
|
222 * Author : Amit Sethi |
|
223 * First Reviewer : |
|
224 * Second Reviewer : Nishanth |