Python Sets
Sets are unordered data type and doesn't allow duplicate values.
Change Sets Elements
Sets cannot access through index so we even can't change his elements.
So set elements are immutable and we cannot change them.
Even if we can't change the value of an element in the set but we can add or remove elements from the set using the add or remove methods.
Add Set Elements
With the help of add method we can add elements to sets.
add()
add helps us to add the element to the set.
Delete Set Elements
With the help of below methods we can delete elements of a Set.
pop()
As set is unordered we can't pass index number in pop like we did in list and tuple. but we can simply write pop and it will not delete the last element from set like it does in list and tuple because there is nothing like last element in set. so it will delete the random element from set.
discard()
We can delete the particular element by using the discard. Discard will not throw an error if the value is to be deleted is not present in a set.
remove()
We can delete the particular element by using the remove. Remove will throw an error if the value is to be deleted is not present in a set.​
del a
Delete can help us to delete the complete set.
clear()
Clear will help us to clear the set and make it empty.
Create a new Set
When we want to create one new set with the help of other two existing set we can use this four methods provided below.
Below methods will help us to create the set 'c' with the help of two sets 'a' and 'b'.
union()
intersection()
symmetric_difference()
difference()
Modify Existing Set
When we want to modify existing set with the help of other set we can use this four methods provided below. In every method 'update' word will be there.
Below methods will help us to modify set 'a' with the help of set 'b'.