Example of a three basic mathematical operations with sets in Python

Sets can be used to perform mathematical set operations like union, intersection, symmetric difference, etc.

Example:

my_set = {1, 6, 2, 'java', 'ruby', 8, 9, 10, 21, 1000, 'python', 6}
programming_set = {'java', 'ruby', 'javascript', 'python', 'c'}

print(my_set.intersection(programming_set))
print(my_set.union(programming_set))
print(my_set.difference(programming_set))
Output:
{'ruby', 'python', 'java'}
{1, 2, 'c', 6, 8, 9, 10, 1000, 'ruby', 'python', 21, 'javascript', 'java'}
{1, 2, 6, 8, 9, 10, 1000, 21}
Intersection of A and B is a set of elements that are common in both the sets.
Union of A and B is a set of all elements from both sets.
Difference of the set B from set A (A - B) is a set of elements that are only in A but not in B. Similarly, B - A is a set of elements in B but not in A.