Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Sets in Mathematics?
3.
What are Sets in Python?
4.
Python Set In-built Functions
5.
Python Set Difference
6.
Frequently Asked Questions
6.1.
What is the main use of sets in python?
6.2.
What does a python set difference function return?
6.3.
How to subtract two sets in python?
6.4.
What is the syntax to find set difference in python?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python Set Difference ()

Introduction

Do you know we have various in-built functions in python that are helpful while coding? Do you know what Python set difference function returns?

python set difference

In this article, we will discuss about sets in python. We will see various in-built functions in sets. We will also learn about the python set difference function and how to use them. Moving forward, let’s first understand about sets.

Must read, Divmod in Python, and Convert String to List Python

What are Sets in Mathematics?

Sets are a collection of unique elements. In maths, Sets are used to represent a group of numbers like a group of natural numbers or a group of rational numbers. A set having no element is called an empty set, whereas a set with one element is called a singleton. Similarly, we have set concepts in python language as well. Moving forward, let’s understand sets in python language.

What are Sets in Python?

Sets are built-in functions in python that are useful in storing unique elements in one variable. 

what are sets

In sets, the order of elements is not preserved because the items in sets are not stored in the order in which they appearThe unique elements in a set are written in curly brackets. For example, set1 = {1, 2, 3, 4, 5}. A Set can have values from different data types, for example, set2 = {1, 2, “coding”, 3, “ninja”}The output of the set is always sorted. If you print set1 and set2, then you will get output in sorted order. See the output of the program, which is mentioned below to understand it more clearly.

Let’s understand this by executing a program. 

Code

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

print("set1: ",set1)




set2 = { "coding", 1, 2, 3, "ninja"}

print("set2: ",set2)

Output

output

Some points that must be kept in mind while creating a set are:

  • A set can have items in any order, as they are not referred to or called by keys or indexes.
  • The items inside a set can not be changed once created, but we can add and delete items from a set.
  • A set cannot have duplicate items.

 

Also check out Python Square Root here.

Python Set In-built Functions

👉 add() function is used to add elements in the set.

👉 clear() function is used for removing all the elements from the set.

👉 copy() function returns a copy of the set.

👉 difference() function returns the difference between two or more sets.

👉 difference_update() removes items from one set that are common in another set.

👉 discard() function is used to remove specific items.

👉 intersection() is used to return the intersection of two or more sets.

👉 intersection_update() removes items from one set that are not present in another set.

👉 isdisjoint() is used to return whether two sets are intersecting or not.

👉 issubset() returns true if the given set is a subset of another set otherwise returns false.

👉 issuperset() returns true if the given set is a superset of another set otherwise returns false.

👉 pop() helps to remove an element from the set.

👉 remove() helps to remove a specified element from a set.

👉 symmetric_difference()  is used to return symmetric difference between two sets..

👉 union() returns a union of sets.

Let’s make a program with a few functions to understand it more clearly.

Code

set = {'c++', 'python', 'java'}

set.add('c')

set.add('c#')

print("Set after using add() function: ",set, "\n")

setCopy = set.copy();

print("Copied Set: ",setCopy, "\n")

set.discard('c#')

print("Set after discarding c#: ",set, "\n")

print("Intersection of set and copied set: ",set.intersection(setCopy), "\n")

print("Union of set and copied set: ",set.union(setCopy), "\n")

set.clear()

print("Set is cleared")

Output

output

Moving forward, let’s discuss the python set difference function.

Python Set Difference

In Python, there is a difference() method that is used in sets to return the difference between two sets. Let’s suppose we have two sets, set1 = {1,2,3,4,5} and set2 = {1,2,5,6,7}, and we want the difference between these two sets then the python set difference function will return a set having elements from set1, which are not present in set2. See the below-mentioned code to understand it more clearly.

Code

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

print("Set 1: " , set1)


set2 = {1, 2, 5, 6, 7}

print("Set 2: " , set2)


diff = set1.difference(set2)

print("Difference between set1 and set2: " , diff)

Output

output

If there is nothing common in two sets, then we can say the python set difference function will return the first set. 

Code

set1 = {"Hello", "Coders"}

print("Set 1: " , set1)

set2 = {"Coding", "world"}

print("Set 2: " , set2)

diff = set1.difference(set2)

print("Difference between set1 and set2: " , diff)

Output

output

You can practice by yourself with the help of online python compiler.

Frequently Asked Questions

What is the main use of sets in python?

Sets are used to store multiple values in one variable.

What does a python set difference function return?

The python set difference function returns difference between two sets.

How to subtract two sets in python?

Using the python set difference method, you can return the subtraction of two sets.

What is the syntax to find set difference in python?

The Syntax to find set difference in python is setA.difference(setB).

Conclusion

In this article, we have discussed about python set difference function. We have talked about sets and their in-built functions in python. We have made programs to better understand python set difference and other set functions more clearly. To know more about python, refer to below-mentioned articles.

We hope this article has helped you in understanding the python set difference method. If this article helped you in any way, then you can read more such articles on our platform, Coding Ninjas Studio. You will find articles on almost every topic on our platform. Also, you can practice coding questions at Coding Ninjas to crack good product-based companies. For interview preparations, you can read the Interview Experiences of popular companies
Happy Learning!!

Live masterclass