| Show Slide 1
Containing title, name of the production team along with the logo of MHRD
|Hello friends and welcome to the tutorial on 'Sets'.|
| Show Slide 2
| At the end of this tutorial, you will be able to,
| Show Slide 3
|Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists"|
|ipython|| Now, What are sets? Sets are data structures which contain unique elements. In other words, duplicates are not allowed in sets.
First let us invoke our ipython interpreter
| a_list = [1, 2, 1, 4, 5, 6, 2]
a = set(a_list) a
|Lets look at how to input sets. type|
| f10 = set([1, 2, 3, 5, 8])
p10 = set([2, 3, 5, 7])
|We can see that duplicates are removed and the set contains only unique elements. Now let us perform some operations on sets. For this, we shall first create a pair of sets|
|p10|| f10 is the set of fibonacci numbers from 1 to 10. p10 is the set of prime numbers from 1 to 10.
Various operations can be performed on sets. For example, The | (pipe) character stands for union
|f10 & p10|| It gave the union of f10 and p10
The '&' character stands for intersection.
|f10 - p10|| It gave the intersection of f10 and p10
similarly,f10 - p10 gives all the elements that are in f10 but not in p10
|f10 ^ p10||and f10 ^ p10 gives all the elements in f10 union p10 but not in f10 intersection p10.|
| b = set([1, 2])
b < f10
| In mathematical terms, it gives the symmetric difference.
Sets also support checking of subsets.
|f10 < f10||It gives a True since b is a proper subset of f10. Similarly,|
|f10 <= f10||It gives a False since f10 is not a proper subset. hence the right way to do would be|
| for i in f10:
| we get a True since every set is a subset of itself.
Sets can be iterated upon just like lists and tuples.
|len(f10)|| It prints the elements of f10.
The length and containership check on sets is similar as in lists and tuples.
| 1 in f10
2 in f10
|It shows 5. And|
| prints True and False respectively
The order in which elements are organized in a set is not to be relied upon, since sets do not support indexing. Hence, slicing and striding are not valid on sets.
Pause the video here, try out the following exercise and resume the video.
| Show Slide 4
| Given a list of marks, marks = [20, 23, 22, 23, 20, 21, 23]
list all the duplicates
|Continue from paused state Switch to the terminal||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)
| duplicates = set(marks)
|We are now left with only duplicates in the list marks Hence,|
|We obtained our required solution|
| Show Slide 5
| This brings us to the end of the tutorial. In this tutorial, we have learnt to,
| Show Slide 6
Self assessment questions slide
| Here are some self assessment questions for you to solve
| Show Slide 7
Solution of self assessment questions on slide
| And the answers,
Enumerated list ends without a blank line; unexpected unindent.
odd ^ squares
3. To check the containership, we say,
b in a
| Show Slide 8
|Hope you have enjoyed this tutorial and found it useful. Thank you!|