Latest posts

10/recent/ticker-posts

Python Sets and it's Methods

 Python Sets and their Methods 

What are the sets?

In Python, a set is a collection of unique elements. It is an unordered, mutable data structure that supports operations like union, intersection, difference, and membership testing.

A set can be defined using the built-in 'set' type. Sets are used to store multiple items in a single variable

Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.


Sets properties

A set can be enclosed with curly bracket '{ }' and values inside the set are separated with commas

For example:

 a = {1, 2, 3}
 print(type(a))

 //Output
 <class 'set'>

Python sets are mutable

Sets in Python are mutable. You can create a set of numbers and add elements to it. but you cannot change the elements using their index positions, once they have been added to the set.
 numbers = {1, 2, 3, 4} 
 numbers[0] = 5 
 print(numbers) 

 //Output 
 TypeError: 'set' object does not support item assignment


Sets do not contain duplicate entries 

For example:
 a {10, 10, 10}
 print(a)        # only one 10 get stored

 //Output
 {10}

A set can store dissimilar values 

For example:
 a = set()               # empty set, use () instead of {}
 b = {20}                # set with one item
 c = {'John', 3.5, 5}    # set with multiple items
 print(a)
 print(b)
 print(c)

 //Output
 {}
 {20}
 {'John', 3.5, 5}


It is possible to create a set of strings and tuples, but not a set of lists.

For examples:
 numbers = {12, 23, 45, 16, 52}
 print(numbers)
 
 //Output
 {16, 52, 23, 12, 45}

Set slicing

Slicing of sets is not possible in Python because sets are unordered collections of *unique elements and don't have indices

Methods of Set :

Function Name Description
add() Adds a given element to a set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set that is the difference between two sets
difference_update() Updates the existing caller set with the difference between the two sets
discard() Removes the element from the set
frozenset() return an immutable frozenset object
intersection() Updates the existing caller set with the intersection of sets
intersection_update() Updates the existing caller set with the intersection of sets
isdisjoint() Checks whether the sets are disjoint or not
issubset() Returns True if all elements of a set are present in another set
issubset() Returns
issuperset() Returns True if all elements of a set occupy another set
pop() Returns and removes a random element from the set
remove() Removes the element from the set
symmetric_difference()) Returns a set which is the symmetric difference between the two sets/td>
symmetric_difference_update() Updates the existing caller set with the Symmetric difference of sets
union() Returns a set that has the union of all sets
update() Adds elements to the set





Also Visit


Post a Comment

0 Comments