tuples/script.rst
author Nishanth <nishanth@fossee.in>
Thu, 07 Oct 2010 14:10:32 +0530
changeset 232 da873a5ac918
child 282 8c35d7977f04
permissions -rw-r--r--
made the tuples.rst into new template form
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
232
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     1
.. Objectives
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     2
.. ----------
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     3
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     4
.. A - Students and teachers from Science and engineering backgrounds
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     5
   B - Will learn what are tuples and why they are needed
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     6
       Will learn the various methods of accessing elements in tuples
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     7
   C - 
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     8
   D - 
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     9
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    10
.. Prerequisites
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    11
.. -------------
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    12
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    13
..   1. Getting started with lists
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    14
     
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    15
.. Author              : Nishanth Amuluru
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    16
   Internal Reviewer   : 
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    17
   External Reviewer   :
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    18
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    19
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    20
Script
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    21
------
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    22
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    23
Hello friends and welcome to the tutorial on Tuples
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    24
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    25
{{{ Show the slide containing title }}}
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    26
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    27
{{{ Show the slide containing the outline slide }}}
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    28
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    29
In this tutorial, we shall learn
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    30
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    31
 * what are tuples
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    32
 * their similarities and dissimilarities with lists
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    33
 * why are they needed
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    34
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    35
Let`s get started by defining a tuple. A tuple is defined by enclosing
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    36
parantheses around a sequence of items seperated by commas. It is similar to
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    37
defining a list except that parantheses are used instead of square brackets.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    38
::
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    39
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    40
    t = (1, 2.5, "hello", -4, "world", 1.24, 5)
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    41
    t
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    42
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    43
defines a tuple. The items in the tuple are indexed using numbers and can be 
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    44
accessed by using their position.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    45
::
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    46
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    47
    t[3]
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    48
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    49
prints -4 which is the fourth item of the tuple.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    50
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    51
::
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    52
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    53
    t[1:5:2]
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    54
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    55
prints the corresponding slice
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    56
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    57
This is the behaviour similar as to lists. But the difference can be seen when
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    58
we try to change an element in the tuple.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    59
::
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    60
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    61
    t[2] = "Hello"
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    62
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    63
We can see that, it raises an error saying tuple does not support item
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    64
assignment. It only implies that tuples are immutable or in simple words,
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    65
tuples cannot be changed.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    66
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    67
But what is the use of tuples!!!
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    68
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    69
We shall understand that soon. But let us look at a simple problem of swapping
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    70
values.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    71
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    72
{{{ Pause here and try out the following exercises }}}
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    73
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    74
%% 1 %% a = 5 and b = 7. swap the values of a and b
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    75
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    76
{{{ continue from paused state }}}
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    77
::
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    78
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    79
    a = 5
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    80
    b = 7
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    81
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    82
    a
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    83
    b
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    84
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    85
We define the two values
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    86
::
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    87
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    88
    temp = a
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    89
    a = b
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    90
    b = temp
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    91
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    92
    a
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    93
    b
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    94
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    95
This is the traditional approach
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    96
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    97
Now let us do it the python way
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    98
::
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    99
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   100
    a
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   101
    b
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   102
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   103
    a, b = b, a
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   104
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   105
    a
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   106
    b
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   107
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   108
We see that the values are swapped.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   109
This idiom works for different datatypes also.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   110
::
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   111
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   112
    a = 2.5
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   113
    b = "hello"
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   114
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   115
    a
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   116
    b
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   117
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   118
Moreover this type of behaviour is straight forward and what you would expect
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   119
should happen naturally.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   120
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   121
This is possible because of the immutability of tuples. This process is called
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   122
tuple packing and unpacking.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   123
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   124
Let us first see what is tuple packing. Type
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   125
::
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   126
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   127
    5,
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   128
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   129
What we see is a tuple with one element.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   130
::
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   131
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   132
    5, "hello", 2.5
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   133
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   134
Now it is a tuple with two elements.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   135
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   136
So when we are actually typing two or more elements seperated by commas, those
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   137
elements are packed and a tuple is made from them.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   138
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   139
When you type
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   140
::
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   141
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   142
    a, b = b, a
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   143
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   144
First the values of b and a are packed into a tuple on the right side and then
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   145
unpacked into the variables a and b.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   146
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   147
Immutability of tuples ensures that the values are not changed during the
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   148
packing and unpacking.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   149
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   150
{{{ Show summary slide }}}
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   151
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   152
This brings us to the end of the tutorial.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   153
we have learnt
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   154
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   155
 * How to define tuples
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   156
 * The similarities of tuples with lists, like indexing and iterability
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   157
 * The immutability of tuples
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   158
 * The value swapping idiom in Python
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   159
 * packing and unpacking of tuples
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   160
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   161
{{{ Show the "sponsored by FOSSEE" slide }}}
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   162
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   163
#[Nishanth]: Will add this line after all of us fix on one.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   164
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   165
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   166
Hope you have enjoyed and found it useful.
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   167
Thankyou
da873a5ac918 made the tuples.rst into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   168