11 |
11 |
12 .. Author : Amit Sethi |
12 .. Author : Amit Sethi |
13 Internal Reviewer : |
13 Internal Reviewer : |
14 External Reviewer : |
14 External Reviewer : |
15 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
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. |
16 |
|
17 .. #[Puneeth: Fill in pre-requisites.] |
|
18 |
|
19 Hello friends and welcome to the tutorial on Basic Data types and operators |
|
20 in Python. |
|
21 |
17 {{{ Show the slide containing title }}} |
22 {{{ Show the slide containing title }}} |
18 |
23 |
19 {{{ Show the slide containing the outline slide }}} |
24 {{{ Show the slide containing the outline slide }}} |
20 |
25 |
21 In this tutorial, we shall look at:: |
26 In this tutorial, we shall look at |
22 |
27 |
23 * Datatypes in Python |
28 * Datatypes in Python |
24 * Operators in Python |
29 * Operators in Python |
25 |
30 |
|
31 .. #[Puneeth: Use double colon only for code blocks.] |
|
32 .. #[Puneeth: include more details in the outline.] |
|
33 |
26 with a little hands-on on how they can be applied to the different data types. |
34 with a little hands-on on how they can be applied to the different data types. |
27 |
35 |
28 |
36 |
29 |
37 |
30 First we will explore python data structures in the domain of numbers. |
38 First we will explore python data structures in the domain of numbers. |
32 |
40 |
33 {{{ A slide to make a memory note of this }}} |
41 {{{ A slide to make a memory note of this }}} |
34 |
42 |
35 These are: |
43 These are: |
36 |
44 |
37 * Integers |
45 * int for integers |
38 * float and |
46 * float for floating point numbers and |
39 * Complex |
47 * complex for complex numbers |
|
48 |
|
49 .. #[Puneeth: Changed to int, float and complex.] |
|
50 |
|
51 .. #[Puneeth: Loss of consistency. You talk of built-in data types, but |
|
52 .. then you were calling them integers, floats and complex. Clean up |
|
53 .. required.] |
40 |
54 |
41 Lets first talk about integers. :: |
55 Lets first talk about integers. :: |
42 |
56 |
43 a = 13 |
57 a = 13 |
44 a |
58 a |
45 |
59 |
46 |
60 |
47 Thats it, there we have our first integer variable a. |
61 Now, we have our first integer variable a. |
48 |
|
49 |
62 |
50 |
63 |
51 If we now see :: |
64 If we now see :: |
52 |
65 |
53 type(a) |
66 type(a) |
54 <type 'int'> |
67 <type 'int'> |
55 |
68 |
56 This means that a is a type of int. Being an int data structure |
69 This means that a is a type of int. Being an int data structure in python |
57 in python means that there are various functions that this variable |
70 means that there are various functions that this variable has to manipulate |
58 has to manipulate it different ways. You can explore these by doing, |
71 it different ways. You can explore these by doing, |
59 |
72 |
60 a.<Tab> |
73 a.<Tab> |
61 |
74 |
62 |
75 .. #[Puneeth: Why are we suddenly talking of limits? |
|
76 .. Something like this would be better. |
|
77 .. int data-type can hold integers of any size. for example - ] |
63 |
78 |
64 Lets see the limits of this int. |
79 Lets see the limits of this int. |
65 |
80 |
66 b = 99999999999999999999 |
81 b = 99999999999999999999 |
67 b |
82 b |
68 |
83 |
69 As you can see even when we put a value of 9 repeated 20 times |
84 As you can see even when we put a value of 9 repeated 20 times python did |
70 python did not complain. However when you asked python to print |
85 not complain. However when you asked python to print the number again it |
71 the number again it put a capital L at the end. Now if you check |
86 put a capital L at the end. Now if you check the type of this variable b, |
72 the type of this variable b, :: |
87 :: |
73 |
88 |
74 type(b) |
89 type(b) |
75 <type 'long'> |
90 <type 'long'> |
76 |
91 |
77 |
92 |
78 The reason for this is that python recognizes large integer numbers |
93 The reason for this is that python recognizes large integer numbers by the |
79 by the data type long. However long type and integer type share there |
94 data type long. However long type and integer type share there functions |
80 functions and properties. |
95 and properties. |
81 |
96 |
82 Lets now try out the second type in list called float. |
97 .. #[Puneeth: again, the clean-up that I talked of above. Decide if you are |
83 |
98 .. talking about the different type of numbers and the datatypes that are |
84 Decimal numbers in python are recognized by the term float :: |
99 .. used to represent them or if you are talking of the data-types and what |
|
100 .. kind of numbers they represent. I think you should choose the former.] |
|
101 |
|
102 Let us now look at the float data-type. |
|
103 |
|
104 Decimal numbers in python are represented by the float data-type :: |
85 |
105 |
86 p = 3.141592 |
106 p = 3.141592 |
87 p |
107 p |
88 |
108 |
89 If you notice the value of output of p isn't exactly equal to p. This |
109 If you notice the value of output of p isn't exactly equal to p. This is |
90 is because computer saves floating point values in a specific |
110 because computer saves floating point values in a specific format. There is |
91 format. There is always an aproximationation. This is why we should |
111 always an aproximationation. This is why we should never rely on equality |
92 never rely on equality of floating point numbers in a program. |
112 of floating point numbers in a program. |
93 |
113 |
94 The last data type in the list is complex number :: |
114 The last data type in the list is complex number :: |
95 |
115 |
96 c = 3.2+4.6j |
116 c = 3.2+4.6j |
97 |
117 |
98 as simple as that so essentialy its just a combination of two floats the |
118 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. |
119 imaginary part being defined by j notation instead of i. Complex numbers |
100 Lets check these :: |
120 have a lot of functions specific to them. Lets check these :: |
101 |
121 |
102 c.<Tab> |
122 c.<Tab> |
103 |
123 |
104 Lets try some of them :: |
124 Lets try some of them :: |
105 |
125 |
130 f |
150 f |
131 f or t |
151 f or t |
132 f and t |
152 f and t |
133 |
153 |
134 |
154 |
135 |
155 The results are self explanatory. |
136 The results are explanotary in themselves. |
156 |
137 |
157 .. #[Puneeth: Why does booleans bring us to precedence? I don't see the |
138 The usage of boolean brings us to an interesting question of precendence. |
158 .. connection. Am I missing something?] |
139 What if you want to apply one operator before another. |
159 |
|
160 The usage of boolean brings us to an interesting question of precedence. |
|
161 What if you want to apply one operator before another. |
140 |
162 |
141 Well you can use parenthesis for precedence. |
163 Well you can use parenthesis for precedence. |
142 |
164 |
143 Lets write some piece of code to check this out. |
165 Lets write some piece of code to check this out.:: |
144 |
166 |
145 In[]: a=False |
167 In[]: a=False |
146 In[]: b=True |
168 In[]: b=True |
147 In[]: c=True |
169 In[]: c=True |
148 |
170 |
149 To check how precedence changes with parenthesis. We will try two |
171 |
|
172 .. #[Puneeth: Consistency. In[]: is not present at other places.] |
|
173 |
|
174 To check how precedence changes with parenthesis, we will try two |
150 expressions and their evaluation. |
175 expressions and their evaluation. |
151 |
176 |
152 one :: |
177 one :: |
153 |
178 |
154 (a and b) or c |
179 (a and b) or c |
189 |
219 |
190 is same as :: |
220 is same as :: |
191 |
221 |
192 a=a/23 |
222 a=a/23 |
193 |
223 |
194 |
224 Lets now discuss sequence data types in Python. Sequence data types |
195 Lets now discuss sequence data stypes in python. Sequence |
225 are those in which elements are kept in a sequential order. All the |
196 datatypes are those in which elements are kept in a sequential |
226 elements accessed using index. |
197 order. All the elements accessed using index. |
227 |
198 |
228 .. #[Puneeth: fix the last sentence - it sounds incomplete] |
199 |
229 |
200 {{{ slide to for memory aid }}} |
230 {{{ slide for memory aid }}} |
201 |
231 |
202 The sequence datatypes in python are :: |
232 The sequence datatypes in Python are :: |
203 |
233 |
204 * list |
234 * list |
205 * string |
235 * string |
206 * tuple |
236 * tuple |
207 |
237 |
208 The list type is a container that holds a number of other |
238 The list type is a container that holds a number of other objects, in the |
209 objects, in the given order. |
239 given order. |
210 |
240 |
211 We create our first list by typing :: |
241 We create our first list by typing :: |
212 |
242 |
213 num_list = [1, 2, 3, 4] |
243 num_list = [1, 2, 3, 4] |
214 num_list |
244 num_list |
215 |
245 |
216 |
246 |
217 Items enclosed in square brackets separated by comma |
247 Items enclosed in square brackets separated by comma constitutes a list. |
218 constitutes a list. |
248 |
219 |
249 Lists can store data of any type in them. |
220 Lists can store data of any type in them. |
|
221 |
250 |
222 We can have a list something like :: |
251 We can have a list something like :: |
223 |
252 |
224 var_list = [1, 1.2, [1,2]] |
253 var_list = [1, 1.2, [1,2]] |
225 var_list |
254 var_list |
226 |
255 |
227 |
256 .. #[Puneeth: some continuity, when jumping to strings?] |
228 |
257 |
229 Now we will have a look at strings |
258 Now we will have a look at strings |
230 |
259 |
231 type :: |
260 type :: |
232 |
261 |
233 In[]: greeting_string="hello" |
262 In[]: greeting_string="hello" |
234 |
263 |
241 |
270 |
242 In[]: k='Single quote' |
271 In[]: k='Single quote' |
243 In[]: l="Double quote contain's single quote" |
272 In[]: l="Double quote contain's single quote" |
244 In[]: m='''"Contain's both"''' |
273 In[]: m='''"Contain's both"''' |
245 |
274 |
|
275 .. #[Puneeth: Contain's? That's not a word!] |
|
276 |
246 Thus, single quotes are used as delimiters usually. |
277 Thus, single quotes are used as delimiters usually. |
247 When a string contains a single quote, double quotes are used as delimiters. |
278 |
248 When a string quote contains both single and double quotes, triple quotes are |
279 .. #[Puneeth: Thus?] |
249 used as delimiters. |
280 |
|
281 When a string contains a single quote, double quotes are used as |
|
282 delimiters. When a string quote contains both single and double quotes, |
|
283 triple quotes are used as delimiters. |
250 |
284 |
251 The last in the list of sequence data types is tuple. |
285 The last in the list of sequence data types is tuple. |
252 |
286 |
253 To create a tuple we use normal brackets '(' |
287 To create a tuple we use normal brackets '(' unlike '[' for lists.:: |
254 unlike '[' for lists.:: |
|
255 |
288 |
256 In[]: num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) |
289 In[]: num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) |
257 |
290 |
258 Because of their sequential property there are certain functions and |
291 Because of their sequential property there are certain functions and |
259 operations we can apply to all of them. |
292 operations we can apply to all of them. |
260 |
293 |
261 |
294 |
262 |
295 |
263 The first one is accessing. |
296 The first one is accessing. |
264 |
297 |
349 |
387 |
350 new_num_list=[1,2,3,4,5,6,7,8,9,10] |
388 new_num_list=[1,2,3,4,5,6,7,8,9,10] |
351 new_num_list[1:8:2] |
389 new_num_list[1:8:2] |
352 [2, 4, 6, 8] |
390 [2, 4, 6, 8] |
353 |
391 |
354 The colon two added in the end signifies all the alternate elements. This is why we call this concept |
392 The colon two added in the end signifies all the alternate elements. This |
355 striding because we move through the list with a particular stride or step. The step in this example |
393 is why we call this concept striding because we move through the list with |
356 being 2. |
394 a particular stride or step. The step in this example being 2. |
357 |
395 |
358 We have talked about many similar features of lists, strings and tuples. But there are many important |
396 We have talked about many similar features of lists, strings and tuples. |
359 features in lists that differ from strings and tuples. Lets see this by example.:: |
397 But there are many important features in lists that differ from strings and |
|
398 tuples. Lets see this by example.:: |
360 |
399 |
361 In[]: new_num_list[1]=9 |
400 In[]: new_num_list[1]=9 |
362 In[]: greeting_string[1]='k' |
401 In[]: greeting_string[1]='k' |
363 |
402 |
364 {{{ slide to show the error }}} |
403 {{{ slide to show the error }}} |
365 |
404 |
366 |
405 |
367 |
406 |
368 As you can see while the first command executes with out a problem there is an error on the second one. |
407 As you can see while the first command executes with out a problem there is |
|
408 an error on the second one. |
369 |
409 |
370 Now lets try :: |
410 Now lets try :: |
371 |
411 |
372 In[]: new_tuple[1]=5 |
412 In[]: new_tuple[1]=5 |
373 |
413 |
374 Its the same error. This is because strings and tuples share the property of being immutable. |
414 Its the same error. This is because strings and tuples share the property |
375 We cannot change the value at a particular index just by assigning a new value at that position. |
415 of being immutable. We cannot change the value at a particular index just |
376 |
416 by assigning a new value at that position. |
377 |
417 |
378 We have looked at different types but we need to convert one data type into another. Well lets one |
418 |
379 by one go through methods by which we can convert one data type to other: |
419 We have looked at different types but we need to convert one data type into |
|
420 another. Well lets one by one go through methods by which we can convert |
|
421 one data type to other: |
380 |
422 |
381 We can convert all the number data types to one another :: |
423 We can convert all the number data types to one another :: |
382 |
424 |
383 i=34 |
425 i=34 |
384 d=float(i) |
426 d=float(i) |
385 d |
427 d |
386 |
428 |
387 Python has built in functions int, float and complex to convert one number type |
429 Python has built in functions int, float and complex to convert one number |
388 data structure to another. |
430 type data structure to another. |
|
431 |
|
432 :: |
389 |
433 |
390 dec=2.34 |
434 dec=2.34 |
391 dec_con=int(dec) |
435 dec_con=int(dec) |
392 dec_con |
436 dec_con |
393 |
437 |
394 |
438 |
395 As you can see the decimal part of the number is simply stripped to get the integer.:: |
439 As you can see the decimal part of the number is simply stripped to get the |
|
440 integer.:: |
396 |
441 |
397 com=2.3+4.2j |
442 com=2.3+4.2j |
398 float(com) |
443 float(com) |
399 com |
444 com |
400 |
445 |
401 In case of complex number to floating point only the real value of complex number is taken. |
446 In case of complex number to floating point only the real value of complex |
|
447 number is taken. |
402 |
448 |
403 Similarly we can convert list to tuple and tuple to list :: |
449 Similarly we can convert list to tuple and tuple to list :: |
404 |
450 |
405 lst=[3,4,5,6] |
451 lst=[3,4,5,6] |
406 tup=tuple(lst) |
452 tup=tuple(lst) |
407 tupl=(3,23,4,56) |
453 tupl=(3,23,4,56) |
408 lst=list(tuple) |
454 lst=list(tuple) |
409 |
455 |
410 However string to list and list to string is an interesting problem. |
456 However converting a string to a list and a list to a string is an |
411 Lets say we have a string :: |
457 interesting problem. Let's say we have a string :: |
412 |
458 |
413 In: somestring="Is there a way to split on these spaces." |
459 In: somestring="Is there a way to split on these spaces." |
414 In: somestring.split() |
460 In: somestring.split() |
415 |
461 |
416 |
462 |
417 This produces a list with the string split at whitespace. |
463 This produces a list with the string split at whitespace. Similarly we can |
418 similarly we can split on some other character. |
464 split on some other character. |
|
465 |
|
466 :: |
419 |
467 |
420 In: otherstring="Tim,Amy,Stewy,Boss" |
468 In: otherstring="Tim,Amy,Stewy,Boss" |
421 |
469 |
422 How do we split on comma , simply pass it as argument :: |
470 How do we split on comma , simply pass it as argument :: |
423 |
471 |