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