Sets in Python and it’s Examples

0
816

Introduction To Sets In Python

A Sets in Python are similar to this mathematical set with the following new conditions.

Properties of Sets in Python.

Sets in Python
  • The elements in the set cannot be duplicated.
  • The elements in the set are immutable(cannot be modified) but the set as a whole is mutable.
  • There is no index assigned to any component in a python set.
  • So they do not have any indexing or slicing operation.

Set Operations

The sets in python are used for mathematical methods same union, intersection, difference, and complement, etc. We can create a set, access its elements, and carry out these mathematical operations as shown below.

Creating a set

A set is created by using the set() function or placing all the elements within a pair of curly braces.

Week_Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
Months={"Feb","Mar".”April”}
Dates={11,22,26}

Let’s print the output of the variable

print(Week_Days)
print(Months)
print(Dates)

It produces the following output.

set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
set{"Feb","Mar".”April”}
set(11,22,26)

Adding the items in Set.

We can add elements to a set by using add() method.

Fruits=set([“Apple”,”banana”,”Mango”,"cherry","grapes"])
 
 Fruits.add("Guava")

 print(Fruits)

The output of the above code is

set([“Apple”,”banana”,”Mango”,"cherry","grapes","Guava"])

Removing Item from a Set

We can remove items from a set by using the discard() method.

Fruits=set([“Apple”,”banana”,”Mango”,"cherry","grapes","Guava"])
 
 Fruits.discard("Guava")

 print(Fruits)

The output of the above code is

set([“Apple”,”banana”,”Mango”,"cherry","grapes"])

Union of Sets

The union operation on two sets produces a new set containing all the unique components from both the sets. In the below example the component “Wed” is present in both the sets.

FruitsA = set([“Apple”,”banana”,”Mango”])
 FruitsB = set(["cherry","grapes","Guava"])
 FruitsAB = FruitsA|FruitsB
 print(FruitsAB)

The output of the above code is

set([“Apple”,”banana”,”Mango”,"cherry","grapes","Guava"])

Widget not in any sidebars

Intersection of Sets

The intersection operation on two sets produces a new set containing only the common items from both the sets.

In the example, Fruit “Apple” is present in both the sets.

FruitsA =set([“Apple”,”banana”,”Mango”])
 FruitsB = set([“Apple”,"grapes","Guava"]])
 FruitsAB = FruitsA &FruitsB
 print(FruitsAB)

The output of the above code is:

set([‘Apple'])

Difference of Sets

The difference operation on two sets produces a new set containing only the items from the first set and none from the second set.

In the below example the item “Apple” is present in both the sets so it will not be found in the result set.

FruitsA =set([“Apple”,”banana”,”Mango”])
 FruitsB = set([“Apple”,"grapes","Guava"]])
 FruitsAB = FruitsA -FruitsB
 print(FruitsAB)

The output of the above code is:

set(["grapes","Guava"])

Compare Sets

We can check if a provided set is a subset or superset of another set. The result is True or False depending on the items present in the sets.

FruitsA =set([“Apple”,”banana”,”Mango”])
 FruitsB = set([“Apple”,”banana”,”Mango”,"cherry","grapes","Guava"])

 SubsetFruit = FruitsA <= FruitsB
 SupersetFruit = FruitsB >= FruitsA
 print(SubsetFruit)
 print(SupersetFruit)

The output of the above code is:

True
True

Conclusion


Widget not in any sidebars

In this blog, you will get understanding of all the types of sets and their operations in python.

LEAVE A REPLY

Please enter your comment!
Please enter your name here