Python Sets and Inbuilt functions:

 Python Sets and Inbuilt functions

 Python Set is used to store collection of data. It is one of the four in built data types. They are List, set, tuples and dictionaries. Each one has their own quality and usage. Set is unordered, unchangeable but can add or remove elements and it has no duplicate elements.

Creating a set:

Set is created by curly braces and the elements are placed inside that.


Creating Python Sets

Adding elements to a set:

Sets are unordered so the elements in set cannot be accessed using indexing. We can add elements using add( ) or update( ) methods.


Adding elements to python set

Deleting elements in a set:

An element can be removed or deleted using discard( ) or remove( ) methods. The difference is if the element is not present in the set remove( ) method will return a error. To delete the whole set clear( ) method is used.

Deleting Elements from Python Set


Mathematical operations in Python sets:

Mathematical operation like union, intersection, difference, symmetrical difference can be performed in Python Sets.

Union will return all the elements in both the sets. Intersection will return only common elements between both the sets. Difference will return the elements which are not present in the other set.


Mathematical operations in Python Sets

Inbuilt Python Set Methods:

add( )- Adds an element to the set

update( )- Adds more elements to the set

copy( )- Returns copy of the set

discard( )- Deletes an item from the set

remove( )- Deletes an item from the set

pop( )- Deletes an item from the set

isdisjoint( )- Returns true if the intersection set is null

issubset( )- Returns true if it contains all the elements in the set 

union( )- Returns all the elements in the sets

intersection( )- Returns all the common elements in the sets

difference( )-Returns the elements which are not present in the other set.

Post a Comment