Python Set

11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.22
List Price:  €93.18
You save:  €27.95
£56.04
List Price:  £80.07
You save:  £24.02
CA$96.09
List Price:  CA$137.28
You save:  CA$41.18
A$106.44
List Price:  A$152.07
You save:  A$45.62
S$94.93
List Price:  S$135.63
You save:  S$40.69
HK$547
List Price:  HK$781.46
You save:  HK$234.46
CHF 63.65
List Price:  CHF 90.94
You save:  CHF 27.28
NOK kr765.05
List Price:  NOK kr1,092.98
You save:  NOK kr327.92
DKK kr486.48
List Price:  DKK kr695
You save:  DKK kr208.52
NZ$116.68
List Price:  NZ$166.69
You save:  NZ$50.01
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,685.71
List Price:  ৳10,980.05
You save:  ৳3,294.34
₹5,844.71
List Price:  ₹8,349.95
You save:  ₹2,505.23
RM331.80
List Price:  RM474.03
You save:  RM142.22
₦98,423.43
List Price:  ₦140,610.93
You save:  ₦42,187.50
₨19,474.95
List Price:  ₨27,822.55
You save:  ₨8,347.60
฿2,584.73
List Price:  ฿3,692.63
You save:  ฿1,107.90
₺2,255.06
List Price:  ₺3,221.65
You save:  ₺966.59
B$356.31
List Price:  B$509.04
You save:  B$152.73
R1,298.99
List Price:  R1,855.78
You save:  R556.79
Лв127.57
List Price:  Лв182.26
You save:  Лв54.68
₩95,950.21
List Price:  ₩137,077.60
You save:  ₩41,127.39
₪261.40
List Price:  ₪373.45
You save:  ₪112.04
₱4,015.50
List Price:  ₱5,736.67
You save:  ₱1,721.17
¥10,912.42
List Price:  ¥15,589.84
You save:  ¥4,677.42
MX$1,187.01
List Price:  MX$1,695.81
You save:  MX$508.79
QR254.79
List Price:  QR364.01
You save:  QR109.21
P956.45
List Price:  P1,366.42
You save:  P409.96
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,313.80
List Price:  E£4,734.21
You save:  E£1,420.40
ብር4,020.93
List Price:  ብር5,744.43
You save:  ብር1,723.50
Kz58,536.36
List Price:  Kz83,626.96
You save:  Kz25,090.59
CLP$65,519.73
List Price:  CLP$93,603.63
You save:  CLP$28,083.90
CN¥505.79
List Price:  CN¥722.59
You save:  CN¥216.80
RD$4,064.05
List Price:  RD$5,806.04
You save:  RD$1,741.98
DA9,434.16
List Price:  DA13,477.95
You save:  DA4,043.79
FJ$159.43
List Price:  FJ$227.77
You save:  FJ$68.34
Q544.12
List Price:  Q777.35
You save:  Q233.22
GY$14,659.33
List Price:  GY$20,942.80
You save:  GY$6,283.47
ISK kr9,804.19
List Price:  ISK kr14,006.59
You save:  ISK kr4,202.40
DH702
List Price:  DH1,002.91
You save:  DH300.90
L1,236.68
List Price:  L1,766.77
You save:  L530.08
ден4,019.08
List Price:  ден5,741.79
You save:  ден1,722.70
MOP$563.89
List Price:  MOP$805.60
You save:  MOP$241.70
N$1,302.92
List Price:  N$1,861.40
You save:  N$558.47
C$2,577.98
List Price:  C$3,682.99
You save:  C$1,105
रु9,357.62
List Price:  रु13,368.60
You save:  रु4,010.98
S/260.83
List Price:  S/372.63
You save:  S/111.80
K271.34
List Price:  K387.65
You save:  K116.30
SAR262.49
List Price:  SAR375
You save:  SAR112.51
ZK1,913.50
List Price:  ZK2,733.69
You save:  ZK820.18
L324.50
List Price:  L463.60
You save:  L139.09
Kč1,630.34
List Price:  Kč2,329.15
You save:  Kč698.81
Ft25,331.89
List Price:  Ft36,189.97
You save:  Ft10,858.07
SEK kr764.93
List Price:  SEK kr1,092.80
You save:  SEK kr327.87
ARS$61,714.55
List Price:  ARS$88,167.42
You save:  ARS$26,452.87
Bs483.86
List Price:  Bs691.26
You save:  Bs207.40
COP$272,553.83
List Price:  COP$389,379.31
You save:  COP$116,825.47
₡35,845.57
List Price:  ₡51,210.15
You save:  ₡15,364.58
L1,730.57
List Price:  L2,472.36
You save:  L741.78
₲523,213.21
List Price:  ₲747,479.48
You save:  ₲224,266.27
$U2,704.33
List Price:  $U3,863.50
You save:  $U1,159.16
zł280.23
List Price:  zł400.34
You save:  zł120.11
Already have an account? Log In

Transcript

So welcome to the Python session today in this video we'll be starting with the sets. So sets in Python is an unordered collection of data type that is iterable mutable and has no duplicate in it. So sets are very much similar to lists and dictionaries of Python is just that it has a unique values that is you cannot repeat its values it should have unique values. So sets. So let us first create a set. Let us first create a list and then from that list and create a set.

So here I'll write 23 comma 25 comma 26. Again, I'll repeat few values. Suppose I repeat 25. Again, I write 10, D. So I've repeated here, two values. 25 is repeated twice and 10 is repeated twice. Now, what I'll do is I'll convert this list into one set.

So I'm right print within bracket set and within this and right when these two name so, run it what it returned is the unique values of those least see the duplicate values that I repeated is 10 and 25 see the duplicate values are deleted and we I got the unique values same this way, I have created a set and the value that I got is within this curly braces. So suppose using this first list, if I want to check what I have created, so just write l one equal to set and then again within bracket right l one and then check the type type l one. So let's run it and what we got is a set. So, set is a data type of Python and we can create the set in this way. So, we know what we have done is we have created a set from a list.

Now how set can be created uniquely by its own. So he will do within this curly braces I write one comma two comma three. So few values have given again, run it and check the type of thing what it returned is set. So just within these curly braces if you give any new values beat on numeric or anything, it will create a set. Now suppose I create one new set with the newbie and it stole few values. s four comma, five comma six, seven, comma eight.

And I'll also copy this one and paste it over here. So these are the two sets that I've created. And now I'm using it union function to check what will happen. So, within this first bracket right a dot union and within bracket right B, check it what you got is it joins the two sets, taking the unique values. So up to five, it is from the first sets and see four and five is the duplicate values. So it is not repeated and again from six I got the values from the next set.

So we can use this union method Indian function in a different way also. So you can also Alright print and within this first bracket right a and this operator and right B, the same thing will happen, see, the same thing will happen. So, either you can write union dot A dot union B and or you can just use the operator. Now, if I use the intersection method then what will happen, so, just copy this values and paste it here and write a dot intersection of B. What it returned is it will return only the common values or the duplicate values from the two sets. That is what the two sets have in common.

So, four and five is common in both A and B So, it will return the common values of both the sets you can use this intersection method by using operators so print a using this ampersand sign, and B, it will return you the same thing. So four and five is returned. So you can use either intersection, or you can use this ampersand operator. The next thing that I'll do is using the suppose I have to find the difference then what I will do is using this two sets, so I'll write a dot difference within bracket right Be it return view the different value from a Ok. So what are the values that are differing from B, it will return only those values. Now if you write b dot difference, E. It will return the unique values of set B now this week can also use by using an operator.

So, the operator is print a minus b. So, it returned the values from the first set. And now if you use print b minus a, it will return the unique values of set B. So, the one you want just right in the front. So, these are the few things of sets that we have done. Now, if I use a symmetric difference, that is only the unique values from both sets, then what we can use is copy this value and paste it here.

Now right e dot, symmetric underscore difference we in brackets right be run it. So, what it returned it returned the unique values from both the set see 123678 So, from both the set it will return the unique values also you can use the operator and the operator is print a and use this hat that is the sign just f offs with with six in the key six we have this operator So, just use it and you will get the same result. So, this is the symmetric difference that is what is the unique values from both the sets A and B. So, these are the unique values and it will return you the values okay so, so now I will add few values to my set by using an add method. So first place we create a set vowel within these curly braces. All right, the valance name that is a e, i, o.

Now I haven't written you. So just run this and print it. So it has created a set. And always remember the set will return in a random way, the way you have created will not be returned in the same way. They can return in any way. Now I add you to the set of vowel.

So how we can add it, just write the sets name, then write, add. And then within the first bracket, write the value you want to add. So I've missed you serve written you to just run it and now print Following So, see you is now added to the set of the values of the set. So, now, here I have added a unique value of value that I want to a particular value that want to specify. Now you can add a set of values also by just suppose there is a power and create a set of values again. So, let me just write he and I so, all you I will omit okay have run this set and set it's created.

Now, I want to add I I use added so, III I owe you so I am too I add oh and you vowel to the say by a double. So I'll create a tupple to up with the first bracket, I'll write all you so all you now is within a tupple. And now I want to add this values of a tupple to the list here. So how we can do is I can just write vowels dot add and write the tupple mean. So do you notice run it Okay, and now print vowels. See, a topple is added within a set so I can add a topple within a set by using an add function.

So these are the few methods that we have learned in sets how to create a set from a list how to Create a set uniquely and how to use different functions like union, intersection difference, symmetric difference, and also how you can add a value to the set. So, till then keep practicing. See you in the next video. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.