|
1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. Learn about Python Data Structures and Operators.(Remembering) |
|
5 .. Use them to do basic operations.(Applying) |
|
6 |
|
7 .. Prerequisites |
|
8 .. ------------- |
|
9 |
|
10 |
|
11 |
|
12 .. Author : Amit Sethi |
|
13 Internal Reviewer : |
|
14 External Reviewer : |
|
15 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
16 Hello friends and welcome to the tutorial on Basic Data types and operators in Python. |
|
17 {{{ Show the slide containing title }}} |
|
18 |
|
19 {{{ Show the slide containing the outline slide }}} |
|
20 |
|
21 In this tutorial, we shall look at:: |
|
22 |
|
23 * Datatypes in Python |
|
24 * Operators in Python |
|
25 |
|
26 with a little hands-on on how they can be applied to the different data types. |
|
27 |
|
28 |
|
29 |
|
30 First we will explore python data structures in the domain of numbers. |
|
31 There are three built-in data types in python to represent numbers. |
|
32 |
|
33 {{{ A slide to make a memory note of this }}} |
|
34 |
|
35 These are: |
|
36 |
|
37 * Integers |
|
38 * float and |
|
39 * Complex |
|
40 |
|
41 Lets first talk about integers. :: |
|
42 |
|
43 a = 13 |
|
44 a |
|
45 |
|
46 |
|
47 Thats it, there we have our first integer variable a. |
|
48 |
|
49 |
|
50 |
|
51 If we now see :: |
|
52 |
|
53 type(a) |
|
54 <type 'int'> |
|
55 |
|
56 This means that a is a type of int. Being an int data structure |
|
57 in python means that there are various functions that this variable |
|
58 has to manipulate it different ways. You can explore these by doing, |
|
59 |
|
60 a.<Tab> |
|
61 |
|
62 |
|
63 |
|
64 Lets see the limits of this int. |
|
65 |
|
66 b = 99999999999999999999 |
|
67 b |
|
68 |
|
69 As you can see even when we put a value of 9 repeated 20 times |
|
70 python did not complain. However when you asked python to print |
|
71 the number again it put a capital L at the end. Now if you check |
|
72 the type of this variable b, :: |
|
73 |
|
74 type(b) |
|
75 <type 'long'> |
|
76 |
|
77 |
|
78 The reason for this is that python recognizes large integer numbers |
|
79 by the data type long. However long type and integer type share there |
|
80 functions and properties. |
|
81 |
|
82 Lets now try out the second type in list called float. |
|
83 |
|
84 Decimal numbers in python are recognized by the term float :: |
|
85 |
|
86 p = 3.141592 |
|
87 p |
|
88 |
|
89 If you notice the value of output of p isn't exactly equal to p. This |
|
90 is because computer saves floating point values in a specific |
|
91 format. There is always an aproximationation. This is why we should |
|
92 never rely on equality of floating point numbers in a program. |
|
93 |
|
94 The last data type in the list is complex number :: |
|
95 |
|
96 c = 3.2+4.6j |
|
97 |
|
98 as simple as that so essentialy its just a combination of two floats the |
|
99 imaginary part being defined by j notation instead of i. Complex numbers have a lot of functions specific to them. |
|
100 Lets check these :: |
|
101 |
|
102 c.<Tab> |
|
103 |
|
104 Lets try some of them :: |
|
105 |
|
106 c.real |
|
107 c.imag |
|
108 |
|
109 c.real gives the real part of the number and c.imag the imaginary. |
|
110 |
|
111 We can get the absolute value using the function :: |
|
112 |
|
113 abs(c) |
|
114 |
|
115 |
|
116 |
|
117 {{ Slide for memory aid }} |
|
118 |
|
119 Python also has Boolean as a built-in type. |
|
120 |
|
121 Try it out just type :: |
|
122 |
|
123 t = True |
|
124 |
|
125 note that T in true is capitalized. |
|
126 |
|
127 You can apply different Boolean operations on t now for example :: |
|
128 |
|
129 f = not t |
|
130 f |
|
131 f or t |
|
132 f and t |
|
133 |
|
134 |
|
135 |
|
136 The results are explanotary in themselves. |
|
137 |
|
138 The usage of boolean brings us to an interesting question of precendence. |
|
139 What if you want to apply one operator before another. |
|
140 |
|
141 Well you can use parenthesis for precedence. |
|
142 |
|
143 Lets write some piece of code to check this out. |
|
144 |
|
145 In[]: a=False |
|
146 In[]: b=True |
|
147 In[]: c=True |
|
148 |
|
149 To check how precedence changes with parenthesis. We will try two |
|
150 expressions and their evaluation. |
|
151 |
|
152 one :: |
|
153 |
|
154 (a and b) or c |
|
155 |
|
156 This expression gives the value True |
|
157 |
|
158 where as the expression :: |
|
159 |
|
160 a and (b or c) |
|
161 |
|
162 gives the value False. |
|
163 |
|
164 |
|
165 Lets now look at some operators available in Python to manipulate these data types. |
|
166 |
|
167 |
|
168 |
|
169 Python uses % for modulo operation :: |
|
170 |
|
171 87 % 6 |
|
172 and two stars for a exponent. :: |
|
173 |
|
174 7**8 |
|
175 |
|
176 |
|
177 In case one wishes to use the current value of variable in which the result is stored in the expression one can do that by putting the operator before `equal to`. :: |
|
178 |
|
179 a=73 |
|
180 a*=34 |
|
181 |
|
182 is same as :: |
|
183 |
|
184 a=a*34 |
|
185 |
|
186 and :: |
|
187 |
|
188 a/=23 |
|
189 |
|
190 is same as :: |
|
191 |
|
192 a=a/23 |
|
193 |
|
194 |
|
195 Lets now discuss sequence data stypes in python. Sequence |
|
196 datatypes are those in which elements are kept in a sequential |
|
197 order. All the elements accessed using index. |
|
198 |
|
199 |
|
200 {{{ slide to for memory aid }}} |
|
201 |
|
202 The sequence datatypes in python are :: |
|
203 |
|
204 * list |
|
205 * string |
|
206 * tuple |
|
207 |
|
208 The list type is a container that holds a number of other |
|
209 objects, in the given order. |
|
210 |
|
211 We create our first list by typing :: |
|
212 |
|
213 num_list = [1, 2, 3, 4] |
|
214 num_list |
|
215 |
|
216 |
|
217 Items enclosed in square brackets separated by comma |
|
218 constitutes a list. |
|
219 |
|
220 Lists can store data of any type in them. |
|
221 |
|
222 We can have a list something like :: |
|
223 |
|
224 var_list = [1, 1.2, [1,2]] |
|
225 var_list |
|
226 |
|
227 |
|
228 |
|
229 Now we will have a look at strings |
|
230 |
|
231 type :: |
|
232 |
|
233 In[]: greeting_string="hello" |
|
234 |
|
235 |
|
236 greeting_string is now a string variable with the value "hello" |
|
237 |
|
238 {{{ Memory Aid Slide }}} |
|
239 |
|
240 Python strings can actually be defined in three different ways :: |
|
241 |
|
242 In[]: k='Single quote' |
|
243 In[]: l="Double quote contain's single quote" |
|
244 In[]: m='''"Contain's both"''' |
|
245 |
|
246 Thus, single quotes are used as delimiters usually. |
|
247 When a string contains a single quote, double quotes are used as delimiters. |
|
248 When a string quote contains both single and double quotes, triple quotes are |
|
249 used as delimiters. |
|
250 |
|
251 The last in the list of sequence data types is tuple. |
|
252 |
|
253 To create a tuple we use normal brackets '(' |
|
254 unlike '[' for lists.:: |
|
255 |
|
256 In[]: num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) |
|
257 |
|
258 Because of their sequential property there are certain functions and |
|
259 operations we can apply to all of them. |
|
260 |
|
261 |
|
262 |
|
263 The first one is accessing. |
|
264 |
|
265 They can be accessed using index numbers :: |
|
266 |
|
267 In[]: num_list[2] |
|
268 In[]: num_list[-1] |
|
269 In[]: greeting_string[1] |
|
270 In[]: greeting_string[3] |
|
271 In[]: greeting_string[-2] |
|
272 In[]: num_tuple[2] |
|
273 In[]: num_tuple[-3] |
|
274 |
|
275 |
|
276 Indexing starts from 0 from left to right and from -1 when accessing |
|
277 lists in reverse. Thus num_list[2] refers to the third element 3. |
|
278 and greetings [-2] is the second element from the end , that is 'l'. |
|
279 |
|
280 |
|
281 |
|
282 Addition gives a new sequence containing both sequences :: |
|
283 |
|
284 In[]: num_list+var_list |
|
285 In[]: a_string="another string" |
|
286 In[]: greeting_string+a_string |
|
287 In[]: t2=(3,4,6,7) |
|
288 In[]: num_tuple+t2 |
|
289 |
|
290 len function gives the length :: |
|
291 |
|
292 In[]: len(num_list) |
|
293 In[]: len(greeting_string) |
|
294 In[]: len(num_tuple) |
|
295 |
|
296 Prints the length the variable. |
|
297 |
|
298 We can check the containership of an element using the 'in' keyword :: |
|
299 |
|
300 In[]: 3 in num_list |
|
301 In[]: 'H' in greeting_string |
|
302 In[]: 2 in num_tuple |
|
303 |
|
304 We see that it gives True and False accordingly. |
|
305 |
|
306 Find maximum using max function and minimum using min:: |
|
307 |
|
308 In[]: max(num_tuple) |
|
309 In[]: min(greeting_string) |
|
310 |
|
311 Get a sorted list and reversed list using sorted and reversed function :: |
|
312 |
|
313 In[]: sorted(num_list) |
|
314 In[]: reversed(greeting_string) |
|
315 |
|
316 As a consequence of the order one we access a group of elements together. |
|
317 This is called slicing and striding. |
|
318 |
|
319 First Slicing |
|
320 |
|
321 Given a list :: |
|
322 |
|
323 In[]:j=[1,2,3,4,5,6] |
|
324 |
|
325 Lets say we want elements starting from 2 and ending in 5. |
|
326 |
|
327 For this we can do :: |
|
328 |
|
329 In[]: j[1:4] |
|
330 |
|
331 The syntax for slicing is sequence variable name square bracket |
|
332 first element index, colon, second element index.The last element however is notincluded in the resultant list:: |
|
333 |
|
334 |
|
335 In[]: j[:4] |
|
336 |
|
337 If first element is left blank default is from beginning and if last |
|
338 element is left blank it means till the end. |
|
339 |
|
340 In[]: j[1:] |
|
341 |
|
342 In[]: j[:] |
|
343 |
|
344 This effectively is the whole list. |
|
345 |
|
346 Striding is similar to slicing except that the step size here is not one. |
|
347 |
|
348 Lets see by example :: |
|
349 |
|
350 new_num_list=[1,2,3,4,5,6,7,8,9,10] |
|
351 new_num_list[1:8:2] |
|
352 [2, 4, 6, 8] |
|
353 |
|
354 The colon two added in the end signifies all the alternate elements. This is why we call this concept |
|
355 striding because we move through the list with a particular stride or step. The step in this example |
|
356 being 2. |
|
357 |
|
358 We have talked about many similar features of lists, strings and tuples. But there are many important |
|
359 features in lists that differ from strings and tuples. Lets see this by example.:: |
|
360 |
|
361 In[]: new_num_list[1]=9 |
|
362 In[]: greeting_string[1]='k' |
|
363 |
|
364 {{{ slide to show the error }}} |
|
365 |
|
366 |
|
367 |
|
368 As you can see while the first command executes with out a problem there is an error on the second one. |
|
369 |
|
370 Now lets try :: |
|
371 |
|
372 In[]: new_tuple[1]=5 |
|
373 |
|
374 Its the same error. This is because strings and tuples share the property of being immutable. |
|
375 We cannot change the value at a particular index just by assigning a new value at that position. |
|
376 |
|
377 |
|
378 We have looked at different types but we need to convert one data type into another. Well lets one |
|
379 by one go through methods by which we can convert one data type to other: |
|
380 |
|
381 We can convert all the number data types to one another :: |
|
382 |
|
383 i=34 |
|
384 d=float(i) |
|
385 d |
|
386 |
|
387 Python has built in functions int, float and complex to convert one number type |
|
388 data structure to another. |
|
389 |
|
390 dec=2.34 |
|
391 dec_con=int(dec) |
|
392 dec_con |
|
393 |
|
394 |
|
395 As you can see the decimal part of the number is simply stripped to get the integer.:: |
|
396 |
|
397 com=2.3+4.2j |
|
398 float(com) |
|
399 com |
|
400 |
|
401 In case of complex number to floating point only the real value of complex number is taken. |
|
402 |
|
403 Similarly we can convert list to tuple and tuple to list :: |
|
404 |
|
405 lst=[3,4,5,6] |
|
406 tup=tuple(lst) |
|
407 tupl=(3,23,4,56) |
|
408 lst=list(tuple) |
|
409 |
|
410 However string to list and list to string is an interesting problem. |
|
411 Lets say we have a string :: |
|
412 |
|
413 In: somestring="Is there a way to split on these spaces." |
|
414 In: somestring.split() |
|
415 |
|
416 |
|
417 This produces a list with the string split at whitespace. |
|
418 similarly we can split on some other character. |
|
419 |
|
420 In: otherstring="Tim,Amy,Stewy,Boss" |
|
421 |
|
422 How do we split on comma , simply pass it as argument :: |
|
423 |
|
424 In: otherstring.split(',') |
|
425 |
|
426 join function does the opposite. Joins a list to make a string.:: |
|
427 |
|
428 In[]:','.join['List','joined','on','commas'] |
|
429 |
|
430 Thus we get a list joined on commas. Similarly we can do spaces.:: |
|
431 |
|
432 In[]:' '.join['Now','on','spaces'] |
|
433 |
|
434 Note that the list has to be a list of strings to apply join operation. |
|
435 |
|
436 With this we come to the end of this tutorial . |
|
437 |
|
438 In this tutorial we have discussed |
|
439 |
|
440 1. Number Datatypes , integer,float and complex |
|
441 2. Boolean and datatype and operators |
|
442 3. Sequence data types ,List,String and Tuple |
|
443 4. Accesing sequence |
|
444 5. Slicing sequences |
|
445 6. Finding length , sorting and reversing operations on sequences. |
|
446 7. Immutability. |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 .. #[Nishanth]: string to list is fine. But list to string can be left for |
|
452 string manipulations. Just say it requires some string |
|
453 manipulations and leave it there. |
|
454 |
|
455 .. #[Nishanth]: Where is the summary |
|
456 There are no exercises in the script |
|
457 |
|
458 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
459 |
|
460 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
461 |
|
462 Hope you have enjoyed and found it useful. |
|
463 |
|
464 Thank You. |
|
465 |
|
466 |
|
467 |