basic-data-type/script.rst
author amit
Wed, 13 Oct 2010 17:10:38 +0530
changeset 320 223044cf254f
parent 201 basicdatatype.rst@6b1efb74d914
child 337 c65d0d9fc0c8
permissions -rw-r--r--
Adding new format st-scripts with questions etc for basic-data-type and getting started with lists
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
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
     4
.. Learn about Python Data Structures and Operators.(Remembering)
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
     5
.. Use them to do basic operations.(Applying)
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
     6
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
     7
.. Prerequisites
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
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
     
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    12
.. Author              : Amit Sethi
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    13
   Internal Reviewer   : 
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    14
   External Reviewer   :
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    15
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    16
Hello friends and welcome to the tutorial on Basic Data types and operators in Python.  
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    17
{{{ Show the slide containing title }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    18
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    19
{{{ Show the slide containing the outline slide }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    20
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    21
In this tutorial, we shall look at::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    22
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    23
 * Datatypes in Python
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    24
 * Operators in Python
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    25
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    26
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
    27
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
    28
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    29
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    30
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
    31
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
    32
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    33
{{{ A slide to make a memory note of this }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    34
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    35
These are:
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    36
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    37
  * Integers 
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    38
  * float and 
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    39
  * Complex 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    40
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    41
Lets first talk about integers. ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    42
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    43
   a = 13
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
    44
   a
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    45
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    46
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    47
Thats it, there we have our first integer variable a.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    48
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
    49
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    50
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    51
If we now see ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    52
     
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    53
   type(a)
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    54
   <type 'int'>
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    55
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    56
This means that a is a type of int. Being an int data structure 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    57
in python means that there are various functions that this variable
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    58
has to manipulate it different ways. You can explore these by doing,
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    59
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    60
  a.<Tab>
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    61
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
    62
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    63
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    64
Lets see the limits of this int.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    65
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    66
  b = 99999999999999999999
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    67
  b
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    68
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    69
As you can see even when we put a value of 9 repeated 20 times 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    70
python did not complain. However when you asked python to print
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    71
the number again it put a capital L at the end. Now if you check
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    72
the type of this variable b, ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    73
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    74
  type(b)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    75
  <type 'long'>
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    76
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    77
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    78
The reason for this is that python recognizes large integer numbers
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    79
by the data type long. However long type and integer type share there 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    80
functions and properties.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    81
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    82
Lets now try out the second type in list called float.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    83
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    84
Decimal numbers in python are recognized by the term float ::
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
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    89
If you notice the value of output of p isn't exactly equal to p. This
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    90
is because computer saves floating point values in a specific
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
    91
format. There is always an aproximationation. This is why we should
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    92
never rely on equality of floating point numbers in a program.
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
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    98
as simple as that so essentialy its just a combination of two floats the 
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
    99
imaginary part being defined by j notation instead of i. Complex numbers have a lot of functions specific to them.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   100
Lets check these ::
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
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   116
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   117
{{ Slide for memory aid }} 
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   118
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   119
Python also has Boolean as a built-in type.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   120
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   121
Try it out just type ::  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   122
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   123
  t = True
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   124
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   125
note that T in true is capitalized.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   126
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   127
You can apply different Boolean operations on t now for example ::
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
  f = not t 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   130
  f
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   131
  f or t
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   132
  f and t 
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   133
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   134
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   135
  
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   136
The results are explanotary in themselves.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   137
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   138
The usage of boolean brings us to an interesting question of precendence.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   139
What if you want to apply one operator before another. 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   140
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   141
Well you can use parenthesis for precedence.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   142
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   143
Lets write some piece of code to check this out.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   144
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   145
  In[]: a=False 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   146
  In[]: b=True 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   147
  In[]: c=True
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   148
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   149
To check how precedence changes with parenthesis. We will try two
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   150
expressions and their evaluation.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   151
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   152
one ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   153
 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   154
  (a and b) or c
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   155
 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   156
This expression gives the value True
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   157
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   158
where as the expression :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   159
  
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   160
  a and (b or c) 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   161
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   162
gives the value False.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   163
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   164
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   165
Lets now look at some operators available in Python to manipulate these data types.
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   166
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   167
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   168
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   169
Python uses % for modulo operation ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   170
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   171
    87 % 6
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   172
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
   173
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   174
    7**8
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   175
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   176
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   177
In case one wishes to use the current value of variable in which the result is stored in the expression one can do that by putting the operator before `equal to`. ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   178
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   179
   a=73
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   180
   a*=34
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   181
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   182
is same as ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   183
   
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   184
   a=a*34
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   185
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   186
and ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   187
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   188
    a/=23
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   189
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   190
is same as ::
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   191
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   192
   a=a/23
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   193
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   194
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   195
Lets now discuss sequence data stypes in python. Sequence 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   196
datatypes are those in which elements are kept in a sequential 
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   197
order. All the elements accessed using index. 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   198
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   199
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   200
{{{ slide to for memory aid }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   201
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   202
The sequence datatypes in python are ::
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   203
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   204
 * list
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   205
 * string
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   206
 * tuple
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   207
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   208
The list type is a container that holds a number of other 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   209
objects, in the given order.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   210
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   211
We create our first list by typing :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   212
  
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   213
  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
   214
  num_list
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   215
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   216
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   217
Items enclosed in square brackets separated by comma 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   218
constitutes a list.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   219
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   220
Lists can store data of any type in them. 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   221
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   222
We can have a list something like ::
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   223
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   224
 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
   225
 var_list
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   226
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   227
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   228
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   229
Now we will have a look at strings 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   230
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   231
type :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   232
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   233
 In[]: greeting_string="hello"
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   234
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   235
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   236
greeting_string is now a string variable with the value "hello"
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   237
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   238
{{{ Memory Aid Slide }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   239
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   240
Python strings can actually be defined in three different ways ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   241
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   242
  In[]: k='Single quote'
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   243
  In[]: l="Double quote contain's single quote"
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   244
  In[]: m='''"Contain's both"'''
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   245
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   246
Thus, single quotes are used as delimiters usually.
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   247
When a string contains a single quote, double quotes are used as delimiters.
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   248
When a string quote contains both single and double quotes, triple quotes are
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   249
used as delimiters.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   250
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   251
The last in the list of sequence data types is tuple.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   252
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   253
To create a tuple  we use normal brackets '('
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   254
unlike '[' for lists.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   255
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   256
  In[]: num_tuple = (1, 2, 3, 4, 5, 6, 7, 8)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   257
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   258
Because of their sequential property there are certain functions and 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   259
operations we can apply to all of them. 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   260
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   261
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   262
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   263
The first one is accessing.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   264
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   265
They can be accessed using index numbers ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   266
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   267
  In[]: num_list[2]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   268
  In[]: num_list[-1]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   269
  In[]: greeting_string[1]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   270
  In[]: greeting_string[3]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   271
  In[]: greeting_string[-2]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   272
  In[]: num_tuple[2]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   273
  In[]: num_tuple[-3]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   274
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   275
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   276
Indexing starts from 0 from left to right and from -1 when accessing
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   277
lists in reverse. Thus num_list[2] refers to the third element 3. 
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   278
and greetings [-2] is the second element from the end , that is 'l'. 
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   279
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   280
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   281
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   282
Addition gives a new sequence containing both sequences ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   283
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   284
     In[]: num_list+var_list
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   285
     In[]: a_string="another string"
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   286
     In[]: greeting_string+a_string
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   287
     In[]: t2=(3,4,6,7)
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   288
     In[]: num_tuple+t2
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   289
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   290
len function gives the length  ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   291
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   292
  In[]: len(num_list)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   293
  In[]: len(greeting_string)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   294
  In[]: len(num_tuple)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   295
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   296
Prints the length the variable.
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   297
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   298
We can check the containership of an element using the 'in' keyword ::
176
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
  In[]: 3 in num_list
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   301
  In[]: 'H' in greeting_string
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   302
  In[]: 2 in num_tuple
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   303
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   304
We see that it gives True and False accordingly.
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   305
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   306
Find maximum using max function and minimum using min:: 
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   307
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   308
  In[]: max(num_tuple)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   309
  In[]: min(greeting_string)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   310
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   311
Get a sorted list and reversed list using sorted and reversed function ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   312
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   313
  In[]: sorted(num_list)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   314
  In[]: reversed(greeting_string)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   315
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   316
As a consequence of the order one we access a group of elements together.
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   317
This is called slicing and striding.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   318
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   319
First Slicing 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   320
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   321
Given a list ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   322
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   323
  In[]:j=[1,2,3,4,5,6]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   324
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   325
Lets say we want elements starting from 2 and ending in 5.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   326
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   327
For this we can do ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   328
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   329
  In[]: j[1:4]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   330
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   331
The syntax for slicing is sequence variable name square bracket
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   332
first element index, colon, second element index.The last element however is notincluded in the resultant list::
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   333
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   334
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   335
  In[]: j[:4]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   336
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   337
If first element is left blank default is from beginning and if last
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   338
element is left blank it means till the end.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   339
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   340
 In[]: j[1:]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   341
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   342
 In[]: j[:]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   343
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   344
This effectively is the whole list.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   345
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   346
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
   347
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   348
Lets see by example ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   349
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   350
  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
   351
  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
   352
  [2, 4, 6, 8]
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   353
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   354
The colon two added in the end signifies all the alternate elements. This is why we call this concept
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   355
striding because we move through the list with a particular stride or step. The step in this example
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   356
being 2. 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   357
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   358
We have talked about many similar features of lists, strings and tuples. But there are many important
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   359
features in lists that differ from strings and tuples. Lets see this by example.::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   360
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   361
  In[]: new_num_list[1]=9
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   362
  In[]: greeting_string[1]='k'
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   363
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   364
{{{ slide to show the error }}}
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   365
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   366
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   367
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   368
As you can see while the first command executes with out a problem there is an error on the second one.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   369
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   370
Now lets try ::
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   371
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   372
  In[]: new_tuple[1]=5
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   373
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   374
Its the same error. This is because strings and tuples share the property of being immutable.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   375
We cannot change the value at a particular index just by assigning a new value at that position.
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   376
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   377
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   378
We have looked at different types but we need to convert one data type into another. Well lets one
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   379
by one go through methods by which we can convert one data type to other:
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   380
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   381
We can convert all the number data types to one another ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   382
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   383
  i=34
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   384
  d=float(i)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   385
  d  
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   386
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   387
Python has built in functions int, float and complex to convert one number type
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   388
data structure to another.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   389
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   390
  dec=2.34
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   391
  dec_con=int(dec)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   392
  dec_con
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   393
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   394
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   395
As you can see the decimal part of the number is simply stripped to get the integer.::
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   396
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   397
  com=2.3+4.2j
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   398
  float(com)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   399
  com
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   400
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   401
In case of complex number to floating point only the real value of complex number is taken.
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   402
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   403
Similarly we can convert list to tuple and tuple to list ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   404
  
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   405
  lst=[3,4,5,6]
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   406
  tup=tuple(lst)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   407
  tupl=(3,23,4,56)
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 186
diff changeset
   408
  lst=list(tuple)
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   409
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   410
However string to list and list to string is an interesting problem.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   411
Lets say we have a string ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   412
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   413
  In: somestring="Is there a way to split on these spaces."
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   414
  In: somestring.split()
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   415
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   416
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   417
This produces a list with the string split at whitespace.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   418
similarly we can split on some other character.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   419
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   420
  In: otherstring="Tim,Amy,Stewy,Boss"
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   421
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   422
How do we split on comma , simply pass it as argument ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   423
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   424
  In: otherstring.split(',')
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   425
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   426
join function does the opposite. Joins a list to make a string.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   427
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   428
  In[]:','.join['List','joined','on','commas']
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   429
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   430
Thus we get a list joined on commas. Similarly we can do spaces.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   431
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   432
  In[]:' '.join['Now','on','spaces']
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   433
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   434
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
   435
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   436
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
   437
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   438
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
   439
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   440
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
   441
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
   442
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
   443
4. Accesing sequence
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   444
5. Slicing sequences
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   445
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
   446
7. Immutability.
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   447
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   448
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   449
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 201
diff changeset
   450
186
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   451
.. #[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
   452
                string manipulations. Just say it requires some string 
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   453
                manipulations and leave it there.
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   454
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   455
.. #[Nishanth]: Where is the summary
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   456
                There are no exercises in the script
488f05b1fbcc reviewed basic_data_types_
Nishanth <nishanth@fossee.in>
parents: 176
diff changeset
   457
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   458
{{{ Show the "sponsored by FOSSEE" slide }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   459
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   460
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   461
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   462
Hope you have enjoyed and found it useful.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   463
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   464
Thank You.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   465
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   466
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   467