Python-3.4.3/C3/Sets-in-Python/English-timed
|
|
00:01 | Welcome to the spoken tutorial on Sets in Python. |
00:06 | In this tutorial, you will learn to, Create sets from lists |
00:12 | Perform union, intersection and symmetric difference operations |
00:18 | Check if a set is a subset of other and
Understand various similarities with lists |
00:27 | To record this tutorial, I am using
Ubuntu Linux 16.04 operating system |
00:35 | Python 3.4.3 and IPython 5.1.0 |
00:42 | To practise this tutorial, you should know how to run basic Python commands on the ipython console and use lists |
00:53 | If not, see the relevant Python tutorials on this website. |
00:58 | First let us see the overview of sets. |
01:02 | Sets are unordered collections of unique elements. |
01:07 | The set itself is mutable.
We can add or remove items from it. |
01:14 | Let us start ipython.
Open the terminal. |
01:19 | Type ipython3 and press Enter. |
01:24 | From here onwards, remember to press the Enter key after typing every command on the terminal. |
01:31 | Let us look at how to input sets. |
01:35 | Type,
a underscore list is equal to inside square brackets 1 comma 2 comma 1 comma 4 comma 5 comma 6 comma 2 |
01:47 | Type, a is equal to set inside brackets a underscore list |
01:54 | Then type, a |
01:57 | We can see that duplicates are removed and the set contains only unique elements. |
02:03 | Sets can also be created directly as follows |
02:08 | Type, b is equal to inside curly braces 1 comma 2 comma 1 comma 4 comma 5 comma 6 comma 2 |
02:19 | Type, b. As before, we see that the set contains only unique elements. |
02:27 | To create an empty set, type, c is equal to set open and close brackets |
02:36 | Note that d is equal to open and close curly braces creates an empty dictionary, not an empty set. |
02:45 | Sets can contain numbers, strings and tuples.
But cannot contain mutable elements such as lists or dictionaries. |
02:56 | Type, b underscore list is equal to inside square brackets again inside square brackets 1 comma 2 comma 1 comma 6 comma 2 |
03:09 | Now type, b is equal to set inside brackets b underscore list |
03:17 | As you can see, it gives a TypeError. |
03:21 | Now let us perform some operations on sets.
For this, we will first create a pair of sets. |
03:29 | Type the sets as shown. |
03:33 | Here, f is the set of fibonacci numbers from 1 to 10. |
03:39 | And p is the set of prime numbers from 1 to 15. |
03:44 | Various operations can be performed on sets. |
03:47 | First we will add an element to a set by using add method. |
03:53 | Type, f dot add inside brackets 13 |
03:58 | Now type, f
As you can see, 13 is added to the set f. |
04:06 | add method has no effect if the element is already present. |
04:11 | Type, f dot add inside brackets 13 |
04:16 | Type, f
As you can see, 13 is not added this time. |
04:23 | Next we will learn to remove an element using remove method. |
04:28 | Type, p dot remove inside brackets 13 |
04:34 | Now type, p. As you can see, 13 is removed from the set p. |
04:41 | If the element is not a member, it raises a KeyError. |
04:46 | Type, p dot remove inside brackets 18 |
04:52 | KeyError is raised since element 18 is not present in p. |
04:57 | The pipe character stands for union. |
05:01 | Type, f pipe p |
05:06 | Or type, f dot union inside brackets p
It gave the union of f and p |
05:16 | The ampersand character stands for intersection. |
05:20 | Type, f ampersand p
Or type, f dot intersection inside brackets p |
05:30 | It gave the intersection of f and p. |
05:34 | Type, f minus p
Or type, f dot difference inside brackets p |
05:44 | It gave all the elements that are in f but not in p. |
05:50 | Type, f caret p |
05:54 | Or type, f dot symmetric underscore difference inside brackets p |
06:01 | It gives all the elements in f union p but not in f intersection p. |
06:08 | In mathematical terms, it gives the symmetric difference. |
06:13 | Sets also support checking of subsets. |
06:17 | Type, a is equal to set inside brackets inside square brackets 1 comma 2 comma 3 comma 4 |
06:27 | Type, b is equal to set inside brackets inside square brackets 1 comma 2 |
06:35 | Now type, b less than or equal to a
Or type, b dot issubset inside brackets a |
06:47 | It gives True since b is a subset of a. |
06:52 | Sets also support checking of supersets.
Type, b greater than or equal to a |
07:01 | Or type, b dot issuperset inside brackets a |
07:08 | We get False since b is not a superset of a. |
07:14 | Every set is a subset as well as a superset of itself. |
07:20 | Type, a less than or equal to a
a greater than or equal to a |
07:28 | It gives True in both cases since a is a superset and subset of itself. |
07:35 | Elements of a set can be accessed using for loop. |
07:39 | Type, for x in a colon print inside brackets x |
07:47 | The items from the set will not appear in any specific order. |
07:52 | The length and containership check on sets are similar as in lists and tuples. |
07:59 | Type, len inside brackets a
It shows 4. |
08:07 | Type,
1 in a 7 in a |
08:13 | It prints True and False respectively |
08:18 | Sets do not support indexing.
Hence, slicing and striding are not valid on sets. |
08:25 | Pause the video.
Try this exercise and then resume the video. |
08:31 | Given a list of marks, as shown, list all the duplicate marks. |
08:37 | Switch to the terminal for the solution. |
08:41 | Type, marks is equal to inside square brackets 20 comma 23 comma 22 comma 23 comma 20 comma 21 comma 23 |
08:53 | marks underscore set is equal to set inside brackets marks |
09:00 | for num in marks underscore set colon
marks dot remove inside brackets num |
09:10 | remove method only removes the first occurrence of an element from the list. |
09:17 | Type, duplicates is equal to set inside bracket marks |
09:24 | Now type, duplicates |
09:28 | We now have duplicate marks in the set duplicates.
Hence, we have obtained our required solution. |
09:37 | This brings us to the end of this tutorial. Let us summarize. |
09:43 | In this tutorial, we have learnt to, Make sets from lists or by using curly braces |
09:50 | Perform union, intersection and symmetric difference operations |
09:55 | Check if a set is a subset of other using the less than or equal to operator |
10:02 | Understand the various similarities with lists like length and containership |
10:08 | Here are some self assessment questions for you to solve. |
10:12 | First. If a is assigned as follows, what is set of a |
10:18 | Second. Given odd and squares as shown. How do you find the symmetric difference of these two sets? |
10:27 | Third. If a is a set, how do you check if a variable b exists in a? |
10:34 | And the answers,
First. set of a will have all the common elements in the list a, that is 1 comma 2 comma 3 comma 5 comma 8. |
10:46 | Second. To find the symmetric difference between two sets, we use the operator caret. So we can type odd caret squares |
10:56 | Third. To check the containership, we type, b in a |
11:01 | Please post your timed queries in this forum. |
11:05 | Please post your general queries on Python in this forum. |
11:10 | FOSSEE team coordinates the TBC project. |
11:15 | Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
11:25 | This is Priya from IIT Bombay signing off.
Thanks for watching. |