1
00:00:00 --> 00:00:04
Hello friends and welcome to the tutorial on 'Sets'.
2
00:00:05 --> 00:00:24
At the end of this tutorial, you will be able to
Create sets from lists.
Perform union, intersection and symmetric difference operations.
Check if a set is a subset of other.
Understand various similarities with lists like length and containership.
3
00:00:25 --> 00:00:34
Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with List"
4
00:00:35 --> 00:00:42
Now, type ipython in the command.
5
00:00:43 --> 00:00:45
So, What are sets?
6
00:00:46 --> 00:00:48
Sets are data structures which contain unique elements.
7
00:00:49 --> 00:00:53
In other words, duplicates are not allowed in sets.
8
00:00:54 --> 00:00:58
Lets look at how to input sets.
9
00:00:59 --> 00:01:14
So type a underscore list is equal to within brackets 1,2,1,4,5,6,2.
10
00:01:15 --> 00:01:24
Then a is equal to set in brackets 'a' underscore list.
11
00:01:25 --> 00:01:27
Then type 'a'.
12
00:01:28 --> 00:01:30
We can see that duplicates are removed and the set contains only unique elements.
13
00:01:31 --> 00:01:34
Now let us perform some operations on sets.
14
00:01:35 --> 00:01:39
For this, we shall first create a pair of sets
15
00:01:40 --> 00:01:53
f10 is equal to set in brackets and square brackets 1,2,3,5,8
16
00:01:54 --> 00:02:06
p10 is equal to set of within brackets and square brackets 2,3,5,7.
17
00:02:07 --> 00:02:18
f10 is the set of fibonacci series numbers from 1 to 10.
18
00:02:19 --> 00:02:21
p10 is the set of prime numbers from 1 to 10.
19
00:02:22 --> 00:02:24
Various operations can be performed on sets.
20
00:02:25 --> 00:02:36
For example, The vertical bar and in bracket pipe character stands for union.
21
00:02:37 --> 00:02:45
So type f10 then a vertical bar then p10 and hit Enter.
22
00:02:46 --> 00:02:49
It gave the union of f10 and p10.
23
00:02:50 --> 00:02:52
The ampersand character stands for intersection.
24
00:02:53 --> 00:02:59
So type f10 ampersand p10.
25
00:03:00 --> 00:03:04
It gave the intersection of f10 and p10 similarly.
26
00:03:05 --> 00:03:12
f10 - p10 gives all the elements that are in f10 but not in p10 .
27
00:03:13 --> 00:03:21
So type f10 hyphen p10
28
00:03:22 --> 00:03:39
and f10 charat p10 gives all the elements in f10 union p10 but not in f10 intersection p10. So type f10 charat p10.
29
00:03:40 --> 00:03:45
In mathematical terms, it gives the symmetric difference.
30
00:03:46 --> 00:03:49
Sets also support checking of subsets.
31
00:03:50 --> 00:03:59
So lets type b is equal to in brackets and square brackets 1,2.
32
00:04:00 --> 00:04:04
Then b is less than f10.
33
00:04:05 --> 00:04:08
It gives a True since b is a proper subset of f10.
34
00:04:09 --> 00:04:14
Similarly, type f10 less than f10.
35
00:04:15 --> 00:04:18
It gives a False since f10 is not a proper subset.
36
00:04:19 --> 00:04:30
Hence the right way to do would be f10 less than or equal to f10.
37
00:04:31 --> 00:04:36
we get a True since every set is a subset of itself.
38
00:04:37 --> 00:04:41
Sets can be iterated upon just like lists and tuples.
39
00:04:42 --> 00:05:00
for i in f10 colon in the command and then type print i comma
40
00:05:01 --> 00:05:02
It prints the elements of f10.
41
00:05:03 --> 00:05:11
The length and container ship check on sets is similar as in lists and tuples.
42
00:05:12 --> 00:05:21
So type len within brackets f10 and hit Enter.
43
00:05:22 --> 00:05:26
And It shows 5, type 1 in f10.
44
00:05:27 --> 00:05:32
Then type 2 in f10.
45
00:05:33 --> 00:05:44
And prints True and True respectively
46
00:05:45 --> 00:05:49
Now, pause the video here, try out the following exercise and resume the video.
47
00:05:50 --> 00:06:00
Given a list of marks, marks is equal to within square brackets 20, 23, 22, 23, 20, 21, 23
48
00:06:01 --> 00:06:03
list all the duplicates
49
00:06:04 --> 00:06:12
Duplicates marks are the marks left out when we remove each element of the list exactly one time.
50
00:06:13 --> 00:06:32
So type marks is equal to in square brackets 20,23,22,23,20,21,23.
51
00:06:33 --> 00:06:43
Then type marks underscore set is equal to set within bracket marks.
52
00:06:44 --> 00:07:04
then for mark in marks underscore set colon, then marks dot remove within brackets mark.
53
00:07:05 --> 00:07:09
We are now left with only duplicates in the list marks +
54
00:07:10 --> 00:07:23
Hence Type duplicates is equal to set marks and hit enter.
55
00:07:24 --> 00:07:26
Then type duplicate.
56
00:07:27 --> 00:07:29
Hence, We obtained our required solution
57
00:07:30 --> 00:07:33
This brings us to the end of the tutorial.
58
00:07:34 --> 00:07:35
In this tutorial, we have learnt to,
59
00:07:36 --> 00:07:48
, & and ^ respectively.
60
00:07:49 --> 00:07:57
Check if a set is a subset of other using the ' and ' = operators.
61
00:07:58 --> 00:08:04
Then, Understand various similarities with lists like length and containership.
62
00:08:05 --> 00:08:08
Here are some self assessment questions for you to solve
63
00:08:09 --> 00:08:17
First one, If a is equal to within square brackets 1, 1, 2, 3, 3, 5, 5, 8
64
00:08:18 --> 00:08:22
And What is set(a)
65
00:08:23 --> 00:08:25
The options are
66
00:08:26 --> 00:08:30
set within brackets and square brackets 1, 1, 2, 3, 3, 5, 5, 8
67
00:08:31 --> 00:08:35
Second one set within brackets 1, 2, 3, 5, 8
68
00:08:36 --> 00:08:40
Then the third option set([1, 2, 3, 3, 5, 5])
69
00:08:41 --> 00:08:42
Then the last option is Error.
70
00:08:43 --> 00:08:56
Second question, odd is equal to set within brackets 1, 3, 5, 7, 9 and squares is equal to set within brackets 1, 4, 9, 16
71
00:08:57 --> 00:09:01
How do you find the symmetric difference of these two sets?
72
00:09:02 --> 00:09:04
The third questions is, a is a set.
73
00:09:05 --> 00:09:10
how do you check if a variable b exists in a ?
74
00:09:11 --> 00:09:14
Now And the answers,
75
00:09:15 --> 00:09:27
First answer is, set of a will have all the common elements in the list a , that is set within brackets 1, 2, 3, 5, 8
76
00:09:28 --> 00:09:36
The Second answer is , To find the symmetric difference between two sets, we use the operator charat.
77
00:09:37 --> 00:09:41
So type odd charat squares
78
00:09:42 --> 00:09:44
Final answer is, To check the container ship, we say,
79
00:09:45 --> 00:09:49
b in a
80
00:09:50 --> 00:09:52
So Hope you have enjoyed this tutorial and found it useful.
81
00:09:53 --> 00:09:58
Thank you!