basic-data-type/script.rst
changeset 406 a534e9e79599
parent 364 91d16630c90f
child 412 bb45826efe74
equal deleted inserted replaced
403:9858ca9e3f93 406:a534e9e79599
    25 
    25 
    26 {{{ Show the slide containing the outline slide }}}
    26 {{{ Show the slide containing the outline slide }}}
    27 
    27 
    28 In this tutorial, we shall look at
    28 In this tutorial, we shall look at
    29 
    29 
    30  * Datatypes in Python
    30 * Datatypes in Python
    31     * Numbers
    31     * Numbers
    32     * Boolean
    32     * Boolean
    33     * Sequence
    33     * Sequence
    34 * Operators in Python
    34 * Operators in Python
    35   * Arithmetic Operators
    35   * Arithmetic Operators
    36   * Boolean Operators
    36   * Boolean Operators
    37 
    37 
    38 * Manipulating Sequence datatypes
    38 * Python Sequence Data types
       
    39   * list
       
    40   * string
       
    41   * tuple
    39 
    42 
    40 .. #[Puneeth: Use double colon only for code blocks.]
    43 .. #[Puneeth: Use double colon only for code blocks.]
    41 .. #[Puneeth: include more details in the outline.]
    44 .. #[Puneeth: include more details in the outline.]
    42 
    45 
    43 with a little hands-on on how they can be applied to the different data types.
    46 with a little hands-on on how they can be applied to the different data types.
    45 
    48 
    46 
    49 
    47 First we will explore python data structures in the domain of numbers.
    50 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.
    51 There are three built-in data types in python to represent numbers.
    49 
    52 
    50 {{{ A slide to make a memory note of this }}}
    53 {{{ A slide to make a memory note of the different datatypes }}}
    51 
    54 
    52 These are:
    55 These are:
    53 
    56 
    54   * int 
    57   * int 
    55   * float 
    58   * float 
    73 If we now see ::
    76 If we now see ::
    74      
    77      
    75    type(a)
    78    type(a)
    76    <type 'int'>
    79    <type 'int'>
    77 
    80 
    78 This means that a is a type of int. Being an int data structure in python
    81 This means that a is a type of int. Being an int data type in python
    79 means that there are various functions that this variable has to manipulate
    82 means that there are various functions that this variable has to manipulate
    80 it different ways. You can explore these by doing,
    83 in different ways. You can explore these by doing,
    81 
    84 
    82   a.<Tab>
    85   a.<Tab>
    83 
    86 
    84 .. #[Puneeth: Why are we suddenly talking of limits?
    87 .. #[Puneeth: Why are we suddenly talking of limits?
    85 .. Something like this would be better. 
    88 .. Something like this would be better. 
    86 .. int data-type can hold integers of any size. for example - ]
    89 .. int data-type can hold integers of any size. for example - ]
    87 
    90 
    88 *int* datatype can hold integers of any size lets see this by example.
    91 *int* datatype can hold integers of any size lets see this by an example.
    89 
    92 
    90   b = 99999999999999999999
    93   b = 99999999999999999999
    91   b
    94   b
    92 
    95 
    93 As you can see even when we put a value of 9 repeated 20 times python did
    96 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
    97 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,
    98 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 
    99 
   106 .. #[Puneeth: again, the clean-up that I talked of above. Decide if you are
   100 .. #[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
   101 .. 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
   102 .. 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.]
   103 .. kind of numbers they represent. I think you should choose the former.]
   140 We can get the absolute value using the function ::
   134 We can get the absolute value using the function ::
   141  
   135  
   142   abs(c)
   136   abs(c)
   143 
   137 
   144 
   138 
   145 
   139 Following is are exercises that you must do. 
   146 {{ Slide for memory aid }} 
   140 
       
   141 %% %% Find the absolute value of 3+4j 
       
   142 ::
       
   143 
       
   144         abs(3+4j)
       
   145 
       
   146 %% %% What is the datatype of number 999999999999999999? Is it
       
   147 not int?
       
   148 ::
       
   149 
       
   150         Long
       
   151         Big integers are internally stored in python
       
   152         as Long datatype.  
       
   153 
       
   154 Please, pause the video here. Do the exercises and then continue. 
       
   155 
       
   156 
       
   157 {{ Slide for showing Boolean datatypes }} 
   147 
   158 
   148 Python also has Boolean as a built-in type.
   159 Python also has Boolean as a built-in type.
   149 
   160 
   150 Try it out just type ::  
   161 Try it out just type ::  
   151 
   162 
   214   45*76
   225   45*76
   215 
   226 
   216 '/' for division ::
   227 '/' for division ::
   217     
   228     
   218   384/16
   229   384/16
   219 
   230   8/3 
   220  '%' for modulo operation ::
   231   8.0/3
       
   232 
       
   233 When we did 8/3 the first case results in am integer 
       
   234 output as both the operands are integer however when 
       
   235 8.0/3 is used the answer is float as one of the operands is
       
   236 float. 
       
   237 
       
   238 
       
   239 '%' for modulo operation ::
   221 
   240 
   222     87 % 6
   241     87 % 6
   223 
   242 
   224 and two stars for a exponent. ::
   243 and two stars for a exponent. ::
   225 
   244 
   243 
   262 
   244 is same as ::
   263 is same as ::
   245 
   264 
   246    a=a/23
   265    a=a/23
   247 
   266 
       
   267 Following is an (are) exercise(s) that you must do. 
       
   268 
       
   269 %% %% Using python find sqaure root of 3?
       
   270 ::
       
   271 
       
   272    3**0.5
       
   273 
       
   274 %% %% Is 3**1/2 and 3**0.5 same
       
   275 ::
       
   276     No,One gives an int answer and the other float        
       
   277 
       
   278 Please, pause the video here. Do the exercises and then continue.
       
   279 
       
   280 
   248 Lets now discuss sequence data types in Python. Sequence data types
   281 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 
   282 are those in which elements are kept in a sequential order and all the 
   250 elements accessed using index numbers.
   283 elements are accessed using index numbers.
   251 
   284 
   252 .. #[Puneeth: fix the last sentence - it sounds incomplete]
   285 .. #[Puneeth: fix the last sentence - it sounds incomplete]
   253 
   286 
   254 {{{ slide for memory aid }}}
   287 {{{ slide introducing sequence datatype }}}
   255 
   288 
   256 The sequence datatypes in Python are ::
   289 The sequence datatypes in Python are ::
   257 
   290 
   258  * list
   291  * list
   259  * string
   292  * string
   286   greeting_string="hello"
   319   greeting_string="hello"
   287 
   320 
   288 
   321 
   289 greeting_string is now a string variable with the value "hello"
   322 greeting_string is now a string variable with the value "hello"
   290 
   323 
   291 {{{ Memory Aid Slide }}}
   324 {{{ All the different types of strings shown }}}
   292 
   325 
   293 Python strings can actually be defined in three different ways ::
   326 Python strings can actually be defined in three different ways ::
   294 
   327 
   295    k='Single quote'
   328    k='Single quote'
   296    l="Let's see how to include a single quote"
   329    l="Let's see how to include a single quote"
   363 Find maximum using max function and minimum using min::
   396 Find maximum using max function and minimum using min::
   364 
   397 
   365    max(num_tuple)
   398    max(num_tuple)
   366    min(greeting_string)
   399    min(greeting_string)
   367 
   400 
   368 Get a sorted list and reversed list using sorted and reversed function ::
   401 Get a sorted list  ::
   369 
   402 
   370    sorted(num_list)
   403    sorted(num_list)
   371    reversed(greeting_string)
   404    
   372 
   405 
   373 As a consequence of there order we can access a group of elements of sequence,
   406 As a consequence of there order we can access a group of elements 
   374 together. This is called slicing and striding.
   407 in a sequence,together. This is called slicing and striding.
   375 
   408 
   376 .. #[Puneeth: Fix the sentence above. ]
   409 .. #[Puneeth: Fix the sentence above. ]
   377 
   410 
   378 First Slicing 
   411 First lets discuss Slicing, 
   379 
   412 
   380 Given a list ::
   413 Given a list ::
   381 
   414 
   382   j=[1,2,3,4,5,6]
   415   j=[1,2,3,4,5,6]
   383 
   416 
   504   ' '.join['Now','on','spaces']
   537   ' '.join['Now','on','spaces']
   505 
   538 
   506 Note that the list has to be a list of strings to apply join operation.
   539 Note that the list has to be a list of strings to apply join operation.
   507 
   540 
   508 With this we come to the end of this tutorial .
   541 With this we come to the end of this tutorial .
       
   542 
       
   543 Following is an (are) exercise(s) that you must do. 
       
   544 
       
   545 
       
   546 
       
   547 %% %% Check if 3 is an element of the list [1,7,5,3,4]. In case
       
   548 it is change it to 21.
       
   549 ::
       
   550         l=[1,7,5,3,4]
       
   551         3 in l
       
   552         l[3]=21
       
   553         l
       
   554 
       
   555 %% %% Convert the string "Elizabeth is queen of england" to 
       
   556 "Elizabeth is queen"
       
   557 ::
       
   558 
       
   559            s="Elizabeth is queen of england"
       
   560            stemp=s.split()
       
   561            ' '.join(stemp[:3])
       
   562    
       
   563 Please, pause the video here. Do the exercise(s) and then continue. 
       
   564 
       
   565 
   509 
   566 
   510 In this tutorial we have discussed 
   567 In this tutorial we have discussed 
   511 
   568 
   512 1. Number Datatypes , integer,float and complex 
   569 1. Number Datatypes , integer,float and complex 
   513 2. Boolean and datatype and operators
   570 2. Boolean and datatype and operators