Sitemap

Python Set

3 min readJun 22, 2022

--

Press enter or click to view image in full size

Set is collection of same different data that are enclosed in braces {} separated in comma , Set is Data structure in Python.

Press enter or click to view image in full size

Creating Set

For creating a set you need bunch of values that should be enclosed in Braces {} and separated by comma ,

alpha = {‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’}
number={1,2,3,4,5}
mixed = {5.4,”hello”,(3,2,1)}

Accessing a Set

Accessing a set mean how you gonna to use or access all element in set.

print(alpha)
print(number)
print(mixed)
print(type(alpha))

Set Methods

Press enter or click to view image in full size

Add in Set

To add an element in set.

alpha.add(‘i’)
print(alpha)

Sorted in Set

To sort the set element because all element are unsorted and the result will be in tuple.

print(sorted(alpha))

Remove in Set

To remove an element in set you can use remove method or discard method.

alpha.remove(‘i’)
print(sorted(alpha))
alpha.discard(‘h’)
print(sorted(alpha))

Update in Set

To update an element in set.

alpha.update([‘g’,’h’,’i’])
print(sorted(alpha))#no Duplication

min and max

To find minimum and maximum value in set.

print(min(alpha))
print(max(alpha))
print(min(number))
print(max(number))

Pop in Set

To remove an last element in set but it may remove any element because it set are unsorted.

mixed.pop()
print(mixed)

Clear in Set

To clear all element in set.

print(“Before Clear “,mixed)
mixed.clear()
print(“After Clear “,mixed)

Looping in Set

To loop all element in set.

for value in alpha:
print(value)

Creating some variable for math methods

set1={1,2,3,4,5}
set2={4,5,6,7,8}
set3={1,2,3}
set4={10,20,30}
alpha1 = {‘a’,’b’,’c’,’d’,’e’}
alpha2 = {‘d’,’e’,’f’,’g’,’h’}
alpha3 = {‘a’,’b’,’c’}
alpha4 = {‘x’,’y’,’z’}

Union Set

To print all element on union elements.

result = set1.union(set2)
print(“Union Result is “,result)
result1 = alpha1 | alpha2
print(“Union Result is “,sorted(result1))

Intersection Set

To print an element that present on both intersection elements.

result = set1.intersection(set2)
print(“Intersection Result is “,result)
result1 = alpha1 & alpha2
print(“Intersection Result is “,sorted(result1))

Difference Set

To show what difference on comparing to another set elements.

result = set1.difference(set2)
print(“Differences Result is “,result)
result1 = alpha1 — alpha2
print(“Differences Result is “,sorted(result1))
result2 = set2.difference(set1)
print(“Another Differences side Result “,result2)
result3 = alpha2 — alpha1
print(“Another Differences side Result “,sorted(result3))

Symmetric Difference Set

It will print unmatched element on both set.

result = set1.symmetric_difference(set2)
print(“First Sym-Differences Result “,result)
result1 = alpha1 ^ alpha2
print(“Second Sym-Differences Result “,sorted(result1))

Subset

To find father element on set means all value of set 3 will be in some elements set 1. (Result on boolean)

result = set3.issubset(set1)
print(“Set 3 is child setl is parent “,result)
result1 = set2.issubset(set1)
print(“Set 2 is child setl is parent “,result1)
result2 = alpha3 <= alpha1
print(“alpha 3 is child alphal is parent “,result2)

Superset

To find children method.(Result on boolean)

result = set1.issuperset(set3)
print(“setl is parent Set 3 is child “,result)
result1 = set2.issuperset(set1)
print(“Set 2 is child setl is parent “,result1)
result2 = alpha1 >= alpha3
print(“alphal is parent alpha 3 is child “,result2)

Disjoint

To find that no difference element on both set.(Result on boolean)

result = set3.isdisjoint(set1)
print(“The is no matching values”,result)
result1 = set4.isdisjoint(set1)
print(“The is no matching values”,result1)
result2 = alpha3.isdisjoint(alpha1)
print(“The is no matching values”,result2)
result3 = alpha4.isdisjoint(alpha1)
print(“The is no matching values”,result3)

I hope everyone learn through on Set and its methods. Follow for more topics :-)

Get Narayanan’s stories in your inbox

Join Medium for free to get updates from this writer.

Follow us on
Instagram:- https://www.instagram.com/Coder_033/
Github:- https://github.com/Coder033
Skillshare:- https://www.skillshare.com/profile/Coder033/26410885 Youtube:-https://www.youtube.com/channel/UCjTUcwOms1NJDo2UiIvGfyg

--

--

Narayanan
Narayanan

Written by Narayanan

Front-end Development | Back-end development | Data Science | Data Visualization | Hodophile | Booklover