1 <html> |
|
2 <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Chapter. list_tuples</title><link rel="stylesheet" href="/review/support/styles.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /><link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /><script type="text/javascript" src="/review/support/jquery-min.js"></script><script type="text/javascript" src="/review/support/form.js"></script><script type="text/javascript" src="/review/support/hsbook.js"></script></head> |
|
3 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch3list_tuples"> |
|
4 <div class="titlepage"></div> |
|
5 <div class="toc"> |
|
6 <p><b>Table of Contents</b></p> |
|
7 <dl> |
|
8 <dt><span class="article"><a href="#id2896388">Lists and Tuples</a></span></dt> |
|
9 <dd><dl> |
|
10 <dt><span class="section"><a href="#id2706433">1. Lists</a></span></dt> |
|
11 <dd><dl> |
|
12 <dt><span class="section"><a href="#id2896192">1.1. Common List Operations</a></span></dt> |
|
13 <dt><span class="section"><a href="#id2947658">1.2. None, Empty Lists, and Initialization</a></span></dt> |
|
14 <dt><span class="section"><a href="#id2947698">1.3. Nested Lists</a></span></dt> |
|
15 <dt><span class="section"><a href="#id2947732">1.4. List Methods</a></span></dt> |
|
16 </dl></dd> |
|
17 <dt><span class="section"><a href="#id2948048">2. Tuples</a></span></dt> |
|
18 <dd><dl><dt><span class="section"><a href="#id2948145">2.1. Common Tuple Operations</a></span></dt></dl></dd> |
|
19 <dt><span class="section"><a href="#id2948295">3. Additional Syntax</a></span></dt> |
|
20 <dd><dl> |
|
21 <dt><span class="section"><a href="#id2948312">3.1. range()</a></span></dt> |
|
22 <dt><span class="section"><a href="#id2948344">3.2. for</a></span></dt> |
|
23 </dl></dd> |
|
24 <dt><span class="section"><a href="#id2948403">4. Conclusion</a></span></dt> |
|
25 </dl></dd> |
|
26 </dl> |
|
27 </div> |
|
28 <div class="article" title="Lists and Tuples"> |
|
29 <div class="titlepage"> |
|
30 <div><div><h2 class="title"> |
|
31 <a name="id2896388"></a>Lists and Tuples</h2></div></div> |
|
32 <hr /> |
|
33 </div> |
|
34 <div class="toc"> |
|
35 <p><b>Table of Contents</b></p> |
|
36 <dl> |
|
37 <dt><span class="section"><a href="#id2706433">1. Lists</a></span></dt> |
|
38 <dd><dl> |
|
39 <dt><span class="section"><a href="#id2896192">1.1. Common List Operations</a></span></dt> |
|
40 <dt><span class="section"><a href="#id2947658">1.2. None, Empty Lists, and Initialization</a></span></dt> |
|
41 <dt><span class="section"><a href="#id2947698">1.3. Nested Lists</a></span></dt> |
|
42 <dt><span class="section"><a href="#id2947732">1.4. List Methods</a></span></dt> |
|
43 </dl></dd> |
|
44 <dt><span class="section"><a href="#id2948048">2. Tuples</a></span></dt> |
|
45 <dd><dl><dt><span class="section"><a href="#id2948145">2.1. Common Tuple Operations</a></span></dt></dl></dd> |
|
46 <dt><span class="section"><a href="#id2948295">3. Additional Syntax</a></span></dt> |
|
47 <dd><dl> |
|
48 <dt><span class="section"><a href="#id2948312">3.1. range()</a></span></dt> |
|
49 <dt><span class="section"><a href="#id2948344">3.2. for</a></span></dt> |
|
50 </dl></dd> |
|
51 <dt><span class="section"><a href="#id2948403">4. Conclusion</a></span></dt> |
|
52 </dl> |
|
53 </div> |
|
54 <div class="section" title="1.Lists"> |
|
55 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
56 <a name="id2706433"></a>1.Lists</h2></div></div></div> |
|
57 <p id="ch3list_tuples_1">Python provides an intuitive way to represent a group items, called <span class="emphasis"><em>Lists</em></span>. The |
|
58 items of a <span class="emphasis"><em>List</em></span> are called its elements. Unlike C/C++, elements can be of any |
|
59 type. A <span class="emphasis"><em>List</em></span> is represented as a list of comma-sepated elements with square |
|
60 brackets around them:</p> |
|
61 <pre class="programlisting"> |
|
62 >>> a = [10, 'Python programming', 20.3523, 23, 3534534L] |
|
63 >>> a |
|
64 [10, 'Python programming', 20.3523, 23, 3534534L]</pre> |
|
65 <div class="section" title="1.1.Common List Operations"> |
|
66 <div class="titlepage"><div><div><h3 class="title"> |
|
67 <a name="id2896192"></a>1.1.Common List Operations</h3></div></div></div> |
|
68 <p id="ch3list_tuples_2">The following are some of the most commonly used operations on <span class="emphasis"><em>Lists</em></span>.</p> |
|
69 <div class="section" title="1.1.1.Indexing"> |
|
70 <div class="titlepage"><div><div><h4 class="title"> |
|
71 <a name="id2896236"></a>1.1.1.Indexing</h4></div></div></div> |
|
72 <p id="ch3list_tuples_3">Individual elements of a <span class="emphasis"><em>List</em></span> can be accessed using an index to the element. |
|
73 The indices start at 0. One can also access the elements of the <span class="emphasis"><em>List</em></span> in reverse |
|
74 using negative indices.:</p> |
|
75 <pre class="programlisting"> |
|
76 >>> a[1] |
|
77 'Python programming' |
|
78 >>> a[-1] |
|
79 3534534L</pre> |
|
80 <p id="ch3list_tuples_4">It is important to note here that the last element of the <span class="emphasis"><em>List</em></span> has an index of |
|
81 -1.</p> |
|
82 </div> |
|
83 <div class="section" title="1.1.2.Concatenating"> |
|
84 <div class="titlepage"><div><div><h4 class="title"> |
|
85 <a name="id2947372"></a>1.1.2.Concatenating</h4></div></div></div> |
|
86 <p id="ch3list_tuples_5">Two or more <span class="emphasis"><em>Lists</em></span> can be concatenated using the + operator:</p> |
|
87 <pre class="programlisting"> |
|
88 >>> a + ['foo', 12, 23.3432, 54] |
|
89 [10, 'Python programming', 20.3523, 'foo', 12, 23.3432, 54] |
|
90 >>> [54, 75, 23] + ['write', 67, 'read'] |
|
91 [54, 75, 23, 'write', 67, 'read']</pre> |
|
92 </div> |
|
93 <div class="section" title="1.1.3.Slicing"> |
|
94 <div class="titlepage"><div><div><h4 class="title"> |
|
95 <a name="id2947393"></a>1.1.3.Slicing</h4></div></div></div> |
|
96 <p id="ch3list_tuples_6">A <span class="emphasis"><em>List</em></span> can be sliced off to contain a subset of elements of the <span class="emphasis"><em>List</em></span>. Slicing |
|
97 can be done by using two indices separated by a colon, where the first index is |
|
98 inclusive and the second index is exclusive. The resulting slice is also a <span class="emphasis"><em>List</em></span>.:</p> |
|
99 <pre class="programlisting"> |
|
100 >>> num = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
|
101 >>> num[3:6] |
|
102 [4, 5, 6] |
|
103 >>> num[0:1] |
|
104 [1] |
|
105 >>> num[7:10] |
|
106 [7, 8, 9]</pre> |
|
107 <p id="ch3list_tuples_7">The last example showed how to access last 3 elements of the <span class="emphasis"><em>List</em></span>. There is a |
|
108 small catch here. The second index 10 actually refers to the 11th element of the |
|
109 <span class="emphasis"><em>List</em></span> which is still valid, even though it doesn't exist because the second |
|
110 index is exclusive and tells the Python interpreter to get the last element of |
|
111 the <span class="emphasis"><em>List</em></span>. But this can also be done in a much easier way using negative indices:</p> |
|
112 <pre class="programlisting"> |
|
113 >>> num[-3:-1] |
|
114 [7, 8, 9]</pre> |
|
115 <p id="ch3list_tuples_8">Excluding the first index implies that the slice must start at the beginning of |
|
116 the <span class="emphasis"><em>List</em></span>, while excluding the second index includes all the elements till the |
|
117 end of the <span class="emphasis"><em>List</em></span>. A third parameter to a slice, which is implicitly taken as 1 |
|
118 is the step of the slice. It is specified as a value which follows a colon after |
|
119 the second index:</p> |
|
120 <pre class="programlisting"> |
|
121 >>> num[:4] |
|
122 [1, 2, 3, 4] |
|
123 >>> num[7:] |
|
124 [8, 9] |
|
125 >>> num[-3:] |
|
126 [7, 8, 9] |
|
127 >>> num[:] |
|
128 [1, 2, 3, 4, 5, 6, 7, 8, 9] |
|
129 >>> num[4:9:3] |
|
130 [5, 8] |
|
131 >>> num[3::2] |
|
132 [4, 6, 8] |
|
133 >>> num[::4] |
|
134 [1, 5, 9]</pre> |
|
135 </div> |
|
136 <div class="section" title="1.1.4.Multiplication"> |
|
137 <div class="titlepage"><div><div><h4 class="title"> |
|
138 <a name="id2947472"></a>1.1.4.Multiplication</h4></div></div></div> |
|
139 <p id="ch3list_tuples_9">A <span class="emphasis"><em>List</em></span> can be multiplied with an integer to repeat itself:</p> |
|
140 <pre class="programlisting"> |
|
141 >>> [20] * 5 |
|
142 [20, 20, 20, 20, 20] |
|
143 >>> [42, 'Python', 54] * 3 |
|
144 [42, 'Python', 54, 42, 'Python', 54, 42, 'Python', 54]</pre> |
|
145 </div> |
|
146 <div class="section" title="1.1.5.Membership"> |
|
147 <div class="titlepage"><div><div><h4 class="title"> |
|
148 <a name="id2947497"></a>1.1.5.Membership</h4></div></div></div> |
|
149 <p id="ch3list_tuples_a"><span class="strong"><strong>in</strong></span> operator is used to find whether an element is part of the <span class="emphasis"><em>List</em></span>. It |
|
150 returns <span class="strong"><strong>True</strong></span> if the element is present in the <span class="emphasis"><em>List</em></span> or <span class="strong"><strong>False</strong></span> if it is not |
|
151 present. Since this operator returns a Boolean value it is called a Boolean |
|
152 operator:</p> |
|
153 <pre class="programlisting"> |
|
154 >>> names = ['Guido', 'Alex', 'Tim'] |
|
155 >>> 'Tim' in names |
|
156 True |
|
157 >>> 'Adam' in names |
|
158 False</pre> |
|
159 </div> |
|
160 <div class="section" title="1.1.6.Length, Maximum and Minimum"> |
|
161 <div class="titlepage"><div><div><h4 class="title"> |
|
162 <a name="id2947539"></a>1.1.6.Length, Maximum and Minimum</h4></div></div></div> |
|
163 <p id="ch3list_tuples_b">Length of a <span class="emphasis"><em>List</em></span> can be found out using the len function. The max function |
|
164 returns the element with the largest value and the min function returns the |
|
165 element with the smallest value:</p> |
|
166 <pre class="programlisting"> |
|
167 >>> num = [4, 1, 32, 12, 67, 34, 65] |
|
168 >>> len(num) |
|
169 7 |
|
170 >>> max(num) |
|
171 67 |
|
172 >>> min(num) |
|
173 1</pre> |
|
174 </div> |
|
175 <div class="section" title="1.1.7.Changing Elements"> |
|
176 <div class="titlepage"><div><div><h4 class="title"> |
|
177 <a name="id2947564"></a>1.1.7.Changing Elements</h4></div></div></div> |
|
178 <p id="ch3list_tuples_c">Unlike Strings <span class="emphasis"><em>Lists</em></span> are mutable, i.e. elements of a <span class="emphasis"><em>List</em></span> can be manipulated:</p> |
|
179 <pre class="programlisting"> |
|
180 >>> a = [1, 3, 5, 7] |
|
181 >>> a[2] = 9 |
|
182 >>> a |
|
183 [1, 3, 9, 7]</pre> |
|
184 </div> |
|
185 <div class="section" title="1.1.8.Deleting Elements"> |
|
186 <div class="titlepage"><div><div><h4 class="title"> |
|
187 <a name="id2947590"></a>1.1.8.Deleting Elements</h4></div></div></div> |
|
188 <p id="ch3list_tuples_d">An element or a slice of a <span class="emphasis"><em>List</em></span> can be deleted by using the <span class="strong"><strong>del</strong></span> statement:</p> |
|
189 <pre class="programlisting"> |
|
190 >>> a = [1, 3, 5, 7, 9, 11] |
|
191 >>> del a[-2:] |
|
192 >>> a |
|
193 [1, 3, 5, 7] |
|
194 >>> del a[1] |
|
195 >>> a |
|
196 [1, 5, 7]</pre> |
|
197 </div> |
|
198 <div class="section" title="1.1.9.Assign to Slices"> |
|
199 <div class="titlepage"><div><div><h4 class="title"> |
|
200 <a name="id2947617"></a>1.1.9.Assign to Slices</h4></div></div></div> |
|
201 <p id="ch3list_tuples_e">In the same way, values can be assigned to individual elements of the <span class="emphasis"><em>List</em></span>, |
|
202 a <span class="emphasis"><em>List</em></span> of elements can be assigned to a slice:</p> |
|
203 <pre class="programlisting"> |
|
204 >>> a = [2, 3, 4, 5] |
|
205 >>> a[:2] = [0, 1] |
|
206 [0, 1, 4, 5] |
|
207 >>> a[2:2] = [2, 3] |
|
208 >>> a |
|
209 [0, 1, 2, 3, 4, 5] |
|
210 >>> a[2:4] = [] |
|
211 >>> a |
|
212 [0, 1, 4, 5]</pre> |
|
213 <p id="ch3list_tuples_f">The last two examples should be particularly noted carefully. The last but one |
|
214 example insert elements or a list of elements into a <span class="emphasis"><em>List</em></span> and the last example |
|
215 deletes a list of elements from the <span class="emphasis"><em>List</em></span>.</p> |
|
216 </div> |
|
217 </div> |
|
218 <div class="section" title="1.2.None, Empty Lists, and Initialization"> |
|
219 <div class="titlepage"><div><div><h3 class="title"> |
|
220 <a name="id2947658"></a>1.2.None, Empty Lists, and Initialization</h3></div></div></div> |
|
221 <p id="ch3list_tuples_10">An <span class="emphasis"><em>Empty List</em></span> is a <span class="emphasis"><em>List</em></span> with no elements and is simply represented as |
|
222 []. A <span class="emphasis"><em>None List</em></span> is one with all elements in it being <span class="strong"><strong>None</strong></span>. It serves |
|
223 the purpose having a container list of some fixed number of elements with |
|
224 no value:</p> |
|
225 <pre class="programlisting"> |
|
226 >>> a = [] |
|
227 >>> a |
|
228 [] |
|
229 >>> n = [None] * 10 |
|
230 >>> n |
|
231 [None, None, None, None, None, None, None, None, None, None]</pre> |
|
232 </div> |
|
233 <div class="section" title="1.3.Nested Lists"> |
|
234 <div class="titlepage"><div><div><h3 class="title"> |
|
235 <a name="id2947698"></a>1.3.Nested Lists</h3></div></div></div> |
|
236 <p id="ch3list_tuples_11">As mentioned earlier, a List can contain elements of any data type. This also |
|
237 implies a <span class="emphasis"><em>List</em></span> can have a <span class="emphasis"><em>Lists</em></span> themselves as its elements. These are |
|
238 called as <span class="emphasis"><em>Nested Lists</em></span>. There is no limit on the depth of the <span class="emphasis"><em>Nested Lists</em></span>:</p> |
|
239 <pre class="programlisting"> |
|
240 >>> a = [1, [1, 2, 3], 3, [1, [1, 2, 3]], 7]</pre> |
|
241 </div> |
|
242 <div class="section" title="1.4.List Methods"> |
|
243 <div class="titlepage"><div><div><h3 class="title"> |
|
244 <a name="id2947732"></a>1.4.List Methods</h3></div></div></div> |
|
245 <p id="ch3list_tuples_12">A method is a function that is coupled to an object. More about objects |
|
246 and its methods are discussed in Advanced Python module. In general, a |
|
247 method is called like:</p> |
|
248 <pre class="programlisting"> |
|
249 object.method(arguments)</pre> |
|
250 <p id="ch3list_tuples_13">For now, it is enough to know that a list of elements is an object and |
|
251 so <span class="emphasis"><em>List</em></span> methods can be called upon them. Also some of the methods change |
|
252 the <span class="emphasis"><em>List</em></span> in-place, meaning it modifies the existing list instead of creating |
|
253 a new one, while other methods don't. It must be noted as we run through |
|
254 the <span class="emphasis"><em>List</em></span> methods.</p> |
|
255 <p id="ch3list_tuples_14">Some of the most commonly used <span class="emphasis"><em>List</em></span> methods are as follows:</p> |
|
256 <div class="section" title="1.4.1.append"> |
|
257 <div class="titlepage"><div><div><h4 class="title"> |
|
258 <a name="id2947779"></a>1.4.1.append</h4></div></div></div> |
|
259 <p id="ch3list_tuples_15">The <span class="emphasis"><em>append</em></span> method is used to append an object at the end of the list:</p> |
|
260 <pre class="programlisting"> |
|
261 >>> prime = [2, 3, 5] |
|
262 >>> prime.append(7) |
|
263 >>> prime |
|
264 [2, 3, 5, 7]</pre> |
|
265 <p id="ch3list_tuples_16">It is important to note that append changes the <span class="emphasis"><em>List</em></span> in-place.</p> |
|
266 </div> |
|
267 <div class="section" title="1.4.2.count"> |
|
268 <div class="titlepage"><div><div><h4 class="title"> |
|
269 <a name="id2947808"></a>1.4.2.count</h4></div></div></div> |
|
270 <p id="ch3list_tuples_17">The <span class="emphasis"><em>count</em></span> method returns the number of occurences of a particular element |
|
271 in a list:</p> |
|
272 <pre class="programlisting"> |
|
273 >>> [1, 4, 4, 9, 9, 9].count(9) |
|
274 3 |
|
275 >>> tlst = ['Python', 'is', 'a', 'beautiful', 'language'] |
|
276 >>> tlst.count('Python') |
|
277 1</pre> |
|
278 </div> |
|
279 <div class="section" title="1.4.3.extend"> |
|
280 <div class="titlepage"><div><div><h4 class="title"> |
|
281 <a name="id2947831"></a>1.4.3.extend</h4></div></div></div> |
|
282 <p id="ch3list_tuples_18">The <span class="emphasis"><em>extend</em></span> method extends the list on which it is called by the list supplied |
|
283 as argument to it:</p> |
|
284 <pre class="programlisting"> |
|
285 >>> a = [1, 2, 3] |
|
286 >>> b = [4, 5, 6] |
|
287 >>> a.extend(b) |
|
288 [1, 2, 3, 4, 5, 6]</pre> |
|
289 <p id="ch3list_tuples_19">This is an in-place method. This method is equivalent to using the + operator, but |
|
290 using the + operator returns a new list.</p> |
|
291 </div> |
|
292 <div class="section" title="1.4.4.index"> |
|
293 <div class="titlepage"><div><div><h4 class="title"> |
|
294 <a name="id2947859"></a>1.4.4.index</h4></div></div></div> |
|
295 <p id="ch3list_tuples_1a">The <span class="emphasis"><em>index</em></span> method returns the index position of the element in the list |
|
296 specified as argument:</p> |
|
297 <pre class="programlisting"> |
|
298 >>> a = [1, 2, 3, ,4, 5] |
|
299 >>> a.index(4) |
|
300 3</pre> |
|
301 </div> |
|
302 <div class="section" title="1.4.5.insert"> |
|
303 <div class="titlepage"><div><div><h4 class="title"> |
|
304 <a name="id2947879"></a>1.4.5.insert</h4></div></div></div> |
|
305 <p id="ch3list_tuples_1b">The <span class="emphasis"><em>insert</em></span> method is used to insert an element specified as the second |
|
306 argument to the list at the position specified by the first argument:</p> |
|
307 <pre class="programlisting"> |
|
308 >>> a = ['Python', 'is', 'cool'] |
|
309 >>> a.insert(2, 'so') |
|
310 >>> a |
|
311 ['Python', 'is', 'so', 'cool']</pre> |
|
312 <p id="ch3list_tuples_1c">The <span class="emphasis"><em>insert</em></span> method changes the <span class="emphasis"><em>List</em></span> in-place.</p> |
|
313 </div> |
|
314 <div class="section" title="1.4.6.pop"> |
|
315 <div class="titlepage"><div><div><h4 class="title"> |
|
316 <a name="id2947913"></a>1.4.6.pop</h4></div></div></div> |
|
317 <p id="ch3list_tuples_1d">The <span class="emphasis"><em>pop</em></span> method removes an element from the list. The index position |
|
318 of the element to be removed can be specified as an argument to the |
|
319 <span class="emphasis"><em>pop</em></span> method, if not it removes the last element by default:</p> |
|
320 <pre class="programlisting"> |
|
321 >>> a = [1, 2, 3, 4, 5] |
|
322 >>> a.pop() |
|
323 >>> a |
|
324 5 |
|
325 >>> a.pop(2) |
|
326 >>> a |
|
327 3</pre> |
|
328 <p id="ch3list_tuples_1e">The <span class="emphasis"><em>pop</em></span> method changes the <span class="emphasis"><em>List</em></span> in-place.</p> |
|
329 </div> |
|
330 <div class="section" title="1.4.7.remove"> |
|
331 <div class="titlepage"><div><div><h4 class="title"> |
|
332 <a name="id2947951"></a>1.4.7.remove</h4></div></div></div> |
|
333 <p id="ch3list_tuples_1f">The <span class="emphasis"><em>remove</em></span> method removes the first occurence of an element supplied as a |
|
334 parameter:</p> |
|
335 <pre class="programlisting"> |
|
336 >>> a = [1, 2, 3, 4, 2, 5, 2] |
|
337 >>> a.remove(2) |
|
338 >>> a |
|
339 [1, 3, 4, 2, 5, 2]</pre> |
|
340 </div> |
|
341 <div class="section" title="1.4.8.reverse"> |
|
342 <div class="titlepage"><div><div><h4 class="title"> |
|
343 <a name="id2947972"></a>1.4.8.reverse</h4></div></div></div> |
|
344 <p id="ch3list_tuples_20">The <span class="emphasis"><em>reverse</em></span> method reverses elements in the list. It is important to note |
|
345 here that <span class="emphasis"><em>reverse</em></span> method changes the list in-place and doesn't return any |
|
346 thing:</p> |
|
347 <pre class="programlisting"> |
|
348 >>> a = ['guido', 'alex', 'tim'] |
|
349 >>> a.reverse() |
|
350 >>> a |
|
351 ['tim', 'alex', 'guido']</pre> |
|
352 </div> |
|
353 <div class="section" title="1.4.9.sort"> |
|
354 <div class="titlepage"><div><div><h4 class="title"> |
|
355 <a name="id2947998"></a>1.4.9.sort</h4></div></div></div> |
|
356 <p id="ch3list_tuples_21">The <span class="emphasis"><em>sort</em></span> method is used to sort the elements of the list. The <span class="emphasis"><em>sort</em></span> method |
|
357 also sorts in-place and does not return anything:</p> |
|
358 <pre class="programlisting"> |
|
359 >>> a = [5, 1, 3, 7, 4] |
|
360 >>> a.sort() |
|
361 >>> a |
|
362 [1, 3, 4, 5, 7]</pre> |
|
363 <p id="ch3list_tuples_22">In addition to the sort method on a <span class="emphasis"><em>List</em></span> object we can also use the built-in |
|
364 <span class="strong"><strong>sorted</strong></span> function. This function takes the <span class="emphasis"><em>List</em></span> as a parameter and returns |
|
365 a sorted copy of the list. However the original list is left intact:</p> |
|
366 <pre class="programlisting"> |
|
367 >>> a = [5, 1, 3, 7, 4] |
|
368 >>> b = sorted(a) |
|
369 >>> b |
|
370 [1, 3, 4, 5, 7] |
|
371 >>> a |
|
372 [5, 1, 3, 7, 4]</pre> |
|
373 </div> |
|
374 </div> |
|
375 </div> |
|
376 <div class="section" title="2.Tuples"> |
|
377 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
378 <a name="id2948048"></a>2.Tuples</h2></div></div></div> |
|
379 <p id="ch3list_tuples_23"><span class="emphasis"><em>Tuples</em></span> are sequences just like <span class="emphasis"><em>Lists</em></span>, but they are immutable. In other |
|
380 words <span class="emphasis"><em>Tuples</em></span> provides a way to represent a group of items, where the group |
|
381 of items cannot be changed in any way. The syntax of a <span class="emphasis"><em>Tuple</em></span> is also very |
|
382 similar to <span class="emphasis"><em>List</em></span>. A <span class="emphasis"><em>Tuple</em></span> is represented with the list of items, called |
|
383 elements of the <span class="emphasis"><em>Tuple</em></span> separated by comma, with the entire list being enclosed |
|
384 in parenthesis. It is not compulsory to use parenthesis around a <span class="emphasis"><em>Tuple</em></span> but |
|
385 it may be necessary in some of the cases:</p> |
|
386 <pre class="programlisting"> |
|
387 >>> a = 1, 2, 3 |
|
388 >>> a |
|
389 (1, 2, 3) |
|
390 >>> b = 1, |
|
391 >>> b |
|
392 (1,)</pre> |
|
393 <p id="ch3list_tuples_24">It is interesting to note the second example. Just a value followed by a comma |
|
394 automatically makes that an element of a <span class="emphasis"><em>Tuple</em></span> with only one element. It is |
|
395 also important to note that, irrespective of input having a parenthesis, the |
|
396 output always has a parenthesis.</p> |
|
397 <p id="ch3list_tuples_25">The first example is also known as <span class="emphasis"><em>Tuple packing</em></span>, because values are being |
|
398 packed into a tuple. It is also possible to do <span class="emphasis"><em>Tuple unpacking</em></span> which is more |
|
399 interesting. It is better to understand that by example. Say we have a |
|
400 co-ordinate pair from which we need to separate x and y co-ordinates:</p> |
|
401 <pre class="programlisting"> |
|
402 >>> a = (1, 2) |
|
403 >>> x, y = a |
|
404 >>> x |
|
405 1 |
|
406 >>> y |
|
407 2</pre> |
|
408 <p id="ch3list_tuples_26"><span class="emphasis"><em>Tuple unpacking</em></span> also has several other use-cases of which the most interesting |
|
409 one is to swap the values of two variables. Using programming languages like C |
|
410 would require anywhere around 10 lines of code and an extra temporary variable |
|
411 to do this (including all the #include stuff). Python does it in the most |
|
412 intuitive way in just one line. Say we want to swap the co-ordinates in the |
|
413 above example:</p> |
|
414 <pre class="programlisting"> |
|
415 >>> x, y = y, x |
|
416 >>> x |
|
417 2 |
|
418 >>> y |
|
419 1</pre> |
|
420 <div class="section" title="2.1.Common Tuple Operations"> |
|
421 <div class="titlepage"><div><div><h3 class="title"> |
|
422 <a name="id2948145"></a>2.1.Common Tuple Operations</h3></div></div></div> |
|
423 <p id="ch3list_tuples_27">There is no need to introduce all the <span class="emphasis"><em>Tuple</em></span> operations again, since <span class="emphasis"><em>Tuples</em></span> |
|
424 support the following operations that <span class="emphasis"><em>List</em></span> supports in exactly the same way:</p> |
|
425 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
426 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_28">Indexing</p></li> |
|
427 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_29">Concatenating</p></li> |
|
428 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2a">Slicing</p></li> |
|
429 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2b">Membership</p></li> |
|
430 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2c">Multiplication</p></li> |
|
431 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2d">Length, Maximum, Minimum</p></li> |
|
432 </ul></div> |
|
433 <p id="ch3list_tuples_2e">The following examples illustrate the above operations:</p> |
|
434 <pre class="programlisting"> |
|
435 >>> a = (1, 2, 3, 4, 5, 6) |
|
436 >>> a[5] |
|
437 6 |
|
438 >>> b = (7, 8, 9) |
|
439 >>> a + b |
|
440 (1, 2, 3, 4, 5, 6, 7, 8, 9) |
|
441 >>> a[3:5] |
|
442 (4, 5) |
|
443 >>> 5 in a |
|
444 True |
|
445 >>> c = (1,) |
|
446 >>> c * 5 |
|
447 (1, 1, 1, 1, 1) |
|
448 >>> len(a) |
|
449 6 |
|
450 >>> max(a) |
|
451 6 |
|
452 >>> min(a) |
|
453 1</pre> |
|
454 <p id="ch3list_tuples_2f">However the following <span class="emphasis"><em>List</em></span> operations are not supported by <span class="emphasis"><em>Tuples</em></span> because |
|
455 <span class="emphasis"><em>Tuples</em></span> cannot be changed once they are created:</p> |
|
456 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
457 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_30">Changing elements</p></li> |
|
458 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_31">Deleting elements</p></li> |
|
459 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_32">Assigning to slices</p></li> |
|
460 </ul></div> |
|
461 <p id="ch3list_tuples_33">Similarity to <span class="emphasis"><em>Lists</em></span> leads to the questions like, why not <span class="emphasis"><em>Lists</em></span> only? Why do |
|
462 we even want <span class="emphasis"><em>Tuples</em></span>? Can we do the same with <span class="emphasis"><em>Lists</em></span>? And the answer is <span class="strong"><strong>Yes</strong></span> |
|
463 we can do it, but <span class="emphasis"><em>Tuples</em></span> are helpful at times, like we can return Tuples from |
|
464 functions. They are also returned by some built-in functions and methods. And |
|
465 also there are some use cases like co-ordinate among other things. So <span class="emphasis"><em>Tuples</em></span> |
|
466 are helpful.</p> |
|
467 </div> |
|
468 </div> |
|
469 <div class="section" title="3.Additional Syntax"> |
|
470 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
471 <a name="id2948295"></a>3.Additional Syntax</h2></div></div></div> |
|
472 <p id="ch3list_tuples_34">The following additional syntax are introduced to make it easier to operate on |
|
473 <span class="emphasis"><em>Lists</em></span>.</p> |
|
474 <div class="section" title="3.1.range()"> |
|
475 <div class="titlepage"><div><div><h3 class="title"> |
|
476 <a name="id2948312"></a>3.1.range()</h3></div></div></div> |
|
477 <p id="ch3list_tuples_35">The <span class="emphasis"><em>range</em></span> function takes at least one argument and 2 additional optional |
|
478 arguments. If two or more arguments are specified, the range function returns |
|
479 a list of natural numbers starting from the first argument passed to it to the |
|
480 second argument. The third argument, if specified is used as a step. Suppose |
|
481 only one argument is specified, then <span class="emphasis"><em>range</em></span> function returns a list of natural |
|
482 numbers starting from 0 upto the argument specified:</p> |
|
483 <pre class="programlisting"> |
|
484 >>> range(5, 10, 2) |
|
485 [5, 7, 9] |
|
486 >>> range(2, 15) |
|
487 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] |
|
488 >>> range(12) |
|
489 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]</pre> |
|
490 </div> |
|
491 <div class="section" title="3.2.for"> |
|
492 <div class="titlepage"><div><div><h3 class="title"> |
|
493 <a name="id2948344"></a>3.2.for</h3></div></div></div> |
|
494 <p id="ch3list_tuples_36">The <span class="strong"><strong>for</strong></span> keyword is used as a part of the looping construct. Unlike for loops |
|
495 in other languages, Python's for is used to iterate through the elements of |
|
496 sequences like <span class="emphasis"><em>Lists</em></span>, <span class="emphasis"><em>Tuples</em></span>, <span class="emphasis"><em>Dictionaries</em></span>, etc. The syntax of the for loop |
|
497 consists of <span class="strong"><strong>for</strong></span>, followed by a variable to hold the individual or the current |
|
498 element of the list during iteration and <span class="strong"><strong>in</strong></span>, followed by the sequence and a |
|
499 semicolon(':') The next line which is part of the <span class="strong"><strong>for</strong></span> loop, i.e the statements |
|
500 that are part of the loop should start with a new intend:</p> |
|
501 <pre class="programlisting"> |
|
502 >>> names = ['Guido', 'Alex', 'Tim'] |
|
503 >>> for name in names: |
|
504 ... print "Name =", name |
|
505 ... |
|
506 Name = Guido |
|
507 Name = Alex |
|
508 Name = Tim</pre> |
|
509 </div> |
|
510 </div> |
|
511 <div class="section" title="4.Conclusion"> |
|
512 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
513 <a name="id2948403"></a>4.Conclusion</h2></div></div></div> |
|
514 <p id="ch3list_tuples_37">This section on <span class="emphasis"><em>Lists</em></span> and <span class="emphasis"><em>Tuples</em></span> introduces almost all the necessary |
|
515 machinary required to work on <span class="emphasis"><em>Lists</em></span> and <span class="emphasis"><em>Tuples</em></span>. Topics like how to |
|
516 use these data structures in bigger more useful programs will be introduced |
|
517 in the subsequent chapters.</p> |
|
518 </div> |
|
519 </div> |
|
520 </div></body> |
|
521 </html> |
|
522 |
|