basicdatatype.rst
author amit
Wed, 22 Sep 2010 10:53:51 +0530
changeset 176 c1242f073db3
child 186 488f05b1fbcc
permissions -rw-r--r--
Initial commit basic data types
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
     1
Hello friends and welcome to the tutorial on Basic Data types and
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
     2
operators in Python.  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
     3
{{{ Show the slide containing title }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
     4
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
     5
{{{ Show the slide containing the outline slide }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
     6
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
     7
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
     8
In this tutorial, we shall look at 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
     9
 * Various Datatypes in Python
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    10
 * Operators with a little hands-on on how they can be applied to 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    11
   the different data types.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    12
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    13
Since this a hands on session, you will require python installed in your 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    14
computer. 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    15
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    16
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    17
First we will explore python data structures in the domain of numbers.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    18
There are three built-in data structures in python to represent numbers.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    19
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    20
{{{ A slide to make a memory note of this }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    21
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    22
These are:
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    23
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    24
  * Integers 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    25
  * Complex and 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    26
  * Boolean 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    27
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    28
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    29
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    30
Lets first talk about integers. ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    31
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    32
   In[]: a=13
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    33
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    34
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    35
Thats it , there we have our first integer variable a.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    36
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    37
If we now see ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    38
     
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    39
   In[]: type(a)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    40
   Out[]: <type 'int'>
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    41
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    42
   This means  that a is a type of int. Being an int data structure 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    43
in python means that there are various functions that this variable
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    44
has to manipulate it different ways. You can explore these by doing,
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    45
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    46
  In[]: a.<Tab>
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    47
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    48
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    49
Lets see the limits of this int.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    50
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    51
  In[]: b=99999999999999999999
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    52
  In[]: b
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    53
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    54
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
    55
python did not complain. However when you asked python to print
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    56
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
    57
the type of this variable b, ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    58
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    59
  In[]: type(b)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    60
  <type 'long'>
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    61
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    62
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    63
The reason for this is that python recognizes large integer numbers
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    64
by a data type long. However long type and integer type share there 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    65
functions and properties.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    66
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    67
Lets now try out the second type in list called float.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    68
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    69
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    70
Decimal numbers in python are recognized by the term float ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    71
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    72
  In[]: p=3.141592
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    73
  In[]: p
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    74
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    75
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
    76
is because computer saves floating point values in a specific
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    77
format. This is always an aproximationation. This is why we should
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    78
never rely on equality of floating point numbers in a program.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    79
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    80
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    81
The last data structure in the list is complex number ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    82
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    83
  In: c=3.2+4.6j
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    84
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    85
as simple as that so essentialy its just a combination of two floats the 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    86
imaginary part being define by j notation usually used in electrical 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    87
engineering. Complex numbers have a lot of functions specific to them.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    88
Lets check these ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    89
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    90
  In[]: c.<Tab>
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    91
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    92
Lets try some of them ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    93
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    94
  In[]: c.real
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    95
  In[]: c.imag
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    96
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    97
c.real gives the real part of the number and c.imag the imaginary.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    98
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
    99
We can get the absolute value using the function ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   100
 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   101
  In[]: abs(c)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   102
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   103
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   104
Python also has Boolean as a built-in type.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   105
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   106
Try it out just type ::  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   107
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   108
  In[]: t=True
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   109
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   110
note that T in true is capitalized.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   111
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   112
You can apply different Boolean operations on t now for example ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   113
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   114
  In[]: f=not t 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   115
  In[]: f
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   116
  In[]: f or t
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   117
  In[]: f and t 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   118
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   119
The results explanotary in themselves.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   120
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   121
The usage of boolean brings us to an interesting question of precendence.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   122
What if you want to apply one operator before another. 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   123
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   124
Well you can use parenthesis for precedence ,
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   125
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   126
Lets write some piece of code to check this out.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   127
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   128
  In[]: a=False 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   129
  In[]: b=True 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   130
  In[]: c=True
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   131
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   132
To check how precedence changes with parenthesis. We will try two
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   133
expressions and their evaluation.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   134
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   135
one ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   136
 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   137
  In[]: (a and b) or c
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   138
 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   139
This expression gives the value True
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   140
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   141
where as the expression :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   142
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   143
  In[]: a and (b or c) 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   144
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   145
gives the value False.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   146
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   147
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   148
Lets now discuss sequence data structures in python. Sequence 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   149
datatypes are those in which elements are kept in a sequential 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   150
order.All the elements accessed using index. 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   151
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   152
{{{ slide to for memory aid }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   153
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   154
The sequence datatypes in python are ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   155
 * list
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   156
 * string
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   157
 * tuple
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   158
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   159
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   160
The list type is a container that holds a number of other 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   161
objects, in the given order.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   162
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   163
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   164
We create our first list by typing :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   165
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   166
  In[]: num = [1, 2, 3, 4]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   167
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   168
Items enclosed in square brackets separated by comma 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   169
constitutes a list.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   170
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   171
Lists  can store data of any type in them. 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   172
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   173
We can have a list something like ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   174
 In[]: var = [1, 1.2, [1,2]]	
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   175
print var
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   176
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   177
Now we will have a look at strings 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   178
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   179
type :: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   180
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   181
 In[]: w="hello"
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   182
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   183
w is now a string variable with the value "hello"
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   184
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   185
{{{ Memory Aid Slide }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   186
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   187
Python strings can actually be defined in three different ways ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   188
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   189
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   190
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   191
  In[]: k='Single quote'
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   192
  In[]: l="Double quote contain's single quote"
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   193
  In[]: m='''"Contain's both"'''
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   194
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   195
Thus while single quote string may not contain another single quote
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   196
double quote can do that. While triple quoted strings can contain both.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   197
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   198
The last in the list of sequence data types is tuple.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   199
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   200
To create a tuple  we use normal brackets '('
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   201
unlike '[' for lists.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   202
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   203
  In[]: t = (1, 2, 3, 4, 5, 6, 7, 8)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   204
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   205
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   206
Because of their sequential property there are certain functions and 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   207
operations we can apply to all of them. 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   208
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   209
{{{ Slide for memory aid }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   210
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   211
The first one is accessing.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   212
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   213
They can be accessed using index numbers ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   214
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   215
  In[]: num[2]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   216
  In[]: num[-1]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   217
  In[]: w[1]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   218
  In[]: w[3]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   219
  In[]: w[-2]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   220
  In[]: t[2]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   221
  In[]: t[-3]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   222
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   223
Negative indices can be used to access in reverse
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   224
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   225
Addition gives a new sequence containing both sequences ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   226
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   227
     In[]: num+var
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   228
     In[]: p="another string"
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   229
     In[]: w+p
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   230
     In[]: t2=(3,4,6,7)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   231
     In[]: t+t2
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   232
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   233
len function gives the length  ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   234
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   235
  In[]: len(num)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   236
  In[]: len(w)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   237
  In[]: len(t)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   238
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   239
We can check whether an element is there with 'in' keyword ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   240
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   241
  In[]: 3 in num
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   242
  In[]: 'H' in w
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   243
  In[]: 2 in t
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   244
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   245
Find maximum using max function  and minimum using min:: 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   246
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   247
  In[]: max(t)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   248
  In[]: min(w)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   249
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   250
Get a sorted list and reversed list using sorted and reversed function ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   251
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   252
  In[]: sorted(num)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   253
  In[]: reversed(w)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   254
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   255
As a consequence of the order one can access a group of elements together 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   256
The methods for this are called slicing and striding 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   257
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   258
First Slicing 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   259
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   260
Given a list ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   261
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   262
  In[]:j=[1,2,3,4,5,6]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   263
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   264
Lets say we want elements 2 to 5.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   265
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   266
For this we can do ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   267
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   268
  In[]: j[1:4]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   269
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   270
The syntax for slicing is sequence variable name square bracket
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   271
first element index ,colon,second element index.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   272
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   273
  In[]: j[:4]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   274
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   275
If first element is left blank default is from beginning and if last
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   276
element is left blank it means till the end.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   277
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   278
 In[]: j[1:]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   279
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   280
 In[]: j[:]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   281
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   282
This effectively is the whole list.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   283
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   284
Striding is a concept similar to slicing with the concept of skiping elements
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   285
at regular intervals added.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   286
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   287
Lets see by example ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   288
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   289
  In[]: z=[1,2,3,4,5,6,7,8,9,10]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   290
  In[]: z[1:8:2]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   291
  Out[]:[2, 4, 6, 8]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   292
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   293
The colon two added in the end signifies all the second elemets. This is why we call this concept
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   294
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
   295
being 2. 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   296
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   297
We have talked about many similar features of lists,strings and tuples but there are many is an important
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   298
way in which lists differ from strings and tuples. Lets see this by example.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   299
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   300
  In[]: z[1]=9
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   301
  In[]: w[1]='k'
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   302
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   303
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   304
{{{ slide to show the error }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   305
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
   306
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   307
Now lets try ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   308
  In[]: t[1]=5
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   309
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   310
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
   311
We cannot change the value at a particular index just by assigning a new value at that position.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   312
In case of strings we have special functions to appy relacement and other things while tuples cannot
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   313
be changed at all. 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   314
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   315
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
   316
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
   317
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   318
We can convert all the number data types to one another ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   319
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   320
  In[]: i=34
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   321
  In[]: d=float(i)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   322
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   323
Python has built in functions int , float and complex to convert one number type
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   324
data structure to another.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   325
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   326
  In[]: dec=2.34
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   327
  In[]: dec_con=int(dec)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   328
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   329
As you can see the decimal part of the number is simply stripped  to get the integer.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   330
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   331
  In[]: com=2.3+4.2j
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   332
  In[]: float(com)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   333
  In case of complex number to floating point only the real value of complex number is taken.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   334
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   335
Similarly we can convert list to tuple and tuple to list ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   336
  
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   337
  In[]: lst=[3,4,5,6]
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   338
  In[]: tup=tuple(lst)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   339
  In[]: tupl=(3,23,4,56)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   340
  In[]: lst=list(tuple)
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   341
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   342
However string to list and list to string is an interesting problem.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   343
Lets say we have a string ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   344
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   345
  In: somestring="Is there a way to split on these spaces."
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   346
  In: somestring.split()
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   347
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   348
This produces a list with the string split at whitespace.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   349
similarly we can split on some other character.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   350
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   351
  In: otherstring="Tim,Amy,Stewy,Boss"
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   352
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   353
How do we split on comma , simply pass it as argument ::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   354
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   355
  In: otherstring.split(',')
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   356
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   357
join function does the opposite. Joins a list to make a string.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   358
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   359
  In[]:','.join['List','joined','on','commas']
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   360
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   361
Thus we get a list joined on commas. Similarly we can do spaces.::
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   362
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   363
  In[]:' '.join['Now','on','spaces']
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   364
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   365
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
   366
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   367
{{{ Show the "sponsored by FOSSEE" slide }}}
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   368
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   369
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   370
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   371
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   372
Hope you have enjoyed and found it useful.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   373
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   374
Thank You.
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   375
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   376
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   377
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   378
Author              : Amit Sethi
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   379
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   380
Internal Reviewer 1 : 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   381
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   382
Internal Reviewer 2 : 
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   383
c1242f073db3 Initial commit basic data types
amit
parents:
diff changeset
   384
External Reviewer