13 |
13 |
14 .. Author : Amit Sethi |
14 .. Author : Amit Sethi |
15 Internal Reviewer : |
15 Internal Reviewer : |
16 External Reviewer : |
16 External Reviewer : |
17 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
17 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
18 |
|
19 .. #[Puneeth: Fill in pre-requisites.] |
|
20 |
18 |
21 Hello friends and welcome to the tutorial on Basic Data types and operators |
19 Hello friends and welcome to the tutorial on Basic Data types and operators |
22 in Python. |
20 in Python. |
23 |
21 |
24 {{{ Show the slide containing title }}} |
22 {{{ Show the slide containing title }}} |
38 * Python Sequence Data types |
36 * Python Sequence Data types |
39 * list |
37 * list |
40 * string |
38 * string |
41 * tuple |
39 * tuple |
42 |
40 |
43 .. #[Puneeth: Use double colon only for code blocks.] |
|
44 .. #[Puneeth: include more details in the outline.] |
|
45 |
|
46 with a little hands-on on how they can be applied to the different data types. |
|
47 |
|
48 |
|
49 |
|
50 First we will explore python data structures in the domain of numbers. |
41 First we will explore python data structures in the domain of numbers. |
51 There are three built-in data types in python to represent numbers. |
42 There are three built-in data types in python to represent numbers. |
52 |
43 |
53 {{{ A slide to make a memory note of the different datatypes }}} |
44 {{{ A slide to make a memory note of the different datatypes }}} |
54 |
45 |
55 These are: |
46 These are: |
56 |
47 |
57 * int |
48 * int |
58 * float |
49 * float |
59 * complex |
50 * complex |
60 |
|
61 .. #[Puneeth: Changed to int, float and complex.] |
|
62 |
|
63 .. #[Puneeth: Loss of consistency. You talk of built-in data types, but |
|
64 .. then you were calling them integers, floats and complex. Clean up |
|
65 .. required.] |
|
66 |
51 |
67 Lets first talk about int. :: |
52 Lets first talk about int. :: |
68 |
53 |
69 a = 13 |
54 a = 13 |
70 a |
55 a |
76 If we now see :: |
61 If we now see :: |
77 |
62 |
78 type(a) |
63 type(a) |
79 <type 'int'> |
64 <type 'int'> |
80 |
65 |
81 This means that a is a type of int. Being an int data type in python |
66 This means that a is a type of int. There are lot of functions associated |
82 means that there are various functions that this variable has to manipulate |
67 with the int datatype, to manipulate it in different ways. These can be |
83 in different ways. You can explore these by doing, |
68 explored by doing, :: |
84 |
69 |
85 a.<Tab> |
70 a.<Tab> |
86 |
71 |
87 .. #[Puneeth: Why are we suddenly talking of limits? |
|
88 .. Something like this would be better. |
|
89 .. int data-type can hold integers of any size. for example - ] |
|
90 |
|
91 *int* datatype can hold integers of any size lets see this by an example. |
72 *int* datatype can hold integers of any size lets see this by an example. |
|
73 :: |
92 |
74 |
93 b = 99999999999999999999 |
75 b = 99999999999999999999 |
94 b |
76 b |
95 |
77 |
96 As you can see even when we put a value of 9 repeated 20 times python did |
78 As you can see even when we put a value of 9 repeated 20 times python did |
97 not complain. This is because python's int data-type can hold integers of any |
79 not complain. This is because python's int data-type can hold integers of any |
98 size. |
80 size. |
99 |
81 |
100 .. #[Puneeth: again, the clean-up that I talked of above. Decide if you are |
|
101 .. talking about the different type of numbers and the datatypes that are |
|
102 .. used to represent them or if you are talking of the data-types and what |
|
103 .. kind of numbers they represent. I think you should choose the former.] |
|
104 |
|
105 Let us now look at the float data-type. |
82 Let us now look at the float data-type. |
106 |
83 |
107 Decimal numbers in python are represented by the float data-type :: |
84 Decimal numbers in python are represented by the float data-type :: |
108 |
85 |
109 p = 3.141592 |
86 p = 3.141592 |
110 p |
87 p |
111 |
88 |
112 If you notice the value of output of p isn't exactly equal to p. This is |
89 If you notice the value of output of ``p`` isn't exactly equal to ``p``. |
113 because computer saves floating point values in a specific format. There is |
90 This is because computer saves floating point values in a specific format. |
114 always an aproximationation. This is why we should never rely on equality |
91 There is always an approximation. This is why we should never rely on |
115 of floating point numbers in a program. |
92 equality of floating point numbers in a program. |
116 |
93 |
117 The last data type in the list is complex number :: |
94 The last data type in the list is complex number :: |
118 |
95 |
119 c = 3.2+4.6j |
96 c = 3.2+4.6j |
120 |
97 |
121 as simple as that so essentialy its just a combination of two floats the |
98 as simple as that so essentialy its just a combination of two floats the |
122 imaginary part being defined by j notation instead of i. Complex numbers |
99 imaginary part being defined by j notation instead of i. Complex numbers |
123 have a lot of functions specific to them. Lets check these :: |
100 have a lot of functions specific to them. Let us look at these :: |
124 |
101 |
125 c.<Tab> |
102 c.<Tab> |
126 |
103 |
127 Lets try some of them :: |
104 Lets try some of them :: |
128 |
105 |
172 f and t |
149 f and t |
173 |
150 |
174 |
151 |
175 The results are self explanatory. |
152 The results are self explanatory. |
176 |
153 |
177 .. #[Puneeth: Why does booleans bring us to precedence? I don't see the |
|
178 .. connection. Am I missing something?] |
|
179 |
|
180 |
|
181 What if you want to apply one operator before another. |
154 What if you want to apply one operator before another. |
182 |
155 |
183 Well you can use parenthesis for precedence. |
156 Well you can use parenthesis for precedence. |
184 |
157 |
185 Lets write some piece of code to check this out.:: |
158 Lets write some piece of code to check this out.:: |
186 |
159 |
187 a=False |
160 a=False |
188 b=True |
161 b=True |
189 c=True |
162 c=True |
190 |
163 |
191 |
|
192 .. #[Puneeth: Consistency. In[]: is not present at other places.] |
|
193 |
164 |
194 To check how precedence changes with parenthesis, we will try two |
165 To check how precedence changes with parenthesis, we will try two |
195 expressions and their evaluation. |
166 expressions and their evaluation. |
196 |
167 |
197 one :: |
168 one :: |
262 |
231 |
263 is same as :: |
232 is same as :: |
264 |
233 |
265 a=a/23 |
234 a=a/23 |
266 |
235 |
267 Following is an (are) exercise(s) that you must do. |
236 Following is are exercises that you must do. |
268 |
237 |
269 %% %% Using python find sqaure root of 3? |
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 |
270 :: |
244 :: |
271 |
245 |
272 3**0.5 |
246 3**0.5 |
273 |
247 |
274 %% %% Is 3**1/2 and 3**0.5 same |
|
275 :: |
248 :: |
276 No,One gives an int answer and the other float |
249 No,One gives an int answer and the other float |
277 |
|
278 Please, pause the video here. Do the exercises and then continue. |
|
279 |
250 |
280 |
251 |
281 Lets now discuss sequence data types in Python. Sequence data types |
252 Lets now discuss sequence data types in Python. Sequence data types |
282 are those in which elements are kept in a sequential order and all the |
253 are those in which elements are kept in a sequential order and all the |
283 elements are accessed using index numbers. |
254 elements are accessed using index numbers. |
284 |
|
285 .. #[Puneeth: fix the last sentence - it sounds incomplete] |
|
286 |
255 |
287 {{{ slide introducing sequence datatype }}} |
256 {{{ slide introducing sequence datatype }}} |
288 |
257 |
289 The sequence datatypes in Python are :: |
258 The sequence datatypes in Python are :: |
290 |
259 |
308 We can have a list something like :: |
277 We can have a list something like :: |
309 |
278 |
310 var_list = [1, 1.2, [1,2]] |
279 var_list = [1, 1.2, [1,2]] |
311 var_list |
280 var_list |
312 |
281 |
313 .. #[Puneeth: some continuity, when jumping to strings?] |
|
314 |
|
315 Lets look at another sequence data type, strings |
282 Lets look at another sequence data type, strings |
316 |
283 |
317 type :: |
284 type :: |
318 |
285 |
319 greeting_string="hello" |
286 greeting_string="hello" |
327 |
294 |
328 k='Single quote' |
295 k='Single quote' |
329 l="Let's see how to include a single quote" |
296 l="Let's see how to include a single quote" |
330 m='''"Let's see how to include both"''' |
297 m='''"Let's see how to include both"''' |
331 |
298 |
332 .. #[Puneeth: Contain's? That's not a word!] |
|
333 |
|
334 As you can see, single quotes are used as delimiters usually. |
299 As you can see, single quotes are used as delimiters usually. |
335 |
|
336 .. #[Puneeth: Thus?] |
|
337 |
300 |
338 When a string contains a single quote, double quotes are used as |
301 When a string contains a single quote, double quotes are used as |
339 delimiters. When a string quote contains both single and double quotes, |
302 delimiters. When a string quote contains both single and double quotes, |
340 triple quotes are used as delimiters. |
303 triple quotes are used as delimiters. |
341 |
304 |
561 ' '.join(stemp[:3]) |
522 ' '.join(stemp[:3]) |
562 |
523 |
563 Please, pause the video here. Do the exercise(s) and then continue. |
524 Please, pause the video here. Do the exercise(s) and then continue. |
564 |
525 |
565 |
526 |
566 |
527 This brings us to the end of the tutorial. In this tutorial we have |
567 In this tutorial we have discussed |
528 discussed |
568 |
529 |
569 1. Number Datatypes , integer,float and complex |
530 1. Number Datatypes , integer,float and complex |
570 2. Boolean and datatype and operators |
531 2. Boolean and datatype and operators |
571 3. Sequence data types ,List,String and Tuple |
532 3. Sequence data types ,List,String and Tuple |
572 4. Accesing sequence |
533 4. Accesing sequence |
573 5. Slicing sequences |
534 5. Slicing sequences |
574 6. Finding length , sorting and reversing operations on sequences. |
535 6. Finding length , sorting and reversing operations on sequences. |
575 7. Immutability. |
536 7. Immutability. |
576 |
537 |
577 |
|
578 |
|
579 |
|
580 .. #[Nishanth]: string to list is fine. But list to string can be left for |
|
581 string manipulations. Just say it requires some string |
|
582 manipulations and leave it there. |
|
583 |
|
584 .. #[Nishanth]: Where is the summary |
|
585 There are no exercises in the script |
|
586 |
|
587 {{{ Show the "sponsored by FOSSEE" slide }}} |
538 {{{ Show the "sponsored by FOSSEE" slide }}} |
588 |
539 |
589 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
540 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
590 |
541 |
591 Hope you have enjoyed and found it useful. |
542 Hope you have enjoyed and found it useful. |