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