# Python Set Methods

[et_pb_section][et_pb_row][et_pb_column type="4_4"][et_pb_text]

Python Set Methods

A set is a collection which is unordered and unindexed. Python has a set of built-in methods that you can use on sets.

Example 1

```# Python program explaining
# the set methods
# initializing the set A
A = {"a", "e", "z","g"}
print("Set A: ",A)
# adding the more element in the set A
# After adding the more fruits in the set
print("New set A: ",A)
# the set.copy() method
print("Copied set B:",B)
print("Actual set A:",A)
# the set.difference() method
# Returns a set containing the difference between Set A and SEt B
print("\nAfter subtracting Set A from Set B... ")
print("Differnce Set : ",A.difference(B))
print("Set A:",A)
print("Set B:",B)
# the clear.set() method
# removing all the elements from Set A an Set B
A.clear()
B.clear()
print("\nAfter clearing all the elements...")
print("Set A",A)
print("Set B",B) ```

Output

```Set A:  {'e', 'a', 'g', 'z'}
New set A:  {'z', 'a', 'l', 'e', 'r', 'g'}
After the copying the dictionary...
Copied set B: {'e', 'r', 'z', 'g', 'a', 'l'}
Actual set A: {'z', 'a', 'l', 'e', 'r', 'g'}
After subtracting Set A from Set B...
Differnce Set :  set()
Set A: {'z', 'a', 'l', 'e', 'r', 'g'}
Set B: {'e', 'r', 'z', 'g', 'a', 'j', 'l'}
After clearing all the elements...
Set A: set()
Set B: set() ```

Example 2

```# Python program explaining
# the set methods
#initializing a set with numbers from 1 to 10
set1 = {1,2,3,4,5,6,7,8,9,10}
print("Actual set 1:",set1)
# initializing set2 with even numbers
set2 = {2,4,6,8,10}
print("Actual set2: ",set2)
# the set.difference_update() method
# Removeing the items in set1 that are also included in set2
print("After subtracting set1 from set2... ")
set1.difference_update(set2)
print("New Set 1: ",set1)
print("New Set 2:",set2)
# removing 3  and 5 from set1 and set2
# prinitg the set
print('\nAfter discarding 3 and 5 from set1 and set2...')
print("New set1:",set1)
print("New set2:",set2)
# the set.intersection() method
# returning the common elements between set1 and set2
print("\nIntersection between set1 and set2...")
print("set1 U set2 :",set1.intersection(set2))
# the set.union() method
# returns a new set with different items from all the sets and
#writing the common elements between both the sets only once
print("\nThe union of set1 and set2...")
Union= set1.union(set2)
print("A U B: ",Union) ```

Output

```Actual set 1: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Actual set2:  {8, 10, 2, 4, 6}
After subtracting set1 from set2...
New Set 1:  {1, 3, 5, 7, 9}
New Set 2: {8, 10, 2, 4, 6}
After discarding 3 and 5 from set1 and set2...
New set1: {1, 5, 7, 9}
New set2: {8, 10, 2, 4, 6}
Intersection between set1 and set2...
set1 U set2 : set()
The union of set1 and set2...
A U B:  {1, 2, 4, 5, 6, 7, 8, 9, 10} ```

Example 3

```# Python program explaining
# the set methods
#initializing set1
set1 = {"a", "e", "i", "o","u"}
print("Set 1:",set1)
# initializing set2
set2 = {"u", "f","a","w"}
print("Set 2:",set2)
# the set.issuperset() method
# will check whether set1 is a superset for set2
a= set1.issuperset(set2)
if a == True:
{
print("\nset1 is a superset for set2")}
elif a == False:
{
print("\nset1 is not a superset for set2")}
{
print("\nset1 is a subset for set2")}
elif a == False:
{
print("\nset1 is not a subset for set2")} ```

Output

```Set 1: {'i', 'e', 'a', 'u', 'o'}
Set 2: {'a', 'w', 'u', 'f'}
set1 is not a superset for set2
set1 is not a subset for set2 ```
[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]