examples of set in python programming

0

set:

The number of its elements can be modified (adding only elements and it is not possible to delete an element), and their values can be modified, the elements are unordered and have no addresses, meaning that the set can be printed twice and the elements are printed in a different order each time, the values of the elements cannot be repeated in it.

All Contributions

Union, Intersection, Difference and Symmetric Difference are some operations

which are performed on sets.

Union: Union operation performed on two sets returns all the elements from both

the sets. It is performed by using & operator.

Intersection: Intersection operation performed on two sets returns all the element

which are common or in both the sets. It is performed by using | operator.

Difference: Difference operation performed on two sets set1 and set2 returns the

elements which are present on set1 but not in set2. It is performed by using –

operator.

Symmetric Difference: Symmetric Difference operation performed on two sets

returns the element which are present in either set1 or set2 but not in both. It is

performed by using ^ operator.

set1 = set([1, 2, 4, 1, 2, 8, 5, 4])
set2 = set([1, 9, 3, 2, 5])
print(set1) #Printing set
#set([8, 1, 2, 4, 5]) #Output
print(set2)


intersection = set1 & set2 #intersection of set1 and set2
print(intersection)

union = set1 | set2 # Union of set1 and set2
print(union) #Output set([1, 2, 3, 4, 5, 8, 9]) 

difference = set1 - set2 # Difference of set1 and set2
print(difference) #Output set([8, 4])

symm_diff = set1 ^ set2 # Symmetric difference of set1 and
print(symm_diff)  #Output set([3, 4, 8, 9])

total contributions (1)

New to examplegeek.com?

Join us