Python provides the set type which provides the same features as the mathematical set. Python set elements are immutable and can not be changed or updated but elements can be added or removed. Also, the set elements are unique where a set can not contain multiple instances of the same object.
Create Set Operation
Set can be created by using brackets and putting elements inside these brackets. Elements are separated with commas. String, integer, objects can be used as elements for the set.
names = ("ismail","ahmet","ali")
numbers = (2,4,6,8)
Add Element Operation
One of the most popular operations with set type is adding new elements. The update()
method is used to new elements. The update() method can be used to add single or multiple elements.
names = ("ismail","ahmet","ali")
names.update("elif",)
numbers = (2,4,6,8)
numbers.update(10,)
Remove Element Operation
The remove()
element can be used to remove a specific element from the set. The element we want to remove is provided to the remove() method as parameter.
names = ("ismail","ahmet","ali")
names.remove("ahmet",)
numbers = (2,4,6,8)
numbers.remove(4)
Discard Element Operation
The discard() method provides very similar function to the remove() method. The difference is discard method does not throw an error even the specified element does not exist in the set.
names = ("ismail","ahmet","ali")
names.discard("ahmet",)
numbers = (2,4,6,8)
numbers.discard(4)
Pop Element Operation
The pop()
method is used to remove and return random element from the set.
names = ("ismail","ahmet","ali")
name=names.pop()
numbers = (2,4,6,8)
number=numbers.pop()
Remove All Elements Operation
The clear()
method is used to remove all elements from the set. Simply it clears the set.
names = ("ismail","ahmet","ali")
names.clear()
numbers = (2,4,6,8)
number.clear()
Union Operation
The union operation is used to union two provided sets into a single set. The union()
method is used to union two sets.
names = ("ismail","ahmet","ali")
numbers = (2,4,6,8)
union_set = names.union(numbers)
Alternatively the |
pipe operator can be used for union operation for multiple sets.
names = ("ismail","ahmet","ali")
numbers = (2,4,6,8)
union_set = names | numbers
Intersection Operation
The intersection operation is used to find all common elements for the provided sets. The intersecion() method of the set can be used like below.
numbers = (2,4,6,8)
n = (1,4,8,9)
set_intersection = numbers.intersection(n)
Alternatively the &
ampersand operator can be used to calculate intersection of multiple sets.
numbers1 = (2,4,6,8)
numbers2 = (1,4,8,9)
set_intersection = numbers1 & numbers2
Difference Operation
The difference operation is used to return all non-common elements for the specified set.
numbers1 = (2,4,6,8)
numbers2 = (1,4,8,9)
set_difference = numbers1.difference(numbers2)
Symmetric Operation
The symmetric difference is used to return elements those does not exist in other set.
numbers1 = (2,4,6,8)
numbers2 = (1,4,8,9)
set_difference = numbers1.symmetric_difference(numbers2)
Check Subset Operation
The subset operation is used to check if a set contains all elements of another set. The containing set is named as super set and contained set is named as subset. The issubset()
method is used for subset calculation.
numbers1 = (2,4,6,8)
numbers2 = (2,4)
numbers3 = (2,4,5)
if numbers2.issubset(numbers1):
print("Numbers1 is subset of numbers2")
Check Super Set Operation
If a set contains another set’s all elements it is called as super set. The issuperset()
method is used to check if a set is another set’s super set.
numbers1 = (2,4,6,8)
numbers2 = (2,4)
numbers3 = (2,4,5)
if numbers2.issubset(numbers1):
print("Numbers1 is subset of numbers2")