In Python, the difference() method is a built-in function of the set data type that returns the set of elements that are in one set but not in another set.
Here's an example of how to use the difference() method:
set1 = {1, 2, 3, 4, 5}set2 = {3, 4, 5, 6, 7}diff_set = set1.difference(set2)print(diff_set) |
Output:
{1, 2} |
In this example, we define two sets, set1 and set2, that have some overlapping elements. We use the difference() method on set1 and pass in set2 as an argument to return the set of elements that are in set1 but not in set2. The resulting set, diff_set, contains the elements 1 and 2.
Note that the difference() method does not modify the original set. Instead, it returns a new set that contains the result of the difference operation.
Also note that the order of the arguments passed to difference() matters. The resulting set will contain the elements that are in the first set but not in the second set. If you want to get the elements that are in the second set but not in the first set, you can use the difference() method on set2 and pass in set1 as an argument.