tuples.rst
author Puneeth Chaganti <punchagan@fossee.in>
Thu, 07 Oct 2010 12:28:12 +0530
changeset 242 a33e942379d7
parent 222 d5249a528cae
permissions -rw-r--r--
Cleaned up script for getting started with files LO.
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   :
222
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   151
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   152
Questions
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   153
=========
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   154
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   155
 1. Define a tuple containing two values. The first being integer 4 and second
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   156
    is a float 2.5
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   157
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   158
   Answer: (4, 2.5)
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   159
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   160
 2. If ``a = (5, "Hello", 3.2)``. what is the value of a[2]
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   161
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   162
   Answer: 3.2
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   163
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   164
 3. If ``a = 5,`` then what is the type of a
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   165
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   166
   a. int
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   167
   #. float
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   168
   #. tuple
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   169
   #. string
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   170
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   171
   Answer: tuple
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   172
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   173
 4. if ``a = (2, 3)``. What does ``a[0], a[1] = (3, 4)`` produce
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   174
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   175
   Answer: Error
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   176
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   177
 5. If ``a = ([2, 3], 4, 5)``. What is the value of ``a`` after doing
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   178
    ``a[0].append(6)``
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   179
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   180
    a. ([2, 3, 6], 4, 5)
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   181
    #. Raises an error
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   182
    #. ([2, 3], 4, 5)
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   183
    #. [2, 3, 4, 5, 6]
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   184
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   185
    Answer: ([2, 3, 6], 4, 5)
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   186
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   187
 6. What does the following code produce::
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   188
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   189
      a = 5
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   190
      b = "Hello"
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   191
      a, b = b, a
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   192
      print a
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   193
      print b
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   194
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   195
    Answer: Hello
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   196
            5
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   197
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   198
 7. ``a = ("hello", "world", 5, 6, 8)``. What is the value of a[1:4]
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   199
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   200
    Answer: ("world", 5, 6)
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   201
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   202
 8. ``a = (1, 2, 3, 4, 5, 6, 7, 8)``. What is the value of a[1::3]
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   203
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   204
    Answer: (2, 5, 8)
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   205
d5249a528cae Added questions
Nishanth <nishanth@fossee.in>
parents: 153
diff changeset
   206