×

Python Set difference() Method

Python Set difference() Method

The set.difference() method in Python returns the set difference of two sets(A-B).

Syntax

set.difference(set1)

Parameter

set- This argument represents a set (minuend)

set1- This arguments represents a set(subtrahend)

Return

This method returns the difference of the two specified sets i.e. the returned set will contain the items that exist only in the first set, and not in both sets.

Example 1

# Python program explaining 
# the set.difference() method
#initializing a set with numbers from 1 to 10
Numbers = {1,2,3,4,5,6,7,8,9,10}
print("Numbers:",Numbers) 
# initializing set2 with even numbers
even = {2,4,6,8}
print("Even numbers: ",even)
# Equivalent to A-B 
# Numbers- even=  Odd
print("After subtracting even numbers from Natural numbers... ")
print("Odd numbers: ",Numbers.difference(even)) 

Output

Numbers: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Even numbers:  {8, 2, 4, 6}
After subtracting even numbers from Natural numbers... 
Odd numbers:  {1, 3, 5, 7, 9, 10} 

Example 2

# Python program explaining
# the set.difference() method
#initializing a set with alphabets A to Z
Alphabets = {'a', 'b', 'c', 'd','e',
    'f','g','h','i',
    'j','k','l','m', 
    'n','o','p','u',
    'v','w','x','y','z'}
print("Alphabets:",Alphabets)
 # initializing set2 with vowels 
vowels = {'a', 'e', 'i','o','u'}
print("vowels: ",vowels)
# Equivalent to A-B 
# Alphabets- vowels= Consonants
print("After subracting vowels from Alphabets... ")
print("Consonants: ",Alphabets.difference(vowels)) 

Output

Alphabets: {'o', 'v', 'h', 'p', 'n', 'w', 'b', 'l', 'g', 'z', 'x', 'c', 'e', 'j', 'k', 'f', 'i', 'a', 'm', 'y', 'd', 'u'}
vowels:  {'a', 'u', 'o', 'i', 'e'}
After subracting vowels from Alphabets... 
Consonants:  {'k', 'y', 'v', 'f', 'g', 'z', 'd', 'x', 'h', 'p', 'n', 'w', 'b', 'c', 'l', 'm', 'j'} 


Related Topics

Python maketrans() function

Introduction A static method is the string maketrans() one. It is used to make a one-to-one mapping between a string's character and its translation, i.e., to define the list of characters...

5 minutes read.

Android apps for coding in python

Nowadays, it is the generation of smartphones. The world can be seen and acknowledged with a single click on your mobile phone. Everyone uses mobile phones to do any work,...

9 minutes read.

Mrcnn Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Program of Cumulative Sum in Python

Introduction This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for...

5 minutes read.

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values. Syntax dictionary.fromkeys(sequence[, value]) Parameter sequence- This...

1 minute read.

What is a Script in Python

Have you ever heard that Python is a scripting language? Or when people address a Python program file as a script? Besides programming, script generally means "a written story for...

3 minutes read.

Operator Overloading in Python

In any programming language, + is an arithmetic operator; it adds two numbers to give the sum. Have you ever tried to concatenate two strings? Concatenating two strings is adding...

5 minutes read.

*Args and **Kwargs in Python

Let’s know about ** (dual star / asterisk) and * (star / asterisk) are operators in Python. These stars are used in Python for parameters. Args and kwargs are special...

3 minutes read.

Python len() function

Python len() function The len() function in Python  returns the number of items in an object. Syntax len(s) Parameter s: This parameter represents a sequence (such as a string, bytes, tuple, list, or range) or...

1 minute read.

To Do GUI Application using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Python Project Ideas Based On Django

Introduction If you have learned Python and you are an expert in Django, then your practical skills should be excellent in this field. If you want to check your practical skills,...

9 minutes read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

Palindrome In Python

What is Palindrome? A Palindrome can be defined as the number or a string that resides unchanged when it is reversed. Example: 14341 Output: Yes, this is a Palindrome number Example: RACECAR Output: Yes, this...

2 minutes read.

Data Structures and Algorithms Using Python | Part 1

Data Structures: Data Structure is defined as a way to organize and store the data so that we can access the data and work more efficiently. Data structures also describe the...

18 minutes read.

Python MySQL

In this article, we are going to learn the following: How to connect Python to MySQL.How to create a new Database.Procedure for connecting the newly created database.Procedure for connecting the already...

7 minutes read.

Python Matrix Multiplication

One of the most fundamental mathematical structures, matrices are used often in many disciplines, including mathematics, physics, engineering, computer science, etc. For example, matrices and associated operations (multiplication and addition) are...

8 minutes read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

Loan calculator using Tkinter in Python

Tkinter: Tkinter, part of all common Python distributions, is the de facto method for creating Graphical User Interfaces (GUIs) in Python. The only framework included in the Python standard library is...

4 minutes read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.