basic-data-type/script.rst
author anand
Mon, 15 Nov 2010 15:01:21 +0530
changeset 504 340fafcb92f6
parent 412 bb45826efe74
permissions -rw-r--r--
checklist not OK; one more long answer question has to be added.
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
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
    20
in Python.
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    21
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    22
{{{ Show the slide containing title }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    23
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    24
{{{ Show the slide containing the outline slide }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    25
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    26
In this tutorial, we shall look at
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    27
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    28
* Datatypes in Python
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    29
    * Numbers
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    30
    * Boolean
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    31
    * Sequence
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    32
* Operators in Python
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    33
  * Arithmetic Operators
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    34
  * Boolean Operators
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    35
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    36
* Python Sequence Data types
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    37
  * list
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    38
  * string
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    39
  * tuple
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    40
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    41
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
    42
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
    43
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    44
{{{ A slide to make a memory note of the different datatypes }}}
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    45
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    46
These are:
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    47
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    48
  * int 
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    49
  * float 
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    50
  * complex 
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    51
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    52
Lets first talk about int. ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    53
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    54
   a = 13
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
    55
   a
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    56
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    57
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
    58
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
    59
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    60
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    61
If we now see ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    62
     
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    63
   type(a)
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    64
   <type 'int'>
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    65
412
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
    66
This means that a is a type of int. There are lot of functions associated
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
    67
with the int datatype, to manipulate it in different ways. These can be
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
    68
explored by doing, ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    69
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    70
  a.<Tab>
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    71
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
    72
*int* datatype can hold integers of any size lets see this by an example.
412
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
    73
::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    74
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    75
  b = 99999999999999999999
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    76
  b
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    77
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    78
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
    79
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
    80
size.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    81
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    82
Let us now look at the float data-type. 
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    83
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    84
Decimal numbers in python are represented by the float data-type ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    85
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    86
  p = 3.141592
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    87
  p
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    88
412
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
    89
If you notice the value of output of ``p`` isn't exactly equal to ``p``.
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
    90
This is because computer saves floating point values in a specific format.
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
    91
There is always an approximation. This is why we should never rely on
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
    92
equality of floating point numbers in a program.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    93
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
    94
The last data type in the list is complex number ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    95
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    96
  c = 3.2+4.6j
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    97
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
    98
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
    99
imaginary part being defined by j notation instead of i. Complex numbers
412
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   100
have a lot of functions specific to them. Let us look at these ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   101
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   102
  c.<Tab>
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   103
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   104
Lets try some of them ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   105
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   106
  c.real
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   107
  c.imag
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   108
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   109
c.real gives the real part of the number and c.imag the imaginary.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   110
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   111
We can get the absolute value using the function ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   112
 
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   113
  abs(c)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   114
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   115
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   116
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
   117
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   118
%% %% 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
   119
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   120
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   121
        abs(3+4j)
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   122
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   123
%% %% 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
   124
not int?
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   125
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   126
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   127
        Long
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   128
        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
   129
        as Long datatype.  
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   130
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   131
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
   132
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   133
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   134
{{ 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
   135
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   136
Python also has Boolean as a built-in type.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   137
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   138
Try it out just type ::  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   139
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   140
  t = True
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   141
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   142
note that T in true is capitalized.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   143
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   144
You can apply different Boolean operations on t now for example ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   145
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   146
  f = not t 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   147
  f
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   148
  f or t
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   149
  f and t 
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   150
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   151
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   152
The results are self explanatory.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   153
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   154
What if you want to apply one operator before another.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   155
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   156
Well you can use parenthesis for precedence.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   157
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   158
Lets write some piece of code to check this out.::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   159
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   160
  a=False 
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   161
  b=True 
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   162
  c=True
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   163
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   164
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   165
To check how precedence changes with parenthesis, we will try two
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   166
expressions and their evaluation.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   167
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   168
one ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   169
 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   170
  (a and b) or c
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   171
 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   172
This expression gives the value True
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   173
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   174
where as the expression :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   175
  
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   176
  a and (b or c) 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   177
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   178
gives the value False.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   179
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   180
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   181
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
   182
these data types.
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   183
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   184
Python uses '+' for addition ::
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   185
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   186
  23 + 74
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   187
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   188
'-' for subtraction ::
412
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   189
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   190
  23 - 56
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   191
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   192
'*' for multiplication ::
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   193
 
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   194
  45*76
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   195
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   196
'/' for division ::
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   197
    
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   198
  384/16
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   199
  8/3 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   200
  8.0/3
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   201
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   202
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
   203
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
   204
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
   205
float. 
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   206
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   207
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   208
'%' for modulo operation ::
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   209
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   210
    87 % 6
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   211
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   212
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
   213
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   214
    7**8
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   215
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   216
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   217
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
   218
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
   219
`equal to`. ::
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   220
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   221
   a=73
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   222
   a*=34
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   223
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   224
is same as ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   225
   
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   226
   a=a*34
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   227
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   228
and ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   229
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   230
    a/=23
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   231
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   232
is same as ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   233
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   234
   a=a/23
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   235
412
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   236
Following is are exercises that you must do. 
406
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
%% %% Using python find sqaure root of 3?
412
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   239
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   240
%% %% Is 3**1/2 and 3**0.5 same
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   241
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   242
Please, pause the video here. Do the exercises and then continue.
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   243
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   244
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   245
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   246
   3**0.5
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   247
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   248
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   249
    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
   250
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   251
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   252
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
   253
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
   254
elements are accessed using index numbers.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   255
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   256
{{{ slide introducing sequence datatype }}}
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   257
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   258
The sequence datatypes in Python are ::
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   259
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   260
 * list
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   261
 * string
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   262
 * tuple
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   263
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   264
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
   265
given order.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   266
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   267
We create our first list by typing :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   268
  
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   269
  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
   270
  num_list
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   271
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   272
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   273
Items enclosed in square brackets separated by comma constitutes a list.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   274
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   275
Lists can store data of any type in them.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   276
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   277
We can have a list something like ::
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   278
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   279
 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
   280
 var_list
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   281
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   282
Lets look at another sequence data type, strings
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   283
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   284
type :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   285
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   286
  greeting_string="hello"
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   287
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   288
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   289
greeting_string is now a string variable with the value "hello"
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   290
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   291
{{{ All the different types of strings shown }}}
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   292
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   293
Python strings can actually be defined in three different ways ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   294
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   295
   k='Single quote'
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   296
   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
   297
   m='''"Let's see how to include both"'''
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   298
364
91d16630c90f Made changes to basic data type script based on review
amit
parents: 362
diff changeset
   299
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
   300
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   301
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
   302
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
   303
triple quotes are used as delimiters.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   304
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   305
The last in the list of sequence data types is tuple.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   306
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   307
To create a tuple we use normal brackets '(' unlike '[' for lists.::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   308
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   309
   num_tuple = (1, 2, 3, 4, 5, 6, 7, 8)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   310
  
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   311
Because of their sequential property there are certain functions and
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   312
operations we can apply to all of them.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   313
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   314
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   315
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   316
The first one is accessing.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   317
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   318
They can be accessed using index numbers ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   319
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   320
   num_list[2]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   321
   num_list[-1]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   322
   greeting_string[1]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   323
   greeting_string[3]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   324
   greeting_string[-2]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   325
   num_tuple[2]
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   326
   num_tuple[-3]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   327
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   328
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   329
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
   330
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
   331
[-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
   332
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   333
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   334
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   335
Addition gives a new sequence containing both sequences ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   336
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   337
      num_list+var_list
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   338
      a_string="another string"
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   339
      greeting_string+a_string
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   340
      t2=(3,4,6,7)
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   341
      num_tuple+t2
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   342
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   343
len function gives the length ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   344
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   345
   len(num_list)
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   346
   len(greeting_string)
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   347
   len(num_tuple)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   348
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   349
Prints the length the variable.
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   350
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   351
We can check the containership of an element using the 'in' keyword ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   352
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   353
   3 in num_list
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   354
   'H' in greeting_string
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   355
   2 in num_tuple
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   356
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   357
We see that it gives True and False accordingly.
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   358
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   359
Find maximum using max function and minimum using min::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   360
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   361
   max(num_tuple)
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   362
   min(greeting_string)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   363
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   364
Get a sorted list  ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   365
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   366
   sorted(num_list)
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   367
   
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   368
412
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   369
As a consequence of their order, we can access a group of elements in a
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   370
sequence, together. This is called slicing and striding.
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   371
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   372
First lets discuss Slicing, 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   373
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   374
Given a list ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   375
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   376
  j=[1,2,3,4,5,6]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   377
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   378
Lets say we want elements starting from 2 and ending in 5.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   379
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   380
For this we can do ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   381
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   382
   j[1:4]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   383
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   384
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
   385
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
   386
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
   387
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   388
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   389
   j[:4]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   390
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   391
If first element is left blank default is from beginning and if last
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   392
element is left blank it means till the end.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   393
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   394
::
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   395
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   396
  j[1:]
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
  j[:]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   399
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   400
This effectively is the whole list.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   401
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   402
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
   403
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   404
Lets see by example ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   405
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   406
  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
   407
  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
   408
  [2, 4, 6, 8]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   409
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   410
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
   411
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
   412
a particular stride or step. The step in this example being 2.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   413
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   414
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
   415
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
   416
tuples. Lets see this by example.::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   417
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   418
   new_num_list[1]=9
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   419
   greeting_string[1]='k'
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   420
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   421
{{{ slide to show the error }}}
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   422
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   423
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   424
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   425
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
   426
an error on the second one.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   427
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   428
Now lets try ::
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   429
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   430
   new_tuple[1]=5
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   431
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   432
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
   433
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
   434
by assigning a new value at that position.
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   435
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   436
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   437
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
   438
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
   439
one data type to other:
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   440
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   441
We can convert all the number data types to one another ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   442
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   443
  i=34
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   444
  d=float(i)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   445
  d  
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   446
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   447
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
   448
type data structure to another.
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   449
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   450
::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   451
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   452
  dec=2.34
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   453
  dec_con=int(dec)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   454
  dec_con
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   455
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   456
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   457
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
   458
integer.::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   459
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   460
  com=2.3+4.2j
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   461
  float(com)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   462
  com
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
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
   465
number is taken.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   466
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   467
Similarly we can convert list to tuple and tuple to list ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   468
  
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   469
  lst=[3,4,5,6]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   470
  tup=tuple(lst)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   471
  tupl=(3,23,4,56)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   472
  lst=list(tuple)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   473
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   474
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
   475
interesting problem. Let's say we have a string ::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   476
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   477
  In: somestring="Is there a way to split on these spaces."
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   478
  In: somestring.split()
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   479
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   480
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   481
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
   482
split on some other character.
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   483
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   484
::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   485
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   486
  In: otherstring="Tim,Amy,Stewy,Boss"
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   487
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   488
How do we split on comma , simply pass it as argument ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   489
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   490
  In: otherstring.split(',')
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   491
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   492
join function does the opposite. Joins a list to make a string.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   493
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   494
  ','.join['List','joined','on','commas']
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   495
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   496
Thus we get a list joined on commas. Similarly we can do spaces.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   497
362
a77a27916f81 Added Objectives and other metadata
amit
parents: 337
diff changeset
   498
  ' '.join['Now','on','spaces']
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   499
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   500
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
   501
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   502
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
   503
406
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   504
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
   505
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   506
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   507
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   508
%% %% 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
   509
it is change it to 21.
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   510
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   511
        l=[1,7,5,3,4]
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   512
        3 in l
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   513
        l[3]=21
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   514
        l
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   515
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   516
%% %% 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
   517
"Elizabeth is queen"
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   518
::
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   519
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   520
           s="Elizabeth is queen of england"
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   521
           stemp=s.split()
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   522
           ' '.join(stemp[:3])
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   523
   
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   524
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
   525
a534e9e79599 Completed basic data type based on review and improved on slides
Amit Sethi
parents: 364
diff changeset
   526
412
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   527
This brings us to the end of the tutorial. In this tutorial we have
bb45826efe74 Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents: 406
diff changeset
   528
discussed
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   529
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   530
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
   531
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
   532
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
   533
4. Accesing sequence
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   534
5. Slicing sequences
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   535
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
   536
7. Immutability.
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   537
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   538
{{{ Show the "sponsored by FOSSEE" slide }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   539
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   540
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   541
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   542
Hope you have enjoyed and found it useful.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   543
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   544
Thank You.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   545
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   546
337
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   547
.. 
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   548
   Local Variables:
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   549
   mode: rst
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   550
   indent-tabs-mode: nil
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   551
   sentence-end-double-space: nil
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   552
   fill-column: 75
c65d0d9fc0c8 Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 320
diff changeset
   553
   End: