Skip to content

Python set minus | Example code

  • by

Using the minus set to get the difference between the two sets. Using the difference method there is no difference in the output.

difference_of_sets = set_1 - set_2

Python set minus example

Simple example code subtraction in sets python

set1 = {1, 2, 3, 4, 5}

set2 = {1, 2, 3}

print(set1 - set2)


Python set minus

The 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)

Do comment if you have any doubts or suggestions on this Python set topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *