Posts

Showing posts with the label Sets - Data Structures

Python3 Sets DS

Python Data Structures:  Sets Sets :   -> A set is an unordered collection of items. Each element is unique (no duplicates).   -> The set itself is mutable .We can add or remove items from it.   -> Sets can be used to perform mathematical set operations like union ,intersection ,symmetric ,difference etc. Set Creation : Eg:         #set of integers        s={1,2,3}        print(s)        print(type(s)) output:   set([1,2,3])               < type 'set' >        #set does not allow duplicates. They store only one instance.        s={1,2,3,1,2}        print(s)   output:   {1,2,3} Eg:         #we can make set from a list        s=set([1,2,3,1])        print(s) output:  {1,2,3} Eg:        #initialize a set with set( ) method        s=set( )        print(type(s))   output:   < class 'set' > Add element to a Set : Eg:        #we can add single element using add( ) method and         #add multiple elements using update( ) method        s={1,2,3}