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