# Python Set difference() Method

## Python Set | difference()

The difference between the two sets in Python is equal to the difference between the number of elements in two sets. The function difference() returns a set that is the difference between two sets. Let’s try to find out what will be the difference between two sets A and B. Then (set A – set B) will be the elements present in set A but not in B and (set B – set A) will be the elements present in set B but not in set A.

Example:

```set A = {10, 20, 30, 40, 80}
set B = {100, 30, 80, 40, 60}

set A - set B = {10, 20}
set B - set A = {100, 60}

Explanation: A - B is equal to the elements present in A but not in B
B - A is equal to the elements present in B but not in A
```

Let’s look at the Venn diagram of the following difference set function.

Syntax:

```set_A.difference(set_B) for (A - B)
set _B.difference(set_A) for (B - A)
```

In this program, we will try to find out the difference between two sets set_A and set_B, both the way:

 `# Python code to get the difference between two sets` `# using difference() between set A and set B` ` ` `# Driver Code` `A ``=` `{``10``, ``20``, ``30``, ``40``, ``80``}` `B ``=` `{``100``, ``30``, ``80``, ``40``, ``60``}` `print` `(A.difference(B))` `print` `(B.difference(A))`

Output:

```{10, 20}
{100, 60}
```

We can also use – operator to find the difference between two sets.

 `# Python code to get the difference between two sets` `# using difference() between set A and set B` ` ` `# Driver Code` `A ``=` `{``10``, ``20``, ``30``, ``40``, ``80``}` `B ``=` `{``100``, ``30``, ``80``, ``40``, ``60``}` `print` `(A ``-` `B)` `print` `(B ``-` `A)`

Output:

```{10, 20}
{100, 60}
```

If we have equal sets then it will return the null set.

 `# Python code to get the difference between two sets` `# using difference() between set A and set B` ` ` `# Driver Code` `A ``=` `{``10``, ``20``, ``30``, ``40``, ``80``}` `B ``=` `{``10``, ``20``, ``30``, ``40``, ``80``, ``100``}` `print` `(A ``-` `B)`

Output:

```set()
```

Last Updated on October 25, 2021 by admin

## Django BasicsDjango Basics

Django Basics Django is a Python-based web framework which allows you to quickly create web application without all of the installation or dependency problems that you normally will find with other frameworks. When you’re building a website, you always need

## Python map() functionPython map() function

Python map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.) Syntax : map(fun, iter) Parameters : fun : It is a function

## Python Data TypesPython Data Types

Python Data Types Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types

## Difference between Python and C++Difference between Python and C++

Difference between Python and C++ Python tutorial: Python is a high-level, interpreted programming language. It was invented back in 1991, by Guido Van Rossum. Python is an object-oriented programming language that has large enormous library support making the implementation of various

## How to create an empty Pandas DataFrame and append rows & columns?How to create an empty Pandas DataFrame and append rows & columns?

Let’s discuss how to create an empty DataFrame and append rows & columns to it in Pandas. There are multiple ways in which we can do this task. Method #1: Create a complete empty DataFrame without any column name or

## How to Run a Python Script using Docker?How to Run a Python Script using Docker?

How to Run a Python Script using Docker? Python is one of the most popular and widely used programming languages. Right from building a simple GUI app using Tkinter to building a full-fledged web application, Python is used everywhere. In

## Python program to find sum of elements in listPython program to find sum of elements in list

Given a list of integers, let’s write a Python program to find sum of all the elements in given list. Examples: Input: [3, 8, 7, 10] Output: 28 Input: [9, 5, 1, 2] Output: 17 Now, let’s see the different

## Log functions in PythonLog functions in Python

Log functions in Python Python offers many inbuild logarithmic functions under the module “math” which allows us to compute logs using a single line. There are 4 variants of logarithmic functions, all of which are discussed in this article. 1.