Python Set Functions & Methods – Python Tutorial

6 years ago Lalit Bhagtani 0

In Python, Objects have built-in methods inside it, which can be used to perform actions on the object itself. We can call methods of any object by putting period after object variable and then the method name.

Syntax:    objectVariable.methodName( parameters.. )

                  objectVariable :  variable containing object. 

                  methodName :   name of the method, which we like to execute.

                  parameters :      extra parameter ( 0 or more ) pass into the method.

Python Set Functions

Python Set Functions & Methods: 

Some examples of python set functions & methods are as follows :

  1. add :- It insert a single immutable element in the set.
  2. remove :- It remove a single element from the set. If element is not present in the set, it will raise a KeyError.
  3. discard :- It discard ( remove ) a single element from the set. If element is not present in the set, it will not raise a KeyError.
  4. pop :- It remove and return an arbitrary element from the set. If we try to pop an empty set, it will raise a KeyError.
  5. clear :- It remove all the elements from the set.
  6. copy :- It return a shallow copy of another set.
  7. union :- It return a union of two or more sets.
  8. union_update :- It perform the union operation on two or more sets and then update the set ( on which union_update method is invoked ) with elements generated by union operation.
  9. intersection :- It return a intersection of two or more sets.
  10. intersection_update :- It perform the intersection operation on two or more sets and then update the set ( on which intersection_update method is invoked ) with elements generated by intersection operation.
  11. difference :- It return a difference of two sets.
  12. difference_update :- It perform the difference operation on two sets and then update the set ( on which difference_update method is invoked ) with elements generated by difference operation.
  13. symmetric_difference :- It return a symmetric difference of two sets.
  14. symmetric_difference_update :- It perform the symmetric difference operation on two sets and then update the set ( on which symmetric_difference_update method is invoked ) with elements generated by symmetric difference operation.
  15. issuperset :- It returns true, if a set on which this method is invoked is a superset of a set passed as an argument otherwise it returns false.
  16. issubset :- It returns true, if a set on which this method is invoked is a subset of a set passed as an argument otherwise it returns false.
  17. isdisjoint :- It returns true, if all elements of a set on which this method is invoked is different from a set passed as an argument otherwise it returns false.

References :-

  1. Set Functions & Methods Docs

That’s all for Python Set Functions & Methods. If you liked it, please share your thoughts in comments section and share it with others too.