Python SetPython Set is like a dictionary, an unordered collection of keys, held without any values. The set type is mutable , the contents can be changed using methods like add() and remove() . Sets do not contain duplicates, every element in a Python set can have only one occurrence in that particular set, no multiple occurrences are allowed. There are currently two built-in set types, Set and Frozenset . example
Create a Set from Tupleexample
Create a Set from Listexample
Sets are Mutable
The set type is mutable, the contents can be changed using methods like add() and remove().example
Set.remove() remove elements from set.example
len() function returned the length of a set.example
Union of Sets
In Python, Union operation can be performed on two Python sets using the operator or by using the method union(). Union of two sets x and y is another set that contains all distinct elements in sets x and x.
Using | Operatorexample
Intersection of Sets
In Python, Union operation can be performed on two Python sets using the operator & or by using the method intersection(). Intersection of two sets x and y is another set of elements those are present in both sets x and y (common to both of them).
Using & operatorexample
Difference of Sets
The difference() method returns the set difference of two sets. In Python, Difference operation can be performed on two Python sets using the operator - or by using the method difference().
Using - operatorexample
Symmetric Difference of Sets
Symmetric difference of the sets is the set of elements those are not common to both the sets. The symmetric_difference() method using.
A frozenset value is immutable: it's set up with the frozenset() function call, but once it's set up its contents cannot be altered.example