In this section we learn about the python set and find the intersection and union from set. Python set is store the combination of multiple item. Set is also one of the data types other are list, tuple, Dictionary and set.
Set is also most use data type in programmer. Set is unchanageable don't allow duplicated values. When programmer create the set and programmer haves not to capable to change the value so it is fix if set create. Set have detect duplicate value.Â
we cannot insert same value in one set. set is unordered form. It means set have undefine value. It is also knows as python collection array. there are many set function and method they are:Â
Python set intersection and union |
Â
Python Set MethodÂ
This is the python set method. the most use method in programmer are add(), remove90, pop(), intersection() and union(). this method is always remember for beginner programmer. In this section we learn about the two method of set they are python set intersection() and python set union()
Python Set Intersection
The intersection is the mathematic problem and intersection is the number of two set that is same number between two difference sets example AnB
#finding the intersection
set1 = {1,3,56,67,89,56,45,3,90,9,45}
#codebugfree
set2 = {1,4,5,7,8,3,5,89,45,34,43}
find_intersection = set1.intersection(set2)
#we find the intersection of set1 ans set2
print(find_intersection)
Firt we create set wit name set1 and give elements. again make set2 with giving elements. find_intersection variable is finding the intersection of set1 and set set1.intersection of set2. print the intersection
INOUTPUT
{89, 1, 3, 45}
Python Set Union
#Finding the union
union_set = {1,23,56,78,69,4,3,2,7,9,6,4,56,78,45,345,}
#codebugfree
union_sec_set = {1,34,56,2,4,7,8,98,90,67,45,23,45,67,44,22,77,55,}
find_the_union = union_set.union(union_sec_set)
#Here we find the union of union_set and union_sec_set
print(find_the_union)
We create the first and second sets as union_set and union_sec_set with giving the different elements in to this diffrent set. Find_the_union has capable to find the union of two this set union_set.union of union_sec_set. Last print the find the union
INOUTPUT
{1, 2, 3, 4, 69, 6, 7, 67, 9, 8, 77, 78, 22, 23, 345, 90, 34, 98, 44, 45, 55, 56}
Code bug free is now finish the python set intersection and union method. How to make a empty set and use add() methodÂ
b = set()
b.add(4)
print(b)
INOUTPUT
{4}
We make a empty set b is the empty set. write the set in their because the symbol of set and dictionary is same that way set is write before the creating the empty set. Adding the 4 in empty set than print b.Â
0 Comments
Don't comment a spam message