
python set operations
In this article, you will learn more about python sets that are creating sets, adding sets, combining sets. items of sets, set contractors, removing sets, and all operations sets in python.
Keywords of this article –
- # what is set operators
- # creating sets
- # sets items
- # modify elements
- # set operators
- # set methods
What are python set operations?
Set is an unchangeable, unordered and unique value of collections. Basically, sets used to include membership testing and finding duplicate entries. Its supporting areas are mathematically union, intersection, difference and more.
Python set uses this set() function to provide a return value.
Example of python set operations-
# python set
#openset = {“100”, “200”, “300”}
print(openset)
Output –
Creating python sets
Basically, create a set to input all needed elements under the {} section and it is separated by comma. Here you can use the set () function. Although the set is unchangeable, you can add and remove elements.
Here stay many types of numbers that all are different. Creating sets need to set and dictionaries elements.
For this example of sets-
# Different types of sets in Python programming language
# set of integers
new_set = {10, 12, 14}
print(new_set)
# set of mixed data types
new_set = {10.0, “WigMarketing”, (1, 2, 3)}
print(new_set)
The output of this set-
Set Items
Set items always are unique because its elements are unchangeable, unordered and do not allow any kind of duplicate values.
- Unchangeable – when you create a set just in this moment you are able to edit anything under this set. After completing these sets you can’t add or remove any items.
- Unordered – this is an unordered collection of set items. Here unordered means someone input an element of set items that comes from an order list randomly. Here sets always stay in a different order list.
- Unique items/ duplicate not allowed – set of items altime ensure its quality that means it allows a unique feature.
Example of sets items-
newset = {“Bangladesh”, “USA”, “India”, “UK”}
print(newset)
For this output of –
Modify elements of sets()
If you want to build-in set function then you need to do different functions like add, remove, clear, copy, union, intersection and more. But we cannot access any elements using index or slice sets.
When you want to add new elements under the set function, here you need to declare set.add or add(). If you need to import multiple elements at a time then you use the update() or set.update function.
Always avoid any duplicate to set function.
Note: Update function allows all kinds of data type like string, tuple, lists and more.
Example of set modify –
# add an element
# Output: {11,12, 13}
new_set.add(12)
print(new_set)
# add multiple items or elements
# output: {11, 12, 13, 14}
new_set.update([12, 13, 14])
print(new_set)
# add remove items
#output: {70, 45, 50}
new_set.remove([50])
print(new_set)
Sets operators
Use set operators in python to indicate mathematical set operation. It works 4 types of values that like –
- Union
- Intersection
- Difference
- Symmetric difference
Sets union
Union works two operands and this symbols of this (|) function or union() methods.
Suppose x= 4, 5, 6, 7, and y= 5, 7, 9, 10. This point you want to set up a union set, you declare this function.
#set union
# initialized X and Y
X = {4, 5, 6, 7}
Y = {5, 7, 9, 10}
#use| operators
# output : {4, 5, 6, 7, 9, 10}
print (X|Y)
Output of this –
4, 5, 6, 7, 9, 10
Sets intersection
This set always works with common elements. Intersection performs always & operator or intersection() function.
Suppose, x={4, 5, 6, 7} and y={5, 7, 9, 10}. Output of these elements in python intersection sets.
#set intersection
#initialized x and y
x= {4,5,6,7}
y={5. 7, 9, 10}
#use & operator
# output: {5, 7}
print(x & y)
Output of this –
5, 7
Sets difference
This sets include difference of two sets that performed (- ) operators or difference()
Example of difference set function-
# difference of two sets
# initialized of X and Y
X= 4, 5, 6, 7
Y = 5, 7, 9, 10
# use – operator X
#output: {4, 6}
print(X-Y)
Out out of difference –
{ 4, 6 }
Symmetric difference
This sets of elements that includes x and y elements but not both on. It performed (^) methods or symmetic_difference() function.
For the example-
# symmetric difference of two sets
# initialized X and Y
x= {4, 5, 6, 7}
y= {5, 7, 9, 10}
# use ^ operator
# output: {4, 6, 9, 10}
print( x^ y)
Output –
{4, 6, 9, 10 }
Sets methods
When you want to create sets methods that point you need to know this build in methods
Name of Methods | Explanation |
set.add() | Use this function to Add new elements. (if you want to input some elements that already stay here, you can not add this methods) |
set.pop() | Set up a random elements |
set.clear() | Use this when you ant to remove all elements |
set.remove() | Use this when you want to remove specific elements |
set.union() | Target a distinct elements to returns a new sets |
set.difference() | Get a unique statements to return |
set.update() | When you adding some distinct elements then you need to update set |
set.intersection() | Get a common elements to return |
set.copy() | Get a exact copy of this sets |
set.symmetric difference () | Create a new set using both elements |
In this article you learn all about set operations in the python programming language. If You Have any question drop a comment below.