8 |
8 |
9 * Various Datatypes in Python |
9 * Various Datatypes in Python |
10 * Operators with a little hands-on on how they can be applied to |
10 * Operators with a little hands-on on how they can be applied to |
11 the different data types. |
11 the different data types. |
12 |
12 |
13 Since this a hands on session, you will require python installed in your |
13 |
14 computer. |
|
15 .. #[Nishanth]: this line is not required |
|
16 |
14 |
17 First we will explore python data structures in the domain of numbers. |
15 First we will explore python data structures in the domain of numbers. |
18 There are three built-in data structures in python to represent numbers. |
16 There are three built-in data types in python to represent numbers. |
19 |
|
20 .. #[Nishanth]: Did you mean data types when you said data structures?? |
|
21 Data structures is used for lists and others. |
|
22 |
17 |
23 {{{ A slide to make a memory note of this }}} |
18 {{{ A slide to make a memory note of this }}} |
24 |
19 |
25 These are: |
20 These are: |
26 |
21 |
79 If you notice the value of output of p isn't exactly equal to p. This |
74 If you notice the value of output of p isn't exactly equal to p. This |
80 is because computer saves floating point values in a specific |
75 is because computer saves floating point values in a specific |
81 format. There is always an aproximationation. This is why we should |
76 format. There is always an aproximationation. This is why we should |
82 never rely on equality of floating point numbers in a program. |
77 never rely on equality of floating point numbers in a program. |
83 |
78 |
84 The last data structure in the list is complex number :: |
79 The last data type in the list is complex number :: |
85 |
80 |
86 c = 3.2+4.6j |
81 c = 3.2+4.6j |
87 |
82 |
88 as simple as that so essentialy its just a combination of two floats the |
83 as simple as that so essentialy its just a combination of two floats the |
89 imaginary part being define by j notation usually used in electrical |
84 imaginary part being define by j notation instead of i. Complex numbers have a lot of functions specific to them. |
90 engineering. Complex numbers have a lot of functions specific to them. |
|
91 Lets check these :: |
85 Lets check these :: |
92 |
86 |
93 c.<Tab> |
87 c.<Tab> |
94 |
|
95 .. #[Nishanth]: rephrase the "j used in electrical engineering" |
|
96 Its ok if you skip it also. Just say that here |
|
97 j is used and not i |
|
98 |
88 |
99 Lets try some of them :: |
89 Lets try some of them :: |
100 |
90 |
101 c.real |
91 c.real |
102 c.imag |
92 c.imag |
168 The list type is a container that holds a number of other |
157 The list type is a container that holds a number of other |
169 objects, in the given order. |
158 objects, in the given order. |
170 |
159 |
171 We create our first list by typing :: |
160 We create our first list by typing :: |
172 |
161 |
173 In[]: num = [1, 2, 3, 4] |
162 num_list = [1, 2, 3, 4] |
174 |
163 num_list |
175 .. #[Nishanth]: Show the value of the variable |
164 |
|
165 |
176 Items enclosed in square brackets separated by comma |
166 Items enclosed in square brackets separated by comma |
177 constitutes a list. |
167 constitutes a list. |
178 |
168 |
179 Lists can store data of any type in them. |
169 Lists can store data of any type in them. |
180 |
170 |
181 We can have a list something like :: |
171 We can have a list something like :: |
182 |
172 |
183 In[]: var = [1, 1.2, [1,2]] |
173 var_list = [1, 1.2, [1,2]] |
184 |
174 var_list |
185 .. #[Nishanth]: Show the value of the variable |
175 |
186 print var |
176 |
187 |
177 |
188 Now we will have a look at strings |
178 Now we will have a look at strings |
189 |
179 |
190 type :: |
180 type :: |
191 |
181 |
192 In[]: w="hello" |
182 In[]: greeting_string="hello" |
193 |
183 |
194 .. #[Nishanth]: Show the value of the variable |
184 |
195 w is now a string variable with the value "hello" |
185 greeting_string is now a string variable with the value "hello" |
196 |
186 |
197 {{{ Memory Aid Slide }}} |
187 {{{ Memory Aid Slide }}} |
198 |
188 |
199 Python strings can actually be defined in three different ways :: |
189 Python strings can actually be defined in three different ways :: |
200 |
190 |
210 The last in the list of sequence data types is tuple. |
200 The last in the list of sequence data types is tuple. |
211 |
201 |
212 To create a tuple we use normal brackets '(' |
202 To create a tuple we use normal brackets '(' |
213 unlike '[' for lists.:: |
203 unlike '[' for lists.:: |
214 |
204 |
215 In[]: t = (1, 2, 3, 4, 5, 6, 7, 8) |
205 In[]: num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) |
216 |
206 |
217 Because of their sequential property there are certain functions and |
207 Because of their sequential property there are certain functions and |
218 operations we can apply to all of them. |
208 operations we can apply to all of them. |
219 |
209 |
220 {{{ Slide for memory aid }}} |
210 {{{ Slide for memory aid }}} |
221 |
211 |
222 The first one is accessing. |
212 The first one is accessing. |
223 |
213 |
224 They can be accessed using index numbers :: |
214 They can be accessed using index numbers :: |
225 |
215 |
226 In[]: num[2] |
216 In[]: num_list[2] |
227 In[]: num[-1] |
217 In[]: num_list[-1] |
228 In[]: w[1] |
218 In[]: greeting_string[1] |
229 In[]: w[3] |
219 In[]: greeting_string[3] |
230 In[]: w[-2] |
220 In[]: greeting_string[-2] |
231 In[]: t[2] |
221 In[]: num_tuple[2] |
232 In[]: t[-3] |
222 In[]: num_tuple[-3] |
233 |
223 |
234 Negative indices can be used to access in reverse |
224 |
235 |
225 Indexing starts from 0 from left to right and from -1 when accessing |
236 .. #[Nishanth]: Elaborate on indexing. |
226 lists in reverse. Thus num_list[2] refers to the third element 3. |
237 Indexing starts from 0 when moving from left to right |
227 and greetings [-2] is the second element from the end , that is 'l'. |
238 Indexing starts from -1 when moving from right to left |
228 |
|
229 |
239 |
230 |
240 Addition gives a new sequence containing both sequences :: |
231 Addition gives a new sequence containing both sequences :: |
241 |
232 |
242 In[]: num+var |
233 In[]: num_list+var_list |
243 In[]: p="another string" |
234 In[]: a_string="another string" |
244 In[]: w+p |
235 In[]: greeting_string+a_string |
245 In[]: t2=(3,4,6,7) |
236 In[]: t2=(3,4,6,7) |
246 In[]: t+t2 |
237 In[]: num_tuple+t2 |
247 |
238 |
248 len function gives the length :: |
239 len function gives the length :: |
249 |
240 |
250 In[]: len(num) |
241 In[]: len(num_list) |
251 In[]: len(w) |
242 In[]: len(greeting_string) |
252 In[]: len(t) |
243 In[]: len(num_tuple) |
253 |
244 |
254 Prints the length the variable. |
245 Prints the length the variable. |
255 |
246 |
256 We can check the containership of an element using the 'in' keyword :: |
247 We can check the containership of an element using the 'in' keyword :: |
257 |
248 |
258 In[]: 3 in num |
249 In[]: 3 in num_list |
259 In[]: 'H' in w |
250 In[]: 'H' in greeting_string |
260 In[]: 2 in t |
251 In[]: 2 in num_tuple |
261 |
252 |
262 We see that it gives True and False accordingly. |
253 We see that it gives True and False accordingly. |
263 |
254 |
264 Find maximum using max function and minimum using min:: |
255 Find maximum using max function and minimum using min:: |
265 |
256 |
266 In[]: max(t) |
257 In[]: max(num_tuple) |
267 In[]: min(w) |
258 In[]: min(greeting_string) |
268 |
259 |
269 Get a sorted list and reversed list using sorted and reversed function :: |
260 Get a sorted list and reversed list using sorted and reversed function :: |
270 |
261 |
271 In[]: sorted(num) |
262 In[]: sorted(num_list) |
272 In[]: reversed(w) |
263 In[]: reversed(greeting_string) |
273 |
264 |
274 As a consequence of the order one we access a group of elements together. |
265 As a consequence of the order one we access a group of elements together. |
275 This is called slicing and striding. |
266 This is called slicing and striding. |
276 |
267 |
277 First Slicing |
268 First Slicing |
303 |
294 |
304 Striding is similar to slicing except that the step size here is not one. |
295 Striding is similar to slicing except that the step size here is not one. |
305 |
296 |
306 Lets see by example :: |
297 Lets see by example :: |
307 |
298 |
308 In[]: z=[1,2,3,4,5,6,7,8,9,10] |
299 new_num_list=[1,2,3,4,5,6,7,8,9,10] |
309 In[]: z[1:8:2] |
300 new_num_list[1:8:2] |
310 Out[]:[2, 4, 6, 8] |
301 [2, 4, 6, 8] |
311 |
302 |
312 The colon two added in the end signifies all the alternate elements. This is why we call this concept |
303 The colon two added in the end signifies all the alternate elements. This is why we call this concept |
313 striding because we move through the list with a particular stride or step. The step in this example |
304 striding because we move through the list with a particular stride or step. The step in this example |
314 being 2. |
305 being 2. |
315 |
306 |
316 We have talked about many similar features of lists, strings and tuples. But there are many important |
307 We have talked about many similar features of lists, strings and tuples. But there are many important |
317 features in lists that differ from strings and tuples. Lets see this by example.:: |
308 features in lists that differ from strings and tuples. Lets see this by example.:: |
318 |
309 |
319 In[]: z[1]=9 |
310 In[]: new_num_list[1]=9 |
320 In[]: w[1]='k' |
311 In[]: greeting_string[1]='k' |
321 |
312 |
322 {{{ slide to show the error }}} |
313 {{{ slide to show the error }}} |
323 |
314 |
324 .. #[Nishanth]: Use sensible variable names. At this point no one will remember |
315 |
325 that z is a list and w is tuple. |
|
326 for example you can use names like some_list, a_tuple etc. |
|
327 or you can also use l for list, t for tuple and s for string |
|
328 |
316 |
329 As you can see while the first command executes with out a problem there is an error on the second one. |
317 As you can see while the first command executes with out a problem there is an error on the second one. |
330 |
318 |
331 Now lets try :: |
319 Now lets try :: |
332 |
320 |
333 In[]: t[1]=5 |
321 In[]: new_tuple[1]=5 |
334 |
322 |
335 Its the same error. This is because strings and tuples share the property of being immutable. |
323 Its the same error. This is because strings and tuples share the property of being immutable. |
336 We cannot change the value at a particular index just by assigning a new value at that position. |
324 We cannot change the value at a particular index just by assigning a new value at that position. |
337 |
325 |
338 In case of strings we have special functions to appy relacement and other things while tuples cannot |
|
339 be changed at all. |
|
340 |
|
341 .. #[Nishanth]: Even in strings also the special functions do not modify the |
|
342 original string. A new string is created instead. These have |
|
343 been provided for string manipulation. |
|
344 hence I don't think you have to mention this. |
|
345 |
326 |
346 We have looked at different types but we need to convert one data type into another. Well lets one |
327 We have looked at different types but we need to convert one data type into another. Well lets one |
347 by one go through methods by which we can convert one data type to other: |
328 by one go through methods by which we can convert one data type to other: |
348 |
329 |
349 We can convert all the number data types to one another :: |
330 We can convert all the number data types to one another :: |
350 |
331 |
351 In[]: i=34 |
332 i=34 |
352 In[]: d=float(i) |
333 d=float(i) |
|
334 d |
353 |
335 |
354 Python has built in functions int, float and complex to convert one number type |
336 Python has built in functions int, float and complex to convert one number type |
355 data structure to another. |
337 data structure to another. |
356 |
338 |
357 In[]: dec=2.34 |
339 dec=2.34 |
358 In[]: dec_con=int(dec) |
340 dec_con=int(dec) |
359 |
341 dec_con |
360 .. #[Nishanth]: Show the value of the variables |
342 |
361 |
343 |
362 As you can see the decimal part of the number is simply stripped to get the integer.:: |
344 As you can see the decimal part of the number is simply stripped to get the integer.:: |
363 |
345 |
364 In[]: com=2.3+4.2j |
346 com=2.3+4.2j |
365 In[]: float(com) |
347 float(com) |
|
348 com |
366 |
349 |
367 In case of complex number to floating point only the real value of complex number is taken. |
350 In case of complex number to floating point only the real value of complex number is taken. |
368 |
351 |
369 Similarly we can convert list to tuple and tuple to list :: |
352 Similarly we can convert list to tuple and tuple to list :: |
370 |
353 |
371 In[]: lst=[3,4,5,6] |
354 lst=[3,4,5,6] |
372 In[]: tup=tuple(lst) |
355 tup=tuple(lst) |
373 In[]: tupl=(3,23,4,56) |
356 tupl=(3,23,4,56) |
374 In[]: lst=list(tuple) |
357 lst=list(tuple) |
375 |
358 |
376 However string to list and list to string is an interesting problem. |
359 However string to list and list to string is an interesting problem. |
377 Lets say we have a string :: |
360 Lets say we have a string :: |
378 |
361 |
379 In: somestring="Is there a way to split on these spaces." |
362 In: somestring="Is there a way to split on these spaces." |
380 In: somestring.split() |
363 In: somestring.split() |
381 |
364 |
382 .. #[Nishanth]: Did you try list(somestring). What does it give?? |
|
383 |
365 |
384 This produces a list with the string split at whitespace. |
366 This produces a list with the string split at whitespace. |
385 similarly we can split on some other character. |
367 similarly we can split on some other character. |
386 |
368 |
387 In: otherstring="Tim,Amy,Stewy,Boss" |
369 In: otherstring="Tim,Amy,Stewy,Boss" |