1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. At the end of this tutorial, you should know -- |
|
5 |
|
6 .. 1. Learn about Python Data Structures and Operators.(Remembering) |
|
7 .. #.Use them to do basic operations.(Applying) |
|
8 |
|
9 .. Prerequisites |
|
10 .. ------------- |
|
11 |
|
12 .. None |
|
13 |
|
14 .. Author : Amit Sethi |
|
15 Internal Reviewer : |
|
16 External Reviewer : |
|
17 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
18 |
|
19 Hello friends and welcome to the tutorial on Basic Data types and operators |
|
20 in Python. |
|
21 |
|
22 {{{ Show the slide containing title }}} |
|
23 |
|
24 {{{ Show the slide containing the outline slide }}} |
|
25 |
|
26 In this tutorial, we shall look at |
|
27 |
|
28 * Datatypes in Python |
|
29 * Numbers |
|
30 * Boolean |
|
31 * Sequence |
|
32 * Operators in Python |
|
33 * Arithmetic Operators |
|
34 * Boolean Operators |
|
35 |
|
36 * Python Sequence Data types |
|
37 * list |
|
38 * string |
|
39 * tuple |
|
40 |
|
41 First we will explore python data structures in the domain of numbers. |
|
42 There are three built-in data types in python to represent numbers. |
|
43 |
|
44 {{{ A slide to make a memory note of the different datatypes }}} |
|
45 |
|
46 These are: |
|
47 |
|
48 * int |
|
49 * float |
|
50 * complex |
|
51 |
|
52 Lets first talk about int. :: |
|
53 |
|
54 a = 13 |
|
55 a |
|
56 |
|
57 |
|
58 Now, we have our first int variable a. |
|
59 |
|
60 |
|
61 If we now see :: |
|
62 |
|
63 type(a) |
|
64 <type 'int'> |
|
65 |
|
66 This means that a is a type of int. There are lot of functions associated |
|
67 with the int datatype, to manipulate it in different ways. These can be |
|
68 explored by doing, :: |
|
69 |
|
70 a.<Tab> |
|
71 |
|
72 *int* datatype can hold integers of any size lets see this by an example. |
|
73 :: |
|
74 |
|
75 b = 99999999999999999999 |
|
76 b |
|
77 |
|
78 As you can see even when we put a value of 9 repeated 20 times python did |
|
79 not complain. This is because python's int data-type can hold integers of any |
|
80 size. |
|
81 |
|
82 Let us now look at the float data-type. |
|
83 |
|
84 Decimal numbers in python are represented by the float data-type :: |
|
85 |
|
86 p = 3.141592 |
|
87 p |
|
88 |
|
89 If you notice the value of output of ``p`` isn't exactly equal to ``p``. |
|
90 This is because computer saves floating point values in a specific format. |
|
91 There is always an approximation. This is why we should never rely on |
|
92 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 |
|
100 have a lot of functions specific to them. Let us look at 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 Following is are exercises that you must do. |
|
117 |
|
118 %% %% Find the absolute value of 3+4j |
|
119 :: |
|
120 |
|
121 abs(3+4j) |
|
122 |
|
123 %% %% What is the datatype of number 999999999999999999? Is it |
|
124 not int? |
|
125 :: |
|
126 |
|
127 Long |
|
128 Big integers are internally stored in python |
|
129 as Long datatype. |
|
130 |
|
131 Please, pause the video here. Do the exercises and then continue. |
|
132 |
|
133 |
|
134 {{ Slide for showing Boolean datatypes }} |
|
135 |
|
136 Python also has Boolean as a built-in type. |
|
137 |
|
138 Try it out just type :: |
|
139 |
|
140 t = True |
|
141 |
|
142 note that T in true is capitalized. |
|
143 |
|
144 You can apply different Boolean operations on t now for example :: |
|
145 |
|
146 f = not t |
|
147 f |
|
148 f or t |
|
149 f and t |
|
150 |
|
151 |
|
152 The results are self explanatory. |
|
153 |
|
154 What if you want to apply one operator before another. |
|
155 |
|
156 Well you can use parenthesis for precedence. |
|
157 |
|
158 Lets write some piece of code to check this out.:: |
|
159 |
|
160 a=False |
|
161 b=True |
|
162 c=True |
|
163 |
|
164 |
|
165 To check how precedence changes with parenthesis, we will try two |
|
166 expressions and their evaluation. |
|
167 |
|
168 one :: |
|
169 |
|
170 (a and b) or c |
|
171 |
|
172 This expression gives the value True |
|
173 |
|
174 where as the expression :: |
|
175 |
|
176 a and (b or c) |
|
177 |
|
178 gives the value False. |
|
179 |
|
180 |
|
181 Let's now look at some operators available in Python to manipulate |
|
182 these data types. |
|
183 |
|
184 Python uses '+' for addition :: |
|
185 |
|
186 23 + 74 |
|
187 |
|
188 '-' for subtraction :: |
|
189 |
|
190 23 - 56 |
|
191 |
|
192 '*' for multiplication :: |
|
193 |
|
194 45*76 |
|
195 |
|
196 '/' for division :: |
|
197 |
|
198 384/16 |
|
199 8/3 |
|
200 8.0/3 |
|
201 |
|
202 When we did 8/3 the first case results in am integer |
|
203 output as both the operands are integer however when |
|
204 8.0/3 is used the answer is float as one of the operands is |
|
205 float. |
|
206 |
|
207 |
|
208 '%' for modulo operation :: |
|
209 |
|
210 87 % 6 |
|
211 |
|
212 and two stars for a exponent. :: |
|
213 |
|
214 7**8 |
|
215 |
|
216 |
|
217 In case one wishes to use the current value of variable in which the result |
|
218 is stored in the expression one can do that by putting the operator before |
|
219 `equal to`. :: |
|
220 |
|
221 a=73 |
|
222 a*=34 |
|
223 |
|
224 is same as :: |
|
225 |
|
226 a=a*34 |
|
227 |
|
228 and :: |
|
229 |
|
230 a/=23 |
|
231 |
|
232 is same as :: |
|
233 |
|
234 a=a/23 |
|
235 |
|
236 Following is are exercises that you must do. |
|
237 |
|
238 %% %% Using python find sqaure root of 3? |
|
239 |
|
240 %% %% Is 3**1/2 and 3**0.5 same |
|
241 |
|
242 Please, pause the video here. Do the exercises and then continue. |
|
243 |
|
244 :: |
|
245 |
|
246 3**0.5 |
|
247 |
|
248 :: |
|
249 No,One gives an int answer and the other float |
|
250 |
|
251 |
|
252 Lets now discuss sequence data types in Python. Sequence data types |
|
253 are those in which elements are kept in a sequential order and all the |
|
254 elements are accessed using index numbers. |
|
255 |
|
256 {{{ slide introducing sequence datatype }}} |
|
257 |
|
258 The sequence datatypes in Python are :: |
|
259 |
|
260 * list |
|
261 * string |
|
262 * tuple |
|
263 |
|
264 The list type is a container that holds a number of other objects, in the |
|
265 given order. |
|
266 |
|
267 We create our first list by typing :: |
|
268 |
|
269 num_list = [1, 2, 3, 4] |
|
270 num_list |
|
271 |
|
272 |
|
273 Items enclosed in square brackets separated by comma constitutes a list. |
|
274 |
|
275 Lists can store data of any type in them. |
|
276 |
|
277 We can have a list something like :: |
|
278 |
|
279 var_list = [1, 1.2, [1,2]] |
|
280 var_list |
|
281 |
|
282 Lets look at another sequence data type, strings |
|
283 |
|
284 type :: |
|
285 |
|
286 greeting_string="hello" |
|
287 |
|
288 |
|
289 greeting_string is now a string variable with the value "hello" |
|
290 |
|
291 {{{ All the different types of strings shown }}} |
|
292 |
|
293 Python strings can actually be defined in three different ways :: |
|
294 |
|
295 k='Single quote' |
|
296 l="Let's see how to include a single quote" |
|
297 m='''"Let's see how to include both"''' |
|
298 |
|
299 As you can see, single quotes are used as delimiters usually. |
|
300 |
|
301 When a string contains a single quote, double quotes are used as |
|
302 delimiters. When a string quote contains both single and double quotes, |
|
303 triple quotes are used as delimiters. |
|
304 |
|
305 The last in the list of sequence data types is tuple. |
|
306 |
|
307 To create a tuple we use normal brackets '(' unlike '[' for lists.:: |
|
308 |
|
309 num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) |
|
310 |
|
311 Because of their sequential property there are certain functions and |
|
312 operations we can apply to all of them. |
|
313 |
|
314 |
|
315 |
|
316 The first one is accessing. |
|
317 |
|
318 They can be accessed using index numbers :: |
|
319 |
|
320 num_list[2] |
|
321 num_list[-1] |
|
322 greeting_string[1] |
|
323 greeting_string[3] |
|
324 greeting_string[-2] |
|
325 num_tuple[2] |
|
326 num_tuple[-3] |
|
327 |
|
328 |
|
329 Indexing starts from 0 from left to right and from -1 when accessing lists |
|
330 in reverse. Thus num_list[2] refers to the third element 3. and greetings |
|
331 [-2] is the second element from the end , that is 'l'. |
|
332 |
|
333 |
|
334 |
|
335 Addition gives a new sequence containing both sequences :: |
|
336 |
|
337 num_list+var_list |
|
338 a_string="another string" |
|
339 greeting_string+a_string |
|
340 t2=(3,4,6,7) |
|
341 num_tuple+t2 |
|
342 |
|
343 len function gives the length :: |
|
344 |
|
345 len(num_list) |
|
346 len(greeting_string) |
|
347 len(num_tuple) |
|
348 |
|
349 Prints the length the variable. |
|
350 |
|
351 We can check the containership of an element using the 'in' keyword :: |
|
352 |
|
353 3 in num_list |
|
354 'H' in greeting_string |
|
355 2 in num_tuple |
|
356 |
|
357 We see that it gives True and False accordingly. |
|
358 |
|
359 Find maximum using max function and minimum using min:: |
|
360 |
|
361 max(num_tuple) |
|
362 min(greeting_string) |
|
363 |
|
364 Get a sorted list :: |
|
365 |
|
366 sorted(num_list) |
|
367 |
|
368 |
|
369 As a consequence of their order, we can access a group of elements in a |
|
370 sequence, together. This is called slicing and striding. |
|
371 |
|
372 First lets discuss Slicing, |
|
373 |
|
374 Given a list :: |
|
375 |
|
376 j=[1,2,3,4,5,6] |
|
377 |
|
378 Lets say we want elements starting from 2 and ending in 5. |
|
379 |
|
380 For this we can do :: |
|
381 |
|
382 j[1:4] |
|
383 |
|
384 The syntax for slicing is, sequence variable name square bracket first |
|
385 element index, colon, second element index. The last element however is not |
|
386 included in the resultant list:: |
|
387 |
|
388 |
|
389 j[:4] |
|
390 |
|
391 If first element is left blank default is from beginning and if last |
|
392 element is left blank it means till the end. |
|
393 |
|
394 :: |
|
395 |
|
396 j[1:] |
|
397 |
|
398 j[:] |
|
399 |
|
400 This effectively is the whole list. |
|
401 |
|
402 Striding is similar to slicing except that the step size here is not one. |
|
403 |
|
404 Lets see by example :: |
|
405 |
|
406 new_num_list=[1,2,3,4,5,6,7,8,9,10] |
|
407 new_num_list[1:8:2] |
|
408 [2, 4, 6, 8] |
|
409 |
|
410 The colon two added in the end signifies all the alternate elements. This |
|
411 is why we call this concept striding because we move through the list with |
|
412 a particular stride or step. The step in this example being 2. |
|
413 |
|
414 We have talked about many similar features of lists, strings and tuples. |
|
415 But there are many important features in lists that differ from strings and |
|
416 tuples. Lets see this by example.:: |
|
417 |
|
418 new_num_list[1]=9 |
|
419 greeting_string[1]='k' |
|
420 |
|
421 {{{ slide to show the error }}} |
|
422 |
|
423 |
|
424 |
|
425 As you can see while the first command executes with out a problem there is |
|
426 an error on the second one. |
|
427 |
|
428 Now lets try :: |
|
429 |
|
430 new_tuple[1]=5 |
|
431 |
|
432 Its the same error. This is because strings and tuples share the property |
|
433 of being immutable. We cannot change the value at a particular index just |
|
434 by assigning a new value at that position. |
|
435 |
|
436 |
|
437 We have looked at different types but we need to convert one data type into |
|
438 another. Well lets one by one go through methods by which we can convert |
|
439 one data type to other: |
|
440 |
|
441 We can convert all the number data types to one another :: |
|
442 |
|
443 i=34 |
|
444 d=float(i) |
|
445 d |
|
446 |
|
447 Python has built in functions int, float and complex to convert one number |
|
448 type data structure to another. |
|
449 |
|
450 :: |
|
451 |
|
452 dec=2.34 |
|
453 dec_con=int(dec) |
|
454 dec_con |
|
455 |
|
456 |
|
457 As you can see the decimal part of the number is simply stripped to get the |
|
458 integer.:: |
|
459 |
|
460 com=2.3+4.2j |
|
461 float(com) |
|
462 com |
|
463 |
|
464 In case of complex number to floating point only the real value of complex |
|
465 number is taken. |
|
466 |
|
467 Similarly we can convert list to tuple and tuple to list :: |
|
468 |
|
469 lst=[3,4,5,6] |
|
470 tup=tuple(lst) |
|
471 tupl=(3,23,4,56) |
|
472 lst=list(tuple) |
|
473 |
|
474 However converting a string to a list and a list to a string is an |
|
475 interesting problem. Let's say we have a string :: |
|
476 |
|
477 In: somestring="Is there a way to split on these spaces." |
|
478 In: somestring.split() |
|
479 |
|
480 |
|
481 This produces a list with the string split at whitespace. Similarly we can |
|
482 split on some other character. |
|
483 |
|
484 :: |
|
485 |
|
486 In: otherstring="Tim,Amy,Stewy,Boss" |
|
487 |
|
488 How do we split on comma , simply pass it as argument :: |
|
489 |
|
490 In: otherstring.split(',') |
|
491 |
|
492 join function does the opposite. Joins a list to make a string.:: |
|
493 |
|
494 ','.join['List','joined','on','commas'] |
|
495 |
|
496 Thus we get a list joined on commas. Similarly we can do spaces.:: |
|
497 |
|
498 ' '.join['Now','on','spaces'] |
|
499 |
|
500 Note that the list has to be a list of strings to apply join operation. |
|
501 |
|
502 With this we come to the end of this tutorial . |
|
503 |
|
504 Following is an (are) exercise(s) that you must do. |
|
505 |
|
506 |
|
507 |
|
508 %% %% Check if 3 is an element of the list [1,7,5,3,4]. In case |
|
509 it is change it to 21. |
|
510 :: |
|
511 l=[1,7,5,3,4] |
|
512 3 in l |
|
513 l[3]=21 |
|
514 l |
|
515 |
|
516 %% %% Convert the string "Elizabeth is queen of england" to |
|
517 "Elizabeth is queen" |
|
518 :: |
|
519 |
|
520 s="Elizabeth is queen of england" |
|
521 stemp=s.split() |
|
522 ' '.join(stemp[:3]) |
|
523 |
|
524 Please, pause the video here. Do the exercise(s) and then continue. |
|
525 |
|
526 |
|
527 This brings us to the end of the tutorial. In this tutorial we have |
|
528 discussed |
|
529 |
|
530 1. Number Datatypes , integer,float and complex |
|
531 2. Boolean and datatype and operators |
|
532 3. Sequence data types ,List,String and Tuple |
|
533 4. Accesing sequence |
|
534 5. Slicing sequences |
|
535 6. Finding length , sorting and reversing operations on sequences. |
|
536 7. Immutability. |
|
537 |
|
538 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
539 |
|
540 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
541 |
|
542 Hope you have enjoyed and found it useful. |
|
543 |
|
544 Thank You. |
|
545 |
|
546 |
|
547 .. |
|
548 Local Variables: |
|
549 mode: rst |
|
550 indent-tabs-mode: nil |
|
551 sentence-end-double-space: nil |
|
552 fill-column: 75 |
|
553 End: |
|