# HG changeset patch # User nishanth # Date 1284568771 -19800 # Node ID 7bc28afff7ab6afb157529e2f09177ba6049f1ff # Parent 05a7b05061341367242fe890094f975647df9485 initial commit of sets diff -r 05a7b0506134 -r 7bc28afff7ab sets.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sets.rst Wed Sep 15 22:09:31 2010 +0530 @@ -0,0 +1,127 @@ +Hello friends and welcome to the tutorial on Sets + +{{{ Show the slide containing title }}} + +{{{ Show the slide containing the outline slide }}} + +In this tutorial, we shall learn + + * sets + * operations on sets + +Sets are data structures which contain unique elements. In other words, +duplicates are not allowed in sets. + +Lets look at how to input sets. +type +:: + + a_list = [1, 2, 1, 4, 5, 6, 7] + a = set(a_list) + a + +We can see that duplicates are removed and the set contains only unique +elements. +:: + + f10 = set([1, 2, 3, 5, 8]) + p10 = set([2, 3, 5, 7]) + +f10 is the set of fibonacci numbers from 1 to 10. +p10 is the set of prime numbers from 1 to 10. + +Sets can be iterated upon just like lists and tuples. +:: + + for i in f10: + print i, + +prints the elements of f10. + +The length and containership check on sets is similar as in lists and tuples. +:: + + len(f10) + +shows 5. And +:: + + 2 in f10 + +prints False + +The order in which elements are organised in a set is not to be relied upon +since sets do not support indexing. Hence, slicing and striding are not valid +on sets. + +Various operations that we do on sets are possible here also. +The | character stands for union +:: + + f10 | p10 + +gives us the union of f10 and p10 + +The & character stands for intersection. +:: + + f10 & p10 + +gives the intersection + +similarly, +:: + + f10 - p10 + +gives all the elements that are in f10 but not in p10 + +:: + + f10 ^ p10 + +is all the elements in f10 union p10 but not in f10 intersection p10. In +mathematical terms, it gives the symmectric difference. + +{{{ Pause here and try out the following exercises }}} + +%% 1 %% Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23] + list all the duplicates + +{{{ continue from paused state }}} + +Duplicates marks are the marks left out when we remove each element of the +list exactly one time. + +:: + + marks = [20, 23, 22, 23, 20, 21, 23] + marks_set = set(marks) + for mark in marks_set: + marks.remove(mark) + + # we are now left with only duplicates in the list marks + duplicates = set(marks) + +{{{ Show summary slide }}} + +This brings us to the end of the tutorial. +we have learnt + + * How to make sets from lists + * How to input sets + * The various similarities with lists like length and containership + * How to perform union, intersection and symmectric difference operations + +{{{ Show the "sponsored by FOSSEE" slide }}} + +#[Nishanth]: Will add this line after all of us fix on one. +This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India + +Hope you have enjoyed and found it useful. +Thankyou + +.. Author : Nishanth + Internal Reviewer 1 : + Internal Reviewer 2 : + External Reviewer :