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