the elements of the set s, it will eliminate the duplicate entry of 4 and print {1, 2, 3, 4}
s = {1, 2, 3, 4, 4}
print(s)
The output will be:
{1, 2, 3, 4}
How to get the length of a set?
We can use the len() function to get the length of a set.
s = {1, 2, 3, 4}
print(len(s))
The output will be:
4
Union, Intersection, Difference, and Symmetric Difference Operations on a Set
A set supports operations like union, intersection, difference and symmetric difference.
If there are two sets s1 and s2, then the union operation will generate all elements that are present in s1 or s2 or both s1 and s2. For example,
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5, 6}
print(s1 | s2)
The program will print the following output:
{1, 2, 3, 4, 5, 6}
The intersection operation will generate all elements that are present in both s1 and s2. For example,
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5, 6}
print(s1 & s2)
The output will be:








































0 Comments