subtract sets python code example

Example 1: subtraction in sets python

A = {10, 20, 30, 40, 80}
B = {100, 30, 80, 40, 60}
print (A - B)

Example 2: python subtract one set from another

# Basic syntax:
difference_of_sets = set_1 - set_2

# Example usage:
# Define sets
set_1 = {3, 7, 11, 23, 42}
set_2 = {1, 2, 11, 42, 57}
# Return elements of set_1 that aren't in set_2:
difference_of_sets = set_1 - set_2 
print(difference_of_sets)
--> {3, 23, 7}

# Syntax for other set functions:
set_1 | set_2 # Union of sets (elements in both)
set_1 & set_2 # Intersection of sets (elements in common)

Example 3: difference of two set in python

x = {1, 2, 3, 4, 5, 6}
y = {1, 2, 3, 4}

z = x.difference(y)
# 5, 6