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