{3, 4}
The difference operation s1 – s2 will generate the elements that are present in s1, but not in s2. For example,
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5, 6}
print(s1 – s2)
The output will be:
{1, 2}
Similarly, the symmetric difference operation will generate all the elements that are present in either s1 or s2, but not in both. For example,
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5, 6}
print(s1 ^ s2)
The program will print the following output:
{1, 2, 5, 6}
How to add elements to a set in Python?
In Python, we can use the add() function to add an element to a set. For example,
s = set()
for i in range(5):
s.add(i)
print(s)
Here, we are initializing an empty set. Then we are adding the numbers 0 to 4 in the set. So, the above program will print the following output:
{0, 1, 2, 3, 4}
A set containing different data types
Please note that a set can contain different data types in the same set. For example,








































0 Comments