Python Tutorial - Set Method

Python Exercise Lists

Earlier, you learned about set collections. In this section, you will learn the methods provided by set types one by one. First, you can see what methods it has with the dir (set) command:

dir(set)
           

The specific syntax structure and function of each method are shown in the following table.

Method Name Syntax Format Features
add() set1.add() Add numbers, strings, tuples, or booleans to the set1 collection
clear() set1.clear() Clear all elements in set1
copy() set2 = set1.copy() Copy set1 to set2
difference() set3 = set1.difference(set2) Give set3 the elements in set1 but not in set2
difference_update() set1.difference_update(set2) Remove the same elements from set1 as set2
discard() set1.discard(elem) Remove the elem element in set1
intersection() set3 = set1.intersection(set2) Take the intersection of set1 and set2 to set3
intersection_update() set1.intersection_update(set2) Take the intersection of set1 and set2 and update to set1
isdisjoint() set1.isdisjoint(set2) Determine if set1 and set2 have no intersection, return False if there is intersection; return True if there is no intersection
issubset() set1.issubset(set2) Determines whether set1 is a subset of set2
issuperset() set1.issuperset(set2) Determine if set2 is a subset of set1
pop() a = set1.pop() Take an element from set1 and assign it to a
remove() set1.remove(elem) Remove the element from set1
symmetric_difference() set3 = set1.symmetric_difference(set2) Take different elements in set1 and set2 and give set3
symmetric_difference_update() set1.symmetric_difference_update(set2) Take different elements from set1 and set2 and update to set1
union() set3 = set1.union(set2) Take the union of set1 and set2 and assign to set3
update() set1.update(elem) Add elements from a list or collection to set1

For example:

set1 = {1,2,3}
set1.add((1,2))  #add method
set1
set1 = {1,2,3}
set1.clear()  #clear method
set1
           

The output is:

{(1, 2), 1, 2, 3}
set()

set1 = {1,2,3}
set2 = set1.copy() #copy method
set1.add(4)
set1
           

The output is:

{1, 2, 3, 4}

set1 = {1,2,3}
set2 = {3,4}
set3 = set1.difference(set2) #difference method
set3
           

The output is:

{1, 2}

set1 = {1,2,3}
set2 = {3,4}
set1.difference_update(set2) #difference_update method
set1
           

The output is:

{1, 2}

set1 = {1,2,3}
set1.discard(2)  #discard method
set1
           

The output is:

{1, 3}

set1.discard(4) #discard  method
set1 = {1,2,3}
set2 = {3,4}
set3 = set1.intersection(set2) #intersection method
set3
           

The output is:

{3}

set1 = {1,2,3}
set2 = {3,4}
set1.intersection_update(set2) #intersection_update method
set1
           

The output is:

{3}

set1 = {1,2,3}
set2 = {3,4}
set1.isdisjoint(set2) #isdisjoint method
           

The output is:

False

set1 = {1,2,3}
set2 = {1,2}
set1.issubset(set2) #issubset method
           

The output is:

False

set1 = {1,2,3}
set2 = {1,2}
set1.issuperset(set2) #issuperset method
           

The output is:

True

set1 = {1,2,3}
a = set1.pop() #pop method
set1
a
           

The output is:

{2, 3}
1

set1 = {1,2,3}
set1.remove(2)  #remove method
set1
           

The output is:

{1, 3}

set1 = {1,2,3}
set2 = {3,4}
set3 = set1.symmetric_difference(set2) # symmetric_difference method
set3
           

The output is:

{1, 2, 4}

set1 = {1,2,3}
set2 = {3,4}
set3=set1.union(set2) #union method
set3
           

The output is:

{1, 2, 3, 4}

set1 = {1,2,3}
set1.update([3,4]) #update method
set1
           

The output is:

{1, 2, 3, 4}

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises