basic-data-type/script.rst
author Amit Sethi
Tue, 09 Nov 2010 01:28:00 +0530
changeset 406 a534e9e79599
parent 364 91d16630c90f
child 412 bb45826efe74
permissions -rw-r--r--
Completed basic data type based on review and improved on slides
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
     1
.. Objectives
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
     2
.. ----------
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
     3
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
     4
.. At the end of this tutorial, you should know --
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
     5
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
     6
.. 1. Learn about Python Data Structures and Operators.(Remembering)
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
     7
.. #.Use them to do basic operations.(Applying)
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
     8
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
     9
.. Prerequisites
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    10
.. -------------
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    11
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
    12
.. None
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    13
     
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    14
.. Author              : Amit Sethi
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    15
   Internal Reviewer   : 
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    16
   External Reviewer   :
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    17
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    18
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    19
.. #[Puneeth: Fill in pre-requisites.]
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    20
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    21
Hello friends and welcome to the tutorial on Basic Data types and operators
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    22
in Python.
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    23
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    24
{{{ Show the slide containing title }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    25
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    26
{{{ Show the slide containing the outline slide }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    27
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    28
In this tutorial, we shall look at
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    29
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    30
* Datatypes in Python
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    31
    * Numbers
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    32
    * Boolean
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    33
    * Sequence
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    34
* Operators in Python
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    35
  * Arithmetic Operators
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    36
  * Boolean Operators
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    37
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    38
* Python Sequence Data types
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    39
  * list
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    40
  * string
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    41
  * tuple
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    42
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    43
.. #[Puneeth: Use double colon only for code blocks.]
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    44
.. #[Puneeth: include more details in the outline.]
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    45
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    46
with a little hands-on on how they can be applied to the different data types.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    47
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
    48
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    49
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    50
First we will explore python data structures in the domain of numbers.
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
    51
There are three built-in data types in python to represent numbers.
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    52
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    53
{{{ A slide to make a memory note of the different datatypes }}}
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    54
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    55
These are:
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    56
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    57
  * int 
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    58
  * float 
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    59
  * complex 
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    60
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    61
.. #[Puneeth: Changed to  int, float and complex.]
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    62
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    63
.. #[Puneeth: Loss of consistency. You talk of built-in data types, but
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    64
.. then you were calling them integers, floats and complex. Clean up
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    65
.. required.]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    66
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    67
Lets first talk about int. ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    68
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    69
   a = 13
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
    70
   a
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    71
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    72
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    73
Now, we have our first int variable a.
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
    74
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    75
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    76
If we now see ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    77
     
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    78
   type(a)
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    79
   <type 'int'>
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    80
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    81
This means that a is a type of int. Being an int data type in python
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    82
means that there are various functions that this variable has to manipulate
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    83
in different ways. You can explore these by doing,
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    84
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    85
  a.<Tab>
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    86
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    87
.. #[Puneeth: Why are we suddenly talking of limits?
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    88
.. Something like this would be better. 
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    89
.. int data-type can hold integers of any size. for example - ]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    90
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    91
*int* datatype can hold integers of any size lets see this by an example.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    92
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    93
  b = 99999999999999999999
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    94
  b
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    95
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    96
As you can see even when we put a value of 9 repeated 20 times python did
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    97
not complain. This is because python's int data-type can hold integers of any
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    98
size.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    99
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   100
.. #[Puneeth: again, the clean-up that I talked of above. Decide if you are
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   101
.. talking about the different type of numbers and the datatypes that are
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   102
.. used to represent them or if you are talking of the data-types and what
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   103
.. kind of numbers they represent. I think you should choose the former.]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   104
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   105
Let us now look at the float data-type. 
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   106
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   107
Decimal numbers in python are represented by the float data-type ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   108
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   109
  p = 3.141592
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   110
  p
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   111
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   112
If you notice the value of output of p isn't exactly equal to p. This is
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   113
because computer saves floating point values in a specific format. There is
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   114
always an aproximationation. This is why we should never rely on equality
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   115
of floating point numbers in a program.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   116
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   117
The last data type in the list is complex number ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   118
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   119
  c = 3.2+4.6j
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   120
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   121
as simple as that so essentialy its just a combination of two floats the
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   122
imaginary part being defined by j notation instead of i. Complex numbers
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   123
have a lot of functions specific to them. Lets check these ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   124
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   125
  c.<Tab>
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   126
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   127
Lets try some of them ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   128
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   129
  c.real
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   130
  c.imag
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   131
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   132
c.real gives the real part of the number and c.imag the imaginary.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   133
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   134
We can get the absolute value using the function ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   135
 
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   136
  abs(c)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   137
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   138
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   139
Following is are exercises that you must do. 
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   140
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   141
%% %% Find the absolute value of 3+4j 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   142
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   143
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   144
        abs(3+4j)
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   145
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   146
%% %% What is the datatype of number 999999999999999999? Is it
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   147
not int?
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   148
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   149
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   150
        Long
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   151
        Big integers are internally stored in python
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   152
        as Long datatype.  
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   153
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   154
Please, pause the video here. Do the exercises and then continue. 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   155
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   156
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   157
{{ Slide for showing Boolean datatypes }} 
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   158
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   159
Python also has Boolean as a built-in type.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   160
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   161
Try it out just type ::  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   162
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   163
  t = True
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   164
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   165
note that T in true is capitalized.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   166
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   167
You can apply different Boolean operations on t now for example ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   168
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   169
  f = not t 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   170
  f
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   171
  f or t
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   172
  f and t 
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   173
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   174
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   175
The results are self explanatory.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   176
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   177
.. #[Puneeth: Why does booleans bring us to precedence? I don't see the
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   178
.. connection. Am I missing something?]
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   179
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   180
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   181
What if you want to apply one operator before another.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   182
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   183
Well you can use parenthesis for precedence.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   184
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   185
Lets write some piece of code to check this out.::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   186
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   187
  a=False 
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   188
  b=True 
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   189
  c=True
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   190
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   191
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   192
.. #[Puneeth: Consistency. In[]: is not present at other places.]
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   193
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   194
To check how precedence changes with parenthesis, we will try two
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   195
expressions and their evaluation.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   196
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   197
one ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   198
 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   199
  (a and b) or c
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   200
 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   201
This expression gives the value True
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   202
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   203
where as the expression :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   204
  
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   205
  a and (b or c) 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   206
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   207
gives the value False.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   208
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   209
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   210
Let's now look at some operators available in Python to manipulate
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   211
these data types.
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   212
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   213
.. #[Puneeth: A mention of other operators would be good? Starting
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   214
.. with % and ** is a bit weird.]
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   215
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   216
Python uses '+' for addition ::
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   217
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   218
  23 + 74
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   219
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   220
'-' for subtraction ::
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   221
  23 - 56
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   222
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   223
'*' for multiplication ::
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   224
 
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   225
  45*76
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   226
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   227
'/' for division ::
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   228
    
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   229
  384/16
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   230
  8/3 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   231
  8.0/3
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   232
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   233
When we did 8/3 the first case results in am integer 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   234
output as both the operands are integer however when 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   235
8.0/3 is used the answer is float as one of the operands is
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   236
float. 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   237
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   238
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   239
'%' for modulo operation ::
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   240
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   241
    87 % 6
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   242
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   243
and two stars for a exponent. ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   244
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   245
    7**8
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   246
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   247
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   248
In case one wishes to use the current value of variable in which the result
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   249
is stored in the expression one can do that by putting the operator before
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   250
`equal to`. ::
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   251
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   252
   a=73
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   253
   a*=34
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   254
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   255
is same as ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   256
   
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   257
   a=a*34
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   258
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   259
and ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   260
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   261
    a/=23
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   262
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   263
is same as ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   264
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   265
   a=a/23
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   266
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   267
Following is an (are) exercise(s) that you must do. 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   268
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   269
%% %% Using python find sqaure root of 3?
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   270
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   271
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   272
   3**0.5
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   273
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   274
%% %% Is 3**1/2 and 3**0.5 same
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   275
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   276
    No,One gives an int answer and the other float        
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   277
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   278
Please, pause the video here. Do the exercises and then continue.
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   279
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   280
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   281
Lets now discuss sequence data types in Python. Sequence data types
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   282
are those in which elements are kept in a sequential order and all the 
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   283
elements are accessed using index numbers.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   284
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   285
.. #[Puneeth: fix the last sentence - it sounds incomplete]
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   286
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   287
{{{ slide introducing sequence datatype }}}
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   288
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   289
The sequence datatypes in Python are ::
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   290
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   291
 * list
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   292
 * string
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   293
 * tuple
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   294
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   295
The list type is a container that holds a number of other objects, in the
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   296
given order.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   297
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   298
We create our first list by typing :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   299
  
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   300
  num_list = [1, 2, 3, 4]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   301
  num_list
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   302
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   303
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   304
Items enclosed in square brackets separated by comma constitutes a list.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   305
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   306
Lists can store data of any type in them.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   307
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   308
We can have a list something like ::
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   309
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   310
 var_list = [1, 1.2, [1,2]]	
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   311
 var_list
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   312
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   313
.. #[Puneeth: some continuity, when jumping to strings?]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   314
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   315
Lets look at another sequence data type, strings
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   316
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   317
type :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   318
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   319
  greeting_string="hello"
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   320
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   321
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   322
greeting_string is now a string variable with the value "hello"
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   323
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   324
{{{ All the different types of strings shown }}}
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   325
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   326
Python strings can actually be defined in three different ways ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   327
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   328
   k='Single quote'
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   329
   l="Let's see how to include a single quote"
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   330
   m='''"Let's see how to include both"'''
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   331
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   332
.. #[Puneeth: Contain's? That's not a word!]
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   333
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   334
As you can see, single quotes are used as delimiters usually.
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   335
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   336
.. #[Puneeth: Thus?]
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   338
When a string contains a single quote, double quotes are used as
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   339
delimiters. When a string quote contains both single and double quotes,
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   340
triple quotes are used as delimiters.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   341
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   342
The last in the list of sequence data types is tuple.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   343
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   344
To create a tuple we use normal brackets '(' unlike '[' for lists.::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   345
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   346
   num_tuple = (1, 2, 3, 4, 5, 6, 7, 8)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   347
  
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   348
Because of their sequential property there are certain functions and
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   349
operations we can apply to all of them.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   350
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   351
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   352
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   353
The first one is accessing.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   354
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   355
They can be accessed using index numbers ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   356
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   357
   num_list[2]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   358
   num_list[-1]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   359
   greeting_string[1]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   360
   greeting_string[3]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   361
   greeting_string[-2]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   362
   num_tuple[2]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   363
   num_tuple[-3]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   364
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   365
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   366
Indexing starts from 0 from left to right and from -1 when accessing lists
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   367
in reverse. Thus num_list[2] refers to the third element 3. and greetings
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   368
[-2] is the second element from the end , that is 'l'.
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   369
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   370
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   371
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   372
Addition gives a new sequence containing both sequences ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   373
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   374
      num_list+var_list
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   375
      a_string="another string"
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   376
      greeting_string+a_string
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   377
      t2=(3,4,6,7)
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   378
      num_tuple+t2
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   379
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   380
len function gives the length ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   381
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   382
   len(num_list)
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   383
   len(greeting_string)
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   384
   len(num_tuple)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   385
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   386
Prints the length the variable.
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   387
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   388
We can check the containership of an element using the 'in' keyword ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   389
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   390
   3 in num_list
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   391
   'H' in greeting_string
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   392
   2 in num_tuple
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   393
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   394
We see that it gives True and False accordingly.
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   395
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   396
Find maximum using max function and minimum using min::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   397
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   398
   max(num_tuple)
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   399
   min(greeting_string)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   400
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   401
Get a sorted list  ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   402
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   403
   sorted(num_list)
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   404
   
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   405
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   406
As a consequence of there order we can access a group of elements 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   407
in a sequence,together. This is called slicing and striding.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   408
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   409
.. #[Puneeth: Fix the sentence above. ]
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   410
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   411
First lets discuss Slicing, 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   412
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   413
Given a list ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   414
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   415
  j=[1,2,3,4,5,6]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   416
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   417
Lets say we want elements starting from 2 and ending in 5.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   418
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   419
For this we can do ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   420
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   421
   j[1:4]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   422
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   423
The syntax for slicing is, sequence variable name square bracket first
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   424
element index, colon, second element index. The last element however is not
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   425
included in the resultant list::
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   426
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   427
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   428
   j[:4]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   429
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   430
If first element is left blank default is from beginning and if last
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   431
element is left blank it means till the end.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   432
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   433
::
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   434
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   435
  j[1:]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   436
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   437
  j[:]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   438
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   439
This effectively is the whole list.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   440
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   441
Striding is similar to slicing except that the step size here is not one.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   442
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   443
Lets see by example ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   444
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   445
  new_num_list=[1,2,3,4,5,6,7,8,9,10]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   446
  new_num_list[1:8:2]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   447
  [2, 4, 6, 8]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   448
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   449
The colon two added in the end signifies all the alternate elements. This
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   450
is why we call this concept striding because we move through the list with
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   451
a particular stride or step. The step in this example being 2.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   452
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   453
We have talked about many similar features of lists, strings and tuples.
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   454
But there are many important features in lists that differ from strings and
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   455
tuples. Lets see this by example.::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   456
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   457
   new_num_list[1]=9
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   458
   greeting_string[1]='k'
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   459
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   460
{{{ slide to show the error }}}
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   461
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   462
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   463
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   464
As you can see while the first command executes with out a problem there is
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   465
an error on the second one.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   466
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   467
Now lets try ::
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   468
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   469
   new_tuple[1]=5
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   470
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   471
Its the same error. This is because strings and tuples share the property
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   472
of being immutable. We cannot change the value at a particular index just
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   473
by assigning a new value at that position.
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   474
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   475
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   476
We have looked at different types but we need to convert one data type into
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   477
another. Well lets one by one go through methods by which we can convert
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   478
one data type to other:
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   479
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   480
We can convert all the number data types to one another ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   481
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   482
  i=34
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   483
  d=float(i)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   484
  d  
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   485
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   486
Python has built in functions int, float and complex to convert one number
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   487
type data structure to another.
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   488
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   489
::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   490
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   491
  dec=2.34
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   492
  dec_con=int(dec)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   493
  dec_con
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   494
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   495
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   496
As you can see the decimal part of the number is simply stripped to get the
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   497
integer.::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   498
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   499
  com=2.3+4.2j
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   500
  float(com)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   501
  com
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   502
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   503
In case of complex number to floating point only the real value of complex
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   504
number is taken.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   505
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   506
Similarly we can convert list to tuple and tuple to list ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   507
  
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   508
  lst=[3,4,5,6]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   509
  tup=tuple(lst)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   510
  tupl=(3,23,4,56)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   511
  lst=list(tuple)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   512
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   513
However converting a string to a list and a list to a string is an
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   514
interesting problem. Let's say we have a string ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   515
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   516
  In: somestring="Is there a way to split on these spaces."
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   517
  In: somestring.split()
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   518
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   519
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   520
This produces a list with the string split at whitespace. Similarly we can
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   521
split on some other character.
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   522
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   523
::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   524
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   525
  In: otherstring="Tim,Amy,Stewy,Boss"
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   526
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   527
How do we split on comma , simply pass it as argument ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   528
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   529
  In: otherstring.split(',')
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   530
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   531
join function does the opposite. Joins a list to make a string.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   532
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   533
  ','.join['List','joined','on','commas']
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   534
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   535
Thus we get a list joined on commas. Similarly we can do spaces.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   536
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   537
  ' '.join['Now','on','spaces']
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   538
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   539
Note that the list has to be a list of strings to apply join operation.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   540
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   541
With this we come to the end of this tutorial .
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   542
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   543
Following is an (are) exercise(s) that you must do. 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   544
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   545
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   546
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   547
%% %% Check if 3 is an element of the list [1,7,5,3,4]. In case
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   548
it is change it to 21.
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   549
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   550
        l=[1,7,5,3,4]
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   551
        3 in l
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   552
        l[3]=21
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   553
        l
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   554
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   555
%% %% Convert the string "Elizabeth is queen of england" to 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   556
"Elizabeth is queen"
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   557
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   558
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   559
           s="Elizabeth is queen of england"
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   560
           stemp=s.split()
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   561
           ' '.join(stemp[:3])
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   562
   
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   563
Please, pause the video here. Do the exercise(s) and then continue. 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   564
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   565
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   566
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   567
In this tutorial we have discussed 
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   568
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   569
1. Number Datatypes , integer,float and complex 
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   570
2. Boolean and datatype and operators
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   571
3. Sequence data types ,List,String and Tuple
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   572
4. Accesing sequence
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   573
5. Slicing sequences
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   574
6. Finding length , sorting and reversing operations on sequences.
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   575
7. Immutability.
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   576
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   577
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   578
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   579
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   580
.. #[Nishanth]: string to list is fine. But list to string can be left for
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   581
                string manipulations. Just say it requires some string 
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   582
                manipulations and leave it there.
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   583
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   584
.. #[Nishanth]: Where is the summary
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   585
                There are no exercises in the script
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   586
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   587
{{{ Show the "sponsored by FOSSEE" slide }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   588
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   589
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   590
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   591
Hope you have enjoyed and found it useful.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   592
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   593
Thank You.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   594
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   595
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   596
.. 
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   597
   Local Variables:
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   598
   mode: rst
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   599
   indent-tabs-mode: nil
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   600
   sentence-end-double-space: nil
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   601
   fill-column: 75
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   602
   End: