Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more
Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c
To make everything easy: We refer CPython as Python
Python set is the unordered collections of the elements which are non-duplicated, unindexed, and not changeable. However, the set itself is a mutable data structure in which we can add or remove elements.
Sets are created using two curly brackets.
#Create a set
fruits={"Apple","Banana","Berries","Orange"}
print(fruits)
Output
{"Apple","Banana","Berries","Orange"}
A set cannot have two same elements, i.e, duplicated elements. When we print the set, all the members with the no-duplicate members are printed.
fruits={"Apple","Banana","Berries","Orange","Apple","Berries"}
print(fruits)
Output
{'Berries', 'Apple', 'Orange', 'Banana'}
Set with multiple data types
We can store multiple non-duplicated data types in the set except they are mutable. So, mutable elements cannot be stored in the set
set1 = {"abc", 34, True, 40, "male"}
print(set1)
set2={"Hello","Python",5,(1,2,3)}
print(set2)
set3={"Hello","Python",5,[1,2,3]} #but cannot add list
Output
{"abc", 34, True, 40, "male"}
{'Python', (1, 2, 3), 5, 'Hello'}
set2={"Hello","Python",5,[1,2,3]}
TypeError: unhashable type: 'list'
Set Modifications
Data inside the sets are immutable. But the set itself is a mutable data structure. Inset datatype, we can't perform slicing and indexing but we can add or remove the items to and from the set using different functions available
Add() and Update() FunctionAdd() function simply adds the element to set and Update() function adds an iterable to set. We can append a single element using add() function and we can append iterables using update() function.
>>>number_set={1,2,3,4,5}
>>>print(number_set)
{1,2,3,4,5}
>>>number_set.add(6)
>>>number_set.add(7)
>>>print(number_set)
{1,2,3,4,5,6,7}
>>>number_set.update([12,3,2])
>>>print(number_set)
{1, 2, 3, 4, 5, 12}
>>>#add list and set
>>>number_set.update([12,44,23],{26,35})
>>>print(number_set)
{1, 2, 3, 4, 5, 35, 12, 44, 23, 26}
Removing the elements from the set
Basically, we have two functions in python to remove an item from the set. One is remove() and another one is discard(). Both are quite similar, But only one difference is, if we try to remove the element which is not in the list, the discard() method will leave the set unchanged but the remove() function produces an error.
>>>number_set={1,2,3,4,5}
>>>print(number_list)
{1, 2, 3, 4, 5}
>>>number_set.remove(1)
>>>number_set.remove(2)
>>>print(number_set)
{3, 4, 5}
>>>number_set.discard(3)
>>>print(number_set)
{4,5}
>>>#let's remove the number which is not on the set.
>>>number_set.discard(6)
>>>print(number_set)
{4,5}
>>>number_set.remove(6)
>>>print(number_set)
#KeyError: 6 (Error got)
The pop() and clear() function.
We can remove and return an item from the set using the pop() function. As the set is an unordered, unindexed datatype, there is no way to determine which element is popped and returned. The clear() function is used to remove all the items from the set.
>>>number_set={1,2,3,4,5}
>>>number_set.pop()
>>>number_set.pop()
>>>print(number_set)
{3,4,5}
>>>number_set.clear()
>>>print(number_set)
set()
Set Operations
Set can handle some set operations such as Union of two sets, Intersection of two sets, Difference and Symmetric Difference between two sets.
Firstly let us create two sets, Do some operations which are discussed below.
set1={'a','b','c','d','e'}
set2={'c','d','e','f','g'}
Set union can be performed using either | operator or union() method.
>>>print(set1|set2) #Using Operator.
{'e', 'a', 'g', 'b', 'c', 'f', 'd'}
>>>#Using union() method.
>>>print(set1.union(set2))
{'e', 'a', 'g', 'b', 'c', 'f', 'd'}
The intersection of two sets is the set of common elements. Set Intersection can be performed using either & operator or intersection() method.
>>>print(set1&set2) #Using Operator.
{'c','d','e'}
>>>print(set1.intersection(set2))
{'c','d','e'}
The difference of set1 from set2 i.e, set2-set1 is the set of elements that are only in set2 not in set1. Similarly, the difference of set2 from set1 is the set of elements that are only in set1 not in set2. We can perform set differences using the '-' operator as well as using the difference() method.
>>>#The difference of set2 from set1
>>>print(set1-set2)
{'b', 'a'}
>>>print(set1.difference(set2))
{'b', 'a'}
#The different of set1 from set2
>>>print(set2-set1)
{'f', 'g'}
>>>print(set2.difference(set1))
{'f', 'g'}
The symmetric difference between set1 and set2 is the elements that are in set1 and set2 but not in both. That is the set of elements that are in set1 and set2 except intersection elements are called the symmetric difference.
The symmetric difference is calculated using the '^' operator or symmetric_difference() method as follows:
>>>print(set1^set2)
{'f', 'g', 'a', 'b'}
>>>print(set1.symmetric_difference(set2))
{'f', 'g', 'a', 'b'}
>>>#this is similar to:
>>>print(set2^set1)
{'f', 'g', 'a', 'b'}
>>>print(set2.symmetric_difference(set1))
{'f', 'g', 'a', 'b'}
We can use for loop to iterate through the set.
names={"David","Harry","Alice","John","Donald"}
for n in names:
print(n)
Output
Donald
John
Alice
David
Harry
We can check whether an element is present in the set or not by using the membership test.
names={"David","Harry","Alice","John","Donald"}
if "Harry" in Names:
print("It is in the set.")
Output
It is in the set.
Some of the built-in functions that we can use in the set are discussed below:
>>>number_set={2,3,1,4,5,0} #create a set of number
>>>print(max(number_set)) #prints maximum number from the set.
5
>>>print(min(number_set)) #prints minimum numbers from the set.
0
>>>print(len(number_set)) #prints the length of the set.
6
>>>print(sorted(number_set)) #prints sorted set.
[0, 1, 2, 3, 4, 5]
>>>print(sum(number_set)) #prints sum of the elements of the set.
15
>>>print(enumerate(number_set)) #prints an enumerate object.
<enumerate object at 0x00000244B139FDC0>