sets/script.rst
changeset 233 ab748264f726
child 281 d91133654fc8
equal deleted inserted replaced
232:da873a5ac918 233:ab748264f726
       
     1 .. Objectives
       
     2 .. ----------
       
     3 
       
     4 .. A - Students and teachers from Science and engineering backgrounds
       
     5    B - Will learn what are tuples and why they are needed
       
     6        Will learn the various methods of accessing elements in tuples
       
     7    C - 
       
     8    D - 
       
     9 
       
    10 .. Prerequisites
       
    11 .. -------------
       
    12 
       
    13 ..   1. Getting started with lists
       
    14      
       
    15 .. Author              : Nishanth Amuluru
       
    16    Internal Reviewer   : 
       
    17    External Reviewer   :
       
    18    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
       
    19 
       
    20 Script
       
    21 ------
       
    22 
       
    23 Hello friends and welcome to the tutorial on Sets
       
    24 
       
    25 {{{ Show the slide containing title }}}
       
    26 
       
    27 {{{ Show the slide containing the outline slide }}}
       
    28 
       
    29 In this tutorial, we shall learn
       
    30 
       
    31  * sets
       
    32  * operations on sets
       
    33 
       
    34 Sets are data structures which contain unique elements. In other words,
       
    35 duplicates are not allowed in sets.
       
    36 
       
    37 Lets look at how to input sets.
       
    38 type
       
    39 ::
       
    40  
       
    41     a_list = [1, 2, 1, 4, 5, 6, 7]
       
    42     a = set(a_list)
       
    43     a
       
    44      
       
    45 We can see that duplicates are removed and the set contains only unique
       
    46 elements. 
       
    47 ::
       
    48 
       
    49     f10 = set([1, 2, 3, 5, 8])
       
    50     p10 = set([2, 3, 5, 7])
       
    51 
       
    52 f10 is the set of fibonacci numbers from 1 to 10.
       
    53 p10 is the set of prime numbers from 1 to 10.
       
    54 
       
    55 Various operations that we do on sets are possible here also.
       
    56 The | character stands for union
       
    57 ::
       
    58 
       
    59     f10 | p10
       
    60 
       
    61 gives us the union of f10 and p10
       
    62 
       
    63 The & character stands for intersection.
       
    64 ::
       
    65 
       
    66     f10 & p10
       
    67 
       
    68 gives the intersection
       
    69 
       
    70 similarly,
       
    71 ::
       
    72 
       
    73     f10 - p10
       
    74 
       
    75 gives all the elements that are in f10 but not in p10
       
    76 
       
    77 ::
       
    78 
       
    79     f10 ^ p10
       
    80 
       
    81 is all the elements in f10 union p10 but not in f10 intersection p10. In
       
    82 mathematical terms, it gives the symmectric difference.
       
    83 
       
    84 Sets also support checking of subsets.
       
    85 ::
       
    86 
       
    87     b = set([1, 2])
       
    88     b < f10
       
    89 
       
    90 gives a True since b is a proper subset of f10.
       
    91 Similarly,
       
    92 ::
       
    93 
       
    94     f10 < f10
       
    95 
       
    96 gives a False since f10 is not a proper subset.
       
    97 hence the right way to do would be
       
    98 ::
       
    99 
       
   100     f10 <= f10
       
   101 
       
   102 and we get a True since every set is a subset of itself.
       
   103 
       
   104 Sets can be iterated upon just like lists and tuples. 
       
   105 ::
       
   106 
       
   107     for i in f10:
       
   108         print i,
       
   109 
       
   110 prints the elements of f10.
       
   111 
       
   112 The length and containership check on sets is similar as in lists and tuples.
       
   113 ::
       
   114 
       
   115     len(f10)
       
   116 
       
   117 shows 5. And
       
   118 ::
       
   119     
       
   120     1 in f10
       
   121     2 in f10
       
   122 
       
   123 prints True and False respectively
       
   124 
       
   125 The order in which elements are organised in a set is not to be relied upon 
       
   126 since sets do not support indexing. Hence, slicing and striding are not valid
       
   127 on sets.
       
   128 
       
   129 {{{ Pause here and try out the following exercises }}}
       
   130 
       
   131 %% 1 %% Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23] 
       
   132         list all the duplicates
       
   133 
       
   134 {{{ continue from paused state }}}
       
   135 
       
   136 Duplicates marks are the marks left out when we remove each element of the 
       
   137 list exactly one time.
       
   138 
       
   139 ::
       
   140 
       
   141     marks = [20, 23, 22, 23, 20, 21, 23] 
       
   142     marks_set = set(marks)
       
   143     for mark in marks_set:
       
   144         marks.remove(mark)
       
   145 
       
   146     # we are now left with only duplicates in the list marks
       
   147     duplicates = set(marks)
       
   148 
       
   149 {{{ Show summary slide }}}
       
   150 
       
   151 This brings us to the end of the tutorial.
       
   152 we have learnt
       
   153 
       
   154  * How to make sets from lists
       
   155  * How to input sets
       
   156  * How to perform union, intersection and symmectric difference operations
       
   157  * How to check if a set is a subset of other
       
   158  * The various similarities with lists like length and containership
       
   159 
       
   160 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   161 
       
   162 #[Nishanth]: Will add this line after all of us fix on one.
       
   163 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   164 
       
   165 Hope you have enjoyed and found it useful.
       
   166 Thankyou
       
   167