sets.rst
changeset 142 7bc28afff7ab
child 143 e75538bca178
equal deleted inserted replaced
141:05a7b0506134 142:7bc28afff7ab
       
     1 Hello friends and welcome to the tutorial on Sets
       
     2 
       
     3 {{{ Show the slide containing title }}}
       
     4 
       
     5 {{{ Show the slide containing the outline slide }}}
       
     6 
       
     7 In this tutorial, we shall learn
       
     8 
       
     9  * sets
       
    10  * operations on sets
       
    11 
       
    12 Sets are data structures which contain unique elements. In other words,
       
    13 duplicates are not allowed in sets.
       
    14 
       
    15 Lets look at how to input sets.
       
    16 type
       
    17 ::
       
    18  
       
    19     a_list = [1, 2, 1, 4, 5, 6, 7]
       
    20     a = set(a_list)
       
    21     a
       
    22      
       
    23 We can see that duplicates are removed and the set contains only unique
       
    24 elements. 
       
    25 ::
       
    26 
       
    27     f10 = set([1, 2, 3, 5, 8])
       
    28     p10 = set([2, 3, 5, 7])
       
    29 
       
    30 f10 is the set of fibonacci numbers from 1 to 10.
       
    31 p10 is the set of prime numbers from 1 to 10.
       
    32 
       
    33 Sets can be iterated upon just like lists and tuples. 
       
    34 ::
       
    35 
       
    36     for i in f10:
       
    37         print i,
       
    38 
       
    39 prints the elements of f10.
       
    40 
       
    41 The length and containership check on sets is similar as in lists and tuples.
       
    42 ::
       
    43 
       
    44     len(f10)
       
    45 
       
    46 shows 5. And
       
    47 ::
       
    48 
       
    49     2 in f10
       
    50 
       
    51 prints False
       
    52 
       
    53 The order in which elements are organised in a set is not to be relied upon 
       
    54 since sets do not support indexing. Hence, slicing and striding are not valid
       
    55 on sets.
       
    56 
       
    57 Various operations that we do on sets are possible here also.
       
    58 The | character stands for union
       
    59 ::
       
    60 
       
    61     f10 | p10
       
    62 
       
    63 gives us the union of f10 and p10
       
    64 
       
    65 The & character stands for intersection.
       
    66 ::
       
    67 
       
    68     f10 & p10
       
    69 
       
    70 gives the intersection
       
    71 
       
    72 similarly,
       
    73 ::
       
    74 
       
    75     f10 - p10
       
    76 
       
    77 gives all the elements that are in f10 but not in p10
       
    78 
       
    79 ::
       
    80 
       
    81     f10 ^ p10
       
    82 
       
    83 is all the elements in f10 union p10 but not in f10 intersection p10. In
       
    84 mathematical terms, it gives the symmectric difference.
       
    85 
       
    86 {{{ Pause here and try out the following exercises }}}
       
    87 
       
    88 %% 1 %% Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23] 
       
    89         list all the duplicates
       
    90 
       
    91 {{{ continue from paused state }}}
       
    92 
       
    93 Duplicates marks are the marks left out when we remove each element of the 
       
    94 list exactly one time.
       
    95 
       
    96 ::
       
    97 
       
    98     marks = [20, 23, 22, 23, 20, 21, 23] 
       
    99     marks_set = set(marks)
       
   100     for mark in marks_set:
       
   101         marks.remove(mark)
       
   102 
       
   103     # we are now left with only duplicates in the list marks
       
   104     duplicates = set(marks)
       
   105 
       
   106 {{{ Show summary slide }}}
       
   107 
       
   108 This brings us to the end of the tutorial.
       
   109 we have learnt
       
   110 
       
   111  * How to make sets from lists
       
   112  * How to input sets
       
   113  * The various similarities with lists like length and containership
       
   114  * How to perform union, intersection and symmectric difference operations
       
   115 
       
   116 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   117 
       
   118 #[Nishanth]: Will add this line after all of us fix on one.
       
   119 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   120 
       
   121 Hope you have enjoyed and found it useful.
       
   122 Thankyou
       
   123  
       
   124 .. Author              : Nishanth
       
   125    Internal Reviewer 1 : 
       
   126    Internal Reviewer 2 : 
       
   127    External Reviewer   :