tuples/script.rst
changeset 425 c943cdbee397
parent 282 8c35d7977f04
child 505 394c3642cf9c
equal deleted inserted replaced
424:19f49715e83f 425:c943cdbee397
     1 .. Objectives
     1 .. Objectives
     2 .. ----------
     2 .. ----------
     3 
     3 
     4 .. A - Students and teachers from Science and engineering backgrounds
     4 .. At the end of the tutorial, you will
     5    B - Will learn what are tuples and why they are needed
     5 .. #. have a clear understand of what tuples are
     6        Will learn the various methods of accessing elements in tuples
     6 .. #. be able to compare them with lists
     7    C - 
     7 .. #. know why they are needed and where to use them 
     8    D - 
       
     9 
       
    10 .. #. what are tuples
       
    11 .. #. comparison with lists
       
    12 .. #. why are they needed
       
    13 
     8 
    14 
     9 
    15 .. Prerequisites
    10 .. Prerequisites
    16 .. -------------
    11 .. -------------
    17 
    12 
    23    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    18    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    24 
    19 
    25 Script
    20 Script
    26 ------
    21 ------
    27 
    22 
    28 Hello friends and welcome to the tutorial on Tuples
    23 Hello friends and welcome to the tutorial on getting started with
       
    24 tuples. 
    29 
    25 
    30 {{{ Show the slide containing title }}}
    26 {{{ Show the slide containing title }}}
    31 
    27 
    32 {{{ Show the slide containing the outline slide }}}
    28 {{{ Show the slide containing the outline slide }}}
    33 
    29 
    35 
    31 
    36  * what are tuples
    32  * what are tuples
    37  * their similarities and dissimilarities with lists
    33  * their similarities and dissimilarities with lists
    38  * why are they needed
    34  * why are they needed
    39 
    35 
    40 Let`s get started by defining a tuple. A tuple is defined by enclosing
    36 Let's get started by defining a tuple. A tuple is defined by enclosing
    41 parantheses around a sequence of items seperated by commas. It is similar to
    37 parentheses around a sequence of items seperated by commas. It is
    42 defining a list except that parantheses are used instead of square brackets.
    38 similar to defining a list except that parentheses are used instead of
       
    39 square brackets.  
    43 ::
    40 ::
    44 
    41 
    45     t = (1, 2.5, "hello", -4, "world", 1.24, 5)
    42     t = (1, 2.5, "hello", -4, "world", 1.24, 5)
    46     t
    43     t
    47 
    44 
    64 ::
    61 ::
    65 
    62 
    66     t[2] = "Hello"
    63     t[2] = "Hello"
    67 
    64 
    68 We can see that, it raises an error saying tuple does not support item
    65 We can see that, it raises an error saying tuple does not support item
    69 assignment. It only implies that tuples are immutable or in simple words,
    66 assignment. Tuples are immutable, and cannot be changed after
    70 tuples cannot be changed.
    67 creation. 
    71 
    68 
    72 But what is the use of tuples!!!
    69 Then, what's the use of tuples?
    73 
    70 
    74 We shall understand that soon. But let us look at a simple problem of swapping
    71 We shall understand that soon. But let us look at a simple problem of swapping
    75 values.
    72 values.
    76 
    73 
    77 {{{ Pause here and try out the following exercises }}}
    74 {{{ Pause here and try out the following exercises }}}
   108     a, b = b, a
   105     a, b = b, a
   109 
   106 
   110     a
   107     a
   111     b
   108     b
   112 
   109 
   113 We see that the values are swapped.
   110 We see that the values are swapped.  This idiom works for different
   114 This idiom works for different datatypes also.
   111 data-types also.  
       
   112 
   115 ::
   113 ::
   116 
   114 
   117     a = 2.5
   115     a = 2.5
   118     b = "hello"
   116     b = "hello"
   119 
   117 
   120     a
   118     a
   121     b
   119     b
   122 
   120 
   123 Moreover this type of behaviour is straight forward and what you would expect
   121 Moreover this type of behaviour is something that feels natural and
   124 should happen naturally.
   122 you'd expect to happen. 
   125 
   123 
   126 This is possible because of the immutability of tuples. This process is called
   124 This is possible because of the immutability of tuples. This process is called
   127 tuple packing and unpacking.
   125 tuple packing and unpacking.
   128 
   126 
   129 Let us first see what is tuple packing. Type
   127 Let us first see what is tuple packing. Type
   134 What we see is a tuple with one element.
   132 What we see is a tuple with one element.
   135 ::
   133 ::
   136 
   134 
   137     5, "hello", 2.5
   135     5, "hello", 2.5
   138 
   136 
   139 Now it is a tuple with two elements.
   137 Now it is a tuple with three elements.
   140 
   138 
   141 So when we are actually typing two or more elements seperated by commas, those
   139 So when we are actually typing two or more elements seperated by commas, those
   142 elements are packed and a tuple is made from them.
   140 elements are packed into a tuple. 
   143 
   141 
   144 When you type
   142 When you type
   145 ::
   143 ::
   146 
   144 
   147     a, b = b, a
   145     a, b = b, a
   163  * The value swapping idiom in Python
   161  * The value swapping idiom in Python
   164  * packing and unpacking of tuples
   162  * packing and unpacking of tuples
   165 
   163 
   166 {{{ Show the "sponsored by FOSSEE" slide }}}
   164 {{{ Show the "sponsored by FOSSEE" slide }}}
   167 
   165 
   168 #[Nishanth]: Will add this line after all of us fix on one.
       
   169 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   166 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   170 
   167 
   171 Hope you have enjoyed and found it useful.
   168 Hope you have enjoyed and found it useful.
   172 Thankyou
   169 Thank you
   173 
   170