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