basic-data-type/script.rst
changeset 457 68813d8d80fb
parent 412 bb45826efe74
equal deleted inserted replaced
456:be96dc6c9743 457:68813d8d80fb
    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 
    18 
    19 .. #[Puneeth: Fill in pre-requisites.]
       
    20 
       
    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 }}}
    25 
    23 
    26 {{{ Show the slide containing the outline slide }}}
    24 {{{ Show the slide containing the outline slide }}}
    27 
    25 
    28 In this tutorial, we shall look at
    26 In this tutorial, we shall look at
    29 
    27 
    30  * Datatypes in Python
    28 * Datatypes in Python
    31     * Numbers
    29     * Numbers
    32     * Boolean
    30     * Boolean
    33     * Sequence
    31     * Sequence
    34 * Operators in Python
    32 * Operators in Python
    35   * Arithmetic Operators
    33   * Arithmetic Operators
    36   * Boolean Operators
    34   * Boolean Operators
    37 
    35 
    38 * Manipulating Sequence datatypes
    36 * Python Sequence Data types
    39 
    37   * list
    40 .. #[Puneeth: Use double colon only for code blocks.]
    38   * string
    41 .. #[Puneeth: include more details in the outline.]
    39   * tuple
    42 
       
    43 with a little hands-on on how they can be applied to the different data types.
       
    44 
       
    45 
       
    46 
    40 
    47 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.
    48 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.
    49 
    43 
    50 {{{ A slide to make a memory note of this }}}
    44 {{{ A slide to make a memory note of the different datatypes }}}
    51 
    45 
    52 These are:
    46 These are:
    53 
    47 
    54   * int 
    48   * int 
    55   * float 
    49   * float 
    56   * complex 
    50   * complex 
    57 
       
    58 .. #[Puneeth: Changed to  int, float and complex.]
       
    59 
       
    60 .. #[Puneeth: Loss of consistency. You talk of built-in data types, but
       
    61 .. then you were calling them integers, floats and complex. Clean up
       
    62 .. required.]
       
    63 
    51 
    64 Lets first talk about int. ::
    52 Lets first talk about int. ::
    65 
    53 
    66    a = 13
    54    a = 13
    67    a
    55    a
    73 If we now see ::
    61 If we now see ::
    74      
    62      
    75    type(a)
    63    type(a)
    76    <type 'int'>
    64    <type 'int'>
    77 
    65 
    78 This means that a is a type of int. Being an int data structure in python
    66 This means that a is a type of int. There are lot of functions associated
    79 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
    80 it different ways. You can explore these by doing,
    68 explored by doing, ::
    81 
    69 
    82   a.<Tab>
    70   a.<Tab>
    83 
    71 
    84 .. #[Puneeth: Why are we suddenly talking of limits?
    72 *int* datatype can hold integers of any size lets see this by an example.
    85 .. Something like this would be better. 
    73 ::
    86 .. int data-type can hold integers of any size. for example - ]
       
    87 
       
    88 *int* datatype can hold integers of any size lets see this by example.
       
    89 
    74 
    90   b = 99999999999999999999
    75   b = 99999999999999999999
    91   b
    76   b
    92 
    77 
    93 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
    94 not complain. However when you asked python to print the number again it
    79 not complain. This is because python's int data-type can hold integers of any
    95 put a capital L at the end. Now if you check the type of this variable b,
    80 size.
    96 ::
       
    97 
       
    98   type(b)
       
    99   <type 'long'>
       
   100 
       
   101 
       
   102 The reason for this is that python recognizes large integer numbers by the
       
   103 data type long. However long type and int type share there functions
       
   104 and properties.
       
   105 
       
   106 .. #[Puneeth: again, the clean-up that I talked of above. Decide if you are
       
   107 .. talking about the different type of numbers and the datatypes that are
       
   108 .. used to represent them or if you are talking of the data-types and what
       
   109 .. kind of numbers they represent. I think you should choose the former.]
       
   110 
    81 
   111 Let us now look at the float data-type. 
    82 Let us now look at the float data-type. 
   112 
    83 
   113 Decimal numbers in python are represented by the float data-type ::
    84 Decimal numbers in python are represented by the float data-type ::
   114 
    85 
   115   p = 3.141592
    86   p = 3.141592
   116   p
    87   p
   117 
    88 
   118 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``.
   119 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.
   120 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
   121 of floating point numbers in a program.
    92 equality of floating point numbers in a program.
   122 
    93 
   123 The last data type in the list is complex number ::
    94 The last data type in the list is complex number ::
   124 
    95 
   125   c = 3.2+4.6j
    96   c = 3.2+4.6j
   126 
    97 
   127 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
   128 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
   129 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 ::
   130 
   101 
   131   c.<Tab>
   102   c.<Tab>
   132 
   103 
   133 Lets try some of them ::
   104 Lets try some of them ::
   134 
   105 
   140 We can get the absolute value using the function ::
   111 We can get the absolute value using the function ::
   141  
   112  
   142   abs(c)
   113   abs(c)
   143 
   114 
   144 
   115 
   145 
   116 Following is are exercises that you must do. 
   146 {{ Slide for memory aid }} 
   117 
       
   118 %% %% Find the absolute value of 3+4j 
       
   119 ::
       
   120 
       
   121         abs(3+4j)
       
   122 
       
   123 %% %% What is the datatype of number 999999999999999999? Is it
       
   124 not int?
       
   125 ::
       
   126 
       
   127         Long
       
   128         Big integers are internally stored in python
       
   129         as Long datatype.  
       
   130 
       
   131 Please, pause the video here. Do the exercises and then continue. 
       
   132 
       
   133 
       
   134 {{ Slide for showing Boolean datatypes }} 
   147 
   135 
   148 Python also has Boolean as a built-in type.
   136 Python also has Boolean as a built-in type.
   149 
   137 
   150 Try it out just type ::  
   138 Try it out just type ::  
   151 
   139 
   161   f and t 
   149   f and t 
   162 
   150 
   163 
   151 
   164 The results are self explanatory.
   152 The results are self explanatory.
   165 
   153 
   166 .. #[Puneeth: Why does booleans bring us to precedence? I don't see the
       
   167 .. connection. Am I missing something?]
       
   168 
       
   169 
       
   170 What if you want to apply one operator before another.
   154 What if you want to apply one operator before another.
   171 
   155 
   172 Well you can use parenthesis for precedence.
   156 Well you can use parenthesis for precedence.
   173 
   157 
   174 Lets write some piece of code to check this out.::
   158 Lets write some piece of code to check this out.::
   175 
   159 
   176   a=False 
   160   a=False 
   177   b=True 
   161   b=True 
   178   c=True
   162   c=True
   179 
   163 
   180 
       
   181 .. #[Puneeth: Consistency. In[]: is not present at other places.]
       
   182 
   164 
   183 To check how precedence changes with parenthesis, we will try two
   165 To check how precedence changes with parenthesis, we will try two
   184 expressions and their evaluation.
   166 expressions and their evaluation.
   185 
   167 
   186 one ::
   168 one ::
   197 
   179 
   198 
   180 
   199 Let's now look at some operators available in Python to manipulate
   181 Let's now look at some operators available in Python to manipulate
   200 these data types.
   182 these data types.
   201 
   183 
   202 .. #[Puneeth: A mention of other operators would be good? Starting
       
   203 .. with % and ** is a bit weird.]
       
   204 
       
   205 Python uses '+' for addition ::
   184 Python uses '+' for addition ::
   206 
   185 
   207   23 + 74
   186   23 + 74
   208 
   187 
   209 '-' for subtraction ::
   188 '-' for subtraction ::
       
   189 
   210   23 - 56
   190   23 - 56
   211 
   191 
   212 '*' for multiplication ::
   192 '*' for multiplication ::
   213  
   193  
   214   45*76
   194   45*76
   215 
   195 
   216 '/' for division ::
   196 '/' for division ::
   217     
   197     
   218   384/16
   198   384/16
   219 
   199   8/3 
   220  '%' for modulo operation ::
   200   8.0/3
       
   201 
       
   202 When we did 8/3 the first case results in am integer 
       
   203 output as both the operands are integer however when 
       
   204 8.0/3 is used the answer is float as one of the operands is
       
   205 float. 
       
   206 
       
   207 
       
   208 '%' for modulo operation ::
   221 
   209 
   222     87 % 6
   210     87 % 6
   223 
   211 
   224 and two stars for a exponent. ::
   212 and two stars for a exponent. ::
   225 
   213 
   243 
   231 
   244 is same as ::
   232 is same as ::
   245 
   233 
   246    a=a/23
   234    a=a/23
   247 
   235 
       
   236 Following is are exercises that you must do. 
       
   237 
       
   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 
       
   244 ::
       
   245 
       
   246    3**0.5
       
   247 
       
   248 ::
       
   249     No,One gives an int answer and the other float        
       
   250 
       
   251 
   248 Lets now discuss sequence data types in Python. Sequence data types
   252 Lets now discuss sequence data types in Python. Sequence data types
   249 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 
   250 elements accessed using index numbers.
   254 elements are accessed using index numbers.
   251 
   255 
   252 .. #[Puneeth: fix the last sentence - it sounds incomplete]
   256 {{{ slide introducing sequence datatype }}}
   253 
       
   254 {{{ slide for memory aid }}}
       
   255 
   257 
   256 The sequence datatypes in Python are ::
   258 The sequence datatypes in Python are ::
   257 
   259 
   258  * list
   260  * list
   259  * string
   261  * string
   275 We can have a list something like ::
   277 We can have a list something like ::
   276 
   278 
   277  var_list = [1, 1.2, [1,2]]	
   279  var_list = [1, 1.2, [1,2]]	
   278  var_list
   280  var_list
   279 
   281 
   280 .. #[Puneeth: some continuity, when jumping to strings?]
       
   281 
       
   282 Lets look at another sequence data type, strings
   282 Lets look at another sequence data type, strings
   283 
   283 
   284 type :: 
   284 type :: 
   285 
   285 
   286   greeting_string="hello"
   286   greeting_string="hello"
   287 
   287 
   288 
   288 
   289 greeting_string is now a string variable with the value "hello"
   289 greeting_string is now a string variable with the value "hello"
   290 
   290 
   291 {{{ Memory Aid Slide }}}
   291 {{{ All the different types of strings shown }}}
   292 
   292 
   293 Python strings can actually be defined in three different ways ::
   293 Python strings can actually be defined in three different ways ::
   294 
   294 
   295    k='Single quote'
   295    k='Single quote'
   296    l="Let's see how to include a single quote"
   296    l="Let's see how to include a single quote"
   297    m='''"Let's see how to include both"'''
   297    m='''"Let's see how to include both"'''
   298 
   298 
   299 .. #[Puneeth: Contain's? That's not a word!]
       
   300 
       
   301 As you can see, single quotes are used as delimiters usually.
   299 As you can see, single quotes are used as delimiters usually.
   302 
       
   303 .. #[Puneeth: Thus?]
       
   304 
   300 
   305 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
   306 delimiters. When a string quote contains both single and double quotes,
   302 delimiters. When a string quote contains both single and double quotes,
   307 triple quotes are used as delimiters.
   303 triple quotes are used as delimiters.
   308 
   304 
   363 Find maximum using max function and minimum using min::
   359 Find maximum using max function and minimum using min::
   364 
   360 
   365    max(num_tuple)
   361    max(num_tuple)
   366    min(greeting_string)
   362    min(greeting_string)
   367 
   363 
   368 Get a sorted list and reversed list using sorted and reversed function ::
   364 Get a sorted list  ::
   369 
   365 
   370    sorted(num_list)
   366    sorted(num_list)
   371    reversed(greeting_string)
   367    
   372 
   368 
   373 As a consequence of there order we can access a group of elements of sequence,
   369 As a consequence of their order, we can access a group of elements in a
   374 together. This is called slicing and striding.
   370 sequence, together. This is called slicing and striding.
   375 
   371 
   376 .. #[Puneeth: Fix the sentence above. ]
   372 First lets discuss Slicing, 
   377 
       
   378 First Slicing 
       
   379 
   373 
   380 Given a list ::
   374 Given a list ::
   381 
   375 
   382   j=[1,2,3,4,5,6]
   376   j=[1,2,3,4,5,6]
   383 
   377 
   505 
   499 
   506 Note that the list has to be a list of strings to apply join operation.
   500 Note that the list has to be a list of strings to apply join operation.
   507 
   501 
   508 With this we come to the end of this tutorial .
   502 With this we come to the end of this tutorial .
   509 
   503 
   510 In this tutorial we have discussed 
   504 Following is an (are) exercise(s) that you must do. 
       
   505 
       
   506 
       
   507 
       
   508 %% %% Check if 3 is an element of the list [1,7,5,3,4]. In case
       
   509 it is change it to 21.
       
   510 ::
       
   511         l=[1,7,5,3,4]
       
   512         3 in l
       
   513         l[3]=21
       
   514         l
       
   515 
       
   516 %% %% Convert the string "Elizabeth is queen of england" to 
       
   517 "Elizabeth is queen"
       
   518 ::
       
   519 
       
   520            s="Elizabeth is queen of england"
       
   521            stemp=s.split()
       
   522            ' '.join(stemp[:3])
       
   523    
       
   524 Please, pause the video here. Do the exercise(s) and then continue. 
       
   525 
       
   526 
       
   527 This brings us to the end of the tutorial. In this tutorial we have
       
   528 discussed
   511 
   529 
   512 1. Number Datatypes , integer,float and complex 
   530 1. Number Datatypes , integer,float and complex 
   513 2. Boolean and datatype and operators
   531 2. Boolean and datatype and operators
   514 3. Sequence data types ,List,String and Tuple
   532 3. Sequence data types ,List,String and Tuple
   515 4. Accesing sequence
   533 4. Accesing sequence
   516 5. Slicing sequences
   534 5. Slicing sequences
   517 6. Finding length , sorting and reversing operations on sequences.
   535 6. Finding length , sorting and reversing operations on sequences.
   518 7. Immutability.
   536 7. Immutability.
   519 
   537 
   520 
       
   521 
       
   522 
       
   523 .. #[Nishanth]: string to list is fine. But list to string can be left for
       
   524                 string manipulations. Just say it requires some string 
       
   525                 manipulations and leave it there.
       
   526 
       
   527 .. #[Nishanth]: Where is the summary
       
   528                 There are no exercises in the script
       
   529 
       
   530 {{{ Show the "sponsored by FOSSEE" slide }}}
   538 {{{ Show the "sponsored by FOSSEE" slide }}}
   531 
   539 
   532 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
   533 
   541 
   534 Hope you have enjoyed and found it useful.
   542 Hope you have enjoyed and found it useful.